ValidCheck.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\empcrm\controller;
  4. use app\BaseController;
  5. use app\model\Customer;
  6. use app\model\CustomerStateCheck;
  7. use app\model\CustomerVisitLog;
  8. use app\model\Employee;
  9. use app\model\Setting;
  10. use think\facade\View;
  11. class ValidCheck extends BaseController
  12. {
  13. /**
  14. * 有效申请
  15. */
  16. public function apply($customer_id)
  17. {
  18. $team_orgs = orgSubIds($this->request->empcrm['org_id']);
  19. $customer = Customer::where([['id', '=', $customer_id], ['org_id', 'in', $team_orgs]])->find();
  20. if (empty($customer)) return json(['code' => 1, 'msg' => '客户不存在']);
  21. $settingObj = Setting::where(['root_id' => $this->request->empcrm['root_id'], 'name' => 'valid_check_person'])->find();
  22. if (empty($settingObj)) return json(['code' => 1, 'msg' => '申请失败,未配置申请权限']);
  23. $setting = explode(',', $settingObj->content);
  24. if (in_array('xiaoshou', $setting) && $customer->employee_id == $this->request->empcrm['id']) {
  25. } elseif (in_array('sheji', $setting) && $customer->designer_id == $this->request->empcrm['id']) {
  26. } else {
  27. return json(['code' => 1, 'msg' => '申请失败,未开启申请权限']);
  28. }
  29. // 查找是否已经申请
  30. $exist = CustomerStateCheck::where(['customer_id' => $customer_id, 'root_id' => $this->request->empcrm['root_id']])->find();
  31. if ($exist) {
  32. if ($exist->check_state < 2)
  33. return json(['code' => 1, 'msg' => '申请失败,请勿重复申请']);
  34. else
  35. $exist->save(['check_state' => 0, 'check_employee_id' => null, 'check_time' => null, 'employee_id' => $this->request->empcrm['id']]);
  36. } else {
  37. // 创建申请
  38. CustomerStateCheck::create([
  39. 'customer_id' => $customer_id,
  40. 'employee_id' => $this->request->empcrm['id'],
  41. 'root_id' => $this->request->empcrm['root_id'],
  42. 'org_id' => $customer->org_id,
  43. 'state' => 1,
  44. 'check_state' => 0
  45. ]);
  46. }
  47. // 添加跟进记录
  48. $employee = Employee::find($this->request->empcrm['id']);
  49. CustomerVisitLog::create([
  50. 'customer_id' => $customer_id,
  51. 'employee_id' => $this->request->empcrm['id'],
  52. 'remark' => '申请有效客户',
  53. 'user_id' => $employee->uid,
  54. 'type' => ''
  55. ]);
  56. return json(['code' => 0, 'msg' => '申请成功']);
  57. }
  58. /**
  59. * 申请列表
  60. */
  61. public function list(int $page = 1, int $limit = 20)
  62. {
  63. if (!$this->request->empcrm->is_manager) return json(['code' => 1, 'msg' => '没有权限']);
  64. if (!$this->request->isAjax()) {
  65. return View::fetch();
  66. }
  67. $param = $this->request->except(['page', 'limit']);
  68. $conditionOr = [];
  69. $condition = [['root_id', '=', $this->request->empcrm['root_id']]];
  70. $team_orgs = orgSubIds($this->request->empcrm['org_id']);
  71. $condition[] = ['org_id', 'in', $team_orgs];
  72. if (isset($param['keyword'])) {
  73. // 查询员工姓名
  74. $empId = Employee::where([['name', 'like', '%' . $param['keyword'] . '%'], ['root_id', '=', $this->request->empcrm['root_id']]])->column('id');
  75. // 客户姓名
  76. $cid = CustomerStateCheck::hasWhere('customer', function ($query) use ($param) {
  77. $query->where('name', 'like', '%' . $param['keyword'] . '%');
  78. })->where(['CustomerStateCheck.root_id' => $this->request->empcrm['root_id']])->order('CustomerStateCheck.id', 'desc')->column('customer_id');
  79. $conditionOr = [
  80. [['customer_id', 'in', $cid]],
  81. [['employee_id', 'in', $empId]]
  82. ];
  83. }
  84. if (isset($param['state']) && $param['state'] != '') {
  85. $condition[] = ['check_state', '=', $param['state']];
  86. }
  87. $checkCustomerIds = CustomerStateCheck::where($condition)->where(function ($query) use ($conditionOr) {
  88. $query->whereOr($conditionOr);
  89. })->order('id', 'desc')->page($page, $limit)->column('check_state', 'customer_id');
  90. $count = CustomerStateCheck::where($condition)->whereOr($conditionOr)->count();
  91. $customerIds = array_keys($checkCustomerIds);
  92. $lastTime = CustomerVisitLog::whereIn('customer_id', $customerIds)->group('customer_id')->column('max(addtime)', 'customer_id');
  93. // 状态统计获取
  94. $customersStateList = CustomerVisitLog::whereIn('customer_id', $customerIds)->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
  95. $customersState = [];
  96. foreach ($customersStateList as $s) {
  97. if (!isset($customersState[$s['customer_id']]))
  98. $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  99. $customersState[$s['customer_id']]['count'] += $s['num'];
  100. // 到店,量房,活动,定金,签单
  101. if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
  102. elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
  103. elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
  104. elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
  105. elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
  106. }
  107. $data = Customer::whereIn('id', $customerIds)->select()->visible(['id', 'name', 'sex', 'level', 'community_name', 'state', 'square'])->toArray();
  108. foreach ($data as &$cus) {
  109. if (isset($customersState[$cus['id']]) && !in_array($cus['state'], Customer::changeState('待确认', 'chaos'), true)) {
  110. $cus['stateNum'] = $customersState[$cus['id']];
  111. } else {
  112. $cus['stateNum'] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  113. }
  114. $cus['last_visit_day'] = isset($lastTime[$cus['id']]) ? get_date_diff(time(), $lastTime[$cus['id']])->days : -1;
  115. $cus['check_state'] = $checkCustomerIds[$cus['id']] ?? 0;
  116. }
  117. return json(['code' => 0, 'msg' => '成功', 'data' => $data, 'count' => $count]);
  118. }
  119. /**
  120. * 申请通过或者驳回
  121. */
  122. public function check($customer_id, $state)
  123. {
  124. $team_orgs = orgSubIds($this->request->empcrm['org_id']);
  125. $customer = Customer::where([['id', '=', $customer_id], ['org_id', 'in', $team_orgs]])->find();
  126. if (!$customer) return json(['code' => 1, 'msg' => '申请失败,客户不存在']);
  127. $checkCustomer = CustomerStateCheck::where(['customer_id' => $customer_id, 'root_id' => $this->request->empcrm['root_id']])->find();
  128. if (!$checkCustomer) return json(['code' => 1, 'msg' => '申请失败,客户不存在']);
  129. if ($checkCustomer['check_state'] != 0) return json(['code' => 1, 'msg' => '申请失败,该客户已审核']);
  130. $checkCustomer->check_state = $state;
  131. $checkCustomer->check_employee_id = $this->request->empcrm['id'];
  132. $checkCustomer->check_time = date('Y-m-d H:i:s');
  133. $checkCustomer->save();
  134. // 添加跟进记录
  135. $employee = Employee::find($this->request->empcrm['id']);
  136. $msg = $state == 1 ? '有效申请已通过' : '有效申请已被拒绝';
  137. CustomerVisitLog::create([
  138. 'customer_id' => $customer_id,
  139. 'employee_id' => $this->request->empcrm['id'],
  140. 'remark' => $msg,
  141. 'user_id' => $employee->uid,
  142. 'type' => ''
  143. ]);
  144. return json(['code' => 0, 'msg' => '审核成功']);
  145. }
  146. /**
  147. * 申请列表
  148. */
  149. public function apply_list($keyword = null, int $page = 1, int $limit = 20)
  150. {
  151. if (!$this->request->isAjax()) {
  152. return View::fetch();
  153. }
  154. $param = $this->request->except(['page', 'limit']);
  155. $condition = [['root_id', '=', $this->request->empcrm['root_id']]];
  156. $condition[] = ['employee_id', '=', $this->request->empcrm['id']];
  157. if (isset($param['state']) && $param['state'] != '') {
  158. $condition[] = ['check_state', '=', $param['state']];
  159. }
  160. if (!empty($keyword)) {
  161. // 客户姓名
  162. $cid = CustomerStateCheck::hasWhere('customer', function ($query) use ($keyword) {
  163. $query->where([['name', 'like', '%' . $keyword . '%']]);
  164. })->where(['CustomerStateCheck.root_id' => $this->request->empcrm['root_id'], ['CustomerStateCheck.employee_id', '=', $this->request->empcrm['id']]])->order('CustomerStateCheck.id', 'desc')->column('customer_id');
  165. $condition = [
  166. ['customer_id', 'in', $cid],
  167. ];
  168. }
  169. $checkCustomerIds = CustomerStateCheck::where($condition)->order('id', 'desc')->page($page, $limit)->column('check_state', 'customer_id');
  170. $count = CustomerStateCheck::where($condition)->count();
  171. $customerIds = array_keys($checkCustomerIds);
  172. $lastTime = CustomerVisitLog::whereIn('customer_id', $customerIds)->group('customer_id')->column('max(addtime)', 'customer_id');
  173. // 状态统计获取
  174. $customersStateList = CustomerVisitLog::whereIn('customer_id', $customerIds)->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
  175. $customersState = [];
  176. foreach ($customersStateList as $s) {
  177. if (!isset($customersState[$s['customer_id']]))
  178. $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  179. $customersState[$s['customer_id']]['count'] += $s['num'];
  180. // 到店,量房,活动,定金,签单
  181. if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
  182. elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
  183. elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
  184. elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
  185. elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
  186. }
  187. $data = Customer::whereIn('id', $customerIds)->select()->visible(['id', 'name', 'sex', 'level', 'community_name', 'state', 'square'])->toArray();
  188. foreach ($data as &$cus) {
  189. if (isset($customersState[$cus['id']]) && !in_array($cus['state'], Customer::changeState('待确认', 'chaos'), true)) {
  190. $cus['stateNum'] = $customersState[$cus['id']];
  191. } else {
  192. $cus['stateNum'] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  193. }
  194. $cus['last_visit_day'] = isset($lastTime[$cus['id']]) ? get_date_diff(time(), $lastTime[$cus['id']])->days : -1;
  195. $cus['check_state'] = $checkCustomerIds[$cus['id']] ?? 0;
  196. }
  197. return json(['code' => 0, 'msg' => '成功', 'data' => $data, 'count' => $count]);
  198. }
  199. }