DistributeServiceImpl.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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.constant.TaskTypeEnum;
  10. import com.cyksj.common.exception.BusinessRuntimeException;
  11. import com.cyksj.common.util.Jsons;
  12. import com.cyksj.mapper.GoodsDonMapper;
  13. import com.cyksj.mapper.UserDistributeSharedRelateUnbindMapper;
  14. import com.cyksj.mapper.UserMapper;
  15. import com.cyksj.mapper.manage.coupon.CouponDistributePopularizeMapper;
  16. import com.cyksj.mapper.manage.coupon.CouponMapper;
  17. import com.cyksj.mapper.manage.distribute.*;
  18. import com.cyksj.mapper.manage.distribute.equipment.UserDistributeEquipmentMapper;
  19. import com.cyksj.mapper.manage.distribute.sub.UserDistributeSubMapper;
  20. import com.cyksj.mapper.market.task.TaskTypeMapper;
  21. import com.cyksj.mapper.shop.YhShopMapper;
  22. import com.cyksj.mapper.shop.YhShopUserPlatDistributeRateMapper;
  23. import com.cyksj.model.entity.*;
  24. import com.cyksj.model.request.UserDistributeReq;
  25. import com.cyksj.redis.RedisService;
  26. import com.cyksj.service.claude.ClaudeCodeService;
  27. import com.cyksj.service.distribute.DistributeService;
  28. import com.cyksj.service.market.task.TaskService;
  29. import com.cyksj.service.relation.GroupRelationFrontService;
  30. import lombok.RequiredArgsConstructor;
  31. import lombok.extern.slf4j.Slf4j;
  32. import org.springframework.dao.DuplicateKeyException;
  33. import org.springframework.stereotype.Service;
  34. import org.springframework.transaction.annotation.Transactional;
  35. import java.math.BigDecimal;
  36. import java.util.ArrayList;
  37. import java.util.List;
  38. import java.util.Map;
  39. import java.util.Optional;
  40. import java.util.concurrent.ConcurrentHashMap;
  41. import java.util.stream.Collectors;
  42. /*
  43. *项目名: netflix
  44. *文件名: DistributeServiceImpl
  45. *创建者: JavaZou
  46. *创建时间:2022/11/14 12:21
  47. */
  48. @Service
  49. @RequiredArgsConstructor
  50. @Slf4j
  51. public class DistributeServiceImpl implements DistributeService {
  52. private final UserDistributeMapper userDistributeMapper;
  53. private final UserPlatDistributeRateMapper userPlatDistributeRateMapper;
  54. private final GoodsDonMapper goodsDonMapper;
  55. private final TaskService taskService;
  56. private final TaskTypeMapper taskTypeMapper;
  57. private final UserDistributeSharedMapper userDistributeSharedMapper;
  58. private final UserBusinessDefaultRateConfigMapper defaultRateConfigMapper;
  59. private final RedisService redisService;
  60. private final CouponMapper couponMapper;
  61. private final CouponDistributePopularizeMapper couponDistributePopularizeMapper;
  62. private final YhShopUserPlatDistributeRateMapper yhShopUserPlatDistributeRateMapper;
  63. private final YhShopMapper yhShopMapper;
  64. private final UserPlatSkuDistributeRateMapper userPlatSkuDistributeRateMapper;
  65. private final UserDistributeTargetAlertMapper targetAlertMapper;
  66. private final UserDistributeSharedRelateUnbindMapper userDistributeSharedRelateUnbindMapper;
  67. private final UserDistributeEquipmentMapper userDistributeEquipmentMapper;
  68. private final UserDistributeSubMapper userDistributeSubMapper;
  69. private final DistributeGeneralSkuRateMapper generalSkuRateMapper;
  70. private final ClaudeCodeService claudeCodeService;
  71. private final UserMapper userMapper;
  72. private final GroupRelationFrontService groupRelationFrontService;
  73. @Override
  74. @Transactional(rollbackFor = Throwable.class)
  75. public void insertDistribute(UserDistributeReq userDistributeReq) {
  76. if (!redisService.setNx(RedisService.key.USER_DISTRIBUTE_ADD_KEY.getNameFormat(userDistributeReq.getUserId()), userDistributeReq.getUserId(), 2 * 5l)) {
  77. return;
  78. }
  79. UserDistribute exists = userDistributeMapper.selectOne(Wrappers.lambdaQuery(UserDistribute.class).eq(UserDistribute::getUserId, userDistributeReq.getUserId()).eq(UserDistribute::getDeleted, true).last("limit 1"));
  80. if (exists != null) {
  81. throw BusinessRuntimeException.getInstance("该推广者已添加");
  82. }
  83. Long businessId = userDistributeReq.getBusinessId();
  84. UserDistribute userDistribute = new UserDistribute();
  85. userDistribute.setUserId(userDistributeReq.getUserId());
  86. userDistribute.setRemark(userDistributeReq.getRemark());
  87. userDistribute.setType(userDistributeReq.getType());
  88. userDistribute.setBusinessId(businessId);
  89. if (businessId != null && businessId != 0) {
  90. userDistribute.setBusinessBindTime(DateTime.now());
  91. }
  92. userDistributeMapper.insert(userDistribute);
  93. for (UserPlatDistributeRate rate : userDistributeReq.getRates()) {
  94. if (rate.getRate() == null) {
  95. rate.setRate(0);
  96. }
  97. if (rate.getSecondRate() == null) {
  98. rate.setSecondRate(0);
  99. }
  100. if (rate.getRate() < 0 || rate.getRate() > 100) {
  101. throw new BusinessRuntimeException("比例错误");
  102. }
  103. GoodsDon goodsDon = goodsDonMapper.selectById(rate.getGoodsId());
  104. if (goodsDon == null || !goodsDon.getDeleted() || goodsDon.getType() == 3) {
  105. throw new BusinessRuntimeException("平台错误");
  106. }
  107. //分销者虚物分销比例且配置商务
  108. if (businessId != null && goodsDon.getType() == 1) {
  109. checkDistributorAndBusinessRate(rate.getRate(), goodsDon.getId(), goodsDon.getTitle());
  110. }
  111. rate.setDistributeId(userDistribute.getId());
  112. userPlatDistributeRateMapper.insert(rate);
  113. }
  114. List<UserPlatSkuDistributeRate> skuRates = userDistributeReq.getSkuRates();
  115. if (CollUtil.isNotEmpty(skuRates)) {
  116. Map<Long, GoodsDon> map = new ConcurrentHashMap<>();
  117. for (UserPlatSkuDistributeRate skuRate : skuRates) {
  118. if (skuRate.getRate() == null) {
  119. skuRate.setRate(0);
  120. }
  121. if (skuRate.getSecondRate() == null) {
  122. skuRate.setSecondRate(0);
  123. }
  124. if (skuRate.getRate() < 0 || skuRate.getRate() > 100) {
  125. throw new BusinessRuntimeException("比例错误");
  126. }
  127. GoodsDon goodsDon = map.get(skuRate.getGoodsId());
  128. if (goodsDon == null) {
  129. goodsDon = goodsDonMapper.selectById(skuRate.getGoodsId());
  130. map.putIfAbsent(skuRate.getSkuId(), goodsDon);
  131. }
  132. try {
  133. skuRate.setDistributeId(userDistribute.getId());
  134. if (businessId != null && goodsDon.getType() == 1) {
  135. checkDistributorAndBusinessRate(skuRate.getRate(), skuRate.getGoodsId(), goodsDon.getTitle());
  136. }
  137. userPlatSkuDistributeRateMapper.insert(skuRate);
  138. } catch (DuplicateKeyException e) {
  139. }
  140. }
  141. }
  142. //渠道附加功能
  143. handlerDistributeOtherFunc(userDistribute, userDistributeReq);
  144. //设置过个人专属推广码 升级为渠道专属优惠码
  145. setDistributePopularizeCode(userDistribute.getId(), userDistribute.getUserId());
  146. }
  147. @Override
  148. @Transactional(rollbackFor = Throwable.class)
  149. public void editDistribute(UserDistributeReq userDistributeReq) {
  150. if (userDistributeReq.getId() == null || userDistributeReq.getId() < 1) {
  151. throw BusinessRuntimeException.getInstance("id错误");
  152. }
  153. Long id = userDistributeReq.getId();
  154. UserDistribute userDistribute = userDistributeMapper.selectById(id);
  155. if (userDistribute == null || !userDistribute.getDeleted()) {
  156. return;
  157. }
  158. userDistribute.setId(userDistributeReq.getId());
  159. userDistribute.setRemark(userDistributeReq.getRemark());
  160. Long businessId = userDistributeReq.getBusinessId();
  161. if (businessId == null) {
  162. userDistribute.setBusinessId(0l);
  163. } else {
  164. if (userDistribute.getBusinessId() != null && !userDistribute.getBusinessId().equals(businessId)) {
  165. userDistribute.setBusinessBindTime(DateTime.now());
  166. }
  167. userDistribute.setBusinessId(businessId);
  168. }
  169. userDistribute.setType(userDistributeReq.getType());
  170. userDistributeMapper.updateById(userDistribute);
  171. YhShop yhShop = yhShopMapper.selectOne(Wrappers.lambdaQuery(YhShop.class)
  172. .eq(YhShop::getUserId, userDistribute.getUserId())
  173. .eq(YhShop::getVerifyStatus, YhShop.Status.success).last("limit 1"));
  174. for (UserPlatDistributeRate rate : userDistributeReq.getRates()) {
  175. if (rate.getRate() == null) {
  176. rate.setRate(0);
  177. }
  178. if (rate.getSecondRate() == null) {
  179. rate.setSecondRate(0);
  180. }
  181. if (rate.getRate() < 0 || rate.getRate() > 100) {
  182. throw new BusinessRuntimeException("比例错误");
  183. }
  184. GoodsDon goodsDon = goodsDonMapper.selectById(rate.getGoodsId());
  185. if (rate.getId() != null && (goodsDon == null || !goodsDon.getDeleted())) {
  186. userPlatDistributeRateMapper.deleteById(rate.getId());
  187. continue;
  188. }
  189. if (goodsDon == null || !goodsDon.getDeleted() || goodsDon.getType() == 3) {
  190. throw new BusinessRuntimeException("平台错误");
  191. }
  192. //分销者虚物分销比例且配置商务
  193. if (businessId != null && goodsDon.getType() == 1) {
  194. checkDistributorAndBusinessRate(rate.getRate(), goodsDon.getId(), goodsDon.getTitle());
  195. }
  196. rate.setDistributeId(userDistribute.getId());
  197. if (rate.getId() == null) {
  198. try {
  199. userPlatDistributeRateMapper.insert(rate);
  200. } catch (DuplicateKeyException e) {
  201. userPlatDistributeRateMapper.update(null, Wrappers.lambdaUpdate(UserPlatDistributeRate.class)
  202. .eq(UserPlatDistributeRate::getDistributeId, userDistribute.getId())
  203. .eq(UserPlatDistributeRate::getGoodsId, rate.getGoodsId())
  204. .set(UserPlatDistributeRate::getRate, rate.getRate()));
  205. }
  206. if (yhShop != null) {
  207. try {
  208. YhShopUserPlatDistributeRate yhShopUserPlatDistributeRate = new YhShopUserPlatDistributeRate();
  209. yhShopUserPlatDistributeRate.setRate(rate.getRate());
  210. yhShopUserPlatDistributeRate.setGoodsId(rate.getGoodsId());
  211. yhShopUserPlatDistributeRate.setUserId(userDistribute.getUserId());
  212. yhShopUserPlatDistributeRateMapper.insert(yhShopUserPlatDistributeRate);
  213. } catch (DuplicateKeyException e) {
  214. //同步店铺分销比例
  215. yhShopUserPlatDistributeRateMapper.update(null, Wrappers.lambdaUpdate(YhShopUserPlatDistributeRate.class)
  216. .eq(YhShopUserPlatDistributeRate::getUserId, userDistribute.getUserId())
  217. .eq(YhShopUserPlatDistributeRate::getGoodsId, rate.getGoodsId())
  218. .set(YhShopUserPlatDistributeRate::getRate, rate.getRate()));
  219. }
  220. }
  221. } else {
  222. UserPlatDistributeRate dbPlateRate = userPlatDistributeRateMapper.selectById(rate.getId());
  223. if (dbPlateRate == null) {
  224. continue;
  225. }
  226. if (dbPlateRate.getRate() != rate.getRate() || dbPlateRate.getSecondRate() != rate.getSecondRate()) {
  227. userPlatDistributeRateMapper.updateById(rate);
  228. if (yhShop != null && dbPlateRate.getRate() != rate.getRate()) {
  229. //同步店铺分销比例
  230. yhShopUserPlatDistributeRateMapper.update(null, Wrappers.lambdaUpdate(YhShopUserPlatDistributeRate.class)
  231. .eq(YhShopUserPlatDistributeRate::getUserId, userDistribute.getUserId())
  232. .eq(YhShopUserPlatDistributeRate::getGoodsId, rate.getGoodsId())
  233. .set(YhShopUserPlatDistributeRate::getRate, rate.getRate()));
  234. }
  235. }
  236. }
  237. }
  238. //特殊平台 按规格分销比例
  239. List<UserPlatSkuDistributeRate> skuRates = userDistributeReq.getSkuRates();
  240. if (CollUtil.isNotEmpty(skuRates)) {
  241. List<Long> skuIds = skuRates.stream().map(UserPlatSkuDistributeRate::getSkuId).collect(Collectors.toList());
  242. userPlatSkuDistributeRateMapper.delete(Wrappers.lambdaQuery(UserPlatSkuDistributeRate.class)
  243. .notIn(UserPlatSkuDistributeRate::getSkuId, skuIds)
  244. .eq(UserPlatSkuDistributeRate::getDistributeId, userDistribute.getId()));
  245. Map<Long, GoodsDon> map = new ConcurrentHashMap<>();
  246. for (UserPlatSkuDistributeRate skuRate : skuRates) {
  247. if (skuRate.getRate() == null) {
  248. skuRate.setRate(0);
  249. }
  250. if (skuRate.getSecondRate() == null) {
  251. skuRate.setSecondRate(0);
  252. }
  253. if (skuRate.getRate() < 0 || skuRate.getRate() > 100) {
  254. throw new BusinessRuntimeException("比例错误");
  255. }
  256. GoodsDon goodsDon = map.get(skuRate.getGoodsId());
  257. if (goodsDon == null) {
  258. goodsDon = goodsDonMapper.selectById(skuRate.getGoodsId());
  259. map.putIfAbsent(skuRate.getSkuId(), goodsDon);
  260. }
  261. if (businessId != null && goodsDon.getType() == 1) {
  262. checkDistributorAndBusinessRate(skuRate.getRate(), goodsDon.getId(), goodsDon.getTitle());
  263. }
  264. if (skuRate.getId() != null) {
  265. userPlatSkuDistributeRateMapper.updateById(skuRate);
  266. continue;
  267. }
  268. try {
  269. skuRate.setDistributeId(userDistribute.getId());
  270. userPlatSkuDistributeRateMapper.insert(skuRate);
  271. } catch (DuplicateKeyException e) {
  272. }
  273. }
  274. } else {
  275. userPlatSkuDistributeRateMapper.delete(Wrappers.lambdaQuery(UserPlatSkuDistributeRate.class)
  276. .eq(UserPlatSkuDistributeRate::getDistributeId, userDistribute.getId()));
  277. }
  278. handlerDistributeOtherFunc(userDistribute, userDistributeReq);
  279. }
  280. /**
  281. * 渠道附加功能
  282. */
  283. public void handlerDistributeOtherFunc(UserDistribute userDistribute, UserDistributeReq userDistributeReq) {
  284. //处理优惠券关联
  285. handlerDistributeCouponRelate(userDistribute.getId(), userDistribute.getUserId(), userDistributeReq.getCouponRelates());
  286. //渠道目标额配置
  287. handlerDistributeTargetAlert(userDistribute.getId(), userDistributeReq.getTargets());
  288. //设备分销
  289. handlerSpecialEquipmentRate(userDistributeReq.getEpRate(), userDistribute);
  290. //下级渠道绑定
  291. handlerDistributeSub(userDistributeReq.getUserDistributeSub(), userDistribute);
  292. }
  293. @Override
  294. public void saveDistributionInvitation(Long sharedId, Long userId, Long dsPopularizeId, Boolean ccGeneralTg) {
  295. if (sharedId == 0l || sharedId.equals(userId)) {
  296. return;
  297. }
  298. User user = userMapper.selectById(sharedId);
  299. if (user == null) {
  300. return;
  301. }
  302. UserDistributeShared userDistributeShared = new UserDistributeShared();
  303. userDistributeShared.setSharedId(sharedId);
  304. //存在分销
  305. userDistributeShared.setUserId(userId);
  306. userDistributeShared.setDsPopularizeId(dsPopularizeId);
  307. if (ccGeneralTg != null) {
  308. //普通推广
  309. Integer selectCount = userDistributeMapper.selectCount(Wrappers.lambdaQuery(UserDistribute.class).eq(UserDistribute::getUserId, sharedId).eq(UserDistribute::getDeleted, Boolean.TRUE));
  310. if (selectCount == 0) {
  311. //打上普通cc推广标识
  312. userDistributeShared.setRemark(Constant.CC_DISTRIBUTE_MARK);
  313. }
  314. }
  315. try {
  316. userDistributeSharedMapper.insert(userDistributeShared);
  317. if (Constant.CC_DISTRIBUTE_MARK.equals(userDistributeShared.getRemark())) {
  318. //若是cc普通推广 发送claude code积分卡
  319. claudeCodeService.sendCreditCard(null, userDistributeShared.getSharedId(), 1000, Constant.CLAUDE_CODE_CARD_NUMBER + System.currentTimeMillis(), "新用户注册");
  320. //存在Claude code车票不发送体验卡
  321. if (!claudeCodeService.checkClaudeCodeUser(userId)) {
  322. //发送claude code 体验卡
  323. groupRelationFrontService.getTrialTicketByUserIdAndGoodsId(userId, Constant.DISTRIBUTE_CLAUDE_CODE_TRIAL_SKU_ID, 1);
  324. }
  325. }
  326. TaskType taskType = taskTypeMapper.selectOne(Wrappers.lambdaQuery(TaskType.class)
  327. .eq(TaskType::getName, TaskTypeEnum.share.getDesc()).last("limit 1"));
  328. taskService.completeTask(taskType, sharedId);
  329. } catch (DuplicateKeyException e) {
  330. log.info("分销被邀请人重复插入");
  331. }
  332. }
  333. @Override
  334. @Transactional(rollbackFor = Throwable.class)
  335. public void bindPopularizeCode(Long userId, String code) {
  336. Coupon coupon = couponMapper.getCouponById(Constant.GENERAL_POPULARIZE_COUPON_ID);
  337. if (!redisService.setNx(RedisService.key.GENERAL_POPULARIZE_CODE.getName() + code, userId, RedisService.key.GENERAL_POPULARIZE_CODE.getTimeout())) {
  338. throw BusinessRuntimeException.getInstance("该专属推广优惠码已被设置");
  339. }
  340. UserDistribute userDistribute = userDistributeMapper.selectOne(Wrappers.lambdaQuery(UserDistribute.class)
  341. .eq(UserDistribute::getUserId, userId)
  342. .eq(UserDistribute::getDeleted, true)
  343. .last("limit 1"));
  344. if (userDistribute != null) throw BusinessRuntimeException.getInstance("只适用于普通用户进行专属推广");
  345. CouponDistributePopularize is_exists = couponDistributePopularizeMapper.selectOne(Wrappers.lambdaQuery(CouponDistributePopularize.class)
  346. .eq(CouponDistributePopularize::getUserId, userId)
  347. .eq(CouponDistributePopularize::getDeleted, true)
  348. .last("limit 1"));
  349. if (is_exists != null) throw BusinessRuntimeException.getInstance("你已设置专属推广优惠码");
  350. CouponDistributePopularize couponDistributePopularize = new CouponDistributePopularize();
  351. couponDistributePopularize.setCouponId(coupon.getId());
  352. couponDistributePopularize.setUserId(userId);
  353. couponDistributePopularize.setIsGeneral(true);
  354. CouponDistributePopularize exists = couponDistributePopularizeMapper.selectOne(Wrappers.lambdaQuery(CouponDistributePopularize.class)
  355. .eq(CouponDistributePopularize::getExCode, code)
  356. .eq(CouponDistributePopularize::getDeleted, true)
  357. .select(CouponDistributePopularize::getId)
  358. .last("limit 1"));
  359. if (exists != null) {
  360. throw BusinessRuntimeException.getInstance("该专属推广优惠码已被设置");
  361. } else {
  362. Coupon existsCoupon = couponMapper.selectOne(Wrappers.lambdaQuery(Coupon.class)
  363. .eq(Coupon::getExCode, code)
  364. .eq(Coupon::getDeleted, true)
  365. .select(Coupon::getId)
  366. .last("limit 1"));
  367. if (existsCoupon != null) {
  368. throw BusinessRuntimeException.getInstance("该专属推广优惠码已被设置");
  369. }
  370. }
  371. couponDistributePopularize.setExCode(code);
  372. couponDistributePopularizeMapper.insert(couponDistributePopularize);
  373. }
  374. @Override
  375. public void unbindUserDistributeShared(List<Object> userBind) throws Exception {
  376. List<Long> save_userIds = new ArrayList<>(500);
  377. userBind.forEach(e -> {
  378. Map<String, Object> map = Jsons.toMap(e);
  379. Object value0 = map.get("0");
  380. if (value0 != null) {
  381. Long userId = Long.parseLong(value0.toString());
  382. save_userIds.add(userId);
  383. if (save_userIds.size() == 500) {
  384. userDistributeSharedRelateUnbindMapper.batchInsert(save_userIds);
  385. save_userIds.clear();
  386. }
  387. }
  388. });
  389. if (save_userIds.size() > 0) {
  390. userDistributeSharedRelateUnbindMapper.batchInsert(save_userIds);
  391. save_userIds.clear();
  392. }
  393. }
  394. @Override
  395. public void setDistributeCommonSkuRate(List<DistributeGeneralSkuRate> skuRates) {
  396. List<Long> collect = skuRates.stream().map(DistributeGeneralSkuRate::getSkuId).collect(Collectors.toList());
  397. if (CollUtil.isNotEmpty(collect)) {
  398. generalSkuRateMapper.delete(Wrappers.lambdaQuery(DistributeGeneralSkuRate.class)
  399. .notIn(DistributeGeneralSkuRate::getSkuId, collect));
  400. }
  401. skuRates.forEach(e -> {
  402. Integer rate = e.getRate();
  403. if (rate == null || rate < 0 || rate > 100) {
  404. throw BusinessRuntimeException.getInstance("请输入正确的比例");
  405. }
  406. if (e.getId() == null) {
  407. try {
  408. generalSkuRateMapper.insert(e);
  409. } catch (DuplicateKeyException ex) {
  410. }
  411. } else {
  412. generalSkuRateMapper.updateById(e);
  413. }
  414. });
  415. }
  416. public void checkDistributorAndBusinessRate(Integer distributeGoodsRate, Long goodsId, String goodsName) {
  417. UserBusinessDefaultRateConfig defaultGoodsRateConfig = defaultRateConfigMapper.selectOne(Wrappers.lambdaQuery(UserBusinessDefaultRateConfig.class)
  418. .eq(UserBusinessDefaultRateConfig::getGoodsId, goodsId)
  419. .last("limit 1"));
  420. Optional.ofNullable(defaultGoodsRateConfig).ifPresent(config -> {
  421. if (distributeGoodsRate > config.getMaxRate()) {
  422. throw BusinessRuntimeException.getInstance(String.format("该%s平台最高可配置分销比例为%s", goodsName, config.getMaxRate()));
  423. }
  424. });
  425. }
  426. private void handlerDistributeCouponRelate(Long distributeId, Long userId, List<CouponDistributePopularize> couponRelates) {
  427. Map<Long, CouponDistributePopularize> map = new ConcurrentHashMap<>();
  428. couponRelates.forEach(data -> {
  429. data.setDistributeId(distributeId);
  430. if (data.getCouponId() == null) {
  431. throw BusinessRuntimeException.getInstance("优惠券不存在");
  432. }
  433. Integer exists = couponMapper.selectCount(Wrappers.lambdaQuery(Coupon.class).eq(Coupon::getId, data.getCouponId()).eq(Coupon::getDeleted, 1));
  434. if (exists == 0) {
  435. throw BusinessRuntimeException.getInstance("id为{0}的优惠券不存在", data.getCouponId());
  436. }
  437. String exCode = data.getExCode();
  438. if (StrUtil.isEmpty(exCode)) {
  439. throw BusinessRuntimeException.getInstance("优惠码为空");
  440. }
  441. List<CouponDistributePopularize> popularizeList = couponDistributePopularizeMapper.selectList(Wrappers.lambdaQuery(CouponDistributePopularize.class)
  442. .eq(CouponDistributePopularize::getDeleted, 1)
  443. .eq(CouponDistributePopularize::getExCode, exCode));
  444. //个人渠道
  445. for (CouponDistributePopularize couponDistributePopularize : popularizeList) {
  446. Long disId = couponDistributePopularize.getDistributeId();
  447. if (disId != null && !ObjectUtil.equal(disId, distributeId)) {
  448. throw BusinessRuntimeException.getInstance("优惠码:{0}已存在", exCode);
  449. }
  450. Long uid = couponDistributePopularize.getUserId();
  451. if (uid != null && !ObjectUtil.equal(uid, userId)) {
  452. throw BusinessRuntimeException.getInstance("优惠码:{0}已存在", exCode);
  453. }
  454. }
  455. Integer count = couponMapper.selectCount(Wrappers.lambdaQuery(Coupon.class).eq(Coupon::getExCode, exCode).eq(Coupon::getDeleted, true));
  456. if (count > 0) {
  457. throw BusinessRuntimeException.getInstance("优惠码:{0}已存在", exCode);
  458. }
  459. if (data.getId() != null) {
  460. couponDistributePopularizeMapper.updateById(data);
  461. } else {
  462. couponDistributePopularizeMapper.insert(data);
  463. }
  464. map.put(data.getId(), data);
  465. });
  466. List<CouponDistributePopularize> dbCouponPopularizes = couponDistributePopularizeMapper.selectList(Wrappers.lambdaQuery(CouponDistributePopularize.class)
  467. .eq(CouponDistributePopularize::getDistributeId, distributeId)
  468. .eq(CouponDistributePopularize::getDeleted, 1));
  469. //不存在的删除
  470. List<Long> delDbCouponPids = new ArrayList<>();
  471. dbCouponPopularizes.forEach(db -> {
  472. if (map.get(db.getId()) == null) {
  473. delDbCouponPids.add(db.getId());
  474. }
  475. });
  476. if (CollUtil.isNotEmpty(delDbCouponPids)) {
  477. couponDistributePopularizeMapper.update(null, Wrappers.lambdaUpdate(CouponDistributePopularize.class)
  478. .set(CouponDistributePopularize::getDeleted, 0)
  479. .in(CouponDistributePopularize::getId, delDbCouponPids)
  480. .eq(CouponDistributePopularize::getDeleted, 1));
  481. }
  482. }
  483. /**
  484. * 渠道目标额 设置
  485. */
  486. private void handlerDistributeTargetAlert(Long distributeId, List<UserDistributeTargetAlert> targets) {
  487. Map<Long, UserDistributeTargetAlert> map = new ConcurrentHashMap<>();
  488. for (UserDistributeTargetAlert targetAlert : targets) {
  489. targetAlert.setDistributeId(distributeId);
  490. Integer type = targetAlert.getType();
  491. BigDecimal target = targetAlert.getTarget();
  492. Assert.notNull(type, "请选择对应目标类型");
  493. Assert.notNull(target, "请输入对应目标额");
  494. if (type < 1 || type > 3) {
  495. throw BusinessRuntimeException.getInstance("请选择正确的目标类型");
  496. }
  497. if (target.compareTo(BigDecimal.ZERO) <= 0) {
  498. throw BusinessRuntimeException.getInstance("请输入正确的目标额值");
  499. }
  500. if ((type == 2 || type == 3) && target.compareTo(BigDecimal.ZERO) < 0) {
  501. throw BusinessRuntimeException.getInstance("请输入正确的变化比例");
  502. }
  503. Long id = targetAlert.getId();
  504. try {
  505. if (id != null) {
  506. UserDistributeTargetAlert dbAlert = targetAlertMapper.selectById(id);
  507. if (dbAlert != null) {
  508. //目标额 变化 去掉下次提醒时间
  509. if (dbAlert.getTarget().compareTo(targetAlert.getTarget()) != 0) {
  510. targetAlert.setNextAlertTime(null);
  511. }
  512. //关闭一周内提醒 去掉下次提醒时间
  513. if (!targetAlert.getIsWeek()) {
  514. targetAlert.setNextAlertTime(null);
  515. }
  516. targetAlertMapper.updateById(targetAlert);
  517. }
  518. } else {
  519. targetAlertMapper.insert(targetAlert);
  520. }
  521. map.put(targetAlert.getId(), targetAlert);
  522. } catch (DuplicateKeyException e) {
  523. }
  524. }
  525. List<UserDistributeTargetAlert> dbTargetAlert = targetAlertMapper.selectList(Wrappers.lambdaQuery(UserDistributeTargetAlert.class)
  526. .eq(UserDistributeTargetAlert::getDistributeId, distributeId));
  527. //不存在的删除
  528. List<Long> delDbTargetAlert = new ArrayList<>();
  529. dbTargetAlert.forEach(db -> {
  530. if (map.get(db.getId()) == null) {
  531. delDbTargetAlert.add(db.getId());
  532. }
  533. });
  534. if (CollUtil.isNotEmpty(delDbTargetAlert)) {
  535. targetAlertMapper.deleteBatchIds(delDbTargetAlert);
  536. }
  537. }
  538. /**
  539. * 设备分销
  540. */
  541. private void handlerSpecialEquipmentRate(UserDistributeEquipment epRate, UserDistribute userDistribute) {
  542. BigDecimal zero = BigDecimal.ZERO;
  543. Long userId = userDistribute.getUserId();
  544. Long distributeId = userDistribute.getId();
  545. if (epRate != null) {
  546. epRate.setUserId(userId);
  547. epRate.setDistributeId(distributeId);
  548. BigDecimal ep = epRate.getEp();
  549. BigDecimal apTv = epRate.getApTv();
  550. BigDecimal combo = epRate.getCombo();
  551. if (ep == null || apTv == null || combo == null) {
  552. throw BusinessRuntimeException.getInstance("请填写设备分销必填项..");
  553. }
  554. if (ep.compareTo(zero) <= 0 || apTv.compareTo(zero) <= 0 || combo.compareTo(zero) <= 0 || combo.compareTo(BigDecimal.valueOf(100)) >= 0) {
  555. throw BusinessRuntimeException.getInstance("请输入正确的设备分销设置");
  556. }
  557. if (epRate.getId() == null) {
  558. try {
  559. userDistributeEquipmentMapper.insert(epRate);
  560. } catch (DuplicateKeyException e) {
  561. }
  562. } else {
  563. userDistributeEquipmentMapper.updateById(epRate);
  564. }
  565. return;
  566. }
  567. if (userDistributeEquipmentMapper.selectCount(Wrappers.lambdaQuery(UserDistributeEquipment.class)
  568. .eq(UserDistributeEquipment::getDistributeId, distributeId)) > 0) {
  569. userDistributeEquipmentMapper.delete(Wrappers.lambdaQuery(UserDistributeEquipment.class)
  570. .eq(UserDistributeEquipment::getDistributeId, distributeId));
  571. }
  572. }
  573. /**
  574. * 下级渠道
  575. */
  576. private void handlerDistributeSub(UserDistributeSub userDistributeSub, UserDistribute userDistribute) {
  577. if (userDistributeSub != null) {
  578. userDistributeSub.setSubUserId(userDistribute.getUserId());
  579. if (userDistributeSub.getUserId() == null) {
  580. throw BusinessRuntimeException.getInstance("请选择对应的上级渠道..");
  581. }
  582. if (userDistributeSub.getUserId().equals(userDistribute.getUserId())) {
  583. throw BusinessRuntimeException.getInstance("不能选择自己作为上级渠道");
  584. }
  585. UserDistribute superUser = userDistributeMapper.selectOne(Wrappers.lambdaQuery(UserDistribute.class)
  586. .eq(UserDistribute::getUserId, userDistributeSub.getUserId())
  587. .eq(UserDistribute::getDeleted, 1)
  588. .last("limit 1"));
  589. if (superUser == null) {
  590. throw BusinessRuntimeException.getInstance("上级渠道不存在");
  591. }
  592. userDistributeSub.setDistributeId(superUser.getId());
  593. userDistributeSub.setSubDistributeId(userDistribute.getId());
  594. if (userDistributeSub.getId() == null) {
  595. try {
  596. userDistributeSubMapper.insert(userDistributeSub);
  597. } catch (DuplicateKeyException e) {
  598. }
  599. } else {
  600. userDistributeSubMapper.updateById(userDistributeSub);
  601. }
  602. }else {
  603. if (userDistributeSubMapper.selectCount(Wrappers.lambdaQuery(UserDistributeSub.class)
  604. .eq(UserDistributeSub::getSubUserId, userDistribute.getUserId()))>0) {
  605. userDistributeSubMapper.delete(Wrappers.lambdaQuery(UserDistributeSub.class)
  606. .eq(UserDistributeSub::getSubUserId, userDistribute.getUserId()));
  607. }
  608. }
  609. }
  610. private void setDistributePopularizeCode(Long distributeId, Long userId) {
  611. CouponDistributePopularize couponDistributePopularize = couponDistributePopularizeMapper.selectOne(Wrappers.lambdaQuery(CouponDistributePopularize.class)
  612. .eq(CouponDistributePopularize::getCouponId, Constant.GENERAL_POPULARIZE_COUPON_ID)
  613. .eq(CouponDistributePopularize::getUserId, userId)
  614. .eq(CouponDistributePopularize::getDeleted, 1)
  615. .eq(CouponDistributePopularize::getIsGeneral, 1)
  616. .last("limit 1"));
  617. if (couponDistributePopularize != null) {
  618. //先删除
  619. couponDistributePopularize.setDeleted(false);
  620. couponDistributePopularizeMapper.updateById(couponDistributePopularize);
  621. //后升级
  622. CouponDistributePopularize newPopularizeCode = new CouponDistributePopularize();
  623. newPopularizeCode.setDistributeId(distributeId);
  624. newPopularizeCode.setExCode(couponDistributePopularize.getExCode());
  625. newPopularizeCode.setCouponId(Constant.DISTRIBUTE_POPULARIZE_COUPON_ID);
  626. couponDistributePopularizeMapper.insert(newPopularizeCode);
  627. }
  628. }
  629. }