diff --git a/app/adminapi/lists/project/CompetitorLists.php b/app/adminapi/lists/project/CompetitorLists.php index e495f4e96..4df674f71 100644 --- a/app/adminapi/lists/project/CompetitorLists.php +++ b/app/adminapi/lists/project/CompetitorLists.php @@ -16,8 +16,13 @@ namespace app\adminapi\lists\project; use app\adminapi\lists\BaseAdminDataLists; +use app\common\model\custom\Custom; +use app\common\model\custom\CustomerDemand; +use app\common\model\dept\Dept; +use app\common\model\dept\Orgs; use app\common\model\project\Competitor; use app\common\lists\ListsSearchInterface; +use app\common\model\project\Project; /** @@ -38,7 +43,7 @@ class CompetitorLists extends BaseAdminDataLists implements ListsSearchInterface public function setSearch(): array { return [ - '=' => ['project_id', 'competitor_name'], + '%like%' => ['competitor_name','competitor_contacts','competitor_contacts_phone'], ]; } @@ -54,11 +59,36 @@ class CompetitorLists extends BaseAdminDataLists implements ListsSearchInterface */ public function lists(): array { - return Competitor::where($this->searchWhere) - ->field(['id', 'org_id', 'department_id', 'project_id', 'customer_demand_id', 'competitor_name', 'competitor_contacts', 'competitor_contacts_phone', 'competitive_power']) + $params = $this->request->param(); + $where = []; + 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]; + } + if(isset($params['customer_demand_name']) && $params['customer_demand_name'] != ''){ + $demandIds = CustomerDemand::where('theme','like','%'.$params['customer_demand_name'].'%')->column('id'); + $where[] = ['customer_demand_id','in',$demandIds]; + } + return Competitor::where($this->searchWhere)->where($where) + ->field(['id','project_id','customer_demand_id','competitor_name','competitor_contacts','competitor_contacts_phone','competitive_power','remark']) ->limit($this->limitOffset, $this->limitLength) ->order(['id' => 'desc']) - ->select() + ->select()->each(function($item){ + $org = Orgs::field('name')->where('id',$item['org_id'])->findOrEmpty(); + $dept = Dept::field('name')->where('id',$item['dept_id'])->findOrEmpty(); + $project = Project::field('name,custom_id')->where('id',$item['project_id'])->findOrEmpty(); + $custom = Custom::field('name')->where('id',$project['custom_id'])->findOrEmpty(); + $demand = CustomerDemand::field('theme')->where('id',$item['customer_demand_id'])->findOrEmpty(); + $item['project_name'] = $project['name']; + $item['custom_name'] = $custom['name']; + $item['customer_demand_name'] = $demand['theme']; + return $item; + }) ->toArray(); } @@ -71,7 +101,22 @@ class CompetitorLists extends BaseAdminDataLists implements ListsSearchInterface */ public function count(): int { - return Competitor::where($this->searchWhere)->count(); + $params = $this->request->param(); + $where = []; + 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]; + } + if(isset($params['customer_demand_name']) && $params['customer_demand_name'] != ''){ + $demandIds = CustomerDemand::where('theme','like','%'.$params['customer_demand_name'].'%')->column('id'); + $where[] = ['customer_demand_id','in',$demandIds]; + } + return Competitor::where($this->searchWhere)->where($where)->count(); } } \ No newline at end of file diff --git a/app/adminapi/logic/project/CompetitorLogic.php b/app/adminapi/logic/project/CompetitorLogic.php index a2174ca47..5a006ff8f 100644 --- a/app/adminapi/logic/project/CompetitorLogic.php +++ b/app/adminapi/logic/project/CompetitorLogic.php @@ -15,11 +15,15 @@ namespace app\adminapi\logic\project; +use app\common\model\custom\Custom; +use app\common\model\custom\CustomerDemand; +use app\common\model\dept\Dept; +use app\common\model\dept\Orgs; use app\common\model\project\Competitor; use app\common\logic\BaseLogic; +use app\common\model\project\Project; use think\facade\Db; - /** * 竞争对手逻辑 * Class CompetitorLogic @@ -27,8 +31,6 @@ use think\facade\Db; */ class CompetitorLogic extends BaseLogic { - - /** * @notes 添加竞争对手 * @param array $params @@ -42,7 +44,7 @@ class CompetitorLogic extends BaseLogic try { Competitor::create([ 'org_id' => $params['org_id'], - 'department_id' => $params['department_id'], + 'dept_id' => $params['dept_id'], 'project_id' => $params['project_id'], 'customer_demand_id' => $params['customer_demand_id'], 'competitor_name' => $params['competitor_name'], @@ -54,7 +56,6 @@ class CompetitorLogic extends BaseLogic 'remark' => $params['remark'], 'annex' => $params['annex'] ]); - Db::commit(); return true; } catch (\Exception $e) { @@ -78,7 +79,7 @@ class CompetitorLogic extends BaseLogic try { Competitor::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'], 'customer_demand_id' => $params['customer_demand_id'], 'competitor_name' => $params['competitor_name'], @@ -123,6 +124,19 @@ class CompetitorLogic extends BaseLogic */ public static function detail($params): array { - return Competitor::findOrEmpty($params['id'])->toArray(); + $field = 'id,org_id,dept_id,project_id,customer_demand_id,competitor_name,competitor_contacts,competitor_contacts_phone,competitive_power,competitor_advantages,competitor_disadvantages,remark,annex'; + $data = Competitor::field($field)->findOrEmpty($params['id'])->toArray(); + $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(); + $demand = CustomerDemand::field('theme')->where('id',$data['customer_demand_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']; + $data['customer_demand_name'] = $demand['theme']; + return $data; } } \ No newline at end of file diff --git a/app/adminapi/validate/project/CompetitorValidate.php b/app/adminapi/validate/project/CompetitorValidate.php index a055ab94d..45577389c 100644 --- a/app/adminapi/validate/project/CompetitorValidate.php +++ b/app/adminapi/validate/project/CompetitorValidate.php @@ -15,6 +15,10 @@ namespace app\adminapi\validate\project; +use app\common\model\custom\CustomerDemand; +use app\common\model\dept\Dept; +use app\common\model\dept\Orgs; +use app\common\model\project\Project; use app\common\validate\BaseValidate; @@ -32,9 +36,24 @@ class CompetitorValidate extends BaseValidate */ protected $rule = [ 'id' => 'require', + 'org_id' => 'require|checkOrg', + 'dept_id' => 'require|checkDept', + 'project_id' => 'require|checkProject', + 'customer_demand_id' => 'require|checkCustomerDemand', + 'competitor_name' => 'require', + 'competitor_contacts_phone' =>'mobile' ]; - - + + protected $message = [ + 'id.require' => '缺少必要参数', + 'org_id.require' => '请选择组织', + 'dept_id.require' => '请选择部门', + 'project_id.require' => '请选择项目', + 'customer_demand_id.require' => '请选择客户需求', + 'competitor_name.require' => '请填写竞争对手名称', + 'competitor_contacts_phone.mobile' => '联系电话格式错误' + ]; + /** * 参数描述 * @var string[] @@ -63,9 +82,7 @@ class CompetitorValidate extends BaseValidate * @date 2023/11/24 22:01 */ public function sceneEdit() - { - return $this->only(['id']); - } + {} /** @@ -90,5 +107,47 @@ class CompetitorValidate 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; + } + + public function checkCustomerDemand($value,$rule,$data): bool|string + { + $customDemand = CustomerDemand::where('id',$value)->findOrEmpty(); + if($customDemand->isEmpty()){ + return '客户需求不存在'; + } + if($customDemand['project_id'] != $data['project_id']){ + return '客户需求与项目信息不一致'; + } + return true; + } } \ No newline at end of file