Cate.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wugg
  5. * Date: 2024/6/28
  6. * Time: 16:03
  7. * description:分类控制器
  8. */
  9. namespace app\admin\controller;
  10. use app\admin\model\CatSpecs;
  11. use app\admin\model\Cat;
  12. use think\App;
  13. use think\facade\Validate;
  14. class Cate extends Base{
  15. public function __construct(App $app) {
  16. parent::__construct($app);
  17. $this->model = new Cat();
  18. }
  19. /**
  20. * @param cat_name 分类名称
  21. * @param pid 父级分类id
  22. * @param status 状态 0正常 1禁用
  23. * 获取分类列表
  24. * @return \think\Response|\think\response\Json
  25. * @throws \think\db\exception\DataNotFoundException
  26. * @throws \think\db\exception\DbException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. */
  29. public function list(){
  30. $param=$this->request->param(["pid"=>0,"cat_name"=>"","status"=>"","page"=>1,"size"=>10],"post","trim");
  31. $where=[["is_del","=",0]];
  32. if($param["pid"]!=="")$where[]=["pid","=",$param["pid"]];
  33. if($param["cat_name"]!=="")$where[]=["cat_name","like","%".$param["cat_name"]."%"];
  34. if($param["status"]!=="")$where[]=["status","=",$param["status"]];
  35. $data=$this->model->where($where)->order("id","desc")->paginate(['page'=>$param['page'],'list_rows'=>$param['size']])
  36. ->each(function(&$item){
  37. $item["im"]=CatSpecs::with(["specs"])->where("cat_id",$item["id"])->field(["specs_id"])->select();
  38. $depart = \app\user\model\AccountItem::with(["ItemName"])->where(["account_id"=>$item["createrid"]])->findOrEmpty();
  39. $item["company_name"]=$depart->depart_name??"";
  40. });
  41. return success("获取成功",["list"=>$data->items(),"count"=>$data->total()]);
  42. }
  43. /**
  44. * @param cat_name 分类名称
  45. * @param pid 父级分类id
  46. * @param cat_desc 分类描述
  47. * @param fund_code 财务代码
  48. * @param specs_id 规格id
  49. * @param weight 权重
  50. * @param status 状态 0正常 1禁用
  51. * @return
  52. *
  53. */
  54. public function create(){
  55. $param=$this->request->param(['cat_name', 'pid', 'cat_desc' => '', 'fund_code' => '', 'is_del' => '0', 'specs_id'=>[], 'weight'
  56. => 0, 'status' => 0],"post","trim");
  57. $valid=Validate::rule([
  58. "cat_name|分类名称"=>"require|max:255|unique:app\admin\model\Cat,cat_name^is_del",
  59. "pid|父级分类"=>"require|integer|egt:0",
  60. "cat_desc|分类描述"=>"max:255",
  61. "fund_code|财务代码"=>"max:255",
  62. "specs_id|规格id"=>"require|array",
  63. "weight|权重"=>"integer",
  64. "status|状态"=>"integer|in:0,1",
  65. ]);
  66. if(!$valid->check($param))return error($valid->getError());
  67. if($param['pid']!=0){
  68. $parent = $this->model->where('id',$param['pid'])->findOrEmpty();
  69. if($parent->isEmpty())return error('父级分类不存在');
  70. }
  71. if($parent->isEmpty())return error("父级分类不存在");
  72. $this->model->startTrans();
  73. try{
  74. $create=[
  75. "cat_name"=>$param['cat_name'],
  76. "pid"=>$param["pid"],
  77. "cat_desc"=>$param["cat_desc"],
  78. "fund_code"=>$param["fund_code"],
  79. "weight"=>$param["weight"],
  80. "status"=>$param["status"],
  81. 'creater' => $this->uname,
  82. 'createrid' =>$this->uid,
  83. ];
  84. $data=$this->model->create($create);
  85. if($data->isEmpty())throw new \Exception("添加失败");
  86. $spec=$data->specs()->saveAll(array_map(function($item)use($data){return ['specs_id'=>$item];},$param['specs_id']));
  87. if(!$spec)throw new \Exception("添加失败");
  88. $this->model->commit();
  89. }catch (\Exception $e){
  90. $this->model->rollback();
  91. return error($e->getMessage());
  92. }
  93. event("PlatCat",["type"=>"add",'info'=>'cat',"data"=>$data]);
  94. return success("添加成功");
  95. }
  96. /**
  97. * @param id 分类id
  98. * @param cat_name 分类名称
  99. * @param pid 父级分类id
  100. * @param cat_desc 分类描述
  101. * @param fund_code 财务代码
  102. * @param specs_id 规格id
  103. * @param weight 权重
  104. * @param status 状态 0正常 1禁用
  105. * @return
  106. */
  107. public function save(){
  108. $param=$this->request->param(['id','cat_name', 'pid', 'cat_desc' => '', 'fund_code' => '', 'is_del' => '0','specs_id'=>[], 'weight'
  109. => 0, 'status' => 0],"post","trim");
  110. $valid=Validate::rule([
  111. "id|分类id"=>"require|integer|egt:1",
  112. "cat_name|分类名称"=>"require|max:255|unique:app\admin\model\Cat,cat_name^is_del",
  113. "pid|父级分类"=>"require|integer|egt:0",
  114. "cat_desc|分类描述"=>"max:255",
  115. "fund_code|财务代码"=>"max:255",
  116. "specs_id|规格id"=>"require|array",
  117. "weight|权重"=>"integer",
  118. "status|状态"=>"integer|in:0,1",
  119. ]);
  120. if(!$valid->check($param))return error($valid->getError());
  121. if($param['pid']!=0){
  122. $parent = $this->model->where("id",$param["pid"])->findOrEmpty();
  123. if($parent->isEmpty())return error("父级分类不存在");
  124. }
  125. $this->model->startTrans();
  126. try{
  127. $data=$this->model->with(["specs"])->findOrEmpty($param["id"]);
  128. if($data->isEmpty())return error("分类不存在");
  129. $data->cat_name=$param["cat_name"];
  130. $data->pid=$param["pid"];
  131. $data->cat_desc=$param["cat_desc"];
  132. $data->fund_code=$param["fund_code"];
  133. $data->weight=$param["weight"];
  134. $data->status=$param["status"];
  135. $save=$data->save();
  136. if(!$save)throw new \Exception("修改失败");
  137. $add=array_diff($param["specs_id"],array_column($data->specs->toArray(),"specs_id"));
  138. $delte=CatSpecs::where([["cat_id","=",$data->id],["specs_id","not in",$param['specs_id']],['is_del','=',0]])->delete();
  139. if($delte===false)throw new \Exception("删除失败");
  140. if(!empty($add)){
  141. $spec=$data->specs()->saveAll(array_map(function($item)use($data){return ["specs_id"=>$item,"cat_id"=>$data->id];},$add));
  142. if(!$spec)throw new \Exception("添加失败");
  143. }
  144. $this->model->commit();
  145. }catch (\Exception $e){
  146. $this->model->rollback();
  147. return error($e->getMessage());
  148. }
  149. event('PlatCat',['type'=>'add','info'=>'cat','data'=>$data]);
  150. return success("修改成功");
  151. }
  152. /**
  153. * @param id 分类id
  154. * @return
  155. */
  156. public function delete(){
  157. $param=$this->request->param(["id"=>0],"post","trim");
  158. $valid=Validate::rule([
  159. "id|分类id"=>"require|integer|egt:1",
  160. ]);
  161. if(!$valid->check($param))return error($valid->getError());
  162. $data=$this->model->with(["specs"])->findOrEmpty($param["id"]);
  163. if($data->isEmpty())return error("分类不存在");
  164. $data->is_del=1;
  165. if(!$data->save())return error("删除失败");
  166. event("PlatCat",["type"=>"delete",'info'=>'cat',"data"=>$data]);
  167. return success("删除成功");
  168. }
  169. /**
  170. * @param id 分类id
  171. * @param status 状态 0正常 1禁用
  172. * @return
  173. */
  174. public function status(){
  175. $param=$this->request->param(["id"=>0,"status"=>0],"post","trim");
  176. $valid=Validate::rule([
  177. "id|分类id"=>"require|integer|egt:1",
  178. "status|状态"=>"require|integer|in:0,1",
  179. ]);
  180. if(!$valid->check($param))return error($valid->getError());
  181. $data=$this->model->findOrEmpty($param["id"]);
  182. if($data->isEmpty())return error("分类不存在");
  183. if($data->status==$param["status"])return error("状态未改变");
  184. $catIds=[$param['id']];
  185. if($data->level==3){
  186. if($param['status']==1){
  187. $catInfo = Cat::GetCatListByChildId($param['id']);
  188. $catIds=array_map(function($v){
  189. return $v->status==0?$v->id:0;
  190. },$catInfo);
  191. }
  192. }else{
  193. if($param['status']==0){
  194. $catInfo = Cat::where(["pid"=>$param['id'],"status"=>1,"is_del"=>0])->findOrEmpty();
  195. if(!$catInfo->isEmpty())return error("该分类下有启用的子分类");
  196. }else return error('所在等级不能启用');
  197. }
  198. $update=$data->whereIn("id",$catIds)->update(["status"=>$param["status"]]);
  199. if(!$update)return error("修改失败");
  200. event("PlatCat",["type"=>"status",'info'=>'cat',"data"=>["id"=>$catIds,"staus"=>$param['status']]]);
  201. return success(Cat::$statusCn[$param['status']]."成功");
  202. }
  203. /** 获取分类列表
  204. * @param cat_name 分类名称
  205. * @param pid 父级分类id
  206. * @param status 状态
  207. * @return \think\Response|\think\response\Json|void
  208. */
  209. public function query(){
  210. $param = $this->request->only(['cat_name' => '', 'pid' => '', 'status' => '',], 'post', 'trim');
  211. $valid = Validate::rule([
  212. "cat_name|分类名称" => "max:255",
  213. "pid|父级分类" => "integer|egt:0",
  214. "status|状态" => "integer|in:0,1",
  215. ]);
  216. if (!$valid->check($param)) return error($valid->getError());
  217. try {
  218. $data = $this->model->with(["specs"])->where(function ($query) use ($param) {
  219. if ($param['cat_name']!=='') $query->where("cat_name", "like", "%{$param['cat_name']}%");
  220. if ($param['pid']!=='') $query->where("pid", $param['pid']);
  221. if ($param['status'] !== '') $query->where("status", $param['status']);
  222. })->order("pid,weight desc,id")->select();
  223. if (!$data->isEmpty()) {
  224. foreach ($data as &$item) {
  225. $item['child'] = Cat::GetCatListByPid($item['id']);
  226. }
  227. }
  228. }catch (\Exception $e) {
  229. return error($e->getMessage());
  230. }
  231. return success("查询成功", $data);
  232. }
  233. }