CommonLogic.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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\admin\traits\FieldVerificationTrait;
  12. use app\crm\model\Customer;
  13. use think\Db;
  14. use think\Validate;
  15. class CommonLogic
  16. {
  17. use FieldVerificationTrait;
  18. public $error = '操作失败!';
  19. /**
  20. * 快捷编辑【线索、客户、联系人、商机、合同、回款、发票、回访、产品】
  21. *
  22. * @param $param
  23. * @return false|int|string
  24. * @throws \think\Exception
  25. * @throws \think\db\exception\DataNotFoundException
  26. * @throws \think\db\exception\ModelNotFoundException
  27. * @throws \think\exception\DbException
  28. * @throws \think\exception\PDOException
  29. */
  30. public function quickEdit($param)
  31. {
  32. /**
  33. * $param['types'] 表名
  34. * $param['action_id'] 主键ID
  35. * $param['field'] 字段
  36. * $param['name'] 字段中文名,用作提示
  37. * $param['value] 字段值
  38. */
  39. $actionId = $param['action_id'];
  40. $types = $param['types'];
  41. unset($param['action_id']);
  42. unset($param['types']);
  43. # 模型
  44. $model = db($types);
  45. # 主键
  46. $primaryKey = getPrimaryKeyName($types);
  47. $info='';
  48. switch ($types) {
  49. case 'crm_leads' :
  50. $dataModel=new \app\crm\model\Leads();
  51. $info=$dataModel->getDataById($actionId);
  52. break;
  53. case 'crm_customer' :
  54. $info=db('crm_customer')->where('customer_id',$actionId)->find();
  55. break;
  56. case 'crm_contacts' :
  57. $dataModel=new \app\crm\model\Contacts();
  58. $info=$dataModel->getDataById($actionId);
  59. break;
  60. case 'crm_business' :
  61. $dataModel=new \app\crm\model\Business();
  62. $info=$dataModel->getDataById($actionId);
  63. break;
  64. case 'crm_contract' :
  65. $info=db('crm_contract')->where('customer_id',$actionId)->find();
  66. break;
  67. case 'crm_receivables' :
  68. $info=db('crm_receivables')->where('customer_id',$actionId)->find();
  69. break;
  70. case 'crm_invoice' :
  71. $info = $model->where($primaryKey, $actionId)->find();
  72. break;
  73. case 'crm_visit' :
  74. $dataModel=new \app\crm\logic\VisitLogic();
  75. $info=$dataModel->getDataById($actionId);
  76. break;
  77. case 'crm_product' :
  78. $dataModel=new \app\crm\model\Product();
  79. $info=$dataModel->getDataById($actionId);
  80. break;
  81. }
  82. $apiCommon = new ApiCommon();
  83. $userModel = new User();
  84. $userInfo = $apiCommon->userInfo;
  85. if (in_array($types, ['crm_contract', 'crm_receivables'])) {
  86. $checkStatus = $model->where($primaryKey, $actionId)->value('check_status');
  87. if (!in_array($checkStatus, [4, 5, 6])) {
  88. $this->error = '只能编辑状态为撤销、草稿或作废的信息!';
  89. return false;
  90. }
  91. }
  92. // 数据验证
  93. $validateData = [];
  94. if (!empty($param['list'])) {
  95. foreach ($param['list'] AS $key => $value) {
  96. foreach ($value AS $k => $v) {
  97. $validateData[$k] = $v;
  98. }
  99. }
  100. }
  101. $validateResult = $this->fieldDataValidate($validateData, $types, $userInfo['id'], $actionId);
  102. if (!empty($validateResult)) {
  103. $this->error = $validateResult;
  104. return false;
  105. }
  106. # 产品修改验证
  107. if($types == 'crm_product'){
  108. foreach ($param['list'] as $val){
  109. $infoData=db('crm_product')->where(['name'=>$val['name'],'delete_user_id'=>0])->find();
  110. if(!empty($infoData)){
  111. $fieldModel = new \app\admin\model\Field();
  112. $validateArr = $fieldModel->validateField('crm_product'); //获取自定义字段验证规则
  113. $validate = new Validate($validateArr['rule'], $validateArr['message']);
  114. $result = $validate->check($val);
  115. if (!$result) {
  116. $this->error = $validate->getError();
  117. return false;
  118. }
  119. }
  120. }
  121. }
  122. # 客户模块快捷编辑权限验证
  123. if ($types == 'crm_customer') {
  124. $dataInfo = $model->field(['ro_user_id', 'rw_user_id', 'owner_user_id'])->where($primaryKey, $actionId)->find();
  125. $auth_user_ids = $userModel->getUserByPer('crm', 'customer', 'update');
  126. $rwPre = $userModel->rwPre($apiCommon->userInfo['id'], $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  127. $wherePool = (new Customer())->getWhereByPool();
  128. $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $param['action_id']])->where($wherePool)->find();
  129. if ($resPool || (!in_array($dataInfo['owner_user_id'], $auth_user_ids) && !$rwPre)) {
  130. $this->error = '无权操作!';
  131. return false;
  132. }
  133. }
  134. # 商机模块快捷编辑权限验证
  135. if ($types == 'crm_business') {
  136. $dataInfo = $model->field(['ro_user_id', 'rw_user_id', 'owner_user_id'])->where($primaryKey, $actionId)->find();
  137. $auth_user_ids = $userModel->getUserByPer('crm', 'business', 'update');
  138. $rwPre = $userModel->rwPre($apiCommon->userInfo['id'], $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  139. if (!in_array($dataInfo['owner_user_id'], $auth_user_ids) && !$rwPre) {
  140. $this->error = '无权操作!';
  141. return false;
  142. }
  143. }
  144. # 合同模块快捷编辑权限验证
  145. if ($types == 'crm_contract') {
  146. $dataInfo = $model->field(['ro_user_id', 'rw_user_id', 'owner_user_id'])->where($primaryKey, $actionId)->find();
  147. $auth_user_ids = $userModel->getUserByPer('crm', 'contract', 'update');
  148. $rwPre = $userModel->rwPre($apiCommon->userInfo['id'], $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  149. if (!in_array($dataInfo['owner_user_id'], $auth_user_ids) && !$rwPre) {
  150. $this->error = '无权操作!';
  151. return false;
  152. }
  153. }
  154. $fieldModel = new \app\admin\model\Field();
  155. # 日期时间类型
  156. $datetimeField = $fieldModel->getFieldByFormType($types, 'datetime');
  157. # 附件类型
  158. $fileField = $fieldModel->getFieldByFormType($types, 'file');
  159. # 多选类型
  160. $checkboxField = $fieldModel->getFieldByFormType($types, 'checkbox');
  161. # 人员类型
  162. $userField = $fieldModel->getFieldByFormType($types, 'user');
  163. # 部门类型
  164. $structureField = $fieldModel->getFieldByFormType($types, 'structure');
  165. # 地址
  166. $positionField = $fieldModel->getFieldByFormType($types, 'position');
  167. # 定位
  168. $locationField = $fieldModel->getFieldByFormType($types, 'location');
  169. # 日期区间
  170. $dateIntervalField = $fieldModel->getFieldByFormType($types, 'date_interval');
  171. # 手写签名
  172. $handwritingField = $fieldModel->getFieldByFormType($types, 'handwriting_sign');
  173. # 明细表格
  174. $detailTableField = $fieldModel->getFieldByFormType($types, 'detail_table');
  175. # 处理数据 data 常规数据 extraData 扩展数据(地址、定位、日期区间、详细表格)
  176. $data = [];
  177. $extraData = [];
  178. $deleteExtraWhere = [];
  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) && $v == strtotime($v) ? strtotime($v) : $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) || in_array($k, $checkboxField) || in_array($k, $userField) || in_array($k, $structureField)) {
  190. # 处理附件、多选、人员、部门类型数据
  191. $data[$k] = !empty($v) ? arrayToString($v) : '';
  192. } elseif ($types == 'crm_visit' && $k == 'contract_id') {
  193. # 处理回访提交过来的合同编号
  194. if (!empty($v[0]['contract_id'])) $data[$k] = $v[0]['contract_id'];
  195. } elseif (in_array($k, $handwritingField)) {
  196. // 手写签名
  197. $data[$k] = !empty($v['file_id']) ? $v['file_id'] : 0;
  198. } elseif (in_array($k, $positionField)) {
  199. // 地址
  200. if (!empty($v)) {
  201. $extraData[] = [
  202. $primaryKey => $actionId,
  203. 'field' => $k,
  204. 'content' => json_encode($v),
  205. 'create_time' => time()
  206. ];
  207. $positionNames = array_column($v, 'name');
  208. $data[$k] = implode(',', $positionNames);
  209. } else {
  210. $data[$k] = '';
  211. }
  212. $deleteExtraWhere[] = $k;
  213. } elseif (in_array($k, $locationField)) {
  214. // 定位
  215. if (!empty($v)) {
  216. $extraData[] = [
  217. $primaryKey => $actionId,
  218. 'field' => $k,
  219. 'content' => json_encode($v),
  220. 'create_time' => time()
  221. ];
  222. $data[$k] = $v['address'];
  223. } else {
  224. $data[$k] = '';
  225. }
  226. $deleteExtraWhere[] = $k;
  227. } elseif (in_array($k, $dateIntervalField)) {
  228. // 日期区间
  229. if (!empty($v)) {
  230. $extraData[] = [
  231. $primaryKey => $actionId,
  232. 'field' => $k,
  233. 'content' => json_encode($v),
  234. 'create_time' => time()
  235. ];
  236. $data[$k] = implode('_', $v);
  237. } else {
  238. $data[$k] = '';
  239. }
  240. $deleteExtraWhere[] = $k;
  241. } elseif (in_array($k, $detailTableField)) {
  242. // 明细表格
  243. if (!empty($v)) {
  244. $extraData[] = [
  245. $primaryKey => $actionId,
  246. 'field' => $k,
  247. 'content' => json_encode($v),
  248. 'create_time' => time()
  249. ];
  250. }
  251. $deleteExtraWhere[] = $k;
  252. } else {
  253. $data[$k] = $v;
  254. }
  255. }
  256. }
  257. $data[$primaryKey] = $actionId;
  258. $data['update_time'] = time();
  259. }
  260. $res = $model->update($data);
  261. unset($data[$primaryKey]);
  262. unset($data['update_time']);
  263. // 详细信息修改新增操作记录、处理扩展数据
  264. if ($res) {
  265. // 删除扩展数据
  266. if (!empty($deleteExtraWhere)) db($types . '_data')->where([$primaryKey => $actionId, 'field' => ['in', $deleteExtraWhere]])->delete();
  267. // 添加扩展数据
  268. if (!empty($extraData)) db($types . '_data')->insertAll($extraData);
  269. // 修改记录
  270. updateActionLog($userInfo['id'], $types, $actionId, $info, $data);
  271. RecordActionLog($userInfo['id'], $types, 'update',$info['name'], $info, $data);
  272. }
  273. return $res;
  274. }
  275. }