|
@@ -0,0 +1,126 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\admin\controller;
|
|
|
+
|
|
|
+use app\abutment\controller\Record;
|
|
|
+use think\facade\Db;
|
|
|
+use think\facade\Validate;
|
|
|
+
|
|
|
+class SupplierCat extends Base
|
|
|
+{
|
|
|
+ // 创建
|
|
|
+ public function add(){
|
|
|
+ $param = $this->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.'_'.$thirdLevelCat;
|
|
|
+
|
|
|
+ $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, 'creater'=>'','status'=>'','cat_id'=>'']);
|
|
|
+ $where = [['sc.is_del','=','0']];
|
|
|
+
|
|
|
+ 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' => ""]);
|
|
|
+ $isValid = Validate::rule(['id' => 'require', 'status' => '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,'未找到记录');
|
|
|
+ if(!is_array($param['status'],[0,1])) return json_show(1002,"参数status值不合法");
|
|
|
+
|
|
|
+ $record['status'] = $param['status'];
|
|
|
+ $record['updatetime'] = date('Y-m-d H:i:s');
|
|
|
+
|
|
|
+ $isUpdate = Db::name('supplier_cat')->save($record);
|
|
|
+ $message = $param['status'] == 1 ? "启用成功" : "禁用成功";
|
|
|
+ return $isUpdate ? json_show(0, $message) : json_show(1004,$message);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 详情
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+}
|