CustomerConfig.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 客户扩展设置
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\crm\model;
  8. use app\admin\controller\ApiCommon;
  9. use think\Db;
  10. use app\admin\model\Common;
  11. use think\Request;
  12. use think\Validate;
  13. class CustomerConfig extends Common
  14. {
  15. /**
  16. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  17. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  18. */
  19. protected $name = 'crm_customer_config';
  20. protected $createTime = 'create_time';
  21. protected $updateTime = 'update_time';
  22. protected $autoWriteTimestamp = true;
  23. /**
  24. * 列表数据
  25. *
  26. * @param $request
  27. * @return array
  28. * @throws \think\Exception
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. * @throws \think\exception\DbException
  32. */
  33. public function getDataList($request)
  34. {
  35. $userModel = new \app\admin\model\User();
  36. $structureModel = new \app\admin\model\Structure();
  37. $request = $this->fmtRequest( $request );
  38. $map = $request['map'] ? : [];
  39. $order = 'update_time desc'; //排序
  40. $list = $this
  41. ->where($map)
  42. ->page($request['page'], $request['limit'])
  43. ->order($order)
  44. ->select();
  45. foreach ($list as $k=>$v) {
  46. $list[$k]['user_ids_info'] = $userModel->getListByStr($v['user_ids']);
  47. $list[$k]['structure_ids_info'] = $structureModel->getListByStr($v['structure_ids']);
  48. }
  49. $dataCount = $this->where($map)->count('id');
  50. $data = [];
  51. $data['list'] = $list;
  52. $data['dataCount'] = $dataCount ? : 0;
  53. return $data;
  54. }
  55. /**
  56. * 保存/编辑相关信息 todo 创建和编辑走一个接口,前端非要这么搞
  57. *
  58. * @param array $param
  59. * @return array|bool
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. * @throws \think\exception\DbException
  63. */
  64. public function createData($param)
  65. {
  66. $id = !empty($param['id']) ? $param['id'] : 0;
  67. if ($param['value'] <= 0) {
  68. $this->error = '数量上限必须大于0';
  69. return false;
  70. }
  71. # 验证重复
  72. if ($this->checkRepeat($param['types'], $param['user_ids'], $param['structure_ids'], $id)) {
  73. $this->error = '有员工或部门包含在其他的规则里!';
  74. return false;
  75. }
  76. $param['types'] = !empty($param['types']) ? $param['types'] : 1; # 1拥有客户上限2锁定客户上限
  77. $param['user_ids'] = !empty($param['user_ids']) ? arrayToString($param['user_ids']) : ''; # 处理user_id
  78. $param['structure_ids'] = !empty($param['structure_ids']) ? arrayToString($param['structure_ids']) : ''; # 处理structure_id
  79. if ($this->allowField(true)->isUpdate(empty($id) ? false : true)->save($param, !empty($id) ? ['id' => $id] : [])) {
  80. $data['id'] = $this->id;
  81. # 系统操作日志
  82. if(!empty($param['id'])){
  83. $content='编辑员工拥有、锁定客户限制';
  84. $action='update';
  85. }else{
  86. $content='添加员工拥有、锁定客户限制';
  87. $action='update';
  88. }
  89. $user=new ApiCommon();
  90. $userInfo=$user->userInfo;
  91. SystemActionLog($userInfo['id'], 'crm_customer','customer', $this->id, $action,$content , '', '',$content);
  92. return $data;
  93. } else {
  94. $this->error = '创建失败';
  95. return false;
  96. }
  97. }
  98. /**
  99. * 编辑相关信息
  100. *
  101. * @param $param
  102. * @param string $id
  103. * @return array|bool
  104. */
  105. public function updateDataById($param, $id = '')
  106. {
  107. if (!$id) {
  108. $this->error = '参数错误';
  109. return false;
  110. }
  111. if ($param['value'] <= 0) {
  112. $this->error = '数量上限必须大于0';
  113. return false;
  114. }
  115. unset($param['id']);
  116. $param['user_ids'] = is_array($param['user_ids']) ? arrayToString($param['user_ids']) : $param['user_ids']; //处理user_id
  117. $param['structure_ids'] = is_array($param['structure_ids']) ? arrayToString($param['structure_ids']) : ''; //处理structure_id
  118. if ($this->allowField(true)->isUpdate(true)->save($param, ['id' => $id])) {
  119. $data['id'] = $id;
  120. return $data;
  121. } else {
  122. $this->error = '编辑失败';
  123. return false;
  124. }
  125. }
  126. /**
  127. * 相关信息数据
  128. *
  129. * @param string $id
  130. * @return Common|array|bool|\PDOStatement|string|\think\Model|null
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\ModelNotFoundException
  133. * @throws \think\exception\DbException
  134. */
  135. public function getDataById($id = '')
  136. {
  137. $map['id'] = $id;
  138. $dataInfo = $this->where($map)->find();
  139. return $dataInfo ? : [];
  140. }
  141. /**
  142. * 验证是否可以持有或锁定客户
  143. *
  144. * @param int $userId 用户id
  145. * @param int $types 类型:类型:1 拥有客户数,2 锁定客户数
  146. * @param int $isUpdate 是否是更新
  147. * @param int $addCount 多公海分配或领取使用的参数,代表要领取或分配的客户数量
  148. * @return bool|int|mixed
  149. */
  150. public function checkData($userId, $types, $isUpdate = 0, $addCount = 0)
  151. {
  152. # 用户信息
  153. $userinfo = db('admin_user')->field(['realname', 'structure_id'])->where('id', $userId)->find();
  154. $username = $userinfo['realname'];
  155. $structureId = $userinfo['structure_id'];
  156. # 查询客户配置(拥有,锁定),以用户配置优先
  157. $customerConfig = db('crm_customer_config')->field(['value', 'is_deal'])->where(['types' => $types, 'user_ids' => ['like', '%,' . $userId . ',%']])->find();
  158. if (!$customerConfig) $customerConfig = db('crm_customer_config')->field(['value', 'is_deal'])->where(['types' => $types, 'structure_ids' => ['like', '%,' . $structureId . ',%']])->find();
  159. # 提示标题
  160. $title = '';
  161. if ($types == 1) $title = '拥有的客户数量';
  162. if ($types == 2) $title = '锁定的客户数量';
  163. if ($customerConfig) {
  164. if (empty($customerConfig['value'])) {
  165. $this->error = $title . '超出限制,最大:' . $customerConfig['value'] . ' 个';
  166. return false;
  167. }
  168. # 成交用户是否暂用数量
  169. $isDeal = !empty($customerConfig['is_deal']) ? 1 : 0;
  170. # 获取目前拥有或锁定的客户数量
  171. $customerModel = new Customer();
  172. $count = $customerModel->getCountByHave($userId, $isDeal, $types);
  173. $error = false;
  174. if (empty($addCount)) {
  175. # 多公海以外的地方调用
  176. if ($count >= $customerConfig['value']) {
  177. $error = true;
  178. }
  179. if ($isUpdate == 1 && $types == 1 && $customerConfig['is_deal'] == 1) {
  180. //更改成交状态
  181. if ($count = $customerConfig['value']) {
  182. $error = false;
  183. }
  184. }
  185. if ($error == true) {
  186. $this->error = $username.','.$title.'超出限制:'.$customerConfig['value'].'个';
  187. return false;
  188. }
  189. } else {
  190. # 多公海中的领取、分配调用,返回超出的个数
  191. if ($count + $addCount > $customerConfig['value']) {
  192. return ($count + $addCount) - $customerConfig['value'];
  193. }
  194. }
  195. }
  196. return true;
  197. }
  198. // public function checkData($user_id, $types, $is_update = '')
  199. // {
  200. // $userModel = new \app\admin\model\User();
  201. // $customerModel = new \app\crm\model\Customer();
  202. // $userInfo = $userModel->getUserById($user_id);
  203. // $dataInfo = $this->where(['types' => $types,'user_ids' => ['like','%,'.$user_id.',%']])->order('update_time desc')->find();
  204. // if (!$dataInfo) {
  205. // $dataInfo = $this->where(['types' => $types,'structure_ids' => ['like','%,'.$userInfo['structure_id'].',%']])->find();
  206. // }
  207. // switch ($types) {
  208. // case '1' : $types_title = '拥有的客户数量'; break;
  209. // case '2' : $types_title = '锁定的客户数量'; break;
  210. // }
  211. // if ($dataInfo) {
  212. // $is_deal = $dataInfo['is_deal'] ? : 0;
  213. // if (!$dataInfo['value']) {
  214. // $this->error = $types_title.'超出限制:'.$dataInfo['value'].'个';
  215. // return false;
  216. // }
  217. // //拥有数、锁定数
  218. // $count = $customerModel->getCountByHave($user_id,$is_deal,$types);
  219. // $error = false;
  220. // if ($count >= $dataInfo['value']) {
  221. // $error = true;
  222. // }
  223. // if ($is_update == 1 && $types == 1 && $dataInfo['is_deal'] == 1) {
  224. // //更改成交状态
  225. // if ($count = $dataInfo['value']) {
  226. // $error = false;
  227. // }
  228. // }
  229. // if ($error == true) {
  230. // $this->error = $userInfo['realname'].','.$types_title.'超出限制:'.$dataInfo['value'].'个';
  231. // return false;
  232. // }
  233. // }
  234. // return true;
  235. // }
  236. /**
  237. * 验证拥有/锁定客户数中的员工或部门是否重复添加
  238. *
  239. * @param $types
  240. * @param $users
  241. * @param $structures
  242. * @param $id
  243. * @return bool
  244. * @throws \think\db\exception\DataNotFoundException
  245. * @throws \think\db\exception\ModelNotFoundException
  246. * @throws \think\exception\DbException
  247. */
  248. private function checkRepeat($types, $users, $structures, $id)
  249. {
  250. $userArray = [];
  251. $structureArray = [];
  252. $where['types'] = $types;
  253. if (!empty($id)) $where['id'] = ['neq', $id];
  254. $data = db('crm_customer_config')->field(['user_ids', 'structure_ids'])->where($where)->select();
  255. foreach ($data AS $key => $value) {
  256. if (!empty($value['user_ids'])) {
  257. $userArray = array_merge($userArray, explode(',', trim($value['user_ids'], ',')));
  258. }
  259. if (!empty($value['structure_ids'])) {
  260. $structureArray = array_merge($structureArray, explode(',', trim($value['structure_ids'], ',')));
  261. }
  262. }
  263. if (array_intersect($users, $userArray) || array_intersect($structures, $structureArray)) {
  264. return true;
  265. }
  266. return false;
  267. }
  268. }