ValidCheck.php 11 KB

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