SupplierCat.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\admin\controller;
  3. use app\abutment\controller\Record;
  4. use think\facade\Db;
  5. use think\facade\Validate;
  6. class SupplierCat extends Base
  7. {
  8. // 创建
  9. public function add(){
  10. $param = $this->request->filter("trim")->only(['supplierNo','cat_id','token']);
  11. $isValid = Validate::rule(['supplierNo|供应商编码' => 'require', 'cat_id|分类ID集合' => 'require|array|max:9999' ]);
  12. if(!$isValid->check($param)) json_show(1004,$isValid->getError());
  13. $creater = GetUserInfo($param['token']);
  14. if(empty($creater) || $creater['code'] != 0) return json_show(102,"创建人数据不存在");
  15. $userCommon = \app\admin\common\User::getIns();
  16. $supplier = $userCommon->handle('sInfo',['code'=>$param['supplierNo']]);
  17. if(empty($supplier) || $supplier['code'] != 0) return json_show(1002,"未找到供应商数据");
  18. $thirdLevelCatCollection = Db::name('cat')
  19. ->where(['is_del' => 0, 'id' => array_unique($param['cat_id'])])
  20. ->column('id,level,pid');
  21. $notThirdLevelCatCollection = array_filter($thirdLevelCatCollection,function ($value){
  22. return $value['level'] != '3';
  23. });
  24. if(!empty($notThirdLevelCatCollection)){
  25. return json_show(1002,'分类'.implode(array_column($notThirdLevelCatCollection,'id'),'、').'不是三级分类');
  26. }
  27. $secondLevelCatCollection = Db::name('cat')
  28. ->where(['is_del' => 0, 'id' => array_unique(array_column($thirdLevelCatCollection,'pid'))])
  29. ->column('level,pid','id');
  30. $data = [];
  31. $createrid = isset($creater['data']['id']) ? $creater['data']['id'] : "";
  32. $creater = isset($creater['data']['nickname']) ? $creater['data']['nickname'] : '';
  33. $supplierName = isset($supplier['data']['name']) ? $supplier['data']['name'] : "";
  34. $supplierNo = isset($supplier['data']['code']) ? $supplier['data']['code'] : "";
  35. $date = date('Y-m-d H:i:s');
  36. foreach ($thirdLevelCatCollection as $thirdLevelCat){
  37. $firstLevelCatId = $secondLevelCatCollection[$thirdLevelCat['pid']]['pid'];
  38. $secondLevelCatId = $secondLevelCatCollection[$thirdLevelCat['pid']]['id'];
  39. $thirdLevelCatId = $thirdLevelCat['id'];
  40. $catFullId = $firstLevelCatId.'_'.$secondLevelCatId.'_'.$thirdLevelCatId;
  41. $record = Db::name('supplier_cat')
  42. ->where(['supplierNo' => $supplierNo,'cat_id' => $thirdLevelCatId])
  43. ->select();
  44. if(!$record->isEmpty()) continue;
  45. $data[] = [
  46. 'creater' => $creater,
  47. 'createrid' => $createrid,
  48. 'supplierNo' => $supplierNo,
  49. 'supplierName' => $supplierName,
  50. 'cat_id' => $thirdLevelCatId,
  51. 'cat_full_id' => $catFullId,
  52. "updatetime" => $date,
  53. "addtime" => $date,
  54. 'status' => 1,
  55. "is_del" => 0
  56. ];
  57. }
  58. $isAdd = Db::name('supplier_cat')->insertAll($data);
  59. return $isAdd > 0 ? json_show(0,"新增成功") : json_show(1004,"新增失败");
  60. }
  61. // 列表
  62. public function list(){
  63. $param = $this->request->filter("trim")->only(['page' => 1,'size' => 15,'start'=>'','end' => '' ,'supplierNo'=>''
  64. ,'creater'=>'','status'=>'','cat_id'=>'']);
  65. $where = [['sc.is_del','=','0']];
  66. if($param['start']!=="" && $param['end'] !== ""){
  67. $where[]=['sc.addtime',">=", $param['start']." 00:00:00"];
  68. $where[]=['sc.addtime',"<=", $param['end']." 23:59:59"];
  69. }
  70. if($param['supplierNo'] != '') $where[] = ['sc.supplierNo','=',$param['supplierNo']];
  71. if($param['status'] != '') $where[] = ['sc.status','=',$param['status']];
  72. if($param['cat_id'] != '') $where[] = ['sc.cat_id','=',$param['cat_id']];
  73. if($param['creater'] != '') $where[] = ['sc.creater','like',"%{$param['creater']}%"];
  74. $list = Db::name('supplier_cat')
  75. ->alias('sc')
  76. ->leftJoin('cat c','c.id=sc.cat_id')
  77. ->field('sc.id,sc.supplierNo,sc.supplierName,sc.status,sc.cat_id,c.search cat,sc.creater
  78. ,sc.createrid,sc.addtime,sc.updatetime')
  79. ->where($where)
  80. ->order('id','desc')
  81. ->paginate(["page"=>$param['page'],"list_rows"=>$param['size']]);
  82. return json_show(0,'请求成功',["list"=>$list->items(),'count'=>$list->total()]);
  83. }
  84. // 状态
  85. public function status(){
  86. $param = $this->request->filter('trim')->only(['id' => [],'status' => ""]);
  87. if(empty($param['id'])) return json_show(1004,"参数id 不能为空");
  88. if(!is_array($param['id'])) return json_show(1004,"参数id 类型应为数组");
  89. if(!in_array($param['status'],[0,1])) return json_show(1002,"参数status值不合法");
  90. $isUpdate = Db::name("supplier_cat")
  91. ->whereIn('id', $param['id'])
  92. ->save(['status' => $param['status'], 'updatetime' => date("Y-m-d H:i:s")]);
  93. $prefix = $param['status'] == "1" ? "启用" : "禁用";
  94. return $isUpdate > 0 ? json_show(0,$prefix."成功") : json_show(1004,$prefix."失败");
  95. }
  96. // 详情
  97. public function info(){
  98. $param = $this->request->filter('trim')->only(['id' => ""]);
  99. $isValid = Validate::rule(['id' => 'require']);
  100. if(!$isValid->check($param)) return json_show(1004,$isValid->getError());
  101. $record = Db::name('supplier_cat')->where(['id'=>$param['id'],'is_del'=>0])->find();
  102. if(!$record) return json_show(1002,'未找到记录');
  103. return json_show(0,'获取成功',$record);
  104. }
  105. }