request->filter("trim")->only(['supplierNo','cat_id','token']); $isValid = Validate::rule(['supplierNo|供应商编码' => 'require', 'cat_id|分类ID集合' => 'require|array|max:9999' ]); if(!$isValid->check($param)) json_show(1004,$isValid->getError()); $creater = GetUserInfo($param['token']); if(empty($creater) || $creater['code'] != 0) return json_show(102,"创建人数据不存在"); $userCommon = \app\admin\common\User::getIns(); $supplier = $userCommon->handle('sInfo',['code'=>$param['supplierNo']]); if(empty($supplier) || $supplier['code'] != 0) return json_show(1002,"未找到供应商数据"); $thirdLevelCatCollection = Db::name('cat') ->where(['is_del' => 0, 'id' => array_unique($param['cat_id'])]) ->column('id,level,pid'); $notThirdLevelCatCollection = array_filter($thirdLevelCatCollection,function ($value){ return $value['level'] != '3'; }); if(!empty($notThirdLevelCatCollection)){ return json_show(1002,'分类'.implode(array_column($notThirdLevelCatCollection,'id'),'、').'不是三级分类'); } $secondLevelCatCollection = Db::name('cat') ->where(['is_del' => 0, 'id' => array_unique(array_column($thirdLevelCatCollection,'pid'))]) ->column('level,pid','id'); $data = []; $createrid = isset($creater['data']['id']) ? $creater['data']['id'] : ""; $creater = isset($creater['data']['nickname']) ? $creater['data']['nickname'] : ''; $supplierName = isset($supplier['data']['name']) ? $supplier['data']['name'] : ""; $supplierNo = isset($supplier['data']['code']) ? $supplier['data']['code'] : ""; $date = date('Y-m-d H:i:s'); foreach ($thirdLevelCatCollection as $thirdLevelCat){ $firstLevelCatId = $secondLevelCatCollection[$thirdLevelCat['pid']]['pid']; $secondLevelCatId = $secondLevelCatCollection[$thirdLevelCat['pid']]['id']; $thirdLevelCatId = $thirdLevelCat['id']; $catFullId = $firstLevelCatId.'_'.$secondLevelCatId.'_'.$thirdLevelCatId; $record = Db::name('supplier_cat') ->where(['supplierNo' => $supplierNo,'cat_id' => $thirdLevelCatId]) ->select(); if(!$record->isEmpty()) continue; $data[] = [ 'creater' => $creater, 'createrid' => $createrid, 'supplierNo' => $supplierNo, 'supplierName' => $supplierName, 'cat_id' => $thirdLevelCatId, 'cat_full_id' => $catFullId, "updatetime" => $date, "addtime" => $date, 'status' => 1, "is_del" => 0 ]; } $isAdd = Db::name('supplier_cat')->insertAll($data); return $isAdd > 0 ? json_show(0,"新增成功") : json_show(1004,"新增失败"); } // 列表 public function list(){ $param = $this->request->filter("trim")->only(['page' => 1,'size' => 15,'start'=>'','end' => '' ,'supplierNo'=>'' ,'creater'=>'','status'=>'','cat_id'=>'']); $where = [['sc.is_del','=','0']]; if($param['start']!=="" && $param['end'] !== ""){ $where[]=['sc.addtime',">=", $param['start']." 00:00:00"]; $where[]=['sc.addtime',"<=", $param['end']." 23:59:59"]; } if($param['supplierNo'] != '') $where[] = ['sc.supplierNo','=',$param['supplierNo']]; if($param['status'] != '') $where[] = ['sc.status','=',$param['status']]; if($param['cat_id'] != '') $where[] = ['sc.cat_id','=',$param['cat_id']]; if($param['creater'] != '') $where[] = ['sc.creater','like',"%{$param['creater']}%"]; $list = Db::name('supplier_cat') ->alias('sc') ->leftJoin('cat c','c.id=sc.cat_id') ->field('sc.id,sc.supplierNo,sc.supplierName,sc.status,sc.cat_id,c.search cat,sc.creater ,sc.createrid,sc.addtime,sc.updatetime') ->where($where) ->order('id','desc') ->paginate(["page"=>$param['page'],"list_rows"=>$param['size']]); return json_show(0,'请求成功',["list"=>$list->items(),'count'=>$list->total()]); } // 状态 public function status(){ $param = $this->request->filter('trim')->only(['id' => [],'status' => ""]); if(empty($param['id'])) return json_show(1004,"参数id 不能为空"); if(!is_array($param['id'])) return json_show(1004,"参数id 类型应为数组"); if(!in_array($param['status'],[0,1])) return json_show(1002,"参数status值不合法"); $isUpdate = Db::name("supplier_cat") ->whereIn('id', $param['id']) ->save(['status' => $param['status'], 'updatetime' => date("Y-m-d H:i:s")]); $prefix = $param['status'] == "1" ? "启用" : "禁用"; return $isUpdate > 0 ? json_show(0,$prefix."成功") : json_show(1004,$prefix."失败"); } // 详情 public function info(){ $param = $this->request->filter('trim')->only(['id' => ""]); $isValid = Validate::rule(['id' => 'require']); if(!$isValid->check($param)) return json_show(1004,$isValid->getError()); $record = Db::name('supplier_cat')->where(['id'=>$param['id'],'is_del'=>0])->find(); if(!$record) return json_show(1002,'未找到记录'); return json_show(0,'获取成功',$record); } }