GoodsDonController.java 54 KB

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