| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- /**
- * crm模块下的通用功能逻辑类
- *
- * @author qifan
- * @date 2020-12-11
- */
-
- namespace app\crm\logic;
-
- use app\admin\controller\ApiCommon;
- use app\admin\model\User;
- use app\crm\model\Customer;
- use think\Db;
- use think\Validate;
-
- class CommonLogic
- {
- public $error = '操作失败!';
-
- /**
- * 快捷编辑【线索、客户、联系人、商机、合同、回款、回访、产品】
- *
- * @param $param
- * @return false|int|string
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public function quickEdit($param)
- {
- /**
- * $param['types'] 表名
- * $param['action_id'] 主键ID
- * $param['field'] 字段
- * $param['name'] 字段中文名,用作提示
- * $param['value] 字段值
- */
-
- $actionId = $param['action_id'];
- $types = $param['types'];
- unset($param['action_id']);
- unset($param['types']);
-
- # 模型
- $model = db($types);
-
- # 主键
- $primaryKey = '';
- // author guogaobo $item模块
- $info='';
- switch ($types) {
- case 'crm_leads' :
- $primaryKey = 'leads_id';
- $dataModel=new \app\crm\model\Leads();
- $info=$dataModel->getDataById($actionId);
- break;
- case 'crm_customer' :
- $primaryKey = 'customer_id';
- $info=db('crm_customer')->where('customer_id',$actionId)->find();
- break;
- case 'crm_contacts' :
- $primaryKey = 'contacts_id';
- $dataModel=new \app\crm\model\Contacts();
- $info=$dataModel->getDataById($actionId);
- break;
- case 'crm_business' :
- $primaryKey = 'business_id';
- $dataModel=new \app\crm\model\Business();
- $info=$dataModel->getDataById($actionId);
- break;
- case 'crm_contract' :
- $primaryKey = 'contract_id';
- $info=db('crm_contract')->where('customer_id',$actionId)->find();
- break;
- case 'crm_receivables' :
- $primaryKey = 'receivables_id';
- $info=db('crm_receivables')->where('customer_id',$actionId)->find();
- break;
- case 'crm_visit' :
- $primaryKey = 'visit_id';
- $dataModel=new \app\crm\logic\VisitLogic();
- $info=$dataModel->getDataById($actionId);
- break;
- case 'crm_product' :
- $primaryKey = 'product_id';
- $dataModel=new \app\crm\model\Product();
- $info=$dataModel->getDataById($actionId);
- break;
- }
- $apiCommon = new ApiCommon();
- $userModel = new User();
-
-
- if (in_array($types, ['crm_contract', 'crm_receivables'])) {
- $checkStatus = $model->where($primaryKey, $actionId)->value('check_status');
- if (!in_array($checkStatus, [4, 5, 6])) {
- $this->error = '只能编辑状态为撤销、草稿或作废的信息!';
- return false;
- }
- }
- # 产品修改验证
- if($types == 'crm_product'){
- foreach ($param['list'] as $val){
- $infoData=db('crm_product')->where(['name'=>$val['name'],'delete_user_id'=>0])->find();
- if(!empty($infoData)){
- $fieldModel = new \app\admin\model\Field();
- $validateArr = $fieldModel->validateField('crm_product'); //获取自定义字段验证规则
- $validate = new Validate($validateArr['rule'], $validateArr['message']);
- $result = $validate->check($val);
- if (!$result) {
- $this->error = $validate->getError();
- return false;
- }
- }
- }
- }
-
- # 客户模块快捷编辑权限验证
- if ($types == 'crm_customer') {
- $dataInfo = $model->field(['ro_user_id', 'rw_user_id', 'owner_user_id'])->where($primaryKey, $actionId)->find();
- $auth_user_ids = $userModel->getUserByPer('crm', 'customer', 'update');
- $rwPre = $userModel->rwPre($apiCommon->userInfo['id'], $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
- $wherePool = (new Customer())->getWhereByPool();
- $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $param['action_id']])->where($wherePool)->find();
- if ($resPool || (!in_array($dataInfo['owner_user_id'], $auth_user_ids) && !$rwPre)) {
- $this->error = '无权操作!';
- return false;
- }
- }
-
- # 商机模块快捷编辑权限验证
- if ($types == 'crm_business') {
- $dataInfo = $model->field(['ro_user_id', 'rw_user_id', 'owner_user_id'])->where($primaryKey, $actionId)->find();
- $auth_user_ids = $userModel->getUserByPer('crm', 'business', 'update');
- $rwPre = $userModel->rwPre($apiCommon->userInfo['id'], $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
- if (!in_array($dataInfo['owner_user_id'], $auth_user_ids) && !$rwPre) {
- $this->error = '无权操作!';
- return false;
- }
- }
-
- # 合同模块快捷编辑权限验证
- if ($types == 'crm_contract') {
- $dataInfo = $model->field(['ro_user_id', 'rw_user_id', 'owner_user_id'])->where($primaryKey, $actionId)->find();
- $auth_user_ids = $userModel->getUserByPer('crm', 'contract', 'update');
- $rwPre = $userModel->rwPre($apiCommon->userInfo['id'], $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
- if (!in_array($dataInfo['owner_user_id'], $auth_user_ids) && !$rwPre) {
- $this->error = '无权操作!';
- return false;
- }
- }
-
- $fieldModel = new \app\admin\model\Field();
- # 日期时间类型
- $datetimeField = $fieldModel->getFieldByFormType($types, 'datetime');
- # 附件类型
- $fileField = $fieldModel->getFieldByFormType($types, 'file');
- # 多选类型
- $checkboxField = $fieldModel->getFieldByFormType($types, 'checkbox');
- # 人员类型
- $userField = $fieldModel->getFieldByFormType($types, 'user');
- # 部门类型
- $structureField = $fieldModel->getFieldByFormType($types, 'structure');
-
- foreach ($param['list'] as $key => $value) {
- foreach ($value as $k => $v) {
- # 查询自定义字段信息
- $fieldInfo = Db::name('admin_field')->field(['max_length', 'is_unique', 'is_null', 'name'])
- ->where('types', $types)->where('field', $k)->find();
-
- # 字符长度
- if (!empty($fieldInfo['max_length']) && strlen($v) > $fieldInfo['max_length']) {
- $this->error = $fieldInfo['name'] . ' 字符长度不能超过 ' . $fieldInfo['max_length'] . ' 个字符!';
- return false;
- }
-
- # 必填
- if (!empty($fieldInfo['is_null']) && empty($v)) {
- $this->error = $fieldInfo['name'] . ' 是必填信息,不能为空!';
- return false;
- }
-
- # 唯一
- if (!empty($fieldInfo['is_unique']) && $model->where([$primaryKey => ['neq', $actionId]])->where($k, $v)->value($primaryKey)) {
- $this->error = $fieldInfo['name'] . ' 内容重复!';
- return false;
- }
- }
- }
-
- # 编辑参数
- $data = [];
- $item=[];
- if (!empty($param['list'])) {
- foreach ($param['list'] as $key => $value) {
- foreach ($value as $k => $v) {
- if ($k == 'next_time' || in_array($k, $datetimeField)) {
- # 处理下次联系时间格式、datetime类型数据
- $data[$k] = !empty($v) ? strtotime($v) : '';
- } elseif ($types == 'crm_product' && $k == 'category_id') {
- # 处理产品类别
- $categorys = explode(',', $v);
- $data[$k] = $categorys[count($categorys) - 1];
- } elseif (in_array($k, $fileField)) {
- # 处理附件类型数据
- $fileArray = [];
- foreach ($v AS $kk => $vv) {
- if (!empty($vv['file_id'])) $fileArray[] = $vv['file_id'];
- }
- if (!empty($fileArray)) $data[$k] = arrayToString($fileArray);
- } elseif (in_array($k, $checkboxField)) {
- # 处理多选类型数据
- $data[$k] = arrayToString($v);
- } elseif (in_array($k, $userField)) {
- # 处理人员类型数据
- $userArray = [];
- foreach ($v AS $kk => $vv) {
- if (!empty($vv['id'])) $userArray[] = $vv['id'];
- }
- $data[$k] = !empty($userArray) ? arrayToString($userArray) : '';
- } elseif (in_array($k, $structureField)) {
- # 处理部门类型数据
- $structureArray = [];
- foreach ($v AS $kk => $vv) {
- if (!empty($vv['id'])) $structureArray[] = $vv['id'];
- }
- $data[$k] = !empty($structureArray) ? arrayToString($structureArray) : '';
- } elseif ($types == 'crm_visit' && $k == 'contract_id') {
- # 处理回访提交过来的合同编号
- if (!empty($v[0]['contract_id'])) $data[$k] = $v[0]['contract_id'];
- }else {
- $data[$k] = $v;
- }
- }
- $item=$value;
- }
- $data[$primaryKey] = $actionId;
- $data['update_time'] = time();
- }
- $res = $model->update($data);
- //详细信息修改新增操作记录
- if ($res) {
- //修改记录
- $user_id = $apiCommon->userInfo;
- updateActionLog($user_id['id'], $types, $actionId, $info, $item);
- }
- return $res;
- }
- }
|