DistributeServiceImpl.java 22 KB

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