Blame view

fw-valhalla-service/src/main/java/cn/fw/valhalla/service/bus/cust/AccidentPoolBizService.java 12 KB
e95f940e   张志伟   :art:
1
2
  package cn.fw.valhalla.service.bus.cust;
  
bf85c222   张志伟   :rocket:
3
  import cn.fw.common.util.ValidationUtils;
8a272b22   张志伟   :sparkles:
4
  import cn.fw.common.web.annotation.DisLock;
e95f940e   张志伟   :art:
5
  import cn.fw.common.web.auth.LoginAuthBean;
ef3c0145   张志伟   ✨ 主动放弃跟进 70%
6
  import cn.fw.valhalla.common.constant.RoleCode;
b13ab2b8   张志伟   :art:
7
  import cn.fw.valhalla.common.utils.DateUtil;
51a118ce   张志伟   :sparkles:
8
  import cn.fw.valhalla.common.utils.StringUtils;
e95f940e   张志伟   :art:
9
  import cn.fw.valhalla.domain.db.customer.AccidentPool;
8587e21d   张志伟   :art:
10
  import cn.fw.valhalla.domain.db.customer.Customer;
18b1ac66   张志伟   :construction:
11
  import cn.fw.valhalla.domain.db.follow.FollowRecord;
ef3c0145   张志伟   ✨ 主动放弃跟进 70%
12
  import cn.fw.valhalla.domain.db.follow.FollowTask;
fb44222c   张志伟   事故车跟进逻辑调整
13
  import cn.fw.valhalla.domain.db.pool.CustomerCluePool;
e95f940e   张志伟   :art:
14
  import cn.fw.valhalla.domain.dto.AccidentPoolDTO;
172cb4a3   张志伟   :art:
15
  import cn.fw.valhalla.domain.enums.*;
a8b3460b   张志伟   :alien:
16
  import cn.fw.valhalla.rpc.backlog.TodoRpcService;
a8b3460b   张志伟   :alien:
17
  import cn.fw.valhalla.rpc.backlog.dto.BackLogItemDTO;
740805dc   张志伟   :zap:
18
  import cn.fw.valhalla.rpc.erp.UserService;
ef3c0145   张志伟   ✨ 主动放弃跟进 70%
19
  import cn.fw.valhalla.rpc.erp.dto.PostUserDTO;
c7ceac06   张志伟   :art:
20
  import cn.fw.valhalla.rpc.oop.OopService;
ef3c0145   张志伟   ✨ 主动放弃跟进 70%
21
22
  import cn.fw.valhalla.rpc.oop.dto.ShopDTO;
  import cn.fw.valhalla.service.bus.setting.SettingBizService;
8587e21d   张志伟   :art:
23
  import cn.fw.valhalla.service.data.*;
b13ab2b8   张志伟   :art:
24
  import com.baomidou.mybatisplus.core.toolkit.Wrappers;
e95f940e   张志伟   :art:
25
  import lombok.Getter;
18b1ac66   张志伟   :construction:
26
  import lombok.RequiredArgsConstructor;
e95f940e   张志伟   :art:
27
  import lombok.extern.slf4j.Slf4j;
e95f940e   张志伟   :art:
28
  import org.springframework.beans.BeanUtils;
e95f940e   张志伟   :art:
29
  import org.springframework.beans.factory.annotation.Value;
e95f940e   张志伟   :art:
30
31
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
ef3c0145   张志伟   ✨ 主动放弃跟进 70%
32
  import org.springframework.util.CollectionUtils;
e95f940e   张志伟   :art:
33
  
ef3c0145   张志伟   ✨ 主动放弃跟进 70%
34
  import java.sql.Timestamp;
bf85c222   张志伟   :rocket:
35
  import java.time.LocalDate;
ef3c0145   张志伟   ✨ 主动放弃跟进 70%
36
  import java.time.LocalDateTime;
8587e21d   张志伟   :art:
37
  import java.util.*;
e95f940e   张志伟   :art:
38
39
  
  import static cn.fw.common.businessvalidator.Validator.BV;
ef3c0145   张志伟   ✨ 主动放弃跟进 70%
40
  import static cn.fw.valhalla.service.bus.follow.strategy.AbstractFollowStrategy.getCalendarType;
e95f940e   张志伟   :art:
41
42
43
44
45
46
47
48
49
  
  /**
   * @author : kurisu
   * @className : AccidentPoolBizService
   * @description : 事故池
   * @date: 2020-08-15 15:20
   */
  @Service
  @Slf4j
18b1ac66   张志伟   :construction:
50
  @RequiredArgsConstructor
e95f940e   张志伟   :art:
51
  public class AccidentPoolBizService {
e95f940e   张志伟   :art:
52
      private final AccidentPoolService accidentPoolService;
ef3c0145   张志伟   ✨ 主动放弃跟进 70%
53
      private final FollowTaskService followTaskService;
18b1ac66   张志伟   :construction:
54
      private final FollowRecordService followRecordService;
c7ceac06   张志伟   :art:
55
56
      private final UserService userService;
      private final OopService oopService;
ef3c0145   张志伟   ✨ 主动放弃跟进 70%
57
      private final SettingBizService settingBizService;
18b1ac66   张志伟   :construction:
58
      private final TodoRpcService todoRpcService;
fb44222c   张志伟   事故车跟进逻辑调整
59
      private final CustomerCluePoolService customerCluePoolService;
8587e21d   张志伟   :art:
60
      private final CustomerService customerService;
e95f940e   张志伟   :art:
61
62
63
64
65
66
  
  
      @Value("${spring.cache.custom.global-prefix}:AccidentPool")
      @Getter
      private String keyPrefix;
  
18b1ac66   张志伟   :construction:
67
68
69
70
      @Value("${follow.todo.ACCode}")
      @Getter
      private String ACCode;
  
e95f940e   张志伟   :art:
71
72
  
      @Transactional(rollbackFor = Exception.class)
8a272b22   张志伟   :sparkles:
73
      @DisLock(prefix = "#this.getKeyPrefix()", key = "#currentUser.groupId + ':' + #poolDTO.plateNo", message = "请勿重复提交")
bf85c222   张志伟   :rocket:
74
75
76
77
78
79
80
81
82
83
      public Long add2Pool(LoginAuthBean currentUser, AccidentPoolDTO poolDTO) {
          boolean isPlateNo = ValidationUtils.checkCarPlate(poolDTO.getPlateNo());
          BV.isTrue(isPlateNo, () -> "车牌号不正确,请检查是否包含I、O或特殊字符");
          if (!Boolean.TRUE.equals(poolDTO.getForce())) {
              AccidentPool repetition = isRepetition(currentUser.getGroupId(), poolDTO.getPlateNo());
              if (Objects.nonNull(repetition)) {
                  return repetition.getCreateTime().getTime();
              }
          }
  
8a272b22   张志伟   :sparkles:
84
85
          if (StringUtils.isEmpty(poolDTO.getMobile())) {
              poolDTO.setMobile(poolDTO.getReportMobile());
e95f940e   张志伟   :art:
86
          }
8a272b22   张志伟   :sparkles:
87
88
89
90
91
92
          AccidentPool pool = conversion2db(poolDTO, currentUser);
          pool.setShopId(poolDTO.getShopId());
          pool.setShopName(poolDTO.getShopName());
          BV.notNull(pool.getShopId(), () -> "服务站信息不正确,请确认");
          accidentPoolService.save(pool);
          afterAddPool(pool);
bf85c222   张志伟   :rocket:
93
          return 0L;
e95f940e   张志伟   :art:
94
95
      }
  
fb44222c   张志伟   事故车跟进逻辑调整
96
97
98
99
100
      private void afterAddPool(final AccidentPool pool) {
          final String plateNo = pool.getPlateNo();
          CustomerCluePool cluePool = customerCluePoolService.queryByPlateNo(plateNo, pool.getGroupId(), FollowTypeEnum.AC);
          if (Objects.nonNull(cluePool) && ClueStatusEnum.ONGOING.equals(cluePool.getClueStatus())) {
              cluePool.setClueStatus(ClueStatusEnum.FAILURE);
566fc4b1   张志伟   :construction:
101
              cluePool.setCloseTime(new Date());
fb44222c   张志伟   事故车跟进逻辑调整
102
103
              customerCluePoolService.updateById(cluePool);
              clearTask(cluePool.getId(), cluePool.getGroupId());
18b1ac66   张志伟   :construction:
104
          }
fb44222c   张志伟   事故车跟进逻辑调整
105
106
107
          CustomerCluePool clue = createClue(pool);
          FollowTask task = createTask(clue);
          createTaskRecord(task);
ef3c0145   张志伟   ✨ 主动放弃跟进 70%
108
109
      }
  
e95f940e   张志伟   :art:
110
111
112
      private AccidentPool conversion2db(AccidentPoolDTO dto, LoginAuthBean currentUser) {
          AccidentPool pool = new AccidentPool();
          BeanUtils.copyProperties(dto, pool);
a98480b0   张志伟   :bug:
113
          pool.setAddress(dto.getReportAddress());
e95f940e   张志伟   :art:
114
115
116
117
118
          pool.setCreateTime(new Date());
          pool.setUpdateTime(new Date());
          pool.setGroupId(currentUser.getGroupId());
          return pool;
      }
e110c4d9   张志伟   :memo: 更换事故车重复校验
119
  
bf85c222   张志伟   :rocket:
120
121
      private AccidentPool isRepetition(Long groupId, String plateNo) {
          AccidentPool lastOne = accidentPoolService.getOne(Wrappers.<AccidentPool>lambdaQuery()
e110c4d9   张志伟   :memo: 更换事故车重复校验
122
123
                  .eq(AccidentPool::getPlateNo, plateNo)
                  .eq(AccidentPool::getGroupId, groupId)
bf85c222   张志伟   :rocket:
124
125
                  .orderByDesc(AccidentPool::getCreateTime)
                  .last(" limit 1 ")
e110c4d9   张志伟   :memo: 更换事故车重复校验
126
          );
bf85c222   张志伟   :rocket:
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  
          if (Objects.isNull(lastOne)) {
              return null;
          }
  
          if (LocalDate.now().minusDays(7L).isBefore(DateUtil.date2LocalDate(lastOne.getCreateTime()))) {
              FollowTask lastOngoingTask = followTaskService.getOne(Wrappers.<FollowTask>lambdaQuery()
                      .eq(FollowTask::getCustomerId, lastOne.getId())
                      .eq(FollowTask::getType, FollowTypeEnum.AC)
                      .eq(FollowTask::getState, TaskStateEnum.ONGOING)
                      .last(" limit 1 ")
              );
  
              if (Objects.nonNull(lastOngoingTask)) {
                  return lastOne;
              }
          }
  
          return null;
e110c4d9   张志伟   :memo: 更换事故车重复校验
146
      }
18b1ac66   张志伟   :construction:
147
  
18b1ac66   张志伟   :construction:
148
      /**
fb44222c   张志伟   事故车跟进逻辑调整
149
       * 清理跟进任务
18b1ac66   张志伟   :construction:
150
       *
fb44222c   张志伟   事故车跟进逻辑调整
151
       * @param clueId
18b1ac66   张志伟   :construction:
152
       * @param groupId
18b1ac66   张志伟   :construction:
153
       */
fb44222c   张志伟   事故车跟进逻辑调整
154
      private void clearTask(Long clueId, Long groupId) {
18b1ac66   张志伟   :construction:
155
156
          List<FollowTask> taskList = followTaskService.list(Wrappers.<FollowTask>lambdaQuery()
                  .eq(FollowTask::getGroupId, groupId)
172cb4a3   张志伟   :art:
157
                  .eq(FollowTask::getState, TaskStateEnum.ONGOING)
fb44222c   张志伟   事故车跟进逻辑调整
158
                  .eq(FollowTask::getClueId, clueId)
18b1ac66   张志伟   :construction:
159
160
                  .eq(FollowTask::getType, FollowTypeEnum.AC)
          );
fb44222c   张志伟   事故车跟进逻辑调整
161
162
163
          if (!CollectionUtils.isEmpty(taskList)) {
              for (FollowTask task : taskList) {
                  clearRecord(task.getId());
237071fb   张志伟   :bug:
164
165
                  task.setCloseTime(new Date());
                  task.setReason(TaskDefeatTypeEnum.E);
172cb4a3   张志伟   :art:
166
                  task.setState(TaskStateEnum.DEFEAT);
fb44222c   张志伟   事故车跟进逻辑调整
167
168
              }
              followTaskService.updateBatchById(taskList);
18b1ac66   张志伟   :construction:
169
          }
18b1ac66   张志伟   :construction:
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
      }
  
      /**
       * 清理对应任务的记录
       *
       * @param taskId
       */
      private void clearRecord(Long taskId) {
          followRecordService.remove(Wrappers.<FollowRecord>lambdaQuery()
                  .eq(FollowRecord::getTaskId, taskId)
                  .eq(FollowRecord::getAddTodo, Boolean.FALSE)
          );
          List<FollowRecord> list = followRecordService.list(Wrappers.<FollowRecord>lambdaQuery()
                  .eq(FollowRecord::getTaskId, taskId)
                  .eq(FollowRecord::getAddTodo, Boolean.TRUE)
                  .eq(FollowRecord::getOutTime, Boolean.FALSE)
                  .isNull(FollowRecord::getFollowTime)
          );
          if (CollectionUtils.isEmpty(list)) {
              return;
          }
          for (FollowRecord record : list) {
fb44222c   张志伟   事故车跟进逻辑调整
192
              record.setLimitTime(DateUtil.localDateTime2Date(LocalDateTime.now()));
8571fd66   张志伟   :bug:
193
              BackLogItemDTO dto = new BackLogItemDTO(record.getUserId(), getACCode(), String.valueOf(record.getId()), DateUtil.localDateTime2Date(LocalDateTime.now()), record.getShopId());
fb44222c   张志伟   事故车跟进逻辑调整
194
              todoRpcService.cancel(dto);
18b1ac66   张志伟   :construction:
195
196
197
          }
          followRecordService.updateBatchById(list);
      }
fb44222c   张志伟   事故车跟进逻辑调整
198
199
200
201
202
203
204
205
  
      /**
       * 新增线索
       *
       * @param pool
       * @return
       */
      private CustomerCluePool createClue(final AccidentPool pool) {
8587e21d   张志伟   :art:
206
          final Customer customer = customerService.queryByPlateNo(pool.getPlateNo(), pool.getGroupId());
fb44222c   张志伟   事故车跟进逻辑调整
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
          final Date datetime = new Date();
          final CustomerCluePool cluePool = new CustomerCluePool();
          cluePool.setRefererId(pool.getId());
          cluePool.setPlateNo(pool.getPlateNo());
          cluePool.setClueType(FollowTypeEnum.AC);
          cluePool.setAddTime(datetime);
          cluePool.setStartTime(datetime);
          settingBizService.querySettingByType(FollowTypeEnum.AC, SettingTypeEnum.FAIL_TIME, pool.getGroupId())
                  .ifPresent(r -> {
                      Timestamp expired = DateUtil.getExpired(datetime, r.getDetailValue(), getCalendarType(Objects.requireNonNull(SettingUnitEnum.ofValue(r.getUnit()))));
                      cluePool.setDeadline(expired);
                  });
          cluePool.setClueStatus(ClueStatusEnum.ONGOING);
          cluePool.setRedistribution(Boolean.FALSE);
          ShopDTO shop = oopService.shop(pool.getShopId());
          BV.notNull(shop, () -> "门店信息有误");
          List<PostUserDTO> userByRole = userService.getUserByRole(pool.getShopId(), RoleCode.SGCGJ);
          BV.isFalse(CollectionUtils.isEmpty(userByRole), () -> "该门店没有事故车跟进人员");
740805dc   张志伟   :zap:
225
226
          int randomIndex = new Random().nextInt(userByRole.size());
          PostUserDTO userDTO = userByRole.get(randomIndex);
fb44222c   张志伟   事故车跟进逻辑调整
227
228
          cluePool.setOriginalUserId(userDTO.getUserId());
          cluePool.setOriginalUserName(userDTO.getUserName());
8587e21d   张志伟   :art:
229
230
231
232
233
234
235
236
237
          if (Objects.nonNull(customer)) {
              if (shop.getId().equals(customer.getShopId())) {
                  Optional<PostUserDTO> dto = userByRole.stream().filter(u -> u.getUserId().equals(customer.getAdviserId())).findFirst();
                  dto.ifPresent(d -> {
                      cluePool.setOriginalUserId(d.getUserId());
                      cluePool.setOriginalUserName(d.getUserName());
                  });
              }
          }
fb44222c   张志伟   事故车跟进逻辑调整
238
          cluePool.setOriginalShopId(shop.getId());
ae766fec   张志伟   :bug:
239
          cluePool.setOriginalShopName(shop.getShortName());
fb44222c   张志伟   事故车跟进逻辑调整
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
          cluePool.setGroupId(pool.getGroupId());
          cluePool.setCreateTime(new Date());
          customerCluePoolService.save(cluePool);
          return cluePool;
      }
  
      /**
       * 新增跟进任务
       *
       * @param pool
       * @return
       */
      private FollowTask createTask(final CustomerCluePool pool) {
          final FollowTask task = new FollowTask();
          task.setClueId(pool.getId());
          task.setCustomerId(pool.getRefererId());
          task.setType(FollowTypeEnum.AC);
172cb4a3   张志伟   :art:
257
          task.setState(TaskStateEnum.ONGOING);
fb44222c   张志伟   事故车跟进逻辑调整
258
259
260
261
          task.setBeginTime(pool.getStartTime());
          task.setRedistribution(Boolean.FALSE);
          task.setDeadline(pool.getDeadline());
          task.setFollowUser(pool.getOriginalUserId());
4bb0bf70   张志伟   :bug:
262
          task.setFollowUserName(pool.getOriginalUserName());
fb44222c   张志伟   事故车跟进逻辑调整
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
          task.setFollowShop(pool.getOriginalShopId());
          task.setGroupId(pool.getGroupId());
          task.setCreateTime(new Date());
          followTaskService.save(task);
          return task;
      }
  
      /**
       * 新增跟进代办任务
       *
       * @param task
       */
      private void createTaskRecord(final FollowTask task) {
          final FollowRecord record = new FollowRecord();
          record.setTaskId(task.getId());
          record.setType(FollowTypeEnum.AC);
          record.setCustomerId(task.getCustomerId());
bf3704cc   张志伟   :art:
280
281
          record.setUserId(task.getFollowUser());
          record.setUserName(task.getFollowUserName());
fb44222c   张志伟   事故车跟进逻辑调整
282
283
284
285
286
287
288
289
290
291
          record.setPlanTime(task.getBeginTime());
          settingBizService.querySettingByType(FollowTypeEnum.AC, SettingTypeEnum.EFFECTIVE_TIME, task.getGroupId())
                  .ifPresent(r -> {
                      Timestamp expired = DateUtil.getExpired(task.getBeginTime(), r.getDetailValue(), getCalendarType(Objects.requireNonNull(SettingUnitEnum.ofValue(r.getUnit()))));
                      record.setDeadline(expired);
                  });
          record.setOutTime(Boolean.FALSE);
          record.setAddTodo(Boolean.FALSE);
          record.setGroupId(task.getGroupId());
          record.setShopId(task.getFollowShop());
51a118ce   张志伟   :sparkles:
292
          record.setLimitTime(record.getDeadline());
58d889cc   张志伟   新增重复提交校验和事故车跟进逾期时间查询
293
          followRecordService.queryAndSave(record);
fb44222c   张志伟   事故车跟进逻辑调整
294
      }
e95f940e   张志伟   :art:
295
  }