123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * crm模块下的通用功能逻辑类
  4. *
  5. * @author qifan
  6. * @date 2020-12-11
  7. */
  8. namespace app\crm\logic;
  9. use app\admin\controller\ApiCommon;
  10. use app\admin\model\User;
  11. use app\crm\model\Customer;
  12. use think\Db;
  13. use think\Validate;
  14. class CommonLogic
  15. {
  16. public $error = '操作失败!';
  17. /**
  18. * 快捷编辑【线索、客户、联系人、商机、合同、回款、回访、产品】
  19. *
  20. * @param $param
  21. * @return false|int|string
  22. * @throws \think\Exception
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. * @throws \think\exception\DbException
  26. * @throws \think\exception\PDOException
  27. */
  28. public function quickEdit($param)
  29. {
  30. /**
  31. * $param['types'] 表名
  32. * $param['action_id'] 主键ID
  33. * $param['field'] 字段
  34. * $param['name'] 字段中文名,用作提示
  35. * $param['value] 字段值
  36. */
  37. $actionId = $param['action_id'];
  38. $types = $param['types'];
  39. unset($param['action_id']);
  40. unset($param['types']);
  41. # 模型
  42. $model = db($types);
  43. # 主键
  44. $primaryKey = '';
  45. // author guogaobo $item模块
  46. $info='';
  47. switch ($types) {
  48. case 'crm_leads' :
  49. $primaryKey = 'leads_id';
  50. $dataModel=new \app\crm\model\Leads();
  51. $info=$dataModel->getDataById($actionId);
  52. break;
  53. case 'crm_customer' :
  54. $primaryKey = 'customer_id';
  55. $info=db('crm_customer')->where('customer_id',$actionId)->find();
  56. break;
  57. case 'crm_contacts' :
  58. $primaryKey = 'contacts_id';
  59. $dataModel=new \app\crm\model\Contacts();
  60. $info=$dataModel->getDataById($actionId);
  61. break;
  62. case 'crm_business' :
  63. $primaryKey = 'business_id';
  64. $dataModel=new \app\crm\model\Business();
  65. $info=$dataModel->getDataById($actionId);
  66. break;
  67. case 'crm_contract' :
  68. $primaryKey = 'contract_id';
  69. $info=db('crm_contract')->where('customer_id',$actionId)->find();
  70. break;
  71. case 'crm_receivables' :
  72. $primaryKey = 'receivables_id';
  73. $info=db('crm_receivables')->where('customer_id',$actionId)->find();
  74. break;
  75. case 'crm_visit' :
  76. $primaryKey = 'visit_id';
  77. $dataModel=new \app\crm\logic\VisitLogic();
  78. $info=$dataModel->getDataById($actionId);
  79. break;
  80. case 'crm_product' :
  81. $primaryKey = 'product_id';
  82. $dataModel=new \app\crm\model\Product();
  83. $info=$dataModel->getDataById($actionId);
  84. break;
  85. }
  86. $apiCommon = new ApiCommon();
  87. $userModel = new User();
  88. if (in_array($types, ['crm_contract', 'crm_receivables'])) {
  89. $checkStatus = $model->where($primaryKey, $actionId)->value('check_status');
  90. if (!in_array($checkStatus, [4, 5, 6])) {
  91. $this->error = '只能编辑状态为撤销、草稿或作废的信息!';
  92. return false;
  93. }
  94. }
  95. # 产品修改验证
  96. if($types == 'crm_product'){
  97. foreach ($param['list'] as $val){
  98. $infoData=db('crm_product')->where(['name'=>$val['name'],'delete_user_id'=>0])->find();
  99. if(!empty($infoData)){
  100. $fieldModel = new \app\admin\model\Field();
  101. $validateArr = $fieldModel->validateField('crm_product'); //获取自定义字段验证规则
  102. $validate = new Validate($validateArr['rule'], $validateArr['message']);
  103. $result = $validate->check($val);
  104. if (!$result) {
  105. $this->error = $validate->getError();
  106. return false;
  107. }
  108. }
  109. }
  110. }
  111. # 客户模块快捷编辑权限验证
  112. if ($types == 'crm_customer') {
  113. $dataInfo = $model->field(['ro_user_id', 'rw_user_id', 'owner_user_id'])->where($primaryKey, $actionId)->find();
  114. $auth_user_ids = $userModel->getUserByPer('crm', 'customer', 'update');
  115. $rwPre = $userModel->rwPre($apiCommon->userInfo['id'], $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  116. $wherePool = (new Customer())->getWhereByPool();
  117. $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $param['action_id']])->where($wherePool)->find();
  118. if ($resPool || (!in_array($dataInfo['owner_user_id'], $auth_user_ids) && !$rwPre)) {
  119. $this->error = '无权操作!';
  120. return false;
  121. }
  122. }
  123. # 商机模块快捷编辑权限验证
  124. if ($types == 'crm_business') {
  125. $dataInfo = $model->field(['ro_user_id', 'rw_user_id', 'owner_user_id'])->where($primaryKey, $actionId)->find();
  126. $auth_user_ids = $userModel->getUserByPer('crm', 'business', 'update');
  127. $rwPre = $userModel->rwPre($apiCommon->userInfo['id'], $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  128. if (!in_array($dataInfo['owner_user_id'], $auth_user_ids) && !$rwPre) {
  129. $this->error = '无权操作!';
  130. return false;
  131. }
  132. }
  133. # 合同模块快捷编辑权限验证
  134. if ($types == 'crm_contract') {
  135. $dataInfo = $model->field(['ro_user_id', 'rw_user_id', 'owner_user_id'])->where($primaryKey, $actionId)->find();
  136. $auth_user_ids = $userModel->getUserByPer('crm', 'contract', 'update');
  137. $rwPre = $userModel->rwPre($apiCommon->userInfo['id'], $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  138. if (!in_array($dataInfo['owner_user_id'], $auth_user_ids) && !$rwPre) {
  139. $this->error = '无权操作!';
  140. return false;
  141. }
  142. }
  143. $fieldModel = new \app\admin\model\Field();
  144. # 日期时间类型
  145. $datetimeField = $fieldModel->getFieldByFormType($types, 'datetime');
  146. # 附件类型
  147. $fileField = $fieldModel->getFieldByFormType($types, 'file');
  148. # 多选类型
  149. $checkboxField = $fieldModel->getFieldByFormType($types, 'checkbox');
  150. # 人员类型
  151. $userField = $fieldModel->getFieldByFormType($types, 'user');
  152. # 部门类型
  153. $structureField = $fieldModel->getFieldByFormType($types, 'structure');
  154. foreach ($param['list'] as $key => $value) {
  155. foreach ($value as $k => $v) {
  156. # 查询自定义字段信息
  157. $fieldInfo = Db::name('admin_field')->field(['max_length', 'is_unique', 'is_null', 'name'])
  158. ->where('types', $types)->where('field', $k)->find();
  159. # 字符长度
  160. if (!empty($fieldInfo['max_length']) && strlen($v) > $fieldInfo['max_length']) {
  161. $this->error = $fieldInfo['name'] . ' 字符长度不能超过 ' . $fieldInfo['max_length'] . ' 个字符!';
  162. return false;
  163. }
  164. # 必填
  165. if (!empty($fieldInfo['is_null']) && empty($v)) {
  166. $this->error = $fieldInfo['name'] . ' 是必填信息,不能为空!';
  167. return false;
  168. }
  169. # 唯一
  170. if (!empty($fieldInfo['is_unique']) && $model->where([$primaryKey => ['neq', $actionId]])->where($k, $v)->value($primaryKey)) {
  171. $this->error = $fieldInfo['name'] . ' 内容重复!';
  172. return false;
  173. }
  174. }
  175. }
  176. # 编辑参数
  177. $data = [];
  178. $item=[];
  179. if (!empty($param['list'])) {
  180. foreach ($param['list'] as $key => $value) {
  181. foreach ($value as $k => $v) {
  182. if ($k == 'next_time' || in_array($k, $datetimeField)) {
  183. # 处理下次联系时间格式、datetime类型数据
  184. $data[$k] = !empty($v) ? strtotime($v) : '';
  185. } elseif ($types == 'crm_product' && $k == 'category_id') {
  186. # 处理产品类别
  187. $categorys = explode(',', $v);
  188. $data[$k] = $categorys[count($categorys) - 1];
  189. } elseif (in_array($k, $fileField)) {
  190. # 处理附件类型数据
  191. $fileArray = [];
  192. foreach ($v AS $kk => $vv) {
  193. if (!empty($vv['file_id'])) $fileArray[] = $vv['file_id'];
  194. }
  195. if (!empty($fileArray)) $data[$k] = arrayToString($fileArray);
  196. } elseif (in_array($k, $checkboxField)) {
  197. # 处理多选类型数据
  198. $data[$k] = arrayToString($v);
  199. } elseif (in_array($k, $userField)) {
  200. # 处理人员类型数据
  201. $userArray = [];
  202. foreach ($v AS $kk => $vv) {
  203. if (!empty($vv['id'])) $userArray[] = $vv['id'];
  204. }
  205. $data[$k] = !empty($userArray) ? arrayToString($userArray) : '';
  206. } elseif (in_array($k, $structureField)) {
  207. # 处理部门类型数据
  208. $structureArray = [];
  209. foreach ($v AS $kk => $vv) {
  210. if (!empty($vv['id'])) $structureArray[] = $vv['id'];
  211. }
  212. $data[$k] = !empty($structureArray) ? arrayToString($structureArray) : '';
  213. } elseif ($types == 'crm_visit' && $k == 'contract_id') {
  214. # 处理回访提交过来的合同编号
  215. if (!empty($v[0]['contract_id'])) $data[$k] = $v[0]['contract_id'];
  216. }else {
  217. $data[$k] = $v;
  218. }
  219. }
  220. $item=$value;
  221. }
  222. $data[$primaryKey] = $actionId;
  223. $data['update_time'] = time();
  224. }
  225. $res = $model->update($data);
  226. //详细信息修改新增操作记录
  227. if ($res) {
  228. //修改记录
  229. $user_id = $apiCommon->userInfo;
  230. updateActionLog($user_id['id'], $types, $actionId, $info, $item);
  231. }
  232. return $res;
  233. }
  234. }