DistributeServiceImpl.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. package com.cyksj.service.distribute.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateTime;
  4. import cn.hutool.core.lang.Assert;
  5. import cn.hutool.core.util.ObjectUtil;
  6. import cn.hutool.core.util.StrUtil;
  7. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  8. import com.cyksj.common.constant.Constant;
  9. import com.cyksj.common.exception.BusinessRuntimeException;
  10. import com.cyksj.common.util.Jsons;
  11. import com.cyksj.mapper.GoodsDonMapper;
  12. import com.cyksj.mapper.UserDistributeSharedRelateUnbindMapper;
  13. import com.cyksj.mapper.UserMapper;
  14. import com.cyksj.mapper.manage.coupon.CouponDistributePopularizeMapper;
  15. import com.cyksj.mapper.manage.coupon.CouponMapper;
  16. import com.cyksj.mapper.manage.distribute.*;
  17. import com.cyksj.model.entity.*;
  18. import com.cyksj.model.request.UserDistributeReq;
  19. import com.cyksj.redis.RedisService;
  20. import com.cyksj.service.distribute.DistributeService;
  21. import lombok.RequiredArgsConstructor;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.springframework.dao.DuplicateKeyException;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import java.math.BigDecimal;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Optional;
  31. import java.util.concurrent.ConcurrentHashMap;
  32. import java.util.stream.Collectors;
  33. /*
  34. *项目名: netflix
  35. *文件名: DistributeServiceImpl
  36. *创建者: JavaZou
  37. *创建时间:2022/11/14 12:21
  38. */
  39. @Service
  40. @RequiredArgsConstructor
  41. @Slf4j
  42. public class DistributeServiceImpl implements DistributeService {
  43. private final UserDistributeMapper userDistributeMapper;
  44. private final UserPlatDistributeRateMapper userPlatDistributeRateMapper;
  45. private final GoodsDonMapper goodsDonMapper;
  46. private final UserDistributeSharedMapper userDistributeSharedMapper;
  47. private final UserBusinessDefaultRateConfigMapper defaultRateConfigMapper;
  48. private final RedisService redisService;
  49. private final CouponMapper couponMapper;
  50. private final CouponDistributePopularizeMapper couponDistributePopularizeMapper;
  51. private final UserPlatSkuDistributeRateMapper userPlatSkuDistributeRateMapper;
  52. private final UserDistributeTargetAlertMapper targetAlertMapper;
  53. private final UserDistributeSharedRelateUnbindMapper userDistributeSharedRelateUnbindMapper;
  54. private final DistributeGeneralSkuRateMapper generalSkuRateMapper;
  55. private final UserMapper userMapper;
  56. @Override
  57. @Transactional(rollbackFor = Throwable.class)
  58. public void insertDistribute(UserDistributeReq userDistributeReq) {
  59. if (!redisService.setNx(RedisService.key.USER_DISTRIBUTE_ADD_KEY.getNameFormat(userDistributeReq.getUserId()), userDistributeReq.getUserId(), 2 * 5l)) {
  60. return;
  61. }
  62. UserDistribute exists = userDistributeMapper.selectOne(Wrappers.lambdaQuery(UserDistribute.class).eq(UserDistribute::getUserId, userDistributeReq.getUserId()).eq(UserDistribute::getDeleted, true).last("limit 1"));
  63. if (exists != null) {
  64. throw BusinessRuntimeException.getInstance("该推广者已添加");
  65. }
  66. Long businessId = userDistributeReq.getBusinessId();
  67. UserDistribute userDistribute = new UserDistribute();
  68. userDistribute.setUserId(userDistributeReq.getUserId());
  69. userDistribute.setRemark(userDistributeReq.getRemark());
  70. userDistribute.setType(userDistributeReq.getType());
  71. userDistribute.setBusinessId(businessId);
  72. if (businessId != null && businessId != 0) {
  73. userDistribute.setBusinessBindTime(DateTime.now());
  74. }
  75. userDistributeMapper.insert(userDistribute);
  76. for (UserPlatDistributeRate rate : userDistributeReq.getRates()) {
  77. if (rate.getRate() == null) {
  78. rate.setRate(0);
  79. }
  80. if (rate.getSecondRate() == null) {
  81. rate.setSecondRate(0);
  82. }
  83. if (rate.getRate() < 0 || rate.getRate() > 100) {
  84. throw new BusinessRuntimeException("比例错误");
  85. }
  86. GoodsDon goodsDon = goodsDonMapper.selectById(rate.getGoodsId());
  87. if (goodsDon == null || !goodsDon.getDeleted() || goodsDon.getType() == 3) {
  88. throw new BusinessRuntimeException("平台错误");
  89. }
  90. //分销者虚物分销比例且配置商务
  91. if (businessId != null && goodsDon.getType() == 1) {
  92. checkDistributorAndBusinessRate(rate.getRate(), goodsDon.getId(), goodsDon.getTitle());
  93. }
  94. rate.setDistributeId(userDistribute.getId());
  95. userPlatDistributeRateMapper.insert(rate);
  96. }
  97. List<UserPlatSkuDistributeRate> skuRates = userDistributeReq.getSkuRates();
  98. if (CollUtil.isNotEmpty(skuRates)) {
  99. Map<Long, GoodsDon> map = new ConcurrentHashMap<>();
  100. for (UserPlatSkuDistributeRate skuRate : skuRates) {
  101. if (skuRate.getRate() == null) {
  102. skuRate.setRate(0);
  103. }
  104. if (skuRate.getSecondRate() == null) {
  105. skuRate.setSecondRate(0);
  106. }
  107. if (skuRate.getRate() < 0 || skuRate.getRate() > 100) {
  108. throw new BusinessRuntimeException("比例错误");
  109. }
  110. GoodsDon goodsDon = map.get(skuRate.getGoodsId());
  111. if (goodsDon == null) {
  112. goodsDon = goodsDonMapper.selectById(skuRate.getGoodsId());
  113. map.putIfAbsent(skuRate.getSkuId(), goodsDon);
  114. }
  115. try {
  116. skuRate.setDistributeId(userDistribute.getId());
  117. if (businessId != null && goodsDon.getType() == 1) {
  118. checkDistributorAndBusinessRate(skuRate.getRate(), skuRate.getGoodsId(), goodsDon.getTitle());
  119. }
  120. userPlatSkuDistributeRateMapper.insert(skuRate);
  121. } catch (DuplicateKeyException e) {
  122. }
  123. }
  124. }
  125. //渠道附加功能
  126. handlerDistributeOtherFunc(userDistribute, userDistributeReq);
  127. //设置过个人专属推广码 升级为渠道专属优惠码
  128. setDistributePopularizeCode(userDistribute.getId(), userDistribute.getUserId());
  129. }
  130. @Override
  131. @Transactional(rollbackFor = Throwable.class)
  132. public void editDistribute(UserDistributeReq userDistributeReq) {
  133. if (userDistributeReq.getId() == null || userDistributeReq.getId() < 1) {
  134. throw BusinessRuntimeException.getInstance("id错误");
  135. }
  136. Long id = userDistributeReq.getId();
  137. UserDistribute userDistribute = userDistributeMapper.selectById(id);
  138. if (userDistribute == null || !userDistribute.getDeleted()) {
  139. return;
  140. }
  141. userDistribute.setId(userDistributeReq.getId());
  142. userDistribute.setRemark(userDistributeReq.getRemark());
  143. Long businessId = userDistributeReq.getBusinessId();
  144. if (businessId == null) {
  145. userDistribute.setBusinessId(0l);
  146. } else {
  147. if (userDistribute.getBusinessId() != null && !userDistribute.getBusinessId().equals(businessId)) {
  148. userDistribute.setBusinessBindTime(DateTime.now());
  149. }
  150. userDistribute.setBusinessId(businessId);
  151. }
  152. userDistribute.setType(userDistributeReq.getType());
  153. userDistributeMapper.updateById(userDistribute);
  154. for (UserPlatDistributeRate rate : userDistributeReq.getRates()) {
  155. if (rate.getRate() == null) {
  156. rate.setRate(0);
  157. }
  158. if (rate.getSecondRate() == null) {
  159. rate.setSecondRate(0);
  160. }
  161. if (rate.getRate() < 0 || rate.getRate() > 100) {
  162. throw new BusinessRuntimeException("比例错误");
  163. }
  164. GoodsDon goodsDon = goodsDonMapper.selectById(rate.getGoodsId());
  165. if (rate.getId() != null && (goodsDon == null || !goodsDon.getDeleted())) {
  166. userPlatDistributeRateMapper.deleteById(rate.getId());
  167. continue;
  168. }
  169. if (goodsDon == null || !goodsDon.getDeleted() || goodsDon.getType() == 3) {
  170. throw new BusinessRuntimeException("平台错误");
  171. }
  172. //分销者虚物分销比例且配置商务
  173. if (businessId != null && goodsDon.getType() == 1) {
  174. checkDistributorAndBusinessRate(rate.getRate(), goodsDon.getId(), goodsDon.getTitle());
  175. }
  176. rate.setDistributeId(userDistribute.getId());
  177. if (rate.getId() == null) {
  178. try {
  179. userPlatDistributeRateMapper.insert(rate);
  180. } catch (DuplicateKeyException e) {
  181. userPlatDistributeRateMapper.update(null, Wrappers.lambdaUpdate(UserPlatDistributeRate.class)
  182. .eq(UserPlatDistributeRate::getDistributeId, userDistribute.getId())
  183. .eq(UserPlatDistributeRate::getGoodsId, rate.getGoodsId())
  184. .set(UserPlatDistributeRate::getRate, rate.getRate()));
  185. }
  186. } else {
  187. UserPlatDistributeRate dbPlateRate = userPlatDistributeRateMapper.selectById(rate.getId());
  188. if (dbPlateRate == null) {
  189. continue;
  190. }
  191. if (dbPlateRate.getRate() != rate.getRate() || dbPlateRate.getSecondRate() != rate.getSecondRate()) {
  192. userPlatDistributeRateMapper.updateById(rate);
  193. }
  194. }
  195. }
  196. //特殊平台 按规格分销比例
  197. List<UserPlatSkuDistributeRate> skuRates = userDistributeReq.getSkuRates();
  198. if (CollUtil.isNotEmpty(skuRates)) {
  199. Map<Long, GoodsDon> map = new ConcurrentHashMap<>();
  200. for (UserPlatSkuDistributeRate skuRate : skuRates) {
  201. if (skuRate.getRate() == null) {
  202. skuRate.setRate(0);
  203. }
  204. if (skuRate.getSecondRate() == null) {
  205. skuRate.setSecondRate(0);
  206. }
  207. if (skuRate.getRate() < 0 || skuRate.getRate() > 100) {
  208. throw new BusinessRuntimeException("比例错误");
  209. }
  210. GoodsDon goodsDon = map.get(skuRate.getGoodsId());
  211. if (goodsDon == null) {
  212. goodsDon = goodsDonMapper.selectById(skuRate.getGoodsId());
  213. map.putIfAbsent(skuRate.getSkuId(), goodsDon);
  214. }
  215. if (businessId != null && goodsDon.getType() == 1) {
  216. checkDistributorAndBusinessRate(skuRate.getRate(), goodsDon.getId(), goodsDon.getTitle());
  217. }
  218. if (skuRate.getId() != null) {
  219. userPlatSkuDistributeRateMapper.updateById(skuRate);
  220. continue;
  221. }
  222. try {
  223. skuRate.setDistributeId(userDistribute.getId());
  224. userPlatSkuDistributeRateMapper.insert(skuRate);
  225. } catch (DuplicateKeyException e) {
  226. }
  227. }
  228. }
  229. handlerDistributeOtherFunc(userDistribute, userDistributeReq);
  230. }
  231. /**
  232. * 渠道附加功能
  233. */
  234. public void handlerDistributeOtherFunc(UserDistribute userDistribute, UserDistributeReq userDistributeReq) {
  235. //处理优惠券关联
  236. handlerDistributeCouponRelate(userDistribute.getId(), userDistribute.getUserId(), userDistributeReq.getCouponRelates());
  237. // //渠道目标额配置
  238. // handlerDistributeTargetAlert(userDistribute.getId(), userDistributeReq.getTargets());
  239. }
  240. @Override
  241. public void saveDistributionInvitation(Long sharedId, Long userId, Long dsPopularizeId) {
  242. if (sharedId == 0l || sharedId.equals(userId)) {
  243. return;
  244. }
  245. User user = userMapper.selectById(sharedId);
  246. if (user == null) {
  247. return;
  248. }
  249. UserDistributeShared userDistributeShared = new UserDistributeShared();
  250. userDistributeShared.setSharedId(sharedId);
  251. //存在分销
  252. userDistributeShared.setUserId(userId);
  253. userDistributeShared.setDsPopularizeId(dsPopularizeId);
  254. try {
  255. userDistributeSharedMapper.insert(userDistributeShared);
  256. } catch (DuplicateKeyException e) {
  257. log.info("分销被邀请人重复插入");
  258. }
  259. }
  260. @Override
  261. @Transactional(rollbackFor = Throwable.class)
  262. public void bindPopularizeCode(Long userId, String code) {
  263. Coupon coupon = couponMapper.getCouponById(Constant.GENERAL_POPULARIZE_COUPON_ID);
  264. if (!redisService.setNx(RedisService.key.GENERAL_POPULARIZE_CODE.getName() + code, userId, RedisService.key.GENERAL_POPULARIZE_CODE.getTimeout())) {
  265. throw BusinessRuntimeException.getInstance("该专属推广优惠码已被设置");
  266. }
  267. UserDistribute userDistribute = userDistributeMapper.selectOne(Wrappers.lambdaQuery(UserDistribute.class)
  268. .eq(UserDistribute::getUserId, userId)
  269. .eq(UserDistribute::getDeleted, true)
  270. .last("limit 1"));
  271. if (userDistribute != null) throw BusinessRuntimeException.getInstance("只适用于普通用户进行专属推广");
  272. CouponDistributePopularize is_exists = couponDistributePopularizeMapper.selectOne(Wrappers.lambdaQuery(CouponDistributePopularize.class)
  273. .eq(CouponDistributePopularize::getUserId, userId)
  274. .last("limit 1"));
  275. if (is_exists != null) throw BusinessRuntimeException.getInstance("你已设置专属推广优惠码");
  276. CouponDistributePopularize couponDistributePopularize = new CouponDistributePopularize();
  277. couponDistributePopularize.setCouponId(coupon.getId());
  278. couponDistributePopularize.setUserId(userId);
  279. couponDistributePopularize.setIsGeneral(true);
  280. CouponDistributePopularize exists = couponDistributePopularizeMapper.selectOne(Wrappers.lambdaQuery(CouponDistributePopularize.class)
  281. .eq(CouponDistributePopularize::getExCode, code)
  282. .eq(CouponDistributePopularize::getDeleted, true)
  283. .ne(CouponDistributePopularize::getUserId, userId)
  284. .select(CouponDistributePopularize::getId)
  285. .last("limit 1"));
  286. if (exists != null) {
  287. throw BusinessRuntimeException.getInstance("该专属推广优惠码已被设置");
  288. } else {
  289. Coupon existsCoupon = couponMapper.selectOne(Wrappers.lambdaQuery(Coupon.class)
  290. .eq(Coupon::getExCode, code)
  291. .eq(Coupon::getDeleted, true)
  292. .select(Coupon::getId)
  293. .last("limit 1"));
  294. if (existsCoupon != null) {
  295. throw BusinessRuntimeException.getInstance("该专属推广优惠码已被设置");
  296. }
  297. }
  298. couponDistributePopularize.setExCode(code);
  299. couponDistributePopularizeMapper.insert(couponDistributePopularize);
  300. }
  301. @Override
  302. public void unbindUserDistributeShared(List<Object> userBind) throws Exception {
  303. List<Long> save_userIds = new ArrayList<>(500);
  304. userBind.forEach(e -> {
  305. Map<String, Object> map = Jsons.toMap(e);
  306. Object value0 = map.get("0");
  307. if (value0 != null) {
  308. Long userId = Long.parseLong(value0.toString());
  309. save_userIds.add(userId);
  310. if (save_userIds.size() == 500) {
  311. userDistributeSharedRelateUnbindMapper.batchInsert(save_userIds);
  312. save_userIds.clear();
  313. }
  314. }
  315. });
  316. if (save_userIds.size() > 0) {
  317. userDistributeSharedRelateUnbindMapper.batchInsert(save_userIds);
  318. save_userIds.clear();
  319. }
  320. }
  321. @Override
  322. public void setDistributeCommonSkuRate(List<DistributeGeneralSkuRate> skuRates) {
  323. List<Long> collect = skuRates.stream().map(DistributeGeneralSkuRate::getSkuId).collect(Collectors.toList());
  324. if (CollUtil.isNotEmpty(collect)) {
  325. generalSkuRateMapper.delete(Wrappers.lambdaQuery(DistributeGeneralSkuRate.class)
  326. .notIn(DistributeGeneralSkuRate::getSkuId, collect));
  327. }
  328. skuRates.forEach(e -> {
  329. Integer rate = e.getRate();
  330. if (rate == null || rate < 0 || rate > 100) {
  331. throw BusinessRuntimeException.getInstance("请输入正确的比例");
  332. }
  333. if (e.getId() == null) {
  334. try {
  335. generalSkuRateMapper.insert(e);
  336. } catch (DuplicateKeyException ex) {
  337. }
  338. } else {
  339. generalSkuRateMapper.updateById(e);
  340. }
  341. });
  342. }
  343. public void checkDistributorAndBusinessRate(Integer distributeGoodsRate, Long goodsId, String goodsName) {
  344. UserBusinessDefaultRateConfig defaultGoodsRateConfig = defaultRateConfigMapper.selectOne(Wrappers.lambdaQuery(UserBusinessDefaultRateConfig.class)
  345. .eq(UserBusinessDefaultRateConfig::getGoodsId, goodsId)
  346. .last("limit 1"));
  347. Optional.ofNullable(defaultGoodsRateConfig).ifPresent(config -> {
  348. if (distributeGoodsRate > config.getMaxRate()) {
  349. throw BusinessRuntimeException.getInstance(String.format("该%s平台最高可配置分销比例为%s", goodsName, config.getMaxRate()));
  350. }
  351. });
  352. }
  353. private void handlerDistributeCouponRelate(Long distributeId, Long userId, List<CouponDistributePopularize> couponRelates) {
  354. Map<Long, CouponDistributePopularize> map = new ConcurrentHashMap<>();
  355. couponRelates.forEach(data -> {
  356. data.setDistributeId(distributeId);
  357. if (data.getCouponId() == null) {
  358. throw BusinessRuntimeException.getInstance("优惠券不存在");
  359. }
  360. Integer exists = couponMapper.selectCount(Wrappers.lambdaQuery(Coupon.class).eq(Coupon::getId, data.getCouponId()).eq(Coupon::getDeleted, 1));
  361. if (exists == 0) {
  362. throw BusinessRuntimeException.getInstance("id为{0}的优惠券不存在", data.getCouponId());
  363. }
  364. String exCode = data.getExCode();
  365. if (StrUtil.isEmpty(exCode)) {
  366. throw BusinessRuntimeException.getInstance("优惠码为空");
  367. }
  368. List<CouponDistributePopularize> popularizeList = couponDistributePopularizeMapper.selectList(Wrappers.lambdaQuery(CouponDistributePopularize.class)
  369. .eq(CouponDistributePopularize::getDeleted, 1)
  370. .eq(CouponDistributePopularize::getExCode, exCode));
  371. //个人渠道
  372. for (CouponDistributePopularize couponDistributePopularize : popularizeList) {
  373. Long disId = couponDistributePopularize.getDistributeId();
  374. if (disId != null && !ObjectUtil.equal(disId, distributeId)) {
  375. throw BusinessRuntimeException.getInstance("优惠码:{0}已存在", exCode);
  376. }
  377. Long uid = couponDistributePopularize.getUserId();
  378. if (uid != null && !ObjectUtil.equal(uid, userId)) {
  379. throw BusinessRuntimeException.getInstance("优惠码:{0}已存在", exCode);
  380. }
  381. }
  382. Integer count = couponMapper.selectCount(Wrappers.lambdaQuery(Coupon.class).eq(Coupon::getExCode, exCode).eq(Coupon::getDeleted, true));
  383. if (count > 0) {
  384. throw BusinessRuntimeException.getInstance("优惠码:{0}已存在", exCode);
  385. }
  386. if (data.getId() != null) {
  387. couponDistributePopularizeMapper.updateById(data);
  388. } else {
  389. couponDistributePopularizeMapper.insert(data);
  390. }
  391. map.put(data.getId(), data);
  392. });
  393. List<CouponDistributePopularize> dbCouponPopularizes = couponDistributePopularizeMapper.selectList(Wrappers.lambdaQuery(CouponDistributePopularize.class)
  394. .eq(CouponDistributePopularize::getDistributeId, distributeId)
  395. .eq(CouponDistributePopularize::getDeleted, 1));
  396. //不存在的删除
  397. List<Long> delDbCouponPids = new ArrayList<>();
  398. dbCouponPopularizes.forEach(db -> {
  399. if (map.get(db.getId()) == null) {
  400. delDbCouponPids.add(db.getId());
  401. }
  402. });
  403. if (CollUtil.isNotEmpty(delDbCouponPids)) {
  404. couponDistributePopularizeMapper.update(null, Wrappers.lambdaUpdate(CouponDistributePopularize.class)
  405. .set(CouponDistributePopularize::getDeleted, 0)
  406. .in(CouponDistributePopularize::getId, delDbCouponPids)
  407. .eq(CouponDistributePopularize::getDeleted, 1));
  408. }
  409. }
  410. /**
  411. * 渠道目标额 设置
  412. */
  413. private void handlerDistributeTargetAlert(Long distributeId, List<UserDistributeTargetAlert> targets) {
  414. Map<Long, UserDistributeTargetAlert> map = new ConcurrentHashMap<>();
  415. for (UserDistributeTargetAlert targetAlert : targets) {
  416. targetAlert.setDistributeId(distributeId);
  417. Integer type = targetAlert.getType();
  418. BigDecimal target = targetAlert.getTarget();
  419. Assert.notNull(type, "请选择对应目标类型");
  420. Assert.notNull(target, "请输入对应目标额");
  421. if (type < 1 || type > 3) {
  422. throw BusinessRuntimeException.getInstance("请选择正确的目标类型");
  423. }
  424. if (target.compareTo(BigDecimal.ZERO) <= 0) {
  425. throw BusinessRuntimeException.getInstance("请输入正确的目标额值");
  426. }
  427. if ((type == 2 || type == 3) && target.compareTo(BigDecimal.ZERO) < 0) {
  428. throw BusinessRuntimeException.getInstance("请输入正确的变化比例");
  429. }
  430. Long id = targetAlert.getId();
  431. try {
  432. if (id != null) {
  433. UserDistributeTargetAlert dbAlert = targetAlertMapper.selectById(id);
  434. if (dbAlert != null) {
  435. //目标额 变化 去掉下次提醒时间
  436. if (dbAlert.getTarget().compareTo(targetAlert.getTarget()) != 0) {
  437. targetAlert.setNextAlertTime(null);
  438. }
  439. //关闭一周内提醒 去掉下次提醒时间
  440. if (!targetAlert.getIsWeek()) {
  441. targetAlert.setNextAlertTime(null);
  442. }
  443. targetAlertMapper.updateById(targetAlert);
  444. }
  445. } else {
  446. targetAlertMapper.insert(targetAlert);
  447. }
  448. map.put(targetAlert.getId(), targetAlert);
  449. } catch (DuplicateKeyException e) {
  450. }
  451. }
  452. List<UserDistributeTargetAlert> dbTargetAlert = targetAlertMapper.selectList(Wrappers.lambdaQuery(UserDistributeTargetAlert.class)
  453. .eq(UserDistributeTargetAlert::getDistributeId, distributeId));
  454. //不存在的删除
  455. List<Long> delDbTargetAlert = new ArrayList<>();
  456. dbTargetAlert.forEach(db -> {
  457. if (map.get(db.getId()) == null) {
  458. delDbTargetAlert.add(db.getId());
  459. }
  460. });
  461. if (CollUtil.isNotEmpty(delDbTargetAlert)) {
  462. targetAlertMapper.deleteBatchIds(delDbTargetAlert);
  463. }
  464. }
  465. private void setDistributePopularizeCode(Long distributeId, Long userId) {
  466. CouponDistributePopularize couponDistributePopularize = couponDistributePopularizeMapper.selectOne(Wrappers.lambdaQuery(CouponDistributePopularize.class)
  467. .eq(CouponDistributePopularize::getCouponId, Constant.GENERAL_POPULARIZE_COUPON_ID)
  468. .eq(CouponDistributePopularize::getUserId, userId)
  469. .eq(CouponDistributePopularize::getDeleted, 1)
  470. .eq(CouponDistributePopularize::getIsGeneral, 1)
  471. .last("limit 1"));
  472. if (couponDistributePopularize != null) {
  473. //先删除
  474. couponDistributePopularize.setDeleted(false);
  475. couponDistributePopularizeMapper.updateById(couponDistributePopularize);
  476. //后升级
  477. CouponDistributePopularize newPopularizeCode = new CouponDistributePopularize();
  478. newPopularizeCode.setDistributeId(distributeId);
  479. newPopularizeCode.setExCode(couponDistributePopularize.getExCode());
  480. newPopularizeCode.setCouponId(Constant.DISTRIBUTE_POPULARIZE_COUPON_ID);
  481. couponDistributePopularizeMapper.insert(newPopularizeCode);
  482. }
  483. }
  484. }