FollowService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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::where(function ($query) use ($ids) {
  84. $query->orWhereIn('consultant_id', $ids)->orWhereIn('report_consultant_id', $ids);
  85. })->pluck('id')->toArray();
  86. }
  87. $where = [
  88. [function($query) use ($customIds) {
  89. $query->whereIn('market_customer_id',$customIds);
  90. }]
  91. ];
  92. $whereCustom = [];
  93. $whereConsultant = [];
  94. if (!empty($params['market_customer_id'])) {
  95. $where[] = ['market_customer_id', '=', $params['market_customer_id']];
  96. }
  97. if (!empty($params['consultant_id'])) {
  98. $where[] = ['consultant_id', '=', $params['consultant_id']];
  99. }
  100. if (!empty($params['follow_way'])) {
  101. $where[] = ['follow_way', '=', $params['follow_way']];
  102. }
  103. if (!empty($params['follow_time'])) {
  104. $date = $params['follow_time'];
  105. $start = strtotime($date['start'].' 00:00:00');
  106. $end = strtotime($date['end'].' 23:59:59');
  107. $where[] = [function($query) use ($start, $end) {
  108. $query->whereBetween('follow_time', [$start, $end]);
  109. }];
  110. }
  111. if (!empty($params['custom'])) {
  112. $custom = $params['custom'];
  113. $whereCustom[] = [function($query) use ($custom) {
  114. $query->orWhere('name', 'like', "%{$custom}%")->orWhere('mobile', 'like', "%{$custom}%");
  115. }];
  116. }
  117. if (!empty($params['consultant'])) {
  118. $consultant = $params['consultant'];
  119. $whereConsultant[] = [function($query) use ($consultant) {
  120. $query->orWhere('name', 'like', "%{$consultant}%")->orWhere('mobile', 'like', "%{$consultant}%");
  121. }];
  122. }
  123. if (!empty($params['created_at'])) {
  124. $createdAt = $params['created_at'];
  125. if (is_array($createdAt)) {
  126. $createdAtStart = strtotime($createdAt[0]);
  127. $createdAtEnd = strtotime($createdAt[1]);
  128. } else {
  129. //本月开始和结束
  130. $createdAtStart = strtotime(date('Y-m-01 00:00:00'));
  131. $createdAtEnd = strtotime(date('Y-m-t 23:59:59'));
  132. }
  133. $where[] = [function($query) use ($createdAtStart,$createdAtEnd) {
  134. $query->whereBetween('created_at', [$createdAtStart, $createdAtEnd]);
  135. }];
  136. }
  137. $paginator = MarketCustomerFollow::with(['custom', 'consultant'])->whereHas('custom', function($query) use ($whereCustom){
  138. $query->where($whereCustom);
  139. })->whereHas('consultant', function($query) use ($whereConsultant) {
  140. $query->where($whereConsultant);
  141. })->where($where)->orderBy('follow_time', 'desc')->paginate($size, '*', 'page', $page);
  142. $total = $paginator->total();
  143. $items = $paginator->items();
  144. $data = [
  145. 'total' => $total,
  146. 'rows' => $items
  147. ];
  148. return json_success('success', $data);
  149. }
  150. /**
  151. * Notes: 跟进详情
  152. * User: yb
  153. * Date: 2024/8/13
  154. * Time: 16:50
  155. * @param $params
  156. */
  157. public static function info($id)
  158. {
  159. $info = MarketCustomerFollow::with(['custom', 'consultant'])->where('id', $id)->first();
  160. if (empty($info)) {
  161. return json_fail('跟进记录不存在');
  162. }
  163. if (!empty($info->custom->mobile)) {
  164. $mobile = $info->custom->mobile;
  165. $info->custom->mask_mobile = CustomService::handlePhone($mobile);
  166. }
  167. return json_success('请求成功', $info);
  168. }
  169. }