FollowService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. ];
  25. $customInfo = MarketCustomer::where($where)->first();
  26. if (empty($customInfo)) {
  27. return json_fail('客户不存在');
  28. }
  29. $currentStatus = $customInfo->current_status;
  30. if ($currentStatus == -1) {
  31. return json_fail('无效客户,无法跟进');
  32. }
  33. if ($currentStatus == 1) {
  34. $visitTime = $customInfo->visit_time;
  35. $now = time();
  36. $start = strtotime($visitTime);
  37. $end = $start + CustomService::DIFF_TIME;
  38. if ($end < $now) {
  39. return json_fail('已失效,请重新添加客户信息');
  40. }
  41. }
  42. $insertData = [
  43. 'market_customer_id' => $params['market_customer_id'],
  44. 'consultant_id' => $consultantId,
  45. 'visit_type' => $params['visit_type'],
  46. 'intention_type' => $params['intention_type'],
  47. 'follow_time' => strtotime($params['follow_time']),
  48. 'follow_way' => $params['follow_way'],
  49. 'follow_content' => $params['follow_content'],
  50. 'created_at' => time()
  51. ];
  52. $result = false;
  53. Db::beginTransaction();
  54. try {
  55. if ($currentStatus == 1) {
  56. if ($insertData['visit_type'] == 2) {
  57. $customInfo->current_status = 2;
  58. }
  59. }
  60. $customInfo->visit_time = $insertData['follow_time'];
  61. $customInfo->save();
  62. $result = MarketCustomerFollow::insert($insertData);
  63. Db::commit();
  64. }catch (BusinessException|\Exception $e) {
  65. Db::rollBack();
  66. return json_fail($e->getMessage());
  67. }
  68. if ($result) {
  69. return json_success('跟进成功');
  70. } else {
  71. return json_fail('跟进失败');
  72. }
  73. }
  74. /**
  75. * Notes: 跟进列表
  76. * User: yb
  77. * Date: 2024/8/13
  78. * Time: 15:56
  79. * @param $params
  80. */
  81. public static function index($params)
  82. {
  83. $page = $params['page'] ?? 1;
  84. $size = $params['size'] ?? 10;
  85. $ids = UserService::getIds();
  86. $where = [
  87. [function($query) use ($ids) {
  88. $query->whereIn('consultant_id',$ids);
  89. }]
  90. ];
  91. $whereCustom = [];
  92. $whereConsultant = [];
  93. if (!empty($params['market_customer_id'])) {
  94. $where[] = ['market_customer_id', '=', $params['market_customer_id']];
  95. }
  96. if (!empty($params['consultant_id'])) {
  97. $where[] = ['consultant_id', '=', $params['consultant_id']];
  98. }
  99. if (!empty($params['follow_way'])) {
  100. $where[] = ['follow_way', '=', $params['follow_way']];
  101. }
  102. if (!empty($params['follow_time'])) {
  103. $date = $params['follow_time'];
  104. $start = strtotime($date['start'].' 00:00:00');
  105. $end = strtotime($date['end'].' 23:59:59');
  106. $where[] = [function($query) use ($start, $end) {
  107. $query->whereBetween('follow_time', [$start, $end]);
  108. }];
  109. }
  110. if (!empty($params['custom'])) {
  111. $custom = $params['custom'];
  112. $whereCustom[] = [function($query) use ($custom) {
  113. $query->orWhere('name', 'like', "%{$custom}%")->orWhere('mobile', 'like', "%{$custom}%");
  114. }];
  115. }
  116. if (!empty($params['consultant'])) {
  117. $consultant = $params['consultant'];
  118. $whereConsultant[] = [function($query) use ($consultant) {
  119. $query->orWhere('name', 'like', "%{$consultant}%")->orWhere('mobile', 'like', "%{$consultant}%");
  120. }];
  121. }
  122. $paginator = MarketCustomerFollow::with(['custom', 'consultant'])->whereHas('custom', function($query) use ($whereCustom){
  123. $query->where($whereCustom);
  124. })->whereHas('consultant', function($query) use ($whereConsultant) {
  125. $query->where($whereConsultant);
  126. })->where($where)->orderBy('follow_time', 'desc')->paginate($size, '*', 'page', $page);
  127. $total = $paginator->total();
  128. $items = $paginator->items();
  129. $data = [
  130. 'total' => $total,
  131. 'rows' => $items
  132. ];
  133. return json_success('success', $data);
  134. }
  135. /**
  136. * Notes: 跟进详情
  137. * User: yb
  138. * Date: 2024/8/13
  139. * Time: 16:50
  140. * @param $params
  141. */
  142. public static function info($id)
  143. {
  144. $info = MarketCustomerFollow::with(['custom', 'consultant'])->where('id', $id)->first();
  145. if (empty($info)) {
  146. return json_fail('跟进记录不存在');
  147. }
  148. if (!empty($info->custom->mobile)) {
  149. $mobile = $info->custom->mobile;
  150. $info->custom->mask_mobile = CustomService::handlePhone($mobile);
  151. }
  152. return json_success('请求成功', $info);
  153. }
  154. }