Blame view

fw-valhalla-service/src/main/java/cn/fw/valhalla/service/bus/pub/PubFollowBizService.java 10.9 KB
364db3cc   张志伟   :sparkles:
1
2
  package cn.fw.valhalla.service.bus.pub;
  
6f401c2d   张志伟   feature(*): 公共池完成逻辑
3
  import cn.fw.common.constant.CommonConstant;
c2c96eeb   张志伟   feature(*): 公共池站岗联调
4
  import cn.fw.valhalla.common.utils.StringUtils;
5b538595   张志伟   feature(*): 新增查询线...
5
  import cn.fw.valhalla.common.utils.ThreadPoolUtil;
9de0e2d7   张志伟   feature(*): 公共池线索...
6
7
8
9
  import cn.fw.valhalla.domain.db.customer.Customer;
  import cn.fw.valhalla.domain.db.customer.CustomerBaseInfo;
  import cn.fw.valhalla.domain.db.follow.ClueTask;
  import cn.fw.valhalla.domain.db.pool.PublicPool;
1331548a   张志伟   :sparkles:
10
  import cn.fw.valhalla.domain.db.pub.PubCluePool;
9de0e2d7   张志伟   feature(*): 公共池线索...
11
  import cn.fw.valhalla.domain.enums.*;
c2c96eeb   张志伟   feature(*): 公共池站岗联调
12
  import cn.fw.valhalla.domain.vo.pool.PubPoolSimpleVO;
9de0e2d7   张志伟   feature(*): 公共池线索...
13
14
15
  import cn.fw.valhalla.rpc.oop.OopService;
  import cn.fw.valhalla.rpc.oop.dto.ShopDTO;
  import cn.fw.valhalla.service.bus.follow.strategy.impl.PubFollowStrategy;
9de0e2d7   张志伟   feature(*): 公共池线索...
16
  import cn.fw.valhalla.service.data.*;
1331548a   张志伟   :sparkles:
17
18
19
  import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  import lombok.AccessLevel;
  import lombok.Getter;
364db3cc   张志伟   :sparkles:
20
  import lombok.extern.slf4j.Slf4j;
9de0e2d7   张志伟   feature(*): 公共池线索...
21
  import org.springframework.beans.factory.annotation.Autowired;
1331548a   张志伟   :sparkles:
22
  import org.springframework.beans.factory.annotation.Value;
9de0e2d7   张志伟   feature(*): 公共池线索...
23
24
  import org.springframework.data.redis.core.BoundSetOperations;
  import org.springframework.data.redis.core.StringRedisTemplate;
364db3cc   张志伟   :sparkles:
25
  import org.springframework.stereotype.Service;
1331548a   张志伟   :sparkles:
26
27
28
  import org.springframework.transaction.annotation.Transactional;
  import org.springframework.util.CollectionUtils;
  
9de0e2d7   张志伟   feature(*): 公共池线索...
29
  import java.time.LocalDateTime;
c2c96eeb   张志伟   feature(*): 公共池站岗联调
30
  import java.util.ArrayList;
1331548a   张志伟   :sparkles:
31
  import java.util.List;
9de0e2d7   张志伟   feature(*): 公共池线索...
32
33
  import java.util.Objects;
  import java.util.Optional;
c2c96eeb   张志伟   feature(*): 公共池站岗联调
34
35
  import java.util.concurrent.CompletableFuture;
  import java.util.stream.Collectors;
9de0e2d7   张志伟   feature(*): 公共池线索...
36
37
  
  import static cn.fw.common.businessvalidator.Validator.BV;
364db3cc   张志伟   :sparkles:
38
39
40
41
42
43
44
45
46
47
  
  /**
   * 公共池跟进处理
   *
   * @author : kurisu
   * @version : 1.0
   * @className : PubFollowBizService
   * @description : 公共池跟进处理
   * @date : 2023-03-17 17:23
   */
364db3cc   张志伟   :sparkles:
48
  @Slf4j
1331548a   张志伟   :sparkles:
49
  @Service
364db3cc   张志伟   :sparkles:
50
  public class PubFollowBizService {
1331548a   张志伟   :sparkles:
51
      private final PubCluePoolService pubCluePoolService;
9de0e2d7   张志伟   feature(*): 公共池线索...
52
      private final PublicPoolService publicPoolService;
1331548a   张志伟   :sparkles:
53
54
      private final StammkundePoolService stammkundePoolService;
      private final CustomerService customerService;
9de0e2d7   张志伟   feature(*): 公共池线索...
55
      private final CustomerBaseInfoService customerBaseInfoService;
9de0e2d7   张志伟   feature(*): 公共池线索...
56
57
58
59
      private final ClueTaskService clueTaskService;
      private final StringRedisTemplate stringRedisTemplate;
      private final OopService oopService;
      private final PubFollowStrategy pubFollowStrategy;
1331548a   张志伟   :sparkles:
60
61
62
63
      @Value("${follow.todo.PubFollowCode}")
      @Getter(AccessLevel.PRIVATE)
      private String pubFollowCode;
  
9de0e2d7   张志伟   feature(*): 公共池线索...
64
65
66
      @Value("${spring.cache.custom.global-prefix}:role-change-need-close:clue")
      @Getter(AccessLevel.PRIVATE)
      private String roleChangeNeedCloseClueKey;
1331548a   张志伟   :sparkles:
67
  
9de0e2d7   张志伟   feature(*): 公共池线索...
68
69
70
71
72
73
      @Autowired
      public PubFollowBizService(final PubCluePoolService pubCluePoolService,
                                 final PublicPoolService publicPoolService,
                                 final StammkundePoolService stammkundePoolService,
                                 final CustomerService customerService,
                                 final CustomerBaseInfoService customerBaseInfoService,
9de0e2d7   张志伟   feature(*): 公共池线索...
74
75
76
77
78
79
80
81
82
                                 final ClueTaskService clueTaskService,
                                 final StringRedisTemplate stringRedisTemplate,
                                 final OopService oopService,
                                 final PubFollowStrategy pubFollowStrategy) {
          this.pubCluePoolService = pubCluePoolService;
          this.publicPoolService = publicPoolService;
          this.stammkundePoolService = stammkundePoolService;
          this.customerService = customerService;
          this.customerBaseInfoService = customerBaseInfoService;
9de0e2d7   张志伟   feature(*): 公共池线索...
83
84
85
86
87
          this.clueTaskService = clueTaskService;
          this.stringRedisTemplate = stringRedisTemplate;
          this.oopService = oopService;
          this.pubFollowStrategy = pubFollowStrategy;
      }
1331548a   张志伟   :sparkles:
88
  
9de0e2d7   张志伟   feature(*): 公共池线索...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
      /**
       * 人员角色变动或缓存需要战败的vin
       *
       * @param userId
       * @param shopId
       */
      public void onRoleChange(Long userId, Long shopId) {
          List<PubCluePool> list = pubCluePoolService.list(Wrappers.<PubCluePool>lambdaQuery()
                  .eq(PubCluePool::getAdviserId, userId)
                  .eq(PubCluePool::getState, PublicClueStateEnum.ONGOING)
          );
          if (CollectionUtils.isEmpty(list)) {
              return;
          }
          BoundSetOperations<String, String> setOps = stringRedisTemplate.boundSetOps(getRoleChangeNeedCloseClueKey());
          for (PubCluePool pool : list) {
              setOps.add(String.valueOf(pool.getId()));
          }
      }
1331548a   张志伟   :sparkles:
108
  
9de0e2d7   张志伟   feature(*): 公共池线索...
109
      /**
3d6f8270   张志伟   feature(*): 公共池线索...
110
       * 开始线索
c2c96eeb   张志伟   feature(*): 公共池站岗联调
111
       *
3d6f8270   张志伟   feature(*): 公共池线索...
112
113
114
115
116
117
118
119
120
121
       * @param pool
       */
      @Transactional(rollbackFor = Exception.class)
      public void startClue(PubCluePool pool) {
          pubFollowStrategy.startClue(pool);
          pool.setBegun(Boolean.TRUE);
          pubCluePoolService.updateById(pool);
      }
  
      /**
6f401c2d   张志伟   feature(*): 公共池完成逻辑
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
       * 关闭线索
       *
       * @param str
       */
      @Transactional(rollbackFor = Exception.class)
      public void closeClue(String str) {
          String[] dataArr = str.split(CommonConstant.Symbol.AT);
          if (dataArr.length < 4) {
              log.info("关闭公共池线索失败:{}", str);
              return;
          }
          String vin = dataArr[0];
          Long adviserId = Long.parseLong(dataArr[1]);
          Long groupId = Long.parseLong(dataArr[2]);
          Long shopId = Long.parseLong(dataArr[3]);
  
          PubCluePool pool = pubCluePoolService.getOne(Wrappers.<PubCluePool>lambdaQuery()
                          .eq(PubCluePool::getVin, vin)
                          .eq(PubCluePool::getGroupId, groupId)
                          .eq(PubCluePool::getState, PublicClueStateEnum.ONGOING)
                  , Boolean.FALSE
          );
  
          if (Objects.isNull(pool)) {
              return;
          }
          if (pool.getAdviserId().equals(adviserId)) {
              pubFollowStrategy.clueConvertSuccess(pool);
              return;
          }
          pubFollowStrategy.clueConvertFailed(pool, adviserId, shopId);
  
      }
  
      /**
c2c96eeb   张志伟   feature(*): 公共池站岗联调
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
       * 查询最近分配的20条线索
       *
       * @param userId
       * @param groupId
       * @return
       */
      public List<PubPoolSimpleVO> log(Long userId, Long groupId) {
          List<PubCluePool> list = pubCluePoolService.list(Wrappers.<PubCluePool>lambdaQuery()
                  .eq(PubCluePool::getAdviserId, userId)
                  .eq(PubCluePool::getGroupId, groupId)
                  .orderByDesc(PubCluePool::getStartTime)
                  .last(" limit 20")
          );
          if (CollectionUtils.isEmpty(list)) {
              return new ArrayList<>();
          }
          List<PubPoolSimpleVO> simpleList = list.stream()
755a1f60   张志伟   feature(*): 修复bug
174
175
                  .map(PubPoolSimpleVO::with)
                  .collect(Collectors.toList());
c2c96eeb   张志伟   feature(*): 公共池站岗联调
176
177
178
179
180
181
182
183
184
185
186
          CompletableFuture<Void>[] futureArr = simpleList.stream()
                  .map(pool -> CompletableFuture.runAsync(() -> {
                      Customer customer = customerService.queryByFrameNo(pool.getVin(), pool.getGroupId());
                      if (Objects.nonNull(customer)) {
                          pool.setPlateNo(customer.getPlateNo());
                          String brandName = StringUtils.isEmpty(customer.getBrandName()) ? "" : customer.getBrandName() + " ";
                          String seriesName = StringUtils.isEmpty(customer.getSeriesName()) ? "" : customer.getSeriesName() + " ";
                          String specName = StringUtils.isEmpty(customer.getSpecName()) ? "" : customer.getSpecName();
                          String carName = String.format("%s%s%s", brandName, seriesName, specName);
                          pool.setCarName(carName);
                      }
5b538595   张志伟   feature(*): 新增查询线...
187
                  }, ThreadPoolUtil.getInstance().getExecutor())).<CompletableFuture<Void>>toArray(CompletableFuture[]::new);
c2c96eeb   张志伟   feature(*): 公共池站岗联调
188
  
666de3c5   张志伟   feature(*): 优化查询线...
189
          CompletableFuture.allOf(futureArr).join();
c2c96eeb   张志伟   feature(*): 公共池站岗联调
190
191
192
193
          return simpleList;
      }
  
      /**
9de0e2d7   张志伟   feature(*): 公共池线索...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
       * 角色变动结束线索
       *
       * @param id
       */
      @Transactional(rollbackFor = Exception.class)
      public void roleChangeDefeatClue(String id) {
          PubCluePool pool = pubCluePoolService.getById(Long.valueOf(id));
          if (Objects.isNull(pool)) {
              return;
          }
          Customer customer = customerService.queryByFrameNo(pool.getVin(), pool.getGroupId());
          endClue(pool);
          if (Objects.nonNull(customer)) {
              customerRemoval(customer);
1331548a   张志伟   :sparkles:
208
209
210
211
212
          }
      }
  
      /**
       * 结束线索
9de0e2d7   张志伟   feature(*): 公共池线索...
213
       *
1331548a   张志伟   :sparkles:
214
215
       * @param pool
       */
3d6f8270   张志伟   feature(*): 公共池线索...
216
      @Transactional(rollbackFor = Exception.class)
1331548a   张志伟   :sparkles:
217
      public void endClue(PubCluePool pool) {
9de0e2d7   张志伟   feature(*): 公共池线索...
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
          ClueTask task = clueTaskService.getOne(Wrappers.<ClueTask>lambdaQuery()
                          .eq(ClueTask::getClueId, pool.getId())
                          .eq(ClueTask::getType, FollowTypeEnum.PL)
                          .eq(ClueTask::getState, TaskStateEnum.ONGOING)
                  , Boolean.FALSE);
  
          if (Objects.nonNull(task)) {
              pubFollowStrategy.onRoleChangeCloseTask(task);
          }
          pool.setState(PublicClueStateEnum.DEFEAT);
          pool.setCloseTime(LocalDateTime.now());
          pool.setDefeatReason(TaskDefeatTypeEnum.D);
          pubCluePoolService.updateById(pool);
      }
  
      /**
       * 移除档案到公共池
       *
       * @param customer
       */
      private void customerRemoval(Customer customer) {
          PublicPool publicPool = createPublicPool(customer);
          publicPoolService.save(publicPool);
          stammkundePoolService.reject(customer.getId(), customer.getGroupId(), DefeatReasonEnum.RC);
4f92d929   张志伟   bug 修复
242
243
244
245
246
247
248
249
  
          customerService.update(Wrappers.<Customer>lambdaUpdate()
                  .set(Customer::getTemporary, null)
                  .set(Customer::getShopId, null)
                  .set(Customer::getAdviserId, null)
                  .eq(Customer::getId, customer.getId())
          );
  
9de0e2d7   张志伟   feature(*): 公共池线索...
250
      }
1331548a   张志伟   :sparkles:
251
  
9de0e2d7   张志伟   feature(*): 公共池线索...
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
      private PublicPool createPublicPool(Customer customer) {
          final Long shopId = Optional.ofNullable(customer.getLastArrivalShop()).orElse(customer.getShopId());
          ShopDTO shop = oopService.shop(shopId);
          BV.notNull(shop, () -> String.format("门店[%s]信息获取失败", shopId));
          String shopName = shop.getShortName();
          final Long baseId = customer.getBaseId();
          CustomerBaseInfo baseInfo = customerBaseInfoService.getById(baseId);
          BV.notNull(baseInfo, () -> String.format("档案车主信息获取失败[%s]", baseId));
          PublicPool pool = new PublicPool();
          pool.setCustomerId(customer.getId());
          pool.setCustomerName(baseInfo.getName());
          pool.setPlateNo(customer.getPlateNo());
          pool.setFrameNo(customer.getFrameNo());
          pool.setBrandId(customer.getBrandId());
          pool.setBrandName(customer.getBrandName());
          pool.setSeriesId(customer.getSeriesId());
          pool.setSeriesName(customer.getSeriesName());
          pool.setSpecId(customer.getSpecId());
          pool.setSpecName(customer.getSpecName());
          pool.setAddress(baseInfo.getAddress());
          pool.setCompany(baseInfo.getCompanyName());
          pool.setType(PublicPoolTypeEnum.RC);
          pool.setShopId(shopId);
          pool.setShopName(shopName);
          pool.setTimes(customer.getArrivalCount());
          pool.setReason(PublicPoolTypeEnum.RC.getName());
          pool.setGroupId(customer.getGroupId());
          return pool;
1331548a   张志伟   :sparkles:
280
281
      }
  
364db3cc   张志伟   :sparkles:
282
  }