CustomerConfig.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. SystemActionLog($param['user_id'], 'crm_customer','customer', $this->id, $action,$content , '', '',$content);
  90. return $data;
  91. } else {
  92. $this->error = '创建失败';
  93. return false;
  94. }
  95. }
  96. /**
  97. * 编辑相关信息
  98. *
  99. * @param $param
  100. * @param string $id
  101. * @return array|bool
  102. */
  103. public function updateDataById($param, $id = '')
  104. {
  105. if (!$id) {
  106. $this->error = '参数错误';
  107. return false;
  108. }
  109. if ($param['value'] <= 0) {
  110. $this->error = '数量上限必须大于0';
  111. return false;
  112. }
  113. unset($param['id']);
  114. $param['user_ids'] = is_array($param['user_ids']) ? arrayToString($param['user_ids']) : $param['user_ids']; //处理user_id
  115. $param['structure_ids'] = is_array($param['structure_ids']) ? arrayToString($param['structure_ids']) : ''; //处理structure_id
  116. if ($this->allowField(true)->isUpdate(true)->save($param, ['id' => $id])) {
  117. $data['id'] = $id;
  118. return $data;
  119. } else {
  120. $this->error = '编辑失败';
  121. return false;
  122. }
  123. }
  124. /**
  125. * 相关信息数据
  126. *
  127. * @param string $id
  128. * @return Common|array|bool|\PDOStatement|string|\think\Model|null
  129. * @throws \think\db\exception\DataNotFoundException
  130. * @throws \think\db\exception\ModelNotFoundException
  131. * @throws \think\exception\DbException
  132. */
  133. public function getDataById($id = '')
  134. {
  135. $map['id'] = $id;
  136. $dataInfo = $this->where($map)->find();
  137. return $dataInfo ? : [];
  138. }
  139. /**
  140. * 验证是否可以持有或锁定客户
  141. *
  142. * @param int $userId 用户id
  143. * @param int $types 类型:类型:1 拥有客户数,2 锁定客户数
  144. * @param int $isUpdate 是否是更新
  145. * @param int $addCount 多公海分配或领取使用的参数,代表要领取或分配的客户数量
  146. * @return bool|int|mixed
  147. */
  148. public function checkData($userId, $types, $isUpdate = 0, $addCount = 0)
  149. {
  150. # 用户信息
  151. $userinfo = db('admin_user')->field(['realname', 'structure_id'])->where('id', $userId)->find();
  152. $username = $userinfo['realname'];
  153. $structureId = $userinfo['structure_id'];
  154. # 查询客户配置(拥有,锁定),以用户配置优先
  155. $customerConfig = db('crm_customer_config')->field(['value', 'is_deal'])->where(['types' => $types, 'user_ids' => ['like', '%,' . $userId . ',%']])->find();
  156. if (!$customerConfig) $customerConfig = db('crm_customer_config')->field(['value', 'is_deal'])->where(['types' => $types, 'structure_ids' => ['like', '%,' . $structureId . ',%']])->find();
  157. # 提示标题
  158. $title = '';
  159. if ($types == 1) $title = '拥有的客户数量';
  160. if ($types == 2) $title = '锁定的客户数量';
  161. if ($customerConfig) {
  162. if (empty($customerConfig['value'])) {
  163. $this->error = $title . '超出限制,最大:' . $customerConfig['value'] . ' 个';
  164. return false;
  165. }
  166. # 成交用户是否暂用数量
  167. $isDeal = !empty($customerConfig['is_deal']) ? 1 : 0;
  168. # 获取目前拥有或锁定的客户数量
  169. $customerModel = new Customer();
  170. $count = $customerModel->getCountByHave($userId, $isDeal, $types);
  171. $error = false;
  172. if (empty($addCount)) {
  173. # 多公海以外的地方调用
  174. if ($count >= $customerConfig['value']) {
  175. $error = true;
  176. }
  177. if ($isUpdate == 1 && $types == 1 && $customerConfig['is_deal'] == 1) {
  178. //更改成交状态
  179. if ($count = $customerConfig['value']) {
  180. $error = false;
  181. }
  182. }
  183. if ($error == true) {
  184. $this->error = $username.','.$title.'超出限制:'.$customerConfig['value'].'个';
  185. return false;
  186. }
  187. } else {
  188. # 多公海中的领取、分配调用,返回超出的个数
  189. if ($count + $addCount > $customerConfig['value']) {
  190. return ($count + $addCount) - $customerConfig['value'];
  191. }
  192. }
  193. }
  194. return true;
  195. }
  196. // public function checkData($user_id, $types, $is_update = '')
  197. // {
  198. // $userModel = new \app\admin\model\User();
  199. // $customerModel = new \app\crm\model\Customer();
  200. // $userInfo = $userModel->getUserById($user_id);
  201. // $dataInfo = $this->where(['types' => $types,'user_ids' => ['like','%,'.$user_id.',%']])->order('update_time desc')->find();
  202. // if (!$dataInfo) {
  203. // $dataInfo = $this->where(['types' => $types,'structure_ids' => ['like','%,'.$userInfo['structure_id'].',%']])->find();
  204. // }
  205. // switch ($types) {
  206. // case '1' : $types_title = '拥有的客户数量'; break;
  207. // case '2' : $types_title = '锁定的客户数量'; break;
  208. // }
  209. // if ($dataInfo) {
  210. // $is_deal = $dataInfo['is_deal'] ? : 0;
  211. // if (!$dataInfo['value']) {
  212. // $this->error = $types_title.'超出限制:'.$dataInfo['value'].'个';
  213. // return false;
  214. // }
  215. // //拥有数、锁定数
  216. // $count = $customerModel->getCountByHave($user_id,$is_deal,$types);
  217. // $error = false;
  218. // if ($count >= $dataInfo['value']) {
  219. // $error = true;
  220. // }
  221. // if ($is_update == 1 && $types == 1 && $dataInfo['is_deal'] == 1) {
  222. // //更改成交状态
  223. // if ($count = $dataInfo['value']) {
  224. // $error = false;
  225. // }
  226. // }
  227. // if ($error == true) {
  228. // $this->error = $userInfo['realname'].','.$types_title.'超出限制:'.$dataInfo['value'].'个';
  229. // return false;
  230. // }
  231. // }
  232. // return true;
  233. // }
  234. /**
  235. * 验证拥有/锁定客户数中的员工或部门是否重复添加
  236. *
  237. * @param $types
  238. * @param $users
  239. * @param $structures
  240. * @param $id
  241. * @return bool
  242. * @throws \think\db\exception\DataNotFoundException
  243. * @throws \think\db\exception\ModelNotFoundException
  244. * @throws \think\exception\DbException
  245. */
  246. private function checkRepeat($types, $users, $structures, $id)
  247. {
  248. $userArray = [];
  249. $structureArray = [];
  250. $where['types'] = $types;
  251. if (!empty($id)) $where['id'] = ['neq', $id];
  252. $data = db('crm_customer_config')->field(['user_ids', 'structure_ids'])->where($where)->select();
  253. foreach ($data AS $key => $value) {
  254. if (!empty($value['user_ids'])) {
  255. $userArray = array_merge($userArray, explode(',', trim($value['user_ids'], ',')));
  256. }
  257. if (!empty($value['structure_ids'])) {
  258. $structureArray = array_merge($structureArray, explode(',', trim($value['structure_ids'], ',')));
  259. }
  260. }
  261. if (array_intersect($users, $userArray) || array_intersect($structures, $structureArray)) {
  262. return true;
  263. }
  264. return false;
  265. }
  266. }