FollowService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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->follow_num = $customInfo->follow_num + 1;
  61. $customInfo->level = $insertData['intention_type'];
  62. $customInfo->visit_time = $insertData['follow_time'];
  63. $customInfo->save();
  64. $result = MarketCustomerFollow::insert($insertData);
  65. Db::commit();
  66. }catch (BusinessException|\Exception $e) {
  67. Db::rollBack();
  68. return json_fail($e->getMessage());
  69. }
  70. if ($result) {
  71. return json_success('跟进成功');
  72. } else {
  73. return json_fail('跟进失败');
  74. }
  75. }
  76. /**
  77. * Notes: 跟进列表
  78. * User: yb
  79. * Date: 2024/8/13
  80. * Time: 15:56
  81. * @param $params
  82. */
  83. public static function index($params)
  84. {
  85. $page = $params['page'] ?? 1;
  86. $size = $params['size'] ?? 10;
  87. $ids = UserService::getIds();
  88. //查询顾问下的客户信息
  89. $customIds = [];
  90. if (!empty($ids)) {
  91. $customIds = MarketCustomer::whereIn('consultant_id', $ids)->pluck('id')->toArray();
  92. }
  93. $where = [
  94. [function($query) use ($customIds) {
  95. $query->whereIn('market_customer_id',$customIds);
  96. }]
  97. ];
  98. $whereCustom = [];
  99. $whereConsultant = [];
  100. if (!empty($params['market_customer_id'])) {
  101. $where[] = ['market_customer_id', '=', $params['market_customer_id']];
  102. }
  103. if (!empty($params['consultant_id'])) {
  104. $where[] = ['consultant_id', '=', $params['consultant_id']];
  105. }
  106. if (!empty($params['follow_way'])) {
  107. $where[] = ['follow_way', '=', $params['follow_way']];
  108. }
  109. if (!empty($params['follow_time'])) {
  110. $date = $params['follow_time'];
  111. $start = strtotime($date['start'].' 00:00:00');
  112. $end = strtotime($date['end'].' 23:59:59');
  113. $where[] = [function($query) use ($start, $end) {
  114. $query->whereBetween('follow_time', [$start, $end]);
  115. }];
  116. }
  117. if (!empty($params['custom'])) {
  118. $custom = $params['custom'];
  119. $whereCustom[] = [function($query) use ($custom) {
  120. $query->orWhere('name', 'like', "%{$custom}%")->orWhere('mobile', 'like', "%{$custom}%");
  121. }];
  122. }
  123. if (!empty($params['consultant'])) {
  124. $consultant = $params['consultant'];
  125. $whereConsultant[] = [function($query) use ($consultant) {
  126. $query->orWhere('name', 'like', "%{$consultant}%")->orWhere('mobile', 'like', "%{$consultant}%");
  127. }];
  128. }
  129. if (!empty($params['created_at'])) {
  130. $createdAt = $params['created_at'];
  131. if (is_array($createdAt)) {
  132. $createdAtStart = strtotime($createdAt[0]);
  133. $createdAtEnd = strtotime($createdAt[1]);
  134. } else {
  135. //本月开始和结束
  136. $createdAtStart = strtotime(date('Y-m-01 00:00:00'));
  137. $createdAtEnd = strtotime(date('Y-m-t 23:59:59'));
  138. }
  139. $where[] = [function($query) use ($createdAtStart,$createdAtEnd) {
  140. $query->whereBetween('created_at', [$createdAtStart, $createdAtEnd]);
  141. }];
  142. }
  143. $paginator = MarketCustomerFollow::with(['custom', 'consultant'])->whereHas('custom', function($query) use ($whereCustom){
  144. $query->where($whereCustom);
  145. })->whereHas('consultant', function($query) use ($whereConsultant) {
  146. $query->where($whereConsultant);
  147. })->where($where)->orderBy('follow_time', 'desc')->paginate($size, '*', 'page', $page);
  148. $total = $paginator->total();
  149. $items = $paginator->items();
  150. $data = [
  151. 'total' => $total,
  152. 'rows' => $items
  153. ];
  154. return json_success('success', $data);
  155. }
  156. /**
  157. * Notes: 跟进详情
  158. * User: yb
  159. * Date: 2024/8/13
  160. * Time: 16:50
  161. * @param $params
  162. */
  163. public static function info($id)
  164. {
  165. $info = MarketCustomerFollow::with(['custom', 'consultant'])->where('id', $id)->first();
  166. if (empty($info)) {
  167. return json_fail('跟进记录不存在');
  168. }
  169. if (!empty($info->custom->mobile)) {
  170. $mobile = $info->custom->mobile;
  171. $info->custom->mask_mobile = CustomService::handlePhone($mobile);
  172. }
  173. return json_success('请求成功', $info);
  174. }
  175. }