FollowService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\wechat\service;
  3. use app\model\MarketCustomer;
  4. use app\model\MarketCustomerFollow;
  5. use support\Db;
  6. use support\exception\BusinessException;
  7. use Tinywan\Jwt\JwtToken;
  8. class FollowService
  9. {
  10. /**
  11. * Notes: 添加跟进记录
  12. * User: yb
  13. * Date: 2024/8/13
  14. * Time: 11:26
  15. * @param $params
  16. */
  17. public static function add($params)
  18. {
  19. $consultantId = JwtToken::getCurrentId();
  20. //查询客户是否存在
  21. $where = [
  22. ['consultant_id', '=', $consultantId],
  23. ['id', '=', $params['market_customer_id']],
  24. [function($query){
  25. $diffNums = CustomService::DIFF_TIME;
  26. $query->orWhereRaw("check_status IN (-1, 1)")
  27. ->orWhereRaw("check_status = 2 AND current_status IN (-1,3,4)")
  28. ->orWhereRaw("((visit_time + {$diffNums}) - UNIX_TIMESTAMP() > 0 AND current_status = 2 AND check_status = 2)");
  29. }],
  30. ];
  31. $customInfo = MarketCustomer::where($where)->first();
  32. if (empty($customInfo)) {
  33. return json_fail('客户不存在');
  34. }
  35. $currentStatus = $customInfo->current_status;
  36. if ($currentStatus == -1 || $customInfo->check_status == -1) {
  37. return json_fail('报备失败客户,无法跟进');
  38. }
  39. $insertData = [
  40. 'market_customer_id' => $params['market_customer_id'],
  41. 'consultant_id' => $consultantId,
  42. 'visit_type' => $params['visit_type'],
  43. 'intention_type' => $params['intention_type'],
  44. 'follow_time' => strtotime($params['follow_time']),
  45. 'follow_way' => $params['follow_way'],
  46. 'follow_content' => $params['follow_content'],
  47. 'created_at' => time()
  48. ];
  49. $result = false;
  50. Db::beginTransaction();
  51. try {
  52. $customInfo->follow_num = $customInfo->follow_num + 1;
  53. $customInfo->level = $insertData['intention_type'];
  54. $customInfo->visit_time = $insertData['follow_time'];
  55. $customInfo->save();
  56. $result = MarketCustomerFollow::insert($insertData);
  57. Db::commit();
  58. }catch (BusinessException|\Exception $e) {
  59. Db::rollBack();
  60. return json_fail($e->getMessage());
  61. }
  62. if ($result) {
  63. return json_success('跟进成功');
  64. } else {
  65. return json_fail('跟进失败');
  66. }
  67. }
  68. /**
  69. * Notes: 跟进列表
  70. * User: yb
  71. * Date: 2024/8/13
  72. * Time: 15:56
  73. * @param $params
  74. */
  75. public static function index($params)
  76. {
  77. $page = $params['page'] ?? 1;
  78. $size = $params['size'] ?? 10;
  79. $ids = UserService::getIds();
  80. //查询顾问下的客户信息
  81. $customIds = [];
  82. if (!empty($ids)) {
  83. $customIds = MarketCustomer::whereIn('consultant_id', $ids)->pluck('id')->toArray();
  84. }
  85. $where = [
  86. [function($query) use ($customIds) {
  87. $query->whereIn('market_customer_id',$customIds);
  88. }]
  89. ];
  90. $whereCustom = [];
  91. $whereConsultant = [];
  92. if (!empty($params['market_customer_id'])) {
  93. $where[] = ['market_customer_id', '=', $params['market_customer_id']];
  94. }
  95. if (!empty($params['consultant_id'])) {
  96. $where[] = ['consultant_id', '=', $params['consultant_id']];
  97. }
  98. if (!empty($params['follow_way'])) {
  99. $where[] = ['follow_way', '=', $params['follow_way']];
  100. }
  101. if (!empty($params['follow_time'])) {
  102. $date = $params['follow_time'];
  103. $start = strtotime($date['start'].' 00:00:00');
  104. $end = strtotime($date['end'].' 23:59:59');
  105. $where[] = [function($query) use ($start, $end) {
  106. $query->whereBetween('follow_time', [$start, $end]);
  107. }];
  108. }
  109. if (!empty($params['custom'])) {
  110. $custom = $params['custom'];
  111. $whereCustom[] = [function($query) use ($custom) {
  112. $query->orWhere('name', 'like', "%{$custom}%")->orWhere('mobile', 'like', "%{$custom}%");
  113. }];
  114. }
  115. if (!empty($params['consultant'])) {
  116. $consultant = $params['consultant'];
  117. $whereConsultant[] = [function($query) use ($consultant) {
  118. $query->orWhere('name', 'like', "%{$consultant}%")->orWhere('mobile', 'like', "%{$consultant}%");
  119. }];
  120. }
  121. if (!empty($params['created_at'])) {
  122. $createdAt = $params['created_at'];
  123. if (is_array($createdAt)) {
  124. $createdAtStart = strtotime($createdAt[0]);
  125. $createdAtEnd = strtotime($createdAt[1]);
  126. } else {
  127. //本月开始和结束
  128. $createdAtStart = strtotime(date('Y-m-01 00:00:00'));
  129. $createdAtEnd = strtotime(date('Y-m-t 23:59:59'));
  130. }
  131. $where[] = [function($query) use ($createdAtStart,$createdAtEnd) {
  132. $query->whereBetween('created_at', [$createdAtStart, $createdAtEnd]);
  133. }];
  134. }
  135. $paginator = MarketCustomerFollow::with(['custom', 'consultant'])->whereHas('custom', function($query) use ($whereCustom){
  136. $query->where($whereCustom);
  137. })->whereHas('consultant', function($query) use ($whereConsultant) {
  138. $query->where($whereConsultant);
  139. })->where($where)->orderBy('follow_time', 'desc')->paginate($size, '*', 'page', $page);
  140. $total = $paginator->total();
  141. $items = $paginator->items();
  142. $data = [
  143. 'total' => $total,
  144. 'rows' => $items
  145. ];
  146. return json_success('success', $data);
  147. }
  148. /**
  149. * Notes: 跟进详情
  150. * User: yb
  151. * Date: 2024/8/13
  152. * Time: 16:50
  153. * @param $params
  154. */
  155. public static function info($id)
  156. {
  157. $info = MarketCustomerFollow::with(['custom', 'consultant'])->where('id', $id)->first();
  158. if (empty($info)) {
  159. return json_fail('跟进记录不存在');
  160. }
  161. if (!empty($info->custom->mobile)) {
  162. $mobile = $info->custom->mobile;
  163. $info->custom->mask_mobile = CustomService::handlePhone($mobile);
  164. }
  165. return json_success('请求成功', $info);
  166. }
  167. }