CpsPopularizeController.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. package com.cyksj.web.controller.cps;
  2. import cn.hutool.core.util.StrUtil;
  3. import cn.hutool.extra.qrcode.QrCodeUtil;
  4. import cn.hutool.extra.qrcode.QrConfig;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.cyksj.common.EnvCommonService;
  7. import com.cyksj.common.annotation.NoSubmit;
  8. import com.cyksj.dto.Result;
  9. import com.cyksj.enums.GatewayResponse;
  10. import com.cyksj.mapper.DistributePopularizeClickRecordMapper;
  11. import com.cyksj.mapper.GoodsDonMapper;
  12. import com.cyksj.mapper.GoodsDonSkuMapper;
  13. import com.cyksj.mapper.UserDistributePosterRecordMapper;
  14. import com.cyksj.mapper.manage.coupon.CouponDistributePopularizeMapper;
  15. import com.cyksj.mapper.manage.distribute.*;
  16. import com.cyksj.model.entity.*;
  17. import com.cyksj.model.views.DistributePosterGoodsCpsView;
  18. import com.cyksj.model.views.UserDistributePopularizeView;
  19. import com.cyksj.model.views.UserDistributePosterOrderDetailView;
  20. import com.cyksj.service.cps.CpsPopularizeService;
  21. import com.cyksj.service.user.UserService;
  22. import com.cyksj.web.util.StpCpsManageUser;
  23. import com.ejlchina.searcher.BeanSearcher;
  24. import com.ejlchina.searcher.SearchResult;
  25. import com.ejlchina.searcher.param.Operator;
  26. import com.ejlchina.searcher.util.MapBuilder;
  27. import com.ejlchina.searcher.util.MapUtils;
  28. import lombok.RequiredArgsConstructor;
  29. import org.springframework.dao.DuplicateKeyException;
  30. import org.springframework.web.bind.annotation.*;
  31. import javax.servlet.http.HttpServletRequest;
  32. import java.math.BigDecimal;
  33. import java.util.List;
  34. import java.util.Optional;
  35. /*
  36. *项目名: netflix
  37. *文件名: CpsPopularizeController
  38. *创建者: JavaZou
  39. *创建时间:2023/5/15 15:03
  40. */
  41. @RestController
  42. @RequestMapping("/cps/manage/popularize")
  43. @RequiredArgsConstructor
  44. public class CpsPopularizeController {
  45. private final CpsPopularizeService cpsPopularizeService;
  46. private final BeanSearcher beanSearcher;
  47. private final HttpServletRequest request;
  48. private final EnvCommonService envCommonService;
  49. private final CouponDistributePopularizeMapper couponDistributePopularizeMapper;
  50. private final UserService userService;
  51. private final GoodsDonMapper goodsDonMapper;
  52. private final DistributePopularizeClickRecordMapper distributePopularizeClickRecordMapper;
  53. private final UserDistributeMapper userDistributeMapper;
  54. private final UserPlatDistributeRateMapper userPlatDistributeRateMapper;
  55. private final GoodsDonSkuMapper skuMapper;
  56. private final UserPlatSkuDistributeRateMapper userPlatSkuDistributeRateMapper;
  57. private final DistributeGeneralSkuRateMapper distributeGeneralSkuRateMapper;
  58. private final UserDistributePosterRecordMapper userDistributePosterRecordMapper;
  59. private final DistributePosterGoodsMapper distributePosterGoodsMapper;
  60. /**
  61. * 渠道推广链接列表
  62. */
  63. @GetMapping("/get")
  64. public Result<SearchResult<UserDistributePopularizeView>> getList(String startTime, String endTime) {
  65. long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
  66. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  67. builder.put("userId", String.format("and user_id = %s", cpsUserId));
  68. String time = StrUtil.EMPTY;
  69. if (startTime != null) {
  70. time += String.format(" and od.created_time >= '%s'", startTime);
  71. }
  72. if (endTime != null) {
  73. time += String.format(" and od.created_time < '%s'", endTime);
  74. }
  75. if (StrUtil.isNotBlank(time)) {
  76. builder.put("time", time);
  77. }
  78. UserDistribute userDistribute = userDistributeMapper.selectOne(Wrappers.lambdaQuery(UserDistribute.class)
  79. .eq(UserDistribute::getUserId, cpsUserId)
  80. .eq(UserDistribute::getDeleted, 1)
  81. .last("limit 1"));
  82. SearchResult<UserDistributePopularizeView> search = beanSearcher.search(UserDistributePopularizeView.class, builder.build());
  83. search.getDataList().forEach(data -> {
  84. data.setPopularizeLink(String.format(envCommonService.DISTRIBUTE_DEFAULT_URL_PREFIX, data.getCode()));
  85. Optional.ofNullable(goodsDonMapper.selectById(data.getGoodsId())).ifPresent(goods -> {
  86. data.setTitle(goods.getTitle());
  87. data.setLogo(goods.getLogo());
  88. data.setPrice(goods.getMinPrice());
  89. UserPlatDistributeRate userPlatDistributeRate = userPlatDistributeRateMapper.selectOne(Wrappers.lambdaQuery(UserPlatDistributeRate.class)
  90. .eq(UserPlatDistributeRate::getDistributeId, userDistribute.getId())
  91. .eq(UserPlatDistributeRate::getGoodsId, data.getGoodsId())
  92. .last("limit 1"));
  93. if (userPlatDistributeRate != null) {
  94. data.setRate(userPlatDistributeRate.getRate());
  95. }
  96. if (goods.getIsSkuDistribute()) {
  97. UserPlatSkuDistributeRate userPlatSkuDistributeRate = userPlatSkuDistributeRateMapper.selectOne(Wrappers.lambdaQuery(UserPlatSkuDistributeRate.class)
  98. .eq(UserPlatSkuDistributeRate::getDistributeId, userDistribute.getId())
  99. .eq(UserPlatSkuDistributeRate::getGoodsId, data.getGoodsId())
  100. .ne(UserPlatSkuDistributeRate::getRate, 0)
  101. //上架
  102. .apply("exists (select 1 from goods_don_sku sku where sku.id = sku_id and sku.status is true limit 1)")
  103. .orderByAsc(UserPlatSkuDistributeRate::getRate)
  104. .last("limit 1"));
  105. if (userPlatSkuDistributeRate != null) {
  106. data.setRate(userPlatSkuDistributeRate.getRate());
  107. }
  108. }
  109. });
  110. data.setClickNum(distributePopularizeClickRecordMapper.selectCount(Wrappers.lambdaQuery(DistributePopularizeClickRecord.class)
  111. .eq(DistributePopularizeClickRecord::getPopularizeId, data.getId())
  112. .ge(startTime != null, DistributePopularizeClickRecord::getCreatedTime, startTime)
  113. .lt(endTime != null, DistributePopularizeClickRecord::getCreatedTime, endTime)));
  114. String link = data.getPopularizeLink();
  115. if (data.getGoodsId() != null && data.getGoodsId() > 0) {
  116. if (!envCommonService.isPrdEnv()) {
  117. link += "/test/web";
  118. }
  119. link += "/?gid=" + data.getGoodsId();
  120. }
  121. data.setUrlCode(QrCodeUtil.generateAsBase64(link, QrConfig.create(), "png"));
  122. });
  123. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  124. }
  125. /**
  126. * 默认链接详情
  127. */
  128. @GetMapping("/get/default")
  129. public Result<UserDistributePopularizeView> getDefaultDetail(String startTime, String endTime) {
  130. long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
  131. User user = userService.getById(cpsUserId);
  132. UserDistributePopularizeView view = Optional.ofNullable(cpsPopularizeService.getDefaultPopularizeDetail(cpsUserId, startTime, endTime)).orElseGet(() -> {
  133. UserDistributePopularizeView newPopularizeView = new UserDistributePopularizeView();
  134. newPopularizeView.setOrders(0);
  135. newPopularizeView.setOrdersMoney(BigDecimal.ZERO);
  136. newPopularizeView.setWMoney(BigDecimal.ZERO);
  137. newPopularizeView.setGotMoney(BigDecimal.ZERO);
  138. return newPopularizeView;
  139. });
  140. if (StrUtil.isEmpty(user.getShowId())) {
  141. user.setShowId(userService.getUserShowId(user.getId()));
  142. }
  143. view.setPopularizeLink(String.format(envCommonService.DISTRIBUTE_DEFAULT_URL_PREFIX, user.getShowId()));
  144. //优惠码
  145. view.setCouponCode(couponDistributePopularizeMapper.getCouponCodeByUid(cpsUserId));
  146. view.setClickNum(distributePopularizeClickRecordMapper.selectCount(Wrappers.lambdaQuery(DistributePopularizeClickRecord.class).eq(DistributePopularizeClickRecord::getShowId, user.getShowId())));
  147. view.setUrlCode(QrCodeUtil.generateAsBase64(view.getPopularizeLink(), QrConfig.create(), "png"));
  148. return GatewayResponse.SUCCESS.newBuilder().toResult(view);
  149. }
  150. /**
  151. * 新增推广链接
  152. */
  153. @PostMapping("/post")
  154. @NoSubmit
  155. public Result<String> insert(@RequestBody UserDistributePopularize popularize) {
  156. long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
  157. popularize.setUserId(cpsUserId);
  158. cpsPopularizeService.insert(popularize);
  159. return GatewayResponse.SUCCESS.newBuilder().toResult(String.format("%s?dsCode=%s", envCommonService.getDomain2(), popularize.getCode()));
  160. }
  161. /**
  162. * 编辑推广链接
  163. */
  164. @PutMapping("/put")
  165. @NoSubmit
  166. public Result<String> update(@RequestBody UserDistributePopularize popularize) {
  167. long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
  168. popularize.setUserId(cpsUserId);
  169. cpsPopularizeService.update(popularize);
  170. return GatewayResponse.SUCCESS.newBuilder().toResult();
  171. }
  172. /**
  173. * 删除推广链接
  174. */
  175. @DeleteMapping("/delete/{id}")
  176. @NoSubmit
  177. public Result<String> deleteById(@PathVariable Long id) {
  178. long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
  179. cpsPopularizeService.deleteById(cpsUserId, id);
  180. return GatewayResponse.SUCCESS.newBuilder().toResult();
  181. }
  182. /**
  183. * 获取所有平台列表
  184. */
  185. @GetMapping("/get/all/goods")
  186. public Result<List<GoodsDon>> getAllGoodsList(Boolean isNoBuyout, Boolean isRate) {
  187. Long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
  188. MapBuilder mapBuilder = MapUtils.flatBuilder(request.getParameterMap());
  189. //过滤买断制 平台
  190. String conditionSql;
  191. if (isNoBuyout != null && isNoBuyout) {
  192. mapBuilder.field(GoodsDon::getSpecialType).op(Operator.IsNull);
  193. conditionSql = "(select count(*) from goods_don_sku sku where sku.goods_id = g.id and sku.months = 300) = 0";
  194. } else {
  195. conditionSql = "(special_type is null or special_type != 'giftCard')";
  196. }
  197. conditionSql += " and exists (select id from goods_don_sku sku where sku.goods_id = g.id limit 1)";
  198. if (isRate != null && isRate) {
  199. UserDistribute userDistribute = userDistributeMapper.selectOne(Wrappers.lambdaQuery(UserDistribute.class)
  200. .eq(UserDistribute::getUserId, cpsUserId)
  201. .eq(UserDistribute::getDeleted, 1)
  202. .last("limit 1"));
  203. conditionSql += String.format(" and exists (select 1 from user_plat_distribute_rate upr where upr.distribute_id = %s and upr.goods_id = g.id and upr.rate > 0)", userDistribute.getId());
  204. }
  205. List<GoodsDon> goodsDons = beanSearcher.searchAll(GoodsDon.class, mapBuilder
  206. .put("condition", conditionSql)
  207. .field(GoodsDon::getType, 1)
  208. .field(GoodsDon::getStatus, 1)
  209. .field(GoodsDon::getDeleted, true)
  210. .onlySelect(GoodsDon::getId, GoodsDon::getCategory, GoodsDon::getType, GoodsDon::getTitle, GoodsDon::getSecondType).build());
  211. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsDons);
  212. }
  213. /**
  214. * 复制客服活码链接
  215. * 用于cps用户实物分销
  216. */
  217. @GetMapping("/get/service/code")
  218. public Result<String> getServiceCodeUrl() {
  219. long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
  220. return GatewayResponse.SUCCESS.newBuilder().toResult(cpsPopularizeService.getServiceCodeUrl(cpsUserId));
  221. }
  222. /**
  223. * 推广平台海报列表
  224. */
  225. @GetMapping("/get/ds/poster/goods")
  226. public Result<List<DistributePosterGoodsCpsView>> getPosterGoods(String startTime, String endTime) {
  227. long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
  228. UserDistribute userDistribute = userDistributeMapper.selectOne(Wrappers.lambdaQuery(UserDistribute.class)
  229. .eq(UserDistribute::getUserId, cpsUserId)
  230. .eq(UserDistribute::getDeleted, 1)
  231. .last("limit 1"));
  232. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  233. StringBuilder time = new StringBuilder();
  234. if (startTime != null) {
  235. time.append(String.format(" and od.created_time >= '%s'", startTime));
  236. }
  237. if (endTime != null) {
  238. time.append(String.format(" and od.created_time < '%s'", endTime));
  239. }
  240. List<DistributePosterGoodsCpsView> searchAll = beanSearcher.searchAll(DistributePosterGoodsCpsView.class, builder.build());
  241. searchAll.forEach(e -> {
  242. if (userDistribute != null) {
  243. UserPlatDistributeRate userPlatDistributeRate = userPlatDistributeRateMapper.selectOne(Wrappers.lambdaQuery(UserPlatDistributeRate.class)
  244. .eq(UserPlatDistributeRate::getDistributeId, userDistribute.getId())
  245. .eq(UserPlatDistributeRate::getGoodsId, e.getGoodsId())
  246. .last("limit 1"));
  247. if (userPlatDistributeRate != null) {
  248. e.setRate(userPlatDistributeRate.getRate());
  249. }
  250. }
  251. if (e.getIsSkuRate()) {
  252. GoodsDonSku sku = skuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class)
  253. .eq(GoodsDonSku::getGoodsId, e.getGoodsId())
  254. .eq(GoodsDonSku::getStatus, 1)
  255. .orderByAsc(GoodsDonSku::getPrice)
  256. .last("limit 1"));
  257. //先获取价格最低的规格对应的 比例
  258. //渠道
  259. if (userDistribute != null) {
  260. UserPlatSkuDistributeRate minPriceSkuRate = userPlatSkuDistributeRateMapper.selectOne(Wrappers.lambdaQuery(UserPlatSkuDistributeRate.class)
  261. .eq(UserPlatSkuDistributeRate::getDistributeId, userDistribute.getId())
  262. .eq(UserPlatSkuDistributeRate::getGoodsId, e.getGoodsId())
  263. .eq(UserPlatSkuDistributeRate::getSkuId, sku.getId())
  264. .last("limit 1"));
  265. if (minPriceSkuRate != null && minPriceSkuRate.getRate() != 0) {
  266. e.setRate(minPriceSkuRate.getRate());
  267. } else {
  268. UserPlatSkuDistributeRate userPlatSkuDistributeRate = userPlatSkuDistributeRateMapper.selectOne(Wrappers.lambdaQuery(UserPlatSkuDistributeRate.class)
  269. .eq(UserPlatSkuDistributeRate::getDistributeId, userDistribute.getId())
  270. .eq(UserPlatSkuDistributeRate::getGoodsId, e.getGoodsId())
  271. .ne(UserPlatSkuDistributeRate::getRate, 0)
  272. //上架
  273. .apply("exists (select 1 from goods_don_sku sku where sku.id = sku_id and sku.status is true limit 1)")
  274. .orderByAsc(UserPlatSkuDistributeRate::getRate)
  275. .last("limit 1"));
  276. if (userPlatSkuDistributeRate != null) {
  277. e.setRate(userPlatSkuDistributeRate.getRate());
  278. sku = skuMapper.selectById(userPlatSkuDistributeRate.getSkuId());
  279. e.setPrice(sku.getPrice());
  280. }
  281. }
  282. } else {
  283. //普通
  284. DistributeGeneralSkuRate minPriceRate = distributeGeneralSkuRateMapper.selectOne(Wrappers.lambdaQuery(DistributeGeneralSkuRate.class)
  285. .eq(DistributeGeneralSkuRate::getGoodsId, e.getGoodsId())
  286. .eq(DistributeGeneralSkuRate::getSkuId, sku.getId())
  287. .last("limit 1"));
  288. if (minPriceRate != null && minPriceRate.getRate() > 0) {
  289. e.setRate(minPriceRate.getRate());
  290. } else {
  291. DistributeGeneralSkuRate distributeGeneralSkuRate = distributeGeneralSkuRateMapper.selectOne(Wrappers.lambdaQuery(DistributeGeneralSkuRate.class)
  292. .eq(DistributeGeneralSkuRate::getGoodsId, e.getGoodsId())
  293. .apply("exists (select 1 from goods_don_sku sku where sku.id = sku_id and sku.status is true limit 1)")
  294. .ne(DistributeGeneralSkuRate::getRate, 0)
  295. .orderByAsc(DistributeGeneralSkuRate::getRate)
  296. .last("limit 1"));
  297. if (distributeGeneralSkuRate != null) {
  298. e.setRate(distributeGeneralSkuRate.getRate());
  299. sku = skuMapper.selectById(distributeGeneralSkuRate.getSkuId());
  300. e.setPrice(sku.getPrice());
  301. }
  302. }
  303. }
  304. }
  305. //用户推广海报订单详情
  306. MapBuilder posterOrderBuilder = MapUtils.flatBuilder(request.getParameterMap());
  307. if (time.length() > 0) {
  308. posterOrderBuilder.put("time", time.toString());
  309. }
  310. UserDistributePosterOrderDetailView distributePosterOrderDetailView = beanSearcher.searchFirst(UserDistributePosterOrderDetailView.class, posterOrderBuilder.put("condition", String.format(String.format(" where user_id = %s and poster_id = %s", cpsUserId, e.getId()))).build());
  311. if (distributePosterOrderDetailView != null) {
  312. e.setOrdersMoney(distributePosterOrderDetailView.getOrdersMoney());
  313. e.setOrders(distributePosterOrderDetailView.getOrders());
  314. e.setWMoney(distributePosterOrderDetailView.getWMoney());
  315. e.setGotMoney(distributePosterOrderDetailView.getGotMoney());
  316. e.setClickNum(distributePopularizeClickRecordMapper.selectCount(Wrappers.lambdaQuery(DistributePopularizeClickRecord.class)
  317. .eq(DistributePopularizeClickRecord::getRid, distributePosterOrderDetailView.getId())
  318. .ge(startTime != null, DistributePopularizeClickRecord::getCreatedTime, startTime)
  319. .lt(endTime != null, DistributePopularizeClickRecord::getCreatedTime, endTime)));
  320. }
  321. UserDistributePosterRecord distributePosterRecord = userDistributePosterRecordMapper.selectOne(Wrappers.lambdaQuery(UserDistributePosterRecord.class)
  322. .eq(UserDistributePosterRecord::getPosterId, e.getId())
  323. .eq(UserDistributePosterRecord::getUserId, cpsUserId)
  324. .last("limit 1"));
  325. if (distributePosterRecord != null) {
  326. e.setRid(distributePosterRecord.getId());
  327. }
  328. });
  329. return GatewayResponse.SUCCESS.newBuilder().toResult(searchAll);
  330. }
  331. /**
  332. * 用户下载海报记录
  333. */
  334. @PostMapping("/poster/record/{posterId}")
  335. public Result<Long> posterRecord(@PathVariable Long posterId) {
  336. long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
  337. DistributePosterGoods distributePosterGoods = distributePosterGoodsMapper.selectById(posterId);
  338. if (distributePosterGoods == null) {
  339. return GatewayResponse.SUCCESS.newBuilder().toResult();
  340. }
  341. UserDistributePosterRecord distributePosterRecord = new UserDistributePosterRecord();
  342. distributePosterRecord.setPosterId(posterId);
  343. distributePosterRecord.setUserId(cpsUserId);
  344. try {
  345. userDistributePosterRecordMapper.insert(distributePosterRecord);
  346. } catch (DuplicateKeyException e) {
  347. distributePosterRecord = userDistributePosterRecordMapper.selectOne(Wrappers.lambdaQuery(UserDistributePosterRecord.class)
  348. .eq(UserDistributePosterRecord::getUserId, cpsUserId)
  349. .eq(UserDistributePosterRecord::getPosterId, posterId)
  350. .last("limit 1"));
  351. }
  352. return GatewayResponse.SUCCESS.newBuilder().toResult(distributePosterRecord.getId());
  353. }
  354. /**
  355. * 更新推荐海报状态
  356. */
  357. @PutMapping("/update/recommend/status")
  358. public Result<String> updateRecommendStatus() {
  359. long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
  360. UserDistribute userDistribute = userDistributeMapper.selectOne(Wrappers.lambdaQuery(UserDistribute.class)
  361. .eq(UserDistribute::getUserId, cpsUserId)
  362. .eq(UserDistribute::getDeleted, 1)
  363. .last("limit 1"));
  364. if (userDistribute != null) {
  365. Boolean isPoster = userDistribute.getIsPoster();
  366. userDistributeMapper.update(null, Wrappers.lambdaUpdate(UserDistribute.class)
  367. .set(UserDistribute::getIsPoster, !isPoster)
  368. .eq(UserDistribute::getId, userDistribute.getId()));
  369. }
  370. return GatewayResponse.SUCCESS.newBuilder().toResult();
  371. }
  372. }