FollowService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. if (!empty($params['created_at'])) {
  123. $createdAt = $params['created_at'];
  124. if (is_array($createdAt)) {
  125. $createdAtStart = strtotime($createdAt[0]);
  126. $createdAtEnd = strtotime($createdAt[1]);
  127. } else {
  128. //本月开始和结束
  129. $createdAtStart = strtotime(date('Y-m-01 00:00:00'));
  130. $createdAtEnd = strtotime(date('Y-m-t 23:59:59'));
  131. }
  132. $where[] = [function($query) use ($createdAtStart,$createdAtEnd) {
  133. $query->whereBetween('created_at', [$createdAtStart, $createdAtEnd]);
  134. }];
  135. }
  136. $paginator = MarketCustomerFollow::with(['custom', 'consultant'])->whereHas('custom', function($query) use ($whereCustom){
  137. $query->where($whereCustom);
  138. })->whereHas('consultant', function($query) use ($whereConsultant) {
  139. $query->where($whereConsultant);
  140. })->where($where)->orderBy('follow_time', 'desc')->paginate($size, '*', 'page', $page);
  141. $total = $paginator->total();
  142. $items = $paginator->items();
  143. $data = [
  144. 'total' => $total,
  145. 'rows' => $items
  146. ];
  147. return json_success('success', $data);
  148. }
  149. /**
  150. * Notes: 跟进详情
  151. * User: yb
  152. * Date: 2024/8/13
  153. * Time: 16:50
  154. * @param $params
  155. */
  156. public static function info($id)
  157. {
  158. $info = MarketCustomerFollow::with(['custom', 'consultant'])->where('id', $id)->first();
  159. if (empty($info)) {
  160. return json_fail('跟进记录不存在');
  161. }
  162. if (!empty($info->custom->mobile)) {
  163. $mobile = $info->custom->mobile;
  164. $info->custom->mask_mobile = CustomService::handlePhone($mobile);
  165. }
  166. return json_success('请求成功', $info);
  167. }
  168. }