From 101f77759601e7dad74795f96a1e4ad191bf1c00 Mon Sep 17 00:00:00 2001 From: weiz Date: Mon, 11 Dec 2023 17:57:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=A2=E6=88=B7=E9=9C=80=E6=B1=82=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E5=8A=9F=E8=83=BD=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lists/custom/CustomerDemandLists.php | 66 +++++++++++++++++-- .../logic/custom/CustomerDemandLogic.php | 24 +++++-- .../custom/CustomerDemandValidate.php | 51 +++++++++++++- app/common/model/custom/CustomerDemand.php | 8 ++- 4 files changed, 136 insertions(+), 13 deletions(-) diff --git a/app/adminapi/lists/custom/CustomerDemandLists.php b/app/adminapi/lists/custom/CustomerDemandLists.php index 3ec99a165..cd86d9a8a 100644 --- a/app/adminapi/lists/custom/CustomerDemandLists.php +++ b/app/adminapi/lists/custom/CustomerDemandLists.php @@ -16,8 +16,12 @@ namespace app\adminapi\lists\custom; use app\adminapi\lists\BaseAdminDataLists; +use app\common\model\custom\Custom; use app\common\model\custom\CustomerDemand; use app\common\lists\ListsSearchInterface; +use app\common\model\dept\Dept; +use app\common\model\dept\Orgs; +use app\common\model\project\Project; /** @@ -38,7 +42,8 @@ class CustomerDemandLists extends BaseAdminDataLists implements ListsSearchInter public function setSearch(): array { return [ - '=' => ['org_id', 'department_id', 'project_id', 'theme', 'supplier', 'supplier_contacts', 'importance', 'recording_time', 'demand_content', 'annex'], + '=' => ['importance', 'recording_time'], + '%like%' => ['theme', 'supplier', 'supplier_contacts'] ]; } @@ -54,11 +59,43 @@ class CustomerDemandLists extends BaseAdminDataLists implements ListsSearchInter */ public function lists(): array { - return CustomerDemand::where($this->searchWhere) - ->field(['id', 'org_id', 'department_id', 'project_id', 'theme', 'supplier', 'supplier_contacts', 'importance', 'recording_time', 'demand_content', 'annex']) + $params = $this->request->param(); + $where = []; + if(isset($params['org_name']) && $params['org_name'] != ''){ + $orgIds = Orgs::where('name','like','%'.$params['org_name'].'%')->column('id'); + $where[] = ['org_id','in',$orgIds]; + } + if(isset($params['dept_name']) && $params['dept_name'] != ''){ + $deptIds = Dept::where('name','like','%'.$params['dept_name'].'%')->column('id'); + $where[] = ['dept_id','in',$deptIds]; + } + if(isset($params['project_name']) && $params['project_name'] != ''){ + $projectIds = Project::where('name','like','%'.$params['project_name'].'%')->column('id'); + $where[] = ['project_id','in',$projectIds]; + } + if(isset($params['custom_name']) && $params['custom_name'] != ''){ + $customIds = Custom::where('name','like','%'.$params['custom_name'].'%')->column('id'); + $projectIds = Project::where('custom_id','in',$customIds)->column('id'); + $where[] = ['project_id','in',$projectIds]; + } + return CustomerDemand::where($this->searchWhere)->where($where) + ->field(['id', 'org_id', 'dept_id', 'project_id', 'theme', 'supplier', 'supplier_contacts', 'importance', 'recording_time', 'demand_content', 'annex']) ->limit($this->limitOffset, $this->limitLength) ->order(['id' => 'desc']) - ->select() + ->select()->each(function($item){ + $item['importance_text'] = $item->importance_text; + $item['recording_time'] = date('Y-m-d H:i:s',$item['recording_time']); + $org = Orgs::field('name')->where('id',$item['org_id'])->findOrEmpty(); + $dept = Dept::field('name')->where('id',$item['dept_id'])->findOrEmpty(); + $project = Project::field('name,project_code,custom_id')->where('id',$item['project_id'])->findOrEmpty(); + $custom = Custom::field('name')->where('id',$project['custom_id'])->findOrEmpty(); + $item['org_name'] = $org['name']; + $item['dept_name'] = $dept['name']; + $item['project_name'] = $project['name']; + $item['project_code'] = $project['project_code']; + $item['custom_name'] = $custom['name']; + return $item; + }) ->toArray(); } @@ -71,7 +108,26 @@ class CustomerDemandLists extends BaseAdminDataLists implements ListsSearchInter */ public function count(): int { - return CustomerDemand::where($this->searchWhere)->count(); + $params = $this->request->param(); + $where = []; + if(isset($params['org_name']) && $params['org_name'] != ''){ + $orgIds = Orgs::where('name','like','%'.$params['org_name'].'%')->column('id'); + $where[] = ['org_id','in',$orgIds]; + } + if(isset($params['dept_name']) && $params['dept_name'] != ''){ + $deptIds = Dept::where('name','like','%'.$params['dept_name'].'%')->column('id'); + $where[] = ['dept_id','in',$deptIds]; + } + if(isset($params['project_name']) && $params['project_name'] != ''){ + $projectIds = Project::where('name','like','%'.$params['project_name'].'%')->column('id'); + $where[] = ['project_id','in',$projectIds]; + } + if(isset($params['custom_name']) && $params['custom_name'] != ''){ + $customIds = Custom::where('name','like','%'.$params['custom_name'].'%')->column('id'); + $projectIds = Project::where('custom_id','in',$customIds)->column('id'); + $where[] = ['project_id','in',$projectIds]; + } + return CustomerDemand::where($this->searchWhere)->where($where)->count(); } } \ No newline at end of file diff --git a/app/adminapi/logic/custom/CustomerDemandLogic.php b/app/adminapi/logic/custom/CustomerDemandLogic.php index 4ccfb3445..7514ad2a0 100644 --- a/app/adminapi/logic/custom/CustomerDemandLogic.php +++ b/app/adminapi/logic/custom/CustomerDemandLogic.php @@ -15,8 +15,12 @@ namespace app\adminapi\logic\custom; +use app\common\model\custom\Custom; use app\common\model\custom\CustomerDemand; use app\common\logic\BaseLogic; +use app\common\model\dept\Dept; +use app\common\model\dept\Orgs; +use app\common\model\project\Project; use think\facade\Db; @@ -42,7 +46,7 @@ class CustomerDemandLogic extends BaseLogic try { CustomerDemand::create([ 'org_id' => $params['org_id'], - 'department_id' => $params['department_id'], + 'dept_id' => $params['dept_id'], 'project_id' => $params['project_id'], 'theme' => $params['theme'], 'supplier' => $params['supplier'], @@ -52,7 +56,6 @@ class CustomerDemandLogic extends BaseLogic 'demand_content' => $params['demand_content'], 'annex' => $params['annex'] ]); - Db::commit(); return true; } catch (\Exception $e) { @@ -76,7 +79,7 @@ class CustomerDemandLogic extends BaseLogic try { CustomerDemand::where('id', $params['id'])->update([ 'org_id' => $params['org_id'], - 'department_id' => $params['department_id'], + 'dept_id' => $params['dept_id'], 'project_id' => $params['project_id'], 'theme' => $params['theme'], 'supplier' => $params['supplier'], @@ -86,7 +89,6 @@ class CustomerDemandLogic extends BaseLogic 'demand_content' => $params['demand_content'], 'annex' => $params['annex'] ]); - Db::commit(); return true; } catch (\Exception $e) { @@ -119,6 +121,18 @@ class CustomerDemandLogic extends BaseLogic */ public static function detail($params): array { - return CustomerDemand::findOrEmpty($params['id'])->toArray(); + $data = CustomerDemand::field('id,org_id,dept_id,project_id,theme,supplier,supplier_contacts,importance,recording_time,demand_content,annex')->findOrEmpty($params['id']); + $data['importance_text'] = $data->importance_text; + $data['recording_time'] = date('Y-m-d H:i:s',$data['recording_time']); + $org = Orgs::field('name')->where('id',$data['org_id'])->findOrEmpty(); + $dept = Dept::field('name')->where('id',$data['dept_id'])->findOrEmpty(); + $project = Project::field('name,project_code,custom_id')->where('id',$data['project_id'])->findOrEmpty(); + $custom = Custom::field('name')->where('id',$project['custom_id'])->findOrEmpty(); + $data['org_name'] = $org['name']; + $data['dept_name'] = $dept['name']; + $data['project_name'] = $project['name']; + $data['project_code'] = $project['project_code']; + $data['custom_name'] = $custom['name']; + return $data->toArray(); } } \ No newline at end of file diff --git a/app/adminapi/validate/custom/CustomerDemandValidate.php b/app/adminapi/validate/custom/CustomerDemandValidate.php index 2fe22e408..6c8cfce91 100644 --- a/app/adminapi/validate/custom/CustomerDemandValidate.php +++ b/app/adminapi/validate/custom/CustomerDemandValidate.php @@ -15,6 +15,9 @@ namespace app\adminapi\validate\custom; +use app\common\model\dept\Dept; +use app\common\model\dept\Orgs; +use app\common\model\project\Project; use app\common\validate\BaseValidate; @@ -32,7 +35,24 @@ class CustomerDemandValidate extends BaseValidate */ protected $rule = [ 'id' => 'require', + 'org_id' => 'require|checkOrg', + 'dept_id' => 'require|checkDept', + 'project_id' => 'require|checkProject', + 'theme' => 'require', + 'importance' => 'integer|in:1,2,3,4', + 'recording_time' => 'date', ]; + + protected $message = [ + 'id.require' => '缺少必要参数', + 'org_id.require' => '请选择组织', + 'dept_id.require' => '请选择部门', + 'project_id.require' => '请选择项目', + 'theme.require' => '请填写需求主题', + 'importance.integer' => '重要程度数据格式错误', + 'importance.in' => '重要程度数据值错误', + 'recording_time.date' => '记录时间数据格式错误', + ]; /** @@ -64,7 +84,6 @@ class CustomerDemandValidate extends BaseValidate */ public function sceneEdit() { - return $this->only(['id']); } @@ -90,5 +109,35 @@ class CustomerDemandValidate extends BaseValidate { return $this->only(['id']); } + + public function checkOrg($value): bool|string + { + $org = Orgs::where('id',$value)->findOrEmpty(); + if($org->isEmpty()) { + return '组织不存在'; + } + return true; + } + + public function checkDept($value,$rule,$data): bool|string + { + $dept = Dept::where('id',$value)->findOrEmpty(); + if($dept->isEmpty()){ + return '部门不存在'; + } + if($dept['org_id'] != $data['org_id']){ + return '当前部门不属于所选择的组织'; + } + return true; + } + + public function checkProject($value): bool|string + { + $project = Project::where('id',$value)->findOrEmpty(); + if($project->isEmpty()){ + return '项目不存在'; + } + return true; + } } \ No newline at end of file diff --git a/app/common/model/custom/CustomerDemand.php b/app/common/model/custom/CustomerDemand.php index 7d4a36089..993c37312 100644 --- a/app/common/model/custom/CustomerDemand.php +++ b/app/common/model/custom/CustomerDemand.php @@ -29,6 +29,10 @@ class CustomerDemand extends BaseModel use SoftDelete; protected $name = 'customer_demand'; protected $deleteTime = 'delete_time'; - - + + public function getImportanceTextAttr($value,$data): string + { + $res = [1=>'非常重要',2=>'重要',3=>'一般',4=>'不重要']; + return $res[$data['importance']]; + } } \ No newline at end of file