Version.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\service\platform;
  10. use app\admin\service\Service;
  11. use app\common\models\Attachments;
  12. use app\common\models\Version as VersionModel;
  13. class Version extends Service {
  14. /**
  15. * 获取列表数据
  16. * @param $data
  17. * @param array $condition
  18. * @return array
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\DbException
  21. * @throws \think\db\exception\ModelNotFoundException
  22. */
  23. public static function getList($data,$condition=[]){
  24. return [
  25. "count"=>VersionModel::where($condition)->count(),
  26. "data"=>VersionModel::where($condition)->page($data["page"]??1,$data["limit"]??10)->order("id",'desc')->select()->toArray()
  27. ];
  28. }
  29. /**
  30. * 详情
  31. * @param $data
  32. * @return array
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public static function detail($data){
  38. return [
  39. "data"=>VersionModel::where("id",$data["id"]??0)->find(),
  40. "type"=>$data["type"]??1
  41. ];
  42. }
  43. /**
  44. * 保存
  45. * @param array $data
  46. * @return VersionModel|bool|\think\Model
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public static function save($data=[]){
  52. if(!empty($data["id"])){
  53. $obj = VersionModel::where("id",$data["id"])->find();
  54. if($data["url"] != $obj["url"]){
  55. if(Attachments::where("path",$obj["url"])->delete()){
  56. $file = trim($obj["url"],"/");
  57. file_exists($file) && unlink($file);
  58. }
  59. }
  60. return VersionModel::where("id",$data["id"])->save($data);
  61. }else{
  62. return VersionModel::create($data);
  63. }
  64. }
  65. /**
  66. * 删除
  67. * @param $id
  68. * @return bool
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public static function delete($id){
  74. $row = VersionModel::where('id',$id)->find();
  75. if(empty($row)){
  76. throw new \Exception("您要查找的数据不存在!",0);
  77. }
  78. if(Attachments::where("path",$row["url"])->delete()){
  79. $file = trim($row["url"],"/");
  80. file_exists($file) && unlink($file);
  81. }
  82. return VersionModel::where("id",$id)->delete();
  83. }
  84. }