GoodsDonController.java 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. package com.cyksj.web.controller.goods;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateTime;
  4. import cn.hutool.core.date.DateUnit;
  5. import cn.hutool.core.date.DateUtil;
  6. import cn.hutool.core.util.RandomUtil;
  7. import cn.hutool.core.util.StrUtil;
  8. import cn.hutool.extra.servlet.ServletUtil;
  9. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  10. import com.cyksj.common.constant.Constant;
  11. import com.cyksj.common.exception.BusinessRuntimeException;
  12. import com.cyksj.common.util.Jsons;
  13. import com.cyksj.common.util.StringUtil;
  14. import com.cyksj.dto.Result;
  15. import com.cyksj.dto.UserSharedDto;
  16. import com.cyksj.enums.GatewayResponse;
  17. import com.cyksj.mapper.GoodsDonSkuMapper;
  18. import com.cyksj.mapper.OrderDonMapper;
  19. import com.cyksj.mapper.RegisterOrderDonMapper;
  20. import com.cyksj.mapper.sys.SysConfigMapper;
  21. import com.cyksj.model.entity.*;
  22. import com.cyksj.model.request.ClickRecordReq;
  23. import com.cyksj.model.views.*;
  24. import com.cyksj.redis.RedisService;
  25. import com.cyksj.service.goods.GoodsDonFrontService;
  26. import com.cyksj.service.home.ClickRecordFrontService;
  27. import com.cyksj.service.shop.YhShopFrontService;
  28. import com.cyksj.service.user.UserBindRelationService;
  29. import com.cyksj.service.user.UserService;
  30. import com.cyksj.web.util.StpUserUtil;
  31. import com.ejlchina.searcher.BeanSearcher;
  32. import com.ejlchina.searcher.SearchResult;
  33. import com.ejlchina.searcher.param.Operator;
  34. import com.ejlchina.searcher.util.MapBuilder;
  35. import com.ejlchina.searcher.util.MapUtils;
  36. import lombok.RequiredArgsConstructor;
  37. import lombok.extern.slf4j.Slf4j;
  38. import org.springframework.web.bind.annotation.*;
  39. import javax.servlet.http.HttpServletRequest;
  40. import java.math.BigDecimal;
  41. import java.util.*;
  42. import java.util.concurrent.ConcurrentHashMap;
  43. import java.util.concurrent.atomic.AtomicBoolean;
  44. import java.util.concurrent.atomic.AtomicLong;
  45. import java.util.stream.Collectors;
  46. import java.util.stream.Stream;
  47. /**
  48. * @author chan
  49. * @date 2022/9/26 5:33 PM
  50. */
  51. @Slf4j
  52. @RestController
  53. @RequestMapping("/applets/goods")
  54. @RequiredArgsConstructor
  55. public class GoodsDonController {
  56. private final HttpServletRequest request;
  57. private final BeanSearcher beanSearcher;
  58. private final OrderDonMapper orderDonMapper;
  59. private final RegisterOrderDonMapper registerOrderDonMapper;
  60. private final GoodsDonSkuMapper goodsDonSkuMapper;
  61. private final RedisService redisService;
  62. private final SysConfigMapper sysConfigMapper;
  63. private final ClickRecordFrontService clickRecordFrontService;
  64. private final YhShopFrontService yhShopFrontService;
  65. private final UserService userService;
  66. private final GoodsDonFrontService goodsDonFrontService;
  67. private final UserBindRelationService userBindRelationService;
  68. /**
  69. * 获取平台商品分类
  70. */
  71. @GetMapping("/get/category")
  72. public Result<List<GoodsDonCategoryFrontView>> getCategory(String customId) {
  73. Long yhsId = null;
  74. if (StrUtil.isNotBlank(customId)) {
  75. yhsId = yhShopFrontService.getYhsIdByCustomId(customId);
  76. }
  77. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "category";
  78. if (yhsId != null) {
  79. key += ":yhShop:" + yhsId;
  80. }
  81. Object o = redisService.get(key);
  82. List<GoodsDonCategoryFrontView> list;
  83. MapBuilder mapBuilder = MapUtils.flatBuilder(request.getParameterMap());
  84. String condition = " and (select count(*) from goods_don_category_relate gcr inner join goods_don g on g.id = gcr.goods_id %s where gcr.category = gc.id) > 0";
  85. if (yhsId != null) {
  86. condition = String.format(condition, String.format(" and (select count(*) from yh_shop_goods_sku ys where ys.goods_id = g.id and ys.status is true and ys.deleted is true and g.is_distribute is true and ys.yhs_id = %s) > 0", yhsId));
  87. } else {
  88. condition = String.format(condition, StrUtil.EMPTY);
  89. }
  90. mapBuilder.put("condition", condition);
  91. if (o == null) {
  92. list = beanSearcher.searchAll(GoodsDonCategoryFrontView.class, mapBuilder
  93. .build());
  94. redisService.setNx(key, list, 30 * 60l);
  95. o = redisService.get(key);
  96. }
  97. list = Jsons.parseList(o, GoodsDonCategoryFrontView.class);
  98. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  99. }
  100. /**
  101. * 商品搜索
  102. */
  103. @GetMapping("/search")
  104. public Result<SearchResult<GoodsDonSearchView>> search() {
  105. SearchResult<GoodsDonSearchView> search = beanSearcher.search(GoodsDonSearchView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
  106. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  107. }
  108. /**
  109. * 获取商品列表
  110. */
  111. @GetMapping("/get")
  112. public Result<SearchResult<GoodsDon>> get() {
  113. SearchResult<GoodsDon> search = beanSearcher.search(GoodsDon.class, MapUtils.flatBuilder(request.getParameterMap())
  114. .field(GoodsDon::getStatus, true)
  115. .field(GoodsDon::getDeleted, true)
  116. .orderBy(GoodsDon::getSortBy).asc()
  117. .orderBy(GoodsDon::getId).asc()
  118. .build()
  119. );
  120. search.getDataList().forEach(goods -> {
  121. GoodsDonSku goodsDonSku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class).eq(GoodsDonSku::getGoodsId, goods.getId())
  122. .eq(GoodsDonSku::getStatus, true)
  123. .orderByAsc(GoodsDonSku::getPrice)
  124. .last("limit 1")
  125. .select(GoodsDonSku::getPrice));
  126. Optional.ofNullable(goodsDonSku).ifPresent(sku -> {
  127. goods.setPrice(goodsDonSku.getPrice());
  128. goods.setSoldNum(goods.getVirtualSold() + orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  129. .eq(OrderDon::getGoodsId, goods.getId())
  130. .notIn(OrderDon::getStatus, Constant.noOrderStatus)));
  131. });
  132. });
  133. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  134. }
  135. /**
  136. * 推广未登录,虚拟商品列表
  137. */
  138. @GetMapping("/get/popularize")
  139. @Deprecated
  140. public Result<SearchResult<GoodsDonPopularizeView>> getPopularizeVipGoods() {
  141. SearchResult<GoodsDonPopularizeView> search = beanSearcher.search(GoodsDonPopularizeView.class, MapUtils.flatBuilder(request.getParameterMap())
  142. .field(GoodsDonPopularizeView::getStatus, true)
  143. .field(GoodsDonPopularizeView::getDeleted, true)
  144. .field(GoodsDonPopularizeView::getType, 1)
  145. .orderBy(GoodsDonPopularizeView::getSortBy).asc()
  146. .orderBy(GoodsDonPopularizeView::getId).asc()
  147. .build());
  148. DateTime now = DateTime.now();
  149. search.getDataList().forEach(data -> {
  150. data.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, data.getId()).build()));
  151. data.setSkuList(beanSearcher.searchAll(GoodsDonSku.class, MapUtils.builder().field(GoodsDonSku::getGoodsId, data.getId()).field(GoodsDonSku::getStatus, true).orderBy(GoodsDonSku::getPrice).asc().build()));
  152. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  153. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  154. data.setSold(sold + data.getVirtualSold());
  155. Optional.ofNullable(data.getSkuList()).ifPresent(skuList -> {
  156. if (skuList.size() > 0) {
  157. GoodsDonSku goodsDonSku = skuList.get(0);
  158. data.setPrice(goodsDonSku.getPrice());
  159. data.setMonths(goodsDonSku.getMonths());
  160. }
  161. });
  162. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  163. .eq(OrderDon::getGoodsId, data.getId())
  164. .notIn(OrderDon::getStatus, Constant.noOrderStatus).select(OrderDon::getCreatedTime, OrderDon::getPayTime, OrderDon::getId)
  165. .orderByDesc(OrderDon::getId)
  166. .last("limit 1"));
  167. StringBuilder notifyMsg = new StringBuilder();
  168. if (orderDon == null) {
  169. notifyMsg.append("点击购买立即上车");
  170. data.setNotifyMsg(notifyMsg.toString());
  171. return;
  172. } else {
  173. Date payTime = orderDon.getPayTime();
  174. Date startTime = payTime != null ? payTime : orderDon.getCreatedTime();
  175. long hour = DateUtil.between(startTime, now, DateUnit.HOUR);
  176. if (hour == 0) {
  177. long minute = DateUtil.between(startTime, now, DateUnit.MINUTE);
  178. if (minute == 0) {
  179. notifyMsg.append("1分钟");
  180. } else {
  181. notifyMsg.append(minute);
  182. notifyMsg.append("分钟");
  183. }
  184. } else {
  185. if (hour > 3) {
  186. int hours = RandomUtil.randomInt(3) + 1;
  187. notifyMsg.append(hours);
  188. } else {
  189. notifyMsg.append(hour);
  190. }
  191. notifyMsg.append("小时");
  192. }
  193. }
  194. notifyMsg.append("前有人购买");
  195. data.setNotifyMsg(notifyMsg.toString());
  196. });
  197. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  198. }
  199. /**
  200. * 官网首页 平台列表
  201. */
  202. @GetMapping("/get/list")
  203. @Deprecated
  204. public Result<? extends Object> getGoodsList(String customId) {
  205. if (StrUtil.isNotBlank(customId)) {
  206. Result<List<YhShopGoodsFrontView>> shopGoods = getShopGoods(yhShopFrontService.getYhsIdByCustomId(customId));
  207. return shopGoods;
  208. }
  209. //List<Long> filter_goodsIds = getFilterGoodsIds();
  210. List<Long> filter_goodsIds = null;
  211. String pcCacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "PC";
  212. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  213. pcCacheKey += ":hz-ip";
  214. }
  215. Long fUid = StpUserUtil.getUserIdAfterLogin();
  216. Object o = redisService.get(pcCacheKey);
  217. List<GoodsFrontListView> list = null;
  218. if (o == null) {
  219. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  220. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  221. builder.field(GoodsFrontListView::getId, filter_goodsIds).op(Operator.NotIn);
  222. }
  223. builder.field(GoodsFrontListView::getType, 1);
  224. list = beanSearcher.searchAll(GoodsFrontListView.class, builder.build());
  225. redisService.setNx(pcCacheKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  226. o = redisService.get(pcCacheKey);
  227. }
  228. if (list == null) {
  229. list = Jsons.parseList(o, GoodsFrontListView.class);
  230. }
  231. DateTime now = DateTime.now();
  232. list.forEach(data -> {
  233. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  234. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  235. data.setSold(sold + data.getSold());
  236. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, null));
  237. //设置特定价格
  238. setSpecificPrice(data, fUid, null);
  239. });
  240. return GatewayResponse.SUCCESS.newBuilder().toResult(Optional.of(list));
  241. }
  242. /**
  243. * h5 银河首页平台展示
  244. */
  245. @GetMapping("/get/homePage")
  246. @Deprecated
  247. public Result<? extends Object> getGoodsHomePage(String customId) {
  248. if (StrUtil.isNotBlank(customId)) {
  249. Result<List<YhShopGoodsFrontView>> shopGoods = getShopGoods(yhShopFrontService.getYhsIdByCustomId(customId));
  250. return shopGoods;
  251. }
  252. //List<Long> filter_goodsIds = getFilterGoodsIds();
  253. List<Long> filter_goodsIds = null;
  254. String h5CacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "H5";
  255. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  256. h5CacheKey += ":hz-ip";
  257. }
  258. Long fUid = StpUserUtil.getUserIdAfterLogin();
  259. Object o = redisService.get(h5CacheKey);
  260. List<GoodsFrontListView> list = null;
  261. if (o == null) {
  262. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  263. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  264. builder.field(GoodsFrontListView::getId, filter_goodsIds).op(Operator.NotIn);
  265. }
  266. list = beanSearcher.searchAll(GoodsFrontListView.class, builder.build());
  267. redisService.setNx(h5CacheKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  268. o = redisService.get(h5CacheKey);
  269. }
  270. if (list == null) {
  271. list = Jsons.parseList(o, GoodsFrontListView.class);
  272. }
  273. DateTime now = DateTime.now();
  274. list.forEach(data -> {
  275. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  276. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  277. data.setSold(sold + data.getSold());
  278. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, null));
  279. //设置特定价格
  280. setSpecificPrice(data, fUid, null);
  281. });
  282. return GatewayResponse.SUCCESS.newBuilder().toResult(Optional.of(list));
  283. }
  284. /**
  285. * 注册首页平台展示
  286. */
  287. @GetMapping("/get/register")
  288. public Result<List<RegisterGoodsDonView>> getRegisterGoodsDon() {
  289. String registerCacheKey = RedisService.key.REGISTER_HOME_PAGE_CACHE.getEnvName();
  290. Object value = redisService.get(registerCacheKey);
  291. if (value != null) {
  292. try {
  293. List<RegisterGoodsDonView> list = Jsons.parseList(value, RegisterGoodsDonView.class);
  294. if (CollUtil.isNotEmpty(list)) {
  295. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  296. }
  297. } catch (Exception e) {
  298. }
  299. }
  300. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  301. List<RegisterGoodsDonView> dataList = beanSearcher.searchAll(RegisterGoodsDonView.class, builder.build());
  302. DateTime now = DateTime.now();
  303. dataList.forEach(data -> {
  304. Integer sold = registerOrderDonMapper.selectCount(Wrappers.lambdaQuery(RegisterOrderDon.class)
  305. .eq(RegisterOrderDon::getGoodsId, data.getId()).notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus));
  306. data.setSold(sold + data.getVirtualSold());
  307. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 2, null));
  308. });
  309. redisService.setNx(registerCacheKey, dataList, RedisService.key.REGISTER_HOME_PAGE_CACHE.getTimeout());
  310. return GatewayResponse.SUCCESS.newBuilder().toResult(dataList);
  311. }
  312. /**
  313. * 获取商品详情
  314. */
  315. @GetMapping("/get/{id}")
  316. public Result<? extends Object> getDetail(@PathVariable Long id, String customId, String dsCode) {
  317. if (StrUtil.isNotBlank(customId) && id != 15) {
  318. Result<YhShopGoodsFrontView> yhShopGoodsDetail = getGoodsDetailByYhsId(yhShopFrontService.getYhsIdByCustomId(customId), id);
  319. return yhShopGoodsDetail;
  320. }
  321. String goodsDetailKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "goods_detail:" + id;
  322. //List<Long> filter_goodsIds = getFilterGoodsIds();
  323. List<Long> filter_goodsIds = null;
  324. //存在过滤平台 商品详情不返回
  325. if (CollUtil.isNotEmpty(filter_goodsIds) && filter_goodsIds.contains(id)) {
  326. return GatewayResponse.SUCCESS.newBuilder().toResult();
  327. }
  328. Object o = redisService.get(goodsDetailKey);
  329. if (o == null) {
  330. GoodsFrontListView views = beanSearcher.searchFirst(GoodsFrontListView.class, MapUtils.builder().field(GoodsFrontListView::getId, id)
  331. .build());
  332. if (views == null) {
  333. throw BusinessRuntimeException.getInstance("商品已下架..");
  334. }
  335. views.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, views.getId()).build()));
  336. MapBuilder builder = MapUtils.builder().field(GoodsDonSkuFrontView::getGoodsId, views.getId()).field(GoodsDonSkuFrontView::getStatus, true);
  337. if (views.getType() == 1) {
  338. builder.orderBy(GoodsDonSkuFrontView::getSorted).asc();
  339. }
  340. views.setSkuList(beanSearcher.searchAll(GoodsDonSkuFrontView.class, builder.build()));
  341. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  342. .eq(OrderDon::getGoodsId, views.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  343. views.setSold(sold + views.getSold());
  344. redisService.setNx(goodsDetailKey, views, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 60 * RandomUtil.randomInt(10));
  345. o = redisService.get(goodsDetailKey);
  346. }
  347. Long fUid = StpUserUtil.getUserIdAfterLogin();
  348. GoodsFrontListView views = Jsons.parseObject(o, GoodsFrontListView.class);
  349. AtomicLong dsUserId = new AtomicLong();
  350. AtomicBoolean existsChannel = new AtomicBoolean(true);
  351. if (StrUtil.isNotBlank(dsCode)) {
  352. UserSharedDto userSharedDto = userService.getUserShredDtoByDsCode(dsCode);
  353. dsUserId.set(userSharedDto.getSharedId());
  354. }
  355. views.setSkuList(views.getSkuList().stream().filter(sku -> {
  356. if (StrUtil.isNotBlank(sku.getUserOwns())) {
  357. if (dsUserId.get() == 0 && fUid != null && existsChannel.get()) {
  358. Long sharedId = userService.getSharedIdByUserId(fUid);
  359. if (sharedId == 0) {
  360. existsChannel.set(false);
  361. }
  362. dsUserId.set(sharedId);
  363. }
  364. if (dsUserId.get() == 0 || !sku.getUserOwns().contains(String.valueOf(dsUserId.get()))) {
  365. return false;
  366. }
  367. }
  368. if (sku.getIsDp()) {
  369. sku.setDpSkuViews(beanSearcher.searchAll(GoodsDiscountPlusPurchaseRelationFrontView.class, MapUtils.builder()
  370. .field(GoodsDiscountPlusPurchaseRelationFrontView::getSkuId, sku.getId())
  371. .build()));
  372. }
  373. if (views.getIsSp()) {
  374. if (fUid == null) {
  375. sku.setSpecificPrice(null);
  376. sku.setSpecificGoodsIds(null);
  377. }else {
  378. //设置特定价格
  379. if (!isPaySpecificGoodsIds(sku.getSpecificGoodsIds(), fUid)) {
  380. sku.setSpecificGoodsIds(null);
  381. sku.setSpecificPrice(null);
  382. } else {
  383. BigDecimal specificPrice = views.getSpecificPrice();
  384. BigDecimal skuSpecificPrice = sku.getSpecificPrice();
  385. Long specificSkuId = sku.getId();
  386. if (specificPrice == null) {
  387. views.setSpecificPrice(skuSpecificPrice);
  388. views.setSpecificSkuId(specificSkuId);
  389. } else if (specificPrice.compareTo(skuSpecificPrice) > 0) {
  390. views.setSpecificPrice(skuSpecificPrice);
  391. views.setSpecificSkuId(specificSkuId);
  392. }
  393. }
  394. }
  395. }
  396. return true;
  397. }).collect(Collectors.toList()));
  398. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  399. }
  400. /**
  401. * 获取商品推荐平台
  402. */
  403. @GetMapping("/get/recommend/{id}")
  404. public Result<List<GoodsFrontListView>> getRecommendGoodsByGid(@PathVariable Long id, String customId, String dsCode) throws Exception {
  405. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "recommend:" + id;
  406. Long yhsId = null;
  407. if (StrUtil.isNotBlank(customId)) {
  408. yhsId = yhShopFrontService.getYhsIdByCustomId(customId);
  409. key = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getEnvName() + yhsId + ":recommend:" + id;
  410. }
  411. Object o = redisService.get(key);
  412. Long fUid = StpUserUtil.getUserIdAfterLogin();
  413. List<GoodsFrontListView> goodsRecommendViews = null;
  414. if (o == null) {
  415. GoodsDon goodsDon = beanSearcher.searchFirst(GoodsDon.class, MapUtils.builder()
  416. .field(GoodsDon::getId, id).field(GoodsDon::getStatus, true)
  417. .field(GoodsDon::getDeleted, true)
  418. .build());
  419. if (goodsDon != null && StrUtil.isNotEmpty(goodsDon.getRecommendGoods())) {
  420. List<Long> recommend_gidList = Jsons.parseList(goodsDon.getRecommendGoods(), Long.class);
  421. MapBuilder mapBuilder = MapUtils.builder();
  422. if (yhsId != null) {
  423. mapBuilder.put("condition", String.format(" and g.id in (select distinct goods_id from yh_shop_goods_sku where yhs_id = %s and status is true and deleted is true) and g.is_distribute is true", yhsId));
  424. }
  425. mapBuilder.field(GoodsFrontListView::getId, recommend_gidList).op(Operator.InList);
  426. goodsRecommendViews = beanSearcher.searchAll(GoodsFrontListView.class, mapBuilder.build());
  427. redisService.setNx(key, Jsons.toJson(goodsRecommendViews), 60 * 15l);
  428. o = redisService.get(key);
  429. } else {
  430. return GatewayResponse.SUCCESS.newBuilder().toResult();
  431. }
  432. }
  433. if (goodsRecommendViews == null) {
  434. goodsRecommendViews = Jsons.parseList(o.toString(), GoodsFrontListView.class);
  435. }
  436. AtomicLong dsUserId = new AtomicLong();
  437. AtomicBoolean existsChannel = new AtomicBoolean(true);
  438. if (StrUtil.isNotBlank(dsCode)) {
  439. UserSharedDto userSharedDto = userService.getUserShredDtoByDsCode(dsCode);
  440. dsUserId.set(userSharedDto.getSharedId());
  441. }
  442. goodsRecommendViews = goodsRecommendViews.stream().filter(data -> {
  443. if (data.getIsOwns()) {
  444. if (dsUserId.get() == 0 && fUid != null && existsChannel.get()) {
  445. Long sharedId = userService.getSharedIdByUserId(fUid);
  446. if (sharedId == 0) {
  447. existsChannel.set(false);
  448. }
  449. dsUserId.set(sharedId);
  450. }
  451. GoodsDonSku minPriceUserOwnsSku = goodsDonSkuMapper.checkAndReturnMinPriceUserOwnsSku(data.getId(), dsUserId.get());
  452. if (minPriceUserOwnsSku == null) {
  453. return false;
  454. }
  455. data.setPrice(minPriceUserOwnsSku.getPrice());
  456. data.setMonths(minPriceUserOwnsSku.getMonths());
  457. data.setDays(minPriceUserOwnsSku.getDays());
  458. }
  459. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  460. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  461. data.setSold(data.getSold() + sold);
  462. //设置特定价格
  463. setSpecificPrice(data, fUid, dsUserId.get());
  464. return true;
  465. }).collect(Collectors.toList());
  466. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsRecommendViews);
  467. }
  468. /**
  469. * 获取商品多规格列表
  470. */
  471. @GetMapping("/get/sku")
  472. public Result<List<GoodsDonSkuView>> getGoodsDonSkuView(@RequestParam(value = "skuIds") List<Long> skuIds) {
  473. if (skuIds.isEmpty()) {
  474. return GatewayResponse.SUCCESS.newBuilder().toResult();
  475. }
  476. List<GoodsDonSkuView> views = beanSearcher.searchAll(GoodsDonSkuView.class, MapUtils.builder()
  477. .field(GoodsDonSkuView::getSkuId, skuIds).op(Operator.InList)
  478. .build());
  479. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  480. }
  481. /**
  482. * 获取评价列表
  483. */
  484. @GetMapping("/get/comments")
  485. public Result<SearchResult<OrderCommentFrontView>> getComments() {
  486. SearchResult<OrderCommentFrontView> search = beanSearcher.search(OrderCommentFrontView.class, MapUtils.flatBuilder(request.getParameterMap())
  487. .orderBy(OrderCommentFrontView::getCommentTime).desc()
  488. .orderBy(OrderCommentFrontView::getId).desc()
  489. .build());
  490. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  491. }
  492. /**
  493. * 获取礼品卡平台性情
  494. */
  495. @GetMapping("/get/card/{id}")
  496. public Result<Map<Integer, List<GiftCardGoodsSkuFrontView>>> getGiftCardDetail(@PathVariable Long id) {
  497. List<GiftCardGoodsSkuFrontView> giftCardGoodsSkuFrontViews = beanSearcher.searchAll(GiftCardGoodsSkuFrontView.class, MapUtils.builder().field(GiftCardGoodsSkuFrontView::getGoodsId, id).build());
  498. Map<Integer, List<GiftCardGoodsSkuFrontView>> map = giftCardGoodsSkuFrontViews.stream().collect(Collectors.groupingBy(GiftCardGoodsSkuFrontView::getGcType));
  499. return GatewayResponse.SUCCESS.newBuilder().toResult(map);
  500. }
  501. /**
  502. * 获取首页配置列表
  503. */
  504. @GetMapping("/get/homeManage")
  505. public Result<Map<String, List<HomeManagementFrontView>>> getHomeManagementFrontPage(String customId, String dsCode) {
  506. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "homeManage";
  507. Long yhsId = null;
  508. if (StrUtil.isNotBlank(customId)) {
  509. yhsId = yhShopFrontService.getYhsIdByCustomId(customId);
  510. key = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getEnvName() + yhsId + ":homeManage";
  511. }
  512. // List<Long> filter_goodsIds = getFilterGoodsIds();
  513. List<Long> filter_goodsIds = null;
  514. AtomicBoolean isRemoveRg = new AtomicBoolean(false);
  515. // if (isHzIp()) {
  516. // key += ":hz-ip";
  517. // isRemoveRg.set(true);
  518. // }
  519. Long fUid = StpUserUtil.getUserIdAfterLogin();
  520. Object o = redisService.get(key);
  521. if (o == null) {
  522. String condition = "g.deleted is true and g.status is true";
  523. if (yhsId != null) {
  524. condition += String.format(" and g.id in (select distinct goods_id from yh_shop_goods_sku where yhs_id = %s and status is true and deleted is true) and g.is_distribute is true", yhsId);
  525. }
  526. Map<String, List<HomeManagementFrontView>> map = new ConcurrentHashMap<>();
  527. final String conditionSql = condition;
  528. Stream.of(HomeManagementConfig.Type.values())
  529. .forEach(type -> {
  530. if (isRemoveRg.get() && type == HomeManagementConfig.Type.rg) {
  531. return;
  532. }
  533. MapBuilder builder = MapUtils.builder()
  534. .field(HomeManagementFrontView::getType, type.name());
  535. if (type == HomeManagementConfig.Type.ex || type == HomeManagementConfig.Type.vg) {
  536. builder.put("condition", conditionSql);
  537. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  538. builder.field(HomeManagementFrontView::getGoodsId, filter_goodsIds).op(Operator.NotIn);
  539. }
  540. }
  541. List<HomeManagementFrontView> homeManagementFrontViews = beanSearcher.searchAll(HomeManagementFrontView.class, builder.build());
  542. map.putIfAbsent(type.name(), homeManagementFrontViews);
  543. });
  544. redisService.setNx(key, map, 50 * 60l);
  545. o = redisService.get(key);
  546. }
  547. AtomicLong dsUserId = new AtomicLong();
  548. AtomicBoolean existsChannel = new AtomicBoolean(true);
  549. if (StrUtil.isNotBlank(dsCode)) {
  550. UserSharedDto userSharedDto = userService.getUserShredDtoByDsCode(dsCode);
  551. dsUserId.set(userSharedDto.getSharedId());
  552. }
  553. Map<String, List<HomeManagementFrontView>> map = Jsons.parseObject(o, Map.class);
  554. if (map.containsKey(HomeManagementConfig.Type.ex.name()) || map.containsKey(HomeManagementConfig.Type.vg.name())) {
  555. try {
  556. //精彩推荐
  557. List<HomeManagementFrontView> exList = Jsons.parseList(Jsons.toJson(map.get(HomeManagementConfig.Type.ex.name())), HomeManagementFrontView.class);
  558. setHomeConfigGoods(map, HomeManagementConfig.Type.ex, exList, dsUserId, existsChannel, fUid);
  559. //虚拟平台
  560. List<HomeManagementFrontView> vgList = Jsons.parseList(Jsons.toJson(map.get(HomeManagementConfig.Type.vg.name())), HomeManagementFrontView.class);
  561. setHomeConfigGoods(map, HomeManagementConfig.Type.vg, vgList, dsUserId, existsChannel, fUid);
  562. } catch (Exception e) {
  563. }
  564. }
  565. return GatewayResponse.SUCCESS.newBuilder().toResult(map);
  566. }
  567. /**
  568. * 首页点击 记录
  569. */
  570. @PostMapping("/homePage/click")
  571. public Result<String> homeManageClick(@RequestBody ClickRecordReq request) {
  572. Long userId = StpUserUtil.getUserIdAfterLogin();
  573. clickRecordFrontService.click(userId, request);
  574. return GatewayResponse.SUCCESS.newBuilder().toResult();
  575. }
  576. public List<Long> getFilterGoodsIds() {
  577. List<Long> filter_goodsIds = null;
  578. //获取当前ip地址
  579. if (StringUtil.getRealAddressByIP(ServletUtil.getClientIP(request)).contains("杭州")) {
  580. //过滤杭州Ai ChatGPT平台
  581. SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class)
  582. .eq(SysConfig::getSysKey, Constant.LOCATION_FOR_GOODS_IDS).last("limit 1"));
  583. if (sysConfig != null && StrUtil.isNotBlank(sysConfig.getSysValue())) {
  584. try {
  585. filter_goodsIds = Jsons.parseList(sysConfig.getSysValue(), Long.class);
  586. } catch (Exception e) {
  587. }
  588. }
  589. }
  590. return filter_goodsIds;
  591. }
  592. public Boolean isHzIp() {
  593. if (StringUtil.getRealAddressByIP(ServletUtil.getClientIP(request)).contains("杭州")) {
  594. return true;
  595. }
  596. return false;
  597. }
  598. /**
  599. * @param goodsId
  600. * @param type 1、普通,2、注册平台
  601. * @param yhsId 店铺id
  602. * @return
  603. */
  604. public String getPayMsgTime(Long goodsId, DateTime now, Integer type, Long yhsId) {
  605. Date startTime = null;
  606. StringBuilder notifyMsg = new StringBuilder();
  607. if (type == 1) {
  608. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  609. .eq(OrderDon::getGoodsId, goodsId)
  610. .notIn(OrderDon::getStatus, Constant.noOrderStatus).select(OrderDon::getCreatedTime, OrderDon::getPayTime, OrderDon::getId)
  611. .eq(yhsId != null, OrderDon::getYhsId, yhsId)
  612. .orderByDesc(OrderDon::getId)
  613. .last("limit 1"));
  614. if (orderDon == null) {
  615. notifyMsg.append("点击购买立即上车");
  616. return notifyMsg.toString();
  617. }
  618. startTime = orderDon.getPayTime() != null ? orderDon.getPayTime() : orderDon.getCreatedTime();
  619. } else if (type == 2) {
  620. RegisterOrderDon orderDon = registerOrderDonMapper.selectOne(Wrappers.lambdaQuery(RegisterOrderDon.class)
  621. .eq(RegisterOrderDon::getGoodsId, goodsId)
  622. .notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus).select(RegisterOrderDon::getCreatedTime, RegisterOrderDon::getPayTime, RegisterOrderDon::getId)
  623. .orderByDesc(RegisterOrderDon::getId)
  624. .last("limit 1"));
  625. if (orderDon == null) {
  626. return null;
  627. }
  628. startTime = orderDon.getPayTime() != null ? orderDon.getPayTime() : orderDon.getCreatedTime();
  629. }
  630. long hour = DateUtil.between(startTime, now, DateUnit.HOUR);
  631. if (hour == 0) {
  632. long minute = DateUtil.between(startTime, now, DateUnit.MINUTE);
  633. if (minute == 0) {
  634. notifyMsg.append("1分钟");
  635. } else {
  636. notifyMsg.append(minute);
  637. notifyMsg.append("分钟");
  638. }
  639. } else {
  640. if (hour > 3) {
  641. int hours = RandomUtil.randomInt(3) + 1;
  642. notifyMsg.append(hours);
  643. } else {
  644. notifyMsg.append(hour);
  645. }
  646. notifyMsg.append("小时");
  647. }
  648. notifyMsg.append("前有人购买");
  649. return notifyMsg.toString();
  650. }
  651. /**
  652. * 是否购买过特定平台
  653. */
  654. public Boolean isPaySpecificGoodsIds(String specificGoodsIdsStr, Long userId) {
  655. if (StrUtil.isNotBlank(specificGoodsIdsStr)) {
  656. try {
  657. Set<Long> specificGoodsIds = Jsons.parseSet(specificGoodsIdsStr, Long.class);
  658. Number number = beanSearcher.searchCount(OrderDon.class, MapUtils.builder().field(OrderDon::getGoodsId, specificGoodsIds).op(Operator.InList)
  659. .field(OrderDon::getUserId, userId)
  660. .field(OrderDon::getStatus, Constant.noOrderAllStatus).op(Operator.NotIn)
  661. .build());
  662. if (number.intValue() == 0) {
  663. return false;
  664. }
  665. return true;
  666. } catch (Exception e) {
  667. }
  668. }
  669. return false;
  670. }
  671. /**
  672. * 设置平台 特定价格
  673. */
  674. public void setSpecificPrice(GoodsFrontListView data, Long fUid, Long dsUserId) {
  675. if (fUid != null) {
  676. if (data.getIsSp()) {
  677. //查找所有特定规格最小价格 满足条件
  678. String specificGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "specific:" + data.getId();
  679. Object speciObj = redisService.get(specificGoodsKey);
  680. List<GoodsDonSku> specificSkus = null;
  681. if (speciObj != null) {
  682. specificSkus = Jsons.parseList(speciObj, GoodsDonSku.class);
  683. } else {
  684. specificSkus = goodsDonSkuMapper.selectList(
  685. Wrappers.lambdaQuery(GoodsDonSku.class)
  686. .eq(GoodsDonSku::getGoodsId, data.getId())
  687. .eq(GoodsDonSku::getStatus, true)
  688. .gt(GoodsDonSku::getSpecificPrice, 0));
  689. redisService.setNx(specificGoodsKey, specificSkus, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  690. }
  691. specificSkus.forEach(specificSku -> {
  692. if (StrUtil.isNotEmpty(specificSku.getUserOwns()) && (dsUserId == null || !specificSku.getUserOwns().contains(dsUserId.toString()))) {
  693. return;
  694. }
  695. String specificGoodsIdsStr = specificSku.getSpecificGoodsIds();
  696. if (isPaySpecificGoodsIds(specificGoodsIdsStr, fUid)) {
  697. BigDecimal specificPrice = data.getSpecificPrice();
  698. BigDecimal skuSpecificPrice = specificSku.getSpecificPrice();
  699. Long specificSkuId = specificSku.getId();
  700. if (specificPrice == null) {
  701. data.setSpecificPrice(skuSpecificPrice);
  702. data.setSpecificSkuId(specificSkuId);
  703. } else if (specificPrice.compareTo(skuSpecificPrice) > 0) {
  704. data.setSpecificPrice(skuSpecificPrice);
  705. data.setSpecificSkuId(specificSkuId);
  706. }
  707. }
  708. });
  709. List<GoodsDonSkuFrontView> skuList = data.getSkuList();
  710. if (CollUtil.isNotEmpty(skuList)) {
  711. skuList.forEach(sku -> {
  712. //设置特定价格
  713. if (!isPaySpecificGoodsIds(sku.getSpecificGoodsIds(), fUid)) {
  714. sku.setSpecificGoodsIds(null);
  715. sku.setSpecificPrice(null);
  716. }
  717. });
  718. }
  719. }
  720. }
  721. }
  722. /**
  723. * 设置平台 特定价格
  724. */
  725. public void setGcSpecificPrice(GoodsFrontListCategoryView data, Long fUid, Long dsUserId) {
  726. if (fUid != null) {
  727. if (data.getIsSp()) {
  728. //查找所有特定规格最小价格 满足条件
  729. String specificGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "specific:" + data.getId();
  730. Object speciObj = redisService.get(specificGoodsKey);
  731. List<GoodsDonSku> specificSkus = null;
  732. if (speciObj != null) {
  733. specificSkus = Jsons.parseList(speciObj, GoodsDonSku.class);
  734. } else {
  735. specificSkus = goodsDonSkuMapper.selectList(
  736. Wrappers.lambdaQuery(GoodsDonSku.class)
  737. .eq(GoodsDonSku::getGoodsId, data.getId())
  738. .eq(GoodsDonSku::getStatus, true)
  739. .gt(GoodsDonSku::getSpecificPrice, 0));
  740. redisService.setNx(specificGoodsKey, specificSkus, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  741. }
  742. specificSkus.forEach(specificSku -> {
  743. if (StrUtil.isNotEmpty(specificSku.getUserOwns()) && (dsUserId == null || !specificSku.getUserOwns().contains(dsUserId.toString()))) {
  744. return;
  745. }
  746. String specificGoodsIdsStr = specificSku.getSpecificGoodsIds();
  747. if (isPaySpecificGoodsIds(specificGoodsIdsStr, fUid)) {
  748. BigDecimal specificPrice = data.getSpecificPrice();
  749. BigDecimal skuSpecificPrice = specificSku.getSpecificPrice();
  750. Long specificSkuId = specificSku.getId();
  751. if (specificPrice == null) {
  752. data.setSpecificPrice(skuSpecificPrice);
  753. data.setSpecificSkuId(specificSkuId);
  754. } else if (specificPrice.compareTo(skuSpecificPrice) > 0) {
  755. data.setSpecificPrice(skuSpecificPrice);
  756. data.setSpecificSkuId(specificSkuId);
  757. }
  758. }
  759. });
  760. List<GoodsDonSkuFrontView> skuList = data.getSkuList();
  761. if (CollUtil.isNotEmpty(skuList)) {
  762. skuList.forEach(sku -> {
  763. //设置特定价格
  764. if (!isPaySpecificGoodsIds(sku.getSpecificGoodsIds(), fUid)) {
  765. sku.setSpecificGoodsIds(null);
  766. sku.setSpecificPrice(null);
  767. }
  768. });
  769. }
  770. }
  771. }
  772. }
  773. /**
  774. * 获取特定规格
  775. */
  776. public List<GoodsDonSku> getSpecificSku(Long goodsId) {
  777. //查找所有特定规格最小价格 满足条件
  778. String specificGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "specific:" + goodsId;
  779. Object speciObj = redisService.get(specificGoodsKey);
  780. List<GoodsDonSku> specificSkus = null;
  781. if (speciObj != null) {
  782. specificSkus = Jsons.parseList(speciObj, GoodsDonSku.class);
  783. } else {
  784. specificSkus = goodsDonSkuMapper.selectList(
  785. Wrappers.lambdaQuery(GoodsDonSku.class)
  786. .eq(GoodsDonSku::getGoodsId, goodsId)
  787. .eq(GoodsDonSku::getStatus, true)
  788. .gt(GoodsDonSku::getSpecificPrice, 0));
  789. redisService.setNx(specificGoodsKey, specificSkus, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  790. }
  791. specificSkus = Optional.ofNullable(specificSkus).orElse(new ArrayList<>());
  792. return specificSkus;
  793. }
  794. /**
  795. * 设置首页配置 涉及平台特定价格
  796. */
  797. public void setHomeManageSpecific(List<HomeManagementFrontView> list, Long fUid, Long dsUserId) {
  798. DateTime now = DateTime.now();
  799. list.forEach(data -> {
  800. if (data.getIsSp() != null && data.getIsSp() && fUid != null) {
  801. //查找所有特定规格最小价格 满足条件
  802. List<GoodsDonSku> specificSkus = getSpecificSku(data.getGoodsId());
  803. specificSkus.forEach(specificSku -> {
  804. if (StrUtil.isNotEmpty(specificSku.getUserOwns()) && (dsUserId == null || !specificSku.getUserOwns().contains(dsUserId.toString()))) {
  805. return;
  806. }
  807. String specificGoodsIdsStr = specificSku.getSpecificGoodsIds();
  808. if (isPaySpecificGoodsIds(specificGoodsIdsStr, fUid)) {
  809. BigDecimal specificPrice = data.getSpecificPrice();
  810. BigDecimal skuSpecificPrice = specificSku.getSpecificPrice();
  811. Long specificSkuId = specificSku.getId();
  812. if (specificPrice == null) {
  813. data.setSpecificPrice(skuSpecificPrice);
  814. data.setSpecificSkuId(specificSkuId);
  815. } else if (specificPrice.compareTo(skuSpecificPrice) > 0) {
  816. data.setSpecificPrice(skuSpecificPrice);
  817. data.setSpecificSkuId(specificSkuId);
  818. }
  819. }
  820. });
  821. }
  822. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  823. .eq(OrderDon::getGoodsId, data.getGoodsId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  824. data.setSold(sold + data.getSold());
  825. data.setNotifyMsg(getPayMsgTime(data.getGoodsId(), now, 1, null));
  826. });
  827. }
  828. public void setHomeConfigGoods(Map<String, List<HomeManagementFrontView>> map, HomeManagementConfig.Type type, List<HomeManagementFrontView> list, AtomicLong dsUserId, AtomicBoolean existsChannel, Long fUid) throws Exception {
  829. //设置特定价格
  830. if (CollUtil.isNotEmpty(list)) {
  831. list = list.stream().filter(data -> {
  832. if (data.getIsOwns()) {
  833. if (dsUserId.get() == 0 && fUid != null && existsChannel.get()) {
  834. Long sharedId = userService.getSharedIdByUserId(fUid);
  835. if (sharedId == 0) {
  836. existsChannel.set(false);
  837. }
  838. dsUserId.set(sharedId);
  839. }
  840. GoodsDonSku minPriceUserOwnsSku = goodsDonSkuMapper.checkAndReturnMinPriceUserOwnsSku(data.getGoodsId(), dsUserId.get());
  841. if (minPriceUserOwnsSku == null) {
  842. return false;
  843. }
  844. data.setPrice(minPriceUserOwnsSku.getPrice());
  845. data.setMonths(minPriceUserOwnsSku.getMonths());
  846. data.setDays(minPriceUserOwnsSku.getDays());
  847. }
  848. return true;
  849. }).collect(Collectors.toList());
  850. setHomeManageSpecific(list, fUid, dsUserId.get());
  851. map.put(type.name(), list);
  852. }
  853. }
  854. /**
  855. * 店铺平台列表
  856. */
  857. @GetMapping("/get/shop/goods/{yhsId}")
  858. public Result<List<YhShopGoodsFrontView>> getShopGoods(@PathVariable Long yhsId) {
  859. String yhShopCacheKey = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getEnvName() + yhsId;
  860. Object value = redisService.get(yhShopCacheKey);
  861. List<YhShopGoodsFrontView> list = null;
  862. if (value != null) {
  863. try {
  864. list = Jsons.parseList(value, YhShopGoodsFrontView.class);
  865. } catch (Exception e) {
  866. }
  867. }
  868. if (CollUtil.isEmpty(list)) {
  869. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  870. builder.put("condition", String.format("and yhs_id = %s", yhsId));
  871. list = beanSearcher.searchAll(YhShopGoodsFrontView.class, builder.build());
  872. redisService.setNx(yhShopCacheKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  873. }
  874. DateTime now = DateTime.now();
  875. list.forEach(data->{
  876. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  877. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  878. data.setSold(sold + data.getSold());
  879. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, yhsId));
  880. });
  881. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  882. }
  883. /**
  884. * 店铺平台详情
  885. */
  886. @GetMapping("/get/shop/goods/detail/{yhsId}/{goodsId}")
  887. public Result<YhShopGoodsFrontView> getGoodsDetailByYhsId(@PathVariable Long yhsId, @PathVariable Long goodsId) {
  888. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  889. String condition = String.format("and yhs_id = %s and ys.goods_id = %s", yhsId, goodsId);
  890. builder.put("condition", condition);
  891. YhShopGoodsFrontView goodsDetailView = beanSearcher.searchFirst(YhShopGoodsFrontView.class, builder.build());
  892. if (goodsDetailView == null) {
  893. throw BusinessRuntimeException.getInstance("该平台不存在或已下架,请刷新页面");
  894. }
  895. goodsDetailView.setSkuList(beanSearcher.searchAll(YhShopGoodsSkuFrontView.class, MapUtils.builder().put("condition", condition).orderBy(YhShopGoodsSkuFrontView::getSorted).asc().build()));
  896. goodsDetailView.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, goodsDetailView.getId()).build()));
  897. OrderCommentFrontView comments = beanSearcher.searchFirst(OrderCommentFrontView.class, MapUtils.builder()
  898. .field(OrderCommentFrontView::getGoodsId, goodsDetailView.getId())
  899. .orderBy(OrderCommentFrontView::getCommentTime).desc()
  900. .orderBy(OrderCommentFrontView::getId).desc()
  901. .build());
  902. goodsDetailView.setComments(comments);
  903. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsDetailView);
  904. }
  905. /**
  906. * 根据分类获取对应平台
  907. */
  908. @GetMapping("/get/categoryGoods")
  909. public Result<? extends Object> getGoodsByCategory(Long cgy, String customId, String dsCode) {
  910. if (StrUtil.isNotBlank(customId)) {
  911. Result<List<YhShopGoodsFrontCategoryView>> shopGoods = getShopCategoryGoods(yhShopFrontService.getYhsIdByCustomId(customId), cgy);
  912. return shopGoods;
  913. }
  914. Long fUid = StpUserUtil.getUserIdAfterLogin();
  915. String categoryGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "category:goods";
  916. if (cgy != null) {
  917. categoryGoodsKey += ":" + cgy;
  918. }
  919. //List<Long> filter_goodsIds = getFilterGoodsIds();
  920. List<Long> filter_goodsIds = null;
  921. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  922. categoryGoodsKey += ":hz-ip";
  923. }
  924. Object o = redisService.get(categoryGoodsKey);
  925. if (o == null) {
  926. DateTime now = DateTime.now();
  927. MapBuilder mapBuilder = MapUtils.flatBuilder(request.getParameterMap());
  928. if (cgy != null) {
  929. String condition = String.format(" and gcr.category = %s", cgy);
  930. mapBuilder.put("condition", condition);
  931. }
  932. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  933. mapBuilder.field(GoodsFrontListCategoryView::getId, filter_goodsIds).op(Operator.NotIn);
  934. }
  935. List<GoodsFrontListCategoryView> goodsFrontListViews = beanSearcher.searchAll(GoodsFrontListCategoryView.class, mapBuilder.build());
  936. goodsFrontListViews.forEach(data -> {
  937. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  938. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  939. data.setSold(sold + data.getSold());
  940. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, null));
  941. });
  942. redisService.setNx(categoryGoodsKey, goodsFrontListViews, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 10 * 60);
  943. o = redisService.get(categoryGoodsKey);
  944. }
  945. List<GoodsFrontListCategoryView> goodsFrontListViews = Jsons.parseList(o, GoodsFrontListCategoryView.class);
  946. AtomicLong dsUserId = new AtomicLong();
  947. AtomicBoolean existsChannel = new AtomicBoolean(true);
  948. if (StrUtil.isNotBlank(dsCode)) {
  949. UserSharedDto userSharedDto = userService.getUserShredDtoByDsCode(dsCode);
  950. dsUserId.set(userSharedDto.getSharedId());
  951. }
  952. goodsFrontListViews = goodsFrontListViews.stream().filter(data -> {
  953. if (data.getIsOwns()) {
  954. if (dsUserId.get() == 0 && fUid != null && existsChannel.get()) {
  955. Long sharedId = userService.getSharedIdByUserId(fUid);
  956. if (sharedId == 0) {
  957. existsChannel.set(false);
  958. }
  959. dsUserId.set(sharedId);
  960. }
  961. GoodsDonSku minPriceUserOwnsSku = goodsDonSkuMapper.checkAndReturnMinPriceUserOwnsSku(data.getId(), dsUserId.get());
  962. if (minPriceUserOwnsSku == null) {
  963. return false;
  964. }
  965. data.setPrice(minPriceUserOwnsSku.getPrice());
  966. data.setMonths(minPriceUserOwnsSku.getMonths());
  967. data.setDays(minPriceUserOwnsSku.getDays());
  968. }
  969. //设置特定价格
  970. setGcSpecificPrice(data, fUid, dsUserId.get());
  971. return true;
  972. }).collect(Collectors.toList());
  973. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsFrontListViews);
  974. }
  975. @GetMapping("/get/shop/categoryGoods/{yhsId}")
  976. public Result<List<YhShopGoodsFrontCategoryView>> getShopCategoryGoods(@PathVariable Long yhsId, Long category) {
  977. String categoryGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getEnvName() + yhsId + ":category";
  978. if (category != null) {
  979. categoryGoodsKey += ":" + category;
  980. }
  981. Object value = redisService.get(categoryGoodsKey);
  982. if (value == null) {
  983. DateTime now = DateTime.now();
  984. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  985. builder.put("yhsId", String.format(" and yhs_id = %s", yhsId));
  986. String condition = StrUtil.EMPTY;
  987. if (category != null) {
  988. condition += String.format(" and gcr.category = %s", category);
  989. }
  990. builder.put("condition", condition);
  991. List<YhShopGoodsFrontCategoryView> list = beanSearcher.searchAll(YhShopGoodsFrontCategoryView.class, builder.build());
  992. list.forEach(data->{
  993. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  994. .eq(OrderDon::getGoodsId, data.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  995. data.setSold(sold + data.getSold());
  996. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, yhsId));
  997. });
  998. redisService.setNx(categoryGoodsKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 5 * 60);
  999. value = redisService.get(categoryGoodsKey);
  1000. }
  1001. List<YhShopGoodsFrontCategoryView> list = Jsons.parseList(value, YhShopGoodsFrontCategoryView.class);
  1002. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  1003. }
  1004. /**
  1005. * 获取设备活动详情
  1006. */
  1007. @GetMapping("/get/equipment/activity")
  1008. public Result<EquipmentActivityReduce> getEpActivity() {
  1009. DateTime now = DateTime.now();
  1010. EquipmentActivityReduce equipmentActivityReduce = beanSearcher.searchFirst(EquipmentActivityReduce.class, MapUtils.builder()
  1011. .field(EquipmentActivityReduce::getSt, now).op(Operator.LessEqual)
  1012. .field(EquipmentActivityReduce::getEt, now).op(Operator.GreaterThan)
  1013. .field(EquipmentActivityReduce::getDeleted, 1)
  1014. .onlySelect(EquipmentActivityReduce::getF, EquipmentActivityReduce::getQ, EquipmentActivityReduce::getTq, EquipmentActivityReduce::getSt, EquipmentActivityReduce::getEt)
  1015. .build());
  1016. return GatewayResponse.SUCCESS.newBuilder().toResult(equipmentActivityReduce);
  1017. }
  1018. /**
  1019. * 获取租赁商品列表
  1020. */
  1021. @GetMapping("/get/deposit")
  1022. public Result<? extends Object> getDepositGoods() {
  1023. String depositGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "deposit";
  1024. Object o = redisService.get(depositGoodsKey);
  1025. if (o == null) {
  1026. List<GoodsDon> depositList = beanSearcher.searchAll(GoodsDon.class, MapUtils.builder()
  1027. .field(GoodsDon::getType, 4)
  1028. .field(GoodsDon::getStatus, true)
  1029. .field(GoodsDon::getDeleted, true)
  1030. .onlySelect(GoodsDon::getId, GoodsDon::getTitle, GoodsDon::getLogo)
  1031. .build());
  1032. if (CollUtil.isEmpty(depositList)) {
  1033. return GatewayResponse.SUCCESS.newBuilder().toResult();
  1034. }
  1035. redisService.setNx(depositGoodsKey, depositList, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 60 * RandomUtil.randomInt(10));
  1036. o = redisService.get(depositGoodsKey);
  1037. }
  1038. List<GoodsDon> depositList = Jsons.parseList(o, GoodsDon.class);
  1039. return GatewayResponse.SUCCESS.newBuilder().toResult(depositList);
  1040. }
  1041. /**
  1042. * 获取pc广告配置
  1043. */
  1044. @GetMapping("/get/pc/homeManage")
  1045. public Result<Map<String, List<HomeManagementFrontView>>> getPcHomeManagementFrontPage() {
  1046. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "pc:homeManage";
  1047. //移除奈飞客厅 临时
  1048. Long filterConfigId = null;
  1049. // if (isHzIp()) {
  1050. // key += ":hz_ip";
  1051. // filterConfigId = 55l;
  1052. // }
  1053. Object o = redisService.get(key);
  1054. if (o == null) {
  1055. MapBuilder builder = MapUtils.builder()
  1056. .field(HomeManagementFrontView::getAdType, List.of(HomeManagementConfig.AdType.pc.name(), HomeManagementConfig.AdType.pcSec.name(), HomeManagementConfig.AdType.pcDis.name())).op(Operator.InList);
  1057. if (filterConfigId != null) {
  1058. builder.field(HomeManagementFrontView::getHomeManageConfigId, filterConfigId).op(Operator.NotEqual);
  1059. }
  1060. List<HomeManagementFrontView> homeManagementFrontViews = beanSearcher.searchAll(HomeManagementFrontView.class, builder.build());
  1061. Map<HomeManagementConfig.AdType, List<HomeManagementFrontView>> map = homeManagementFrontViews.stream().collect(Collectors.groupingBy(HomeManagementFrontView::getAdType));
  1062. redisService.setNx(key, map, 50 * 60l);
  1063. o = redisService.get(key);
  1064. }
  1065. Map<String, List<HomeManagementFrontView>> map = Jsons.parseObject(o, Map.class);
  1066. return GatewayResponse.SUCCESS.newBuilder().toResult(map);
  1067. }
  1068. /**
  1069. * 获取pc实物配置
  1070. */
  1071. @GetMapping("/get/pc/realGoodsConfig")
  1072. public Result<? extends Object> getPcRealGoodsConfig() throws Exception {
  1073. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "pc:realHomePage";
  1074. Object o = redisService.get(key);
  1075. List<HomeManagementFrontView> list = null;
  1076. if (o == null) {
  1077. list = goodsDonFrontService.getRealGoodsConfig();
  1078. redisService.setNx(key, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 300);
  1079. }
  1080. if (list == null && o != null) {
  1081. list = Jsons.parseList(o, HomeManagementFrontView.class);
  1082. }
  1083. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  1084. }
  1085. /**
  1086. * 百亿补贴
  1087. * 补贴规格列表
  1088. */
  1089. @GetMapping("/get/subsidy")
  1090. public Result<List<GoodsDonSkuSubsidyFrontView>> getSubsidyGoods() {
  1091. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "subsidy";
  1092. Object o = redisService.get(key);
  1093. if (o == null) {
  1094. List<GoodsDonSkuSubsidyFrontView> subsidyList = beanSearcher.searchAll(GoodsDonSkuSubsidyFrontView.class, MapUtils.builder().build());
  1095. subsidyList.forEach(e -> {
  1096. Optional.ofNullable(beanSearcher.searchFirst(GoodsDonSkuSubsidyDetailView.class, MapUtils.builder().field(GoodsDonSkuSubsidyDetailView::getGoodsId, e.getId()).field(GoodsDonSkuSubsidyDetailView::getPrice, e.getPrice()).build())).ifPresent(sku -> {
  1097. e.setOriPrice(sku.getOriPrice());
  1098. e.setMonths(sku.getMonths());
  1099. e.setDays(sku.getDays());
  1100. e.setSkuId(sku.getId());
  1101. e.setPicture(sku.getSubsidyPicture());
  1102. });
  1103. });
  1104. redisService.setNx(key, subsidyList, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 60 * RandomUtil.randomInt(10));
  1105. o = redisService.get(key);
  1106. }
  1107. List<GoodsDonSkuSubsidyFrontView> subsidyList = Jsons.parseList(o, GoodsDonSkuSubsidyFrontView.class);
  1108. if (CollUtil.isEmpty(subsidyList)) {
  1109. return GatewayResponse.SUCCESS.newBuilder().toResult(subsidyList);
  1110. }
  1111. Long userId = StpUserUtil.getUserIdAfterLogin();
  1112. if (userId != null) {
  1113. List<Long> relationUserIdList = userBindRelationService.getRelationUserIdList(userId, null);
  1114. //过滤用户已经购买过平台
  1115. List<Long> goodsIds = orderDonMapper.getPayGoodsIds(relationUserIdList);
  1116. if (CollUtil.isNotEmpty(goodsIds)) {
  1117. subsidyList = subsidyList.stream().filter(data -> !goodsIds.contains(data.getId())).collect(Collectors.toList());
  1118. }
  1119. }
  1120. return GatewayResponse.SUCCESS.newBuilder().toResult(subsidyList);
  1121. }
  1122. /**
  1123. * 百亿补贴规格详情
  1124. */
  1125. @GetMapping("/get/subsidy/detail/{id}")
  1126. public Result<GoodsDonSubsidyDetailView> getSubsidyDetailById(@PathVariable Long id) throws Exception {
  1127. String goodsDetailKey = RedisService.key.YH_HOME_PAGHE_CACHE.getEnvName() + "subsidy:detail:" + id;
  1128. Object o = redisService.get(goodsDetailKey);
  1129. GoodsDonSubsidyDetailView subsidyDetail = null;
  1130. if (o == null) {
  1131. subsidyDetail = beanSearcher.searchFirst(GoodsDonSubsidyDetailView.class, MapUtils.builder().field(GoodsDonSubsidyDetailView::getId, id).build());
  1132. if (subsidyDetail == null) {
  1133. return GatewayResponse.SUCCESS.newBuilder().toResult();
  1134. }
  1135. subsidyDetail.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, subsidyDetail.getId()).build()));
  1136. MapBuilder builder = MapUtils.builder().field(GoodsDonSkuSubsidyDetailView::getGoodsId, subsidyDetail.getId());
  1137. subsidyDetail.setSkuList(beanSearcher.searchAll(GoodsDonSkuSubsidyDetailView.class, builder.build()));
  1138. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  1139. .eq(OrderDon::getGoodsId, subsidyDetail.getId()).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  1140. subsidyDetail.setSold(sold + subsidyDetail.getSold());
  1141. redisService.setNx(goodsDetailKey, Jsons.toJson(subsidyDetail), RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 60 * RandomUtil.randomInt(10));
  1142. }
  1143. if (subsidyDetail == null) {
  1144. o = redisService.get(goodsDetailKey);
  1145. subsidyDetail = Jsons.parseObject(o.toString(), GoodsDonSubsidyDetailView.class);
  1146. }
  1147. return GatewayResponse.SUCCESS.newBuilder().toResult(subsidyDetail);
  1148. }
  1149. }