GoodsDonController.java 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  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.GiftCardGoodsSkuMapper;
  18. import com.cyksj.mapper.GoodsDonSkuMapper;
  19. import com.cyksj.mapper.OrderDonMapper;
  20. import com.cyksj.mapper.RegisterOrderDonMapper;
  21. import com.cyksj.mapper.manage.coupon.CouponUserMapper;
  22. import com.cyksj.mapper.shop.YhShopGoodsSkuMapper;
  23. import com.cyksj.mapper.sys.SysConfigMapper;
  24. import com.cyksj.model.entity.*;
  25. import com.cyksj.model.request.ClickRecordReq;
  26. import com.cyksj.model.request.GoodsSubscribeAlertReq;
  27. import com.cyksj.model.views.*;
  28. import com.cyksj.redis.RedisService;
  29. import com.cyksj.service.goods.GoodsDonFrontService;
  30. import com.cyksj.service.goods.UserSubscribeAlertGoodsRecordService;
  31. import com.cyksj.service.home.ClickRecordFrontService;
  32. import com.cyksj.service.shop.YhShopFrontService;
  33. import com.cyksj.service.translations.BackInfoTranslationsFrontService;
  34. import com.cyksj.service.user.UserBindRelationService;
  35. import com.cyksj.service.user.UserService;
  36. import com.cyksj.service.verify.ApiVerifyService;
  37. import com.cyksj.web.util.StpUserUtil;
  38. import com.ejlchina.searcher.BeanSearcher;
  39. import com.ejlchina.searcher.SearchResult;
  40. import com.ejlchina.searcher.param.Operator;
  41. import com.ejlchina.searcher.util.MapBuilder;
  42. import com.ejlchina.searcher.util.MapUtils;
  43. import lombok.RequiredArgsConstructor;
  44. import lombok.extern.slf4j.Slf4j;
  45. import org.springframework.web.bind.annotation.*;
  46. import javax.servlet.http.HttpServletRequest;
  47. import java.math.BigDecimal;
  48. import java.util.*;
  49. import java.util.concurrent.ConcurrentHashMap;
  50. import java.util.concurrent.atomic.AtomicBoolean;
  51. import java.util.concurrent.atomic.AtomicLong;
  52. import java.util.stream.Collectors;
  53. import java.util.stream.Stream;
  54. /**
  55. * @author chan
  56. * @date 2022/9/26 5:33 PM
  57. */
  58. @Slf4j
  59. @RestController
  60. @RequestMapping("/applets/goods")
  61. @RequiredArgsConstructor
  62. public class GoodsDonController {
  63. private final HttpServletRequest request;
  64. private final BeanSearcher beanSearcher;
  65. private final OrderDonMapper orderDonMapper;
  66. private final RegisterOrderDonMapper registerOrderDonMapper;
  67. private final GoodsDonSkuMapper goodsDonSkuMapper;
  68. private final YhShopGoodsSkuMapper yhShopGoodsSkuMapper;
  69. private final RedisService redisService;
  70. private final SysConfigMapper sysConfigMapper;
  71. private final ClickRecordFrontService clickRecordFrontService;
  72. private final YhShopFrontService yhShopFrontService;
  73. private final UserService userService;
  74. private final CouponUserMapper couponUserMapper;
  75. private final GoodsDonFrontService goodsDonFrontService;
  76. private final UserBindRelationService userBindRelationService;
  77. private final GiftCardGoodsSkuMapper giftCardGoodsSkuMapper;
  78. private final BackInfoTranslationsFrontService backInfoTranslationsFrontService;
  79. private final ApiVerifyService apiVerifyService;
  80. private final UserSubscribeAlertGoodsRecordService userSubscribeAlertGoodsRecordService;
  81. /**
  82. * 获取平台商品分类
  83. */
  84. @GetMapping("/get/category")
  85. public Result<List<GoodsDonCategoryFrontView>> getCategory(String customId, Boolean isFilter) {
  86. Long yhsId = null;
  87. if (StrUtil.isNotBlank(customId)) {
  88. yhsId = yhShopFrontService.getYhsIdByCustomId(customId);
  89. }
  90. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "category";
  91. if (yhsId != null) {
  92. key += ":yhShop:" + yhsId;
  93. }
  94. Long fUid = StpUserUtil.getUserIdAfterLogin();
  95. Boolean filterGoodsIds = isFilterGoodsIds(fUid, isFilter);
  96. if (filterGoodsIds) {
  97. key += ":filter-ip";
  98. }
  99. String lang = request.getHeader("lang");
  100. AtomicBoolean language = new AtomicBoolean(false);
  101. if (lang != null && !StrUtil.equals(UserPageLanguage.Language.zh_CN.name(), lang)) {
  102. key += ":" + lang;
  103. language.set(true);
  104. }
  105. Object o = redisService.get(key);
  106. List<GoodsDonCategoryFrontView> list;
  107. if (o == null) {
  108. MapBuilder mapBuilder = MapUtils.flatBuilder(request.getParameterMap());
  109. 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 and g.deleted and g.status) > 0";
  110. String extraSql;
  111. if (yhsId != null) {
  112. extraSql = 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);
  113. } else {
  114. extraSql = StrUtil.EMPTY;
  115. }
  116. if (filterGoodsIds) {
  117. extraSql += " and g.is_filter = false";
  118. }
  119. condition = String.format(condition, extraSql);
  120. mapBuilder.put("condition", condition);
  121. list = beanSearcher.searchAll(GoodsDonCategoryFrontView.class, mapBuilder
  122. .build());
  123. //多语言
  124. if (language.get()) {
  125. backInfoTranslationsFrontService.setListCategoryNameLanguage(list, lang);
  126. }
  127. redisService.setNx(key, list, 30 * 60l);
  128. o = redisService.get(key);
  129. }
  130. List<Long> gptBuyGids = getGptMirrorGiveawayBuyGids();
  131. list = Jsons.parseList(o, GoodsDonCategoryFrontView.class);
  132. for (GoodsDonCategoryFrontView data : list) {
  133. List<GoodsDonCategoryGoodsView> goodsDonCategoryGoodsViews = beanSearcher.searchAll(GoodsDonCategoryGoodsView.class, MapUtils.builder().field(GoodsDonCategoryGoodsView::getCategory, data.getId()).field(GoodsDonCategoryGoodsView::getStatus, 1).onlySelect(GoodsDonCategoryGoodsView::getGoodsId).build());
  134. List<Long> collect = goodsDonCategoryGoodsViews.stream().map(GoodsDonCategoryGoodsView::getGoodsId).distinct().collect(Collectors.toList());
  135. if (CollUtil.isNotEmpty(gptBuyGids) && gptBuyGids.containsAll(collect)) {
  136. data.setIsGptGift(true);
  137. }
  138. }
  139. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  140. }
  141. /**
  142. * 商品搜索
  143. */
  144. @GetMapping("/search")
  145. public Result<SearchResult<GoodsDonSearchView>> search() {
  146. SearchResult<GoodsDonSearchView> search = beanSearcher.search(GoodsDonSearchView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
  147. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  148. }
  149. /**
  150. * 获取商品列表
  151. */
  152. @GetMapping("/get")
  153. public Result<SearchResult<GoodsDon>> get() {
  154. SearchResult<GoodsDon> search = beanSearcher.search(GoodsDon.class, MapUtils.flatBuilder(request.getParameterMap())
  155. .field(GoodsDon::getStatus, true)
  156. .field(GoodsDon::getDeleted, true)
  157. .orderBy(GoodsDon::getSortBy).asc()
  158. .orderBy(GoodsDon::getId).asc()
  159. .build()
  160. );
  161. search.getDataList().forEach(goods -> {
  162. GoodsDonSku goodsDonSku = goodsDonSkuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class).eq(GoodsDonSku::getGoodsId, goods.getId())
  163. .eq(GoodsDonSku::getStatus, true)
  164. .orderByAsc(GoodsDonSku::getPrice)
  165. .last("limit 1")
  166. .select(GoodsDonSku::getPrice));
  167. Optional.ofNullable(goodsDonSku).ifPresent(sku -> {
  168. goods.setPrice(goodsDonSku.getPrice());
  169. goods.setSoldNum(getGoodsSold(goods.getId(), goods.getVirtualSold()));
  170. });
  171. });
  172. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  173. }
  174. /**
  175. * 推广未登录,虚拟商品列表
  176. */
  177. @GetMapping("/get/popularize")
  178. @Deprecated
  179. public Result<SearchResult<GoodsDonPopularizeView>> getPopularizeVipGoods() {
  180. SearchResult<GoodsDonPopularizeView> search = beanSearcher.search(GoodsDonPopularizeView.class, MapUtils.flatBuilder(request.getParameterMap())
  181. .field(GoodsDonPopularizeView::getStatus, true)
  182. .field(GoodsDonPopularizeView::getDeleted, true)
  183. .field(GoodsDonPopularizeView::getType, 1)
  184. .orderBy(GoodsDonPopularizeView::getSortBy).asc()
  185. .orderBy(GoodsDonPopularizeView::getId).asc()
  186. .build());
  187. DateTime now = DateTime.now();
  188. search.getDataList().forEach(data -> {
  189. data.setSpecs(beanSearcher.searchFirst(GoodsDonSpec.class, MapUtils.builder().field(GoodsDonSpec::getGoodsId, data.getId()).build()));
  190. data.setSkuList(beanSearcher.searchAll(GoodsDonSku.class, MapUtils.builder().field(GoodsDonSku::getGoodsId, data.getId()).field(GoodsDonSku::getStatus, true).orderBy(GoodsDonSku::getPrice).asc().build()));
  191. data.setSold(getGoodsSold(data.getId(), data.getVirtualSold()));
  192. Optional.ofNullable(data.getSkuList()).ifPresent(skuList -> {
  193. if (skuList.size() > 0) {
  194. GoodsDonSku goodsDonSku = skuList.get(0);
  195. data.setPrice(goodsDonSku.getPrice());
  196. data.setMonths(goodsDonSku.getMonths());
  197. }
  198. });
  199. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  200. .eq(OrderDon::getGoodsId, data.getId())
  201. .notIn(OrderDon::getStatus, Constant.noOrderStatus).select(OrderDon::getCreatedTime, OrderDon::getPayTime, OrderDon::getId)
  202. .orderByDesc(OrderDon::getId)
  203. .last("limit 1"));
  204. StringBuilder notifyMsg = new StringBuilder();
  205. if (orderDon == null) {
  206. notifyMsg.append("点击购买立即上车");
  207. data.setNotifyMsg(notifyMsg.toString());
  208. return;
  209. } else {
  210. Date payTime = orderDon.getPayTime();
  211. Date startTime = payTime != null ? payTime : orderDon.getCreatedTime();
  212. long hour = DateUtil.between(startTime, now, DateUnit.HOUR);
  213. if (hour == 0) {
  214. long minute = DateUtil.between(startTime, now, DateUnit.MINUTE);
  215. if (minute == 0) {
  216. notifyMsg.append("1分钟");
  217. } else {
  218. notifyMsg.append(minute);
  219. notifyMsg.append("分钟");
  220. }
  221. } else {
  222. if (hour > 3) {
  223. int hours = RandomUtil.randomInt(3) + 1;
  224. notifyMsg.append(hours);
  225. } else {
  226. notifyMsg.append(hour);
  227. }
  228. notifyMsg.append("小时");
  229. }
  230. }
  231. notifyMsg.append("前有人购买");
  232. data.setNotifyMsg(notifyMsg.toString());
  233. });
  234. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  235. }
  236. /**
  237. * 官网首页 平台列表
  238. */
  239. @GetMapping("/get/list")
  240. @Deprecated
  241. public Result<? extends Object> getGoodsList(String customId) {
  242. if (StrUtil.isNotBlank(customId)) {
  243. Result<List<YhShopGoodsFrontView>> shopGoods = getShopGoods(yhShopFrontService.getYhsIdByCustomId(customId));
  244. return shopGoods;
  245. }
  246. //List<Long> filter_goodsIds = getFilterGoodsIds();
  247. List<Long> filter_goodsIds = null;
  248. String pcCacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "PC";
  249. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  250. pcCacheKey += ":hz-ip";
  251. }
  252. Long fUid = StpUserUtil.getUserIdAfterLogin();
  253. Object o = redisService.get(pcCacheKey);
  254. List<GoodsFrontListView> list = null;
  255. if (o == null) {
  256. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  257. if (CollUtil.isNotEmpty(filter_goodsIds)) {
  258. builder.field(GoodsFrontListView::getId, filter_goodsIds).op(Operator.NotIn);
  259. }
  260. builder.field(GoodsFrontListView::getType, 1);
  261. list = beanSearcher.searchAll(GoodsFrontListView.class, builder.build());
  262. redisService.setNx(pcCacheKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  263. o = redisService.get(pcCacheKey);
  264. }
  265. if (list == null) {
  266. list = Jsons.parseList(o, GoodsFrontListView.class);
  267. }
  268. DateTime now = DateTime.now();
  269. list.forEach(data -> {
  270. data.setSold(getGoodsSold(data.getId(), data.getSold()));
  271. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, null));
  272. //设置特定价格
  273. setSpecificPrice(data, fUid, null);
  274. });
  275. return GatewayResponse.SUCCESS.newBuilder().toResult(Optional.of(list));
  276. }
  277. /**
  278. * h5 银河首页平台展示
  279. */
  280. @GetMapping("/get/homePage")
  281. public Result<? extends Object> getGoodsHomePage(String customId, Boolean isFilter, String dsCode) {
  282. if (StrUtil.isNotBlank(customId)) {
  283. Result<List<YhShopGoodsFrontView>> shopGoods = getShopGoods(yhShopFrontService.getYhsIdByCustomId(customId));
  284. return shopGoods;
  285. }
  286. Long fUid = StpUserUtil.getUserIdAfterLogin();
  287. Boolean filterGoodsIds = isFilterGoodsIds(fUid, isFilter);
  288. String h5CacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "H5";
  289. if (filterGoodsIds) {
  290. h5CacheKey += ":filter-ip";
  291. }
  292. Object o = redisService.get(h5CacheKey);
  293. List<GoodsFrontListView> list = null;
  294. if (o == null) {
  295. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  296. if (filterGoodsIds) {
  297. builder.field(GoodsFrontListView::getFilter, Boolean.FALSE);
  298. }
  299. list = beanSearcher.searchAll(GoodsFrontListView.class, builder.build());
  300. redisService.setNx(h5CacheKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  301. o = redisService.get(h5CacheKey);
  302. }
  303. if (list == null) {
  304. list = Jsons.parseList(o, GoodsFrontListView.class);
  305. }
  306. AtomicLong dsUserId = new AtomicLong();
  307. if (StrUtil.isNotBlank(dsCode)) {
  308. UserSharedDto userSharedDto = userService.getUserShredDtoByDsCode(dsCode);
  309. dsUserId.set(userSharedDto.getSharedId());
  310. }
  311. DateTime now = DateTime.now();
  312. list.forEach(data -> {
  313. data.setSold(getGoodsSold(data.getId(), data.getSold()));
  314. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, null));
  315. //设置特定价格
  316. setSpecificPrice(data, fUid, null);
  317. GoodsDonSku minPriceUserOwnsSku = goodsDonSkuMapper.checkAndReturnMinPriceUserOwnsSku(data.getId(), dsUserId.get());
  318. if (minPriceUserOwnsSku != null) {
  319. data.setPrice(minPriceUserOwnsSku.getPrice());
  320. data.setMonths(minPriceUserOwnsSku.getMonths());
  321. data.setDays(minPriceUserOwnsSku.getDays());
  322. }
  323. });
  324. return GatewayResponse.SUCCESS.newBuilder().toResult(Optional.of(list));
  325. }
  326. /**
  327. * 注册首页平台展示
  328. */
  329. @GetMapping("/get/register")
  330. public Result<List<RegisterGoodsDonView>> getRegisterGoodsDon() {
  331. try {
  332. String registerCacheKey = RedisService.key.REGISTER_HOME_PAGE_CACHE.getName();
  333. Object value = redisService.get(registerCacheKey);
  334. if (value != null) {
  335. try {
  336. List<RegisterGoodsDonView> list = Jsons.parseList(value, RegisterGoodsDonView.class);
  337. if (CollUtil.isNotEmpty(list)) {
  338. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  339. }
  340. } catch (Exception e) {
  341. }
  342. }
  343. String lockKey = registerCacheKey + ":lock";
  344. if (!redisService.setNx(lockKey, "1", 10L)) {
  345. return GatewayResponse.SUCCESS.newBuilder().toResult(Collections.emptyList());
  346. }
  347. try {
  348. value = redisService.get(registerCacheKey);
  349. if (value != null) {
  350. List<RegisterGoodsDonView> list = Jsons.parseList(value, RegisterGoodsDonView.class);
  351. if (CollUtil.isNotEmpty(list)) {
  352. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  353. }
  354. }
  355. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  356. List<RegisterGoodsDonView> dataList = beanSearcher.searchAll(RegisterGoodsDonView.class, builder.build());
  357. DateTime now = DateTime.now();
  358. dataList.forEach(data -> {
  359. Integer sold = registerOrderDonMapper.selectCount(Wrappers.lambdaQuery(RegisterOrderDon.class)
  360. .eq(RegisterOrderDon::getGoodsId, data.getId()).notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus));
  361. data.setSold(sold + data.getVirtualSold());
  362. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 2, null));
  363. });
  364. redisService.set(registerCacheKey, dataList, RedisService.key.REGISTER_HOME_PAGE_CACHE.getTimeout());
  365. return GatewayResponse.SUCCESS.newBuilder().toResult(dataList);
  366. } finally {
  367. redisService.del(lockKey);
  368. }
  369. } catch (Exception e) {
  370. // Redis异常,返回空列表保护数据库
  371. return GatewayResponse.SUCCESS.newBuilder().toResult(Collections.emptyList());
  372. }
  373. }
  374. /**
  375. * 获取商品详情
  376. */
  377. @GetMapping("/get/{id}")
  378. public Result<? extends Object> getDetail(@PathVariable Long id, String customId, String dsCode, BigDecimal discount) {
  379. try {
  380. if (StrUtil.isNotBlank(customId) && id != 15) {
  381. Result<YhShopGoodsFrontView> yhShopGoodsDetail = getGoodsDetailByYhsId(yhShopFrontService.getYhsIdByCustomId(customId), id);
  382. return yhShopGoodsDetail;
  383. }
  384. String goodsDetailKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "goods_detail:" + id;
  385. List<Long> filter_goodsIds = null;
  386. String lang = request.getHeader("lang");
  387. AtomicBoolean language = new AtomicBoolean(false);
  388. if (lang != null && !StrUtil.equals(UserPageLanguage.Language.zh_CN.name(), lang)) {
  389. goodsDetailKey += ":" + lang;
  390. language.set(true);
  391. }
  392. if (CollUtil.isNotEmpty(filter_goodsIds) && filter_goodsIds.contains(id)) {
  393. return GatewayResponse.SUCCESS.newBuilder().toResult();
  394. }
  395. Object o = redisService.get(goodsDetailKey);
  396. if (o == null) {
  397. // 加锁防止缓存击穿
  398. String lockKey = goodsDetailKey + ":lock";
  399. if (redisService.setNx(lockKey, "1", 10L)) {
  400. try {
  401. o = redisService.get(goodsDetailKey);
  402. if (o == null) {
  403. GoodsFrontListView views = beanSearcher.searchFirst(GoodsFrontListView.class, MapUtils.builder().field(GoodsFrontListView::getId, id)
  404. .build());
  405. if (views == null) {
  406. return GatewayResponse.SUCCESS.newBuilder().toResult();
  407. }
  408. views.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, views.getId()).build()));
  409. //把开启订阅提醒的规格展示出来
  410. String condition = "(sku.status or (sku.status = 0 and sku.is_subscribe_alert))";
  411. MapBuilder builder = MapUtils.builder().field(GoodsDonSkuFrontView::getGoodsId, views.getId()).put("condition", condition);
  412. if (views.getType() == 1) {
  413. builder.orderBy(GoodsDonSkuFrontView::getSorted).asc();
  414. }
  415. views.setSkuList(beanSearcher.searchAll(GoodsDonSkuFrontView.class, builder.build()));
  416. views.setSold(getGoodsSold(views.getId(), views.getSold()));
  417. if (language.get()) {
  418. backInfoTranslationsFrontService.setGoodsDetailLanguage(views, lang);
  419. }
  420. List<Long> gptBuyGids = getGptMirrorGiveawayBuyGids();
  421. if (CollUtil.isNotEmpty(gptBuyGids) && gptBuyGids.contains(views.getId())) {
  422. views.setIsGptGift(true);
  423. }
  424. List<GoodsDonQaFrontView> donQaFrontViews = beanSearcher.searchAll(GoodsDonQaFrontView.class, MapUtils.builder().field(GoodsDonQaFrontView::getGoodsId, id).field(GoodsDonQaFrontView::getType, 1).build());
  425. views.setQuestions(donQaFrontViews);
  426. redisService.set(goodsDetailKey, views, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 60 * RandomUtil.randomInt(10));
  427. o = views;
  428. }
  429. } finally {
  430. redisService.del(lockKey);
  431. }
  432. } else {
  433. // 未获取锁,返回空
  434. return GatewayResponse.SUCCESS.newBuilder().toResult();
  435. }
  436. }
  437. Long fUid = StpUserUtil.getUserIdAfterLogin();
  438. GoodsFrontListView views = Jsons.parseObject(o, GoodsFrontListView.class);
  439. AtomicLong dsUserId = new AtomicLong();
  440. AtomicBoolean existsChannel = new AtomicBoolean(true);
  441. if (StrUtil.isNotBlank(dsCode)) {
  442. UserSharedDto userSharedDto = userService.getUserShredDtoByDsCode(dsCode);
  443. dsUserId.set(userSharedDto.getSharedId());
  444. }
  445. views.setIsGift(giftCardGoodsSkuMapper.selectCount(Wrappers.lambdaQuery(GiftCardGoodsSku.class)
  446. .eq(GiftCardGoodsSku::getGcGoodsId, views.getId())
  447. .eq(GiftCardGoodsSku::getStatus, 1)
  448. .eq(GiftCardGoodsSku::getDeleted, 1)) > 0);
  449. views.setSkuList(views.getSkuList().stream().filter(sku -> {
  450. if (StrUtil.isNotBlank(sku.getUserOwns())) {
  451. if (dsUserId.get() == 0 && fUid != null && existsChannel.get()) {
  452. Long sharedId = userService.getSharedIdByUserId(fUid);
  453. if (sharedId == 0) {
  454. existsChannel.set(false);
  455. }
  456. dsUserId.set(sharedId);
  457. }
  458. if (dsUserId.get() == 0 || !sku.getUserOwns().contains(String.valueOf(dsUserId.get()))) {
  459. return false;
  460. }
  461. }
  462. if (sku.getIsDp()) {
  463. sku.setDpSkuViews(beanSearcher.searchAll(GoodsDiscountPlusPurchaseRelationFrontView.class, MapUtils.builder()
  464. .field(GoodsDiscountPlusPurchaseRelationFrontView::getSkuId, sku.getId())
  465. .build()));
  466. //多语言
  467. if (language.get()) {
  468. for (GoodsDiscountPlusPurchaseRelationFrontView dpSkuView : sku.getDpSkuViews()) {
  469. String languageGoodsTitle = backInfoTranslationsFrontService.getGoodsTitleLanguage(dpSkuView.getDisGid(), lang);
  470. if (StrUtil.isNotBlank(languageGoodsTitle)) {
  471. dpSkuView.setTitle(languageGoodsTitle);
  472. }
  473. String languageSpecVal = backInfoTranslationsFrontService.getLanguageSpecVal(dpSkuView.getDisSid(), lang);
  474. if (StrUtil.isNotBlank(languageSpecVal)) {
  475. dpSkuView.setSpecVal(languageSpecVal);
  476. }
  477. }
  478. }
  479. }
  480. if (views.getIsSp()) {
  481. if (fUid == null) {
  482. sku.setSpecificPrice(null);
  483. sku.setSpecificGoodsIds(null);
  484. } else {
  485. //设置特定价格
  486. if (!isPaySpecificGoodsIds(sku.getSpecificGoodsIds(), fUid)) {
  487. sku.setSpecificGoodsIds(null);
  488. sku.setSpecificPrice(null);
  489. } else {
  490. BigDecimal specificPrice = views.getSpecificPrice();
  491. BigDecimal skuSpecificPrice = sku.getSpecificPrice();
  492. Long specificSkuId = sku.getId();
  493. if (specificPrice == null) {
  494. views.setSpecificPrice(skuSpecificPrice);
  495. views.setSpecificSkuId(specificSkuId);
  496. } else if (specificPrice.compareTo(skuSpecificPrice) > 0) {
  497. views.setSpecificPrice(skuSpecificPrice);
  498. views.setSpecificSkuId(specificSkuId);
  499. }
  500. }
  501. }
  502. }
  503. // if (fUid != null && Constant.SPECIAL_PRICE_SKU_IDS.contains(sku.getId())) {
  504. // UserDistributeShared distributeShared = userDistributeSharedMapper.selectOne(Wrappers.lambdaQuery(UserDistributeShared.class)
  505. // .eq(UserDistributeShared::getUserId, fUid)
  506. // .last("limit 1"));
  507. // if (distributeShared != null) {
  508. // Optional.ofNullable(sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class)
  509. // .eq(SysConfig::getSysKey, Constant.MJ_SPECIAL_SKU_PRICE_KEY)
  510. // .last("limit 1"))).ifPresent(sysConfig -> {
  511. // if (StrUtil.isNotBlank(sysConfig.getSysValue())) {
  512. // try {
  513. // GoodsDonPriceDto goodsDonPriceDto = Jsons.parseObject(sysConfig.getSysValue(), GoodsDonPriceDto.class);
  514. // sku.setPrice(goodsDonPriceDto.getPrice());
  515. // } catch (Exception e) {
  516. // }
  517. // }
  518. // });
  519. // }
  520. // }
  521. //设置规格优惠后的价格
  522. sku.setChannelDiscount(discount);
  523. setUserCouponSkuMoney(fUid, sku);
  524. //查看用户是否已订阅到货通知
  525. if (fUid != null && sku.getIsSubscribeAlert()) {
  526. sku.setUserSubscribeStatus(userSubscribeAlertGoodsRecordService.getUserSubscribeGoodsStatus(fUid, null, sku.getId()));
  527. }
  528. return true;
  529. }).collect(Collectors.toList()));
  530. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  531. } catch (Exception e) {
  532. // Redis异常,返回空保护数据库
  533. return GatewayResponse.SUCCESS.newBuilder().toResult();
  534. }
  535. }
  536. /**
  537. * 获取商品推荐平台
  538. */
  539. @GetMapping("/get/recommend/{id}")
  540. public Result<List<GoodsFrontListView>> getRecommendGoodsByGid(@PathVariable Long id, String customId, String dsCode) throws Exception {
  541. try {
  542. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "recommend:" + id;
  543. Long yhsId = null;
  544. if (StrUtil.isNotBlank(customId)) {
  545. yhsId = yhShopFrontService.getYhsIdByCustomId(customId);
  546. key = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getName() + yhsId + ":recommend:" + id;
  547. }
  548. Object o = redisService.get(key);
  549. Long fUid = StpUserUtil.getUserIdAfterLogin();
  550. List<GoodsFrontListView> goodsRecommendViews = null;
  551. if (o == null) {
  552. // 加锁防止缓存击穿
  553. String lockKey = key + ":lock";
  554. if (redisService.setNx(lockKey, "1", 10L)) {
  555. try {
  556. o = redisService.get(key);
  557. if (o == null) {
  558. GoodsDon goodsDon = beanSearcher.searchFirst(GoodsDon.class, MapUtils.builder()
  559. .field(GoodsDon::getId, id).field(GoodsDon::getStatus, true)
  560. .field(GoodsDon::getDeleted, true)
  561. .build());
  562. if (goodsDon != null && StrUtil.isNotEmpty(goodsDon.getRecommendGoods())) {
  563. List<Long> recommend_gidList = Jsons.parseList(goodsDon.getRecommendGoods(), Long.class);
  564. MapBuilder mapBuilder = MapUtils.builder();
  565. Long finalYhsId = yhsId;
  566. if (yhsId != null) {
  567. 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", finalYhsId));
  568. }
  569. mapBuilder.field(GoodsFrontListView::getId, recommend_gidList).op(Operator.InList);
  570. goodsRecommendViews = beanSearcher.searchAll(GoodsFrontListView.class, mapBuilder.build());
  571. redisService.set(key, Jsons.toJson(goodsRecommendViews), 60 * 15L);
  572. o = Jsons.toJson(goodsRecommendViews);
  573. } else {
  574. return GatewayResponse.SUCCESS.newBuilder().toResult();
  575. }
  576. }
  577. } finally {
  578. redisService.del(lockKey);
  579. }
  580. } else {
  581. // 未获取锁,返回空
  582. return GatewayResponse.SUCCESS.newBuilder().toResult();
  583. }
  584. }
  585. if (goodsRecommendViews == null) {
  586. goodsRecommendViews = Jsons.parseList(o.toString(), GoodsFrontListView.class);
  587. }
  588. AtomicLong dsUserId = new AtomicLong();
  589. AtomicBoolean existsChannel = new AtomicBoolean(true);
  590. if (StrUtil.isNotBlank(dsCode)) {
  591. UserSharedDto userSharedDto = userService.getUserShredDtoByDsCode(dsCode);
  592. dsUserId.set(userSharedDto.getSharedId());
  593. }
  594. goodsRecommendViews = goodsRecommendViews.stream().filter(data -> {
  595. if (data.getIsOwns()) {
  596. if (dsUserId.get() == 0 && fUid != null && existsChannel.get()) {
  597. Long sharedId = userService.getSharedIdByUserId(fUid);
  598. if (sharedId == 0) {
  599. existsChannel.set(false);
  600. }
  601. dsUserId.set(sharedId);
  602. }
  603. GoodsDonSku minPriceUserOwnsSku = goodsDonSkuMapper.checkAndReturnMinPriceUserOwnsSku(data.getId(), dsUserId.get());
  604. if (minPriceUserOwnsSku == null) {
  605. return false;
  606. }
  607. data.setPrice(minPriceUserOwnsSku.getPrice());
  608. data.setMonths(minPriceUserOwnsSku.getMonths());
  609. data.setDays(minPriceUserOwnsSku.getDays());
  610. }
  611. data.setSold(getGoodsSold(data.getId(), data.getSold()));
  612. setSpecificPrice(data, fUid, dsUserId.get());
  613. return true;
  614. }).collect(Collectors.toList());
  615. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsRecommendViews);
  616. } catch (Exception e) {
  617. // Redis异常,返回空保护数据库
  618. return GatewayResponse.SUCCESS.newBuilder().toResult();
  619. }
  620. }
  621. /**
  622. * 获取商品多规格列表
  623. */
  624. @GetMapping("/get/sku")
  625. public Result<List<GoodsDonSkuView>> getGoodsDonSkuView(@RequestParam(value = "skuIds") List<Long> skuIds) {
  626. if (skuIds.isEmpty()) {
  627. return GatewayResponse.SUCCESS.newBuilder().toResult();
  628. }
  629. List<GoodsDonSkuView> views = beanSearcher.searchAll(GoodsDonSkuView.class, MapUtils.builder()
  630. .field(GoodsDonSkuView::getSkuId, skuIds).op(Operator.InList)
  631. .build());
  632. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  633. }
  634. /**
  635. * 获取评价列表
  636. */
  637. @GetMapping("/get/comments")
  638. public Result<SearchResult<OrderCommentFrontView>> getComments(Long goodsId, @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size) {
  639. try {
  640. String cacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "comments:" + goodsId + ":" + page + ":" + size;
  641. Object o = redisService.get(cacheKey);
  642. if (o != null) {
  643. return GatewayResponse.SUCCESS.newBuilder().toResult((SearchResult<OrderCommentFrontView>) o);
  644. }
  645. String lockKey = cacheKey + ":lock";
  646. if (redisService.setNx(lockKey, "1", 10L)) {
  647. try {
  648. o = redisService.get(cacheKey);
  649. if (o != null) {
  650. return GatewayResponse.SUCCESS.newBuilder().toResult((SearchResult<OrderCommentFrontView>) o);
  651. }
  652. SearchResult<OrderCommentFrontView> search = beanSearcher.search(OrderCommentFrontView.class, MapUtils.flatBuilder(request.getParameterMap())
  653. .orderBy(OrderCommentFrontView::getCommentTime).desc()
  654. .orderBy(OrderCommentFrontView::getId).desc()
  655. .build());
  656. redisService.set(cacheKey, search, 60 * 30L);
  657. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  658. } finally {
  659. redisService.del(lockKey);
  660. }
  661. } else {
  662. return GatewayResponse.SUCCESS.newBuilder().toResult(new SearchResult<OrderCommentFrontView>());
  663. }
  664. } catch (Exception e) {
  665. return GatewayResponse.SUCCESS.newBuilder().toResult(new SearchResult<OrderCommentFrontView>());
  666. }
  667. }
  668. /**
  669. * 获取礼品卡平台性情
  670. */
  671. @GetMapping("/get/card/{id}")
  672. public Result<Map<Integer, List<GiftCardGoodsSkuFrontView>>> getGiftCardDetail(@PathVariable Long id) {
  673. List<GiftCardGoodsSkuFrontView> giftCardGoodsSkuFrontViews = beanSearcher.searchAll(GiftCardGoodsSkuFrontView.class, MapUtils.builder().field(GiftCardGoodsSkuFrontView::getGoodsId, id).build());
  674. Map<Integer, List<GiftCardGoodsSkuFrontView>> map = giftCardGoodsSkuFrontViews.stream().collect(Collectors.groupingBy(GiftCardGoodsSkuFrontView::getGcType));
  675. return GatewayResponse.SUCCESS.newBuilder().toResult(map);
  676. }
  677. /**
  678. * 获取首页配置列表
  679. */
  680. @GetMapping("/get/homeManage")
  681. public Result<Map<String, List<HomeManagementFrontView>>> getHomeManagementFrontPage(String customId, String dsCode, BigDecimal discount, Boolean isFilter) {
  682. try {
  683. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "homeManage";
  684. Long yhsId = null;
  685. if (StrUtil.isNotBlank(customId)) {
  686. yhsId = yhShopFrontService.getYhsIdByCustomId(customId);
  687. key = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getName() + yhsId + ":homeManage";
  688. }
  689. Long fUid = StpUserUtil.getUserIdAfterLogin();
  690. Boolean isFilterGoodsIds = isFilterGoodsIds(fUid, isFilter);
  691. AtomicBoolean isRemoveRg = new AtomicBoolean(false);
  692. if (isFilterGoodsIds) {
  693. key += ":filter-ip";
  694. }
  695. String lang = request.getHeader("lang");
  696. AtomicBoolean language = new AtomicBoolean(false);
  697. if (lang != null && !StrUtil.equals(UserPageLanguage.Language.zh_CN.name(), lang)) {
  698. key += ":" + lang;
  699. language.set(true);
  700. }
  701. Object o = redisService.get(key);
  702. if (o == null) {
  703. // 加锁防止缓存击穿
  704. String lockKey = key + ":lock";
  705. if (redisService.setNx(lockKey, "1", 10L)) {
  706. try {
  707. o = redisService.get(key);
  708. if (o == null) {
  709. String condition = "g.deleted";
  710. if (yhsId == null) {
  711. condition += " and (g.status or (g.status = 0 and g.is_subscribe_alert))";
  712. } else {
  713. condition += " and g.status";
  714. }
  715. Long finalYhsId = yhsId;
  716. if (yhsId != null) {
  717. 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);
  718. }
  719. Map<String, List<HomeManagementFrontView>> map = new ConcurrentHashMap<>();
  720. final String conditionSql = condition;
  721. Stream.of(HomeManagementConfig.Type.values())
  722. .forEach(type -> {
  723. if (isRemoveRg.get() && type == HomeManagementConfig.Type.rg) {
  724. return;
  725. }
  726. MapBuilder builder = MapUtils.builder()
  727. .field(HomeManagementFrontView::getType, type.name());
  728. if (type == HomeManagementConfig.Type.ex || type == HomeManagementConfig.Type.vg) {
  729. builder.put("condition", conditionSql);
  730. if (isFilterGoodsIds) {
  731. builder.field(HomeManagementFrontView::getFilter, Boolean.FALSE);
  732. }
  733. }
  734. List<HomeManagementFrontView> homeManagementFrontViews = beanSearcher.searchAll(HomeManagementFrontView.class, builder.build());
  735. if (language.get() && type == HomeManagementConfig.Type.rg) {
  736. backInfoTranslationsFrontService.getHomeConfigRealGoodsLanguage(homeManagementFrontViews, lang);
  737. }
  738. map.putIfAbsent(type.name(), homeManagementFrontViews);
  739. });
  740. redisService.set(key, map, 60 * 60L * 24 * 7);
  741. o = map;
  742. }
  743. } finally {
  744. redisService.del(lockKey);
  745. }
  746. } else {
  747. // 未获取锁,返回空map
  748. Map<String, List<HomeManagementFrontView>> emptyMap = new ConcurrentHashMap<>();
  749. return GatewayResponse.SUCCESS.newBuilder().toResult(emptyMap);
  750. }
  751. }
  752. AtomicLong dsUserId = new AtomicLong();
  753. AtomicBoolean existsChannel = new AtomicBoolean(true);
  754. if (StrUtil.isNotBlank(dsCode)) {
  755. UserSharedDto userSharedDto = userService.getUserShredDtoByDsCode(dsCode);
  756. dsUserId.set(userSharedDto.getSharedId());
  757. }
  758. Map<String, List<HomeManagementFrontView>> map = Jsons.parseObject(o, Map.class);
  759. if (map.containsKey(HomeManagementConfig.Type.ex.name()) || map.containsKey(HomeManagementConfig.Type.vg.name())) {
  760. try {
  761. List<HomeManagementFrontView> exList = Jsons.parseList(Jsons.toJson(map.get(HomeManagementConfig.Type.ex.name())), HomeManagementFrontView.class);
  762. setHomeConfigGoods(map, HomeManagementConfig.Type.ex, exList, dsUserId, existsChannel, fUid, discount);
  763. List<HomeManagementFrontView> vgList = Jsons.parseList(Jsons.toJson(map.get(HomeManagementConfig.Type.vg.name())), HomeManagementFrontView.class);
  764. setHomeConfigGoods(map, HomeManagementConfig.Type.vg, vgList, dsUserId, existsChannel, fUid, discount);
  765. } catch (Exception e) {
  766. }
  767. }
  768. return GatewayResponse.SUCCESS.newBuilder().toResult(map);
  769. } catch (Exception e) {
  770. // Redis异常,返回空map保护数据库
  771. Map<String, List<HomeManagementFrontView>> emptyMap = new ConcurrentHashMap<>();
  772. return GatewayResponse.SUCCESS.newBuilder().toResult(emptyMap);
  773. }
  774. }
  775. /**
  776. * 首页点击 记录
  777. */
  778. @PostMapping("/homePage/click")
  779. public Result<String> homeManageClick(@RequestBody ClickRecordReq request) {
  780. Long userId = StpUserUtil.getUserIdAfterLogin();
  781. clickRecordFrontService.click(userId, request);
  782. return GatewayResponse.SUCCESS.newBuilder().toResult();
  783. }
  784. public Boolean isFilterGoodsIds(Long userIdAfLogin, Boolean isFilter) {
  785. //是否需要过滤特殊ip
  786. if (isFilter != null && !isFilter) {
  787. return false;
  788. }
  789. List<Long> filter_goodsIds = null;
  790. //获取当前ip地址
  791. String newestIpAddress = StringUtil.getNewestIpAddress(ServletUtil.getClientIP(request));
  792. if (newestIpAddress.contains("杭州") || newestIpAddress.contains("北京")) {
  793. // //过滤杭州Ai ChatGPT平台
  794. // SysConfig sysConfig = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class)
  795. // .eq(SysConfig::getSysKey, Constant.LOCATION_FOR_GOODS_IDS).last("limit 1"));
  796. // if (sysConfig != null && StrUtil.isNotBlank(sysConfig.getSysValue())) {
  797. // try {
  798. // filter_goodsIds = Jsons.parseList(sysConfig.getSysValue(), Long.class);
  799. // } catch (Exception e) {
  800. // }
  801. // }
  802. if (userIdAfLogin == null) {
  803. //未登录 直接过滤
  804. return true;
  805. }
  806. if (userIdAfLogin != null) {
  807. //是否购买过AI产品
  808. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userIdAfLogin, null);
  809. Integer count = orderDonMapper.hasPaymentAiGoods(userIdList);
  810. if (count == 0) {
  811. //未购买 直接过滤
  812. return true;
  813. }
  814. }
  815. }
  816. return false;
  817. }
  818. public Boolean isHzIp() {
  819. if (StringUtil.getRealAddressByIP(ServletUtil.getClientIP(request)).contains("无锡")) {
  820. return true;
  821. }
  822. return false;
  823. }
  824. /**
  825. * @param goodsId
  826. * @param type 1、普通,2、注册平台
  827. * @param yhsId 店铺id
  828. * @return
  829. */
  830. public String getPayMsgTime(Long goodsId, DateTime now, Integer type, Long yhsId) {
  831. Date startTime = null;
  832. StringBuilder notifyMsg = new StringBuilder();
  833. if (type == 1) {
  834. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  835. .eq(OrderDon::getGoodsId, goodsId)
  836. .notIn(OrderDon::getStatus, Constant.noOrderStatus).select(OrderDon::getCreatedTime, OrderDon::getPayTime, OrderDon::getId)
  837. .eq(yhsId != null, OrderDon::getYhsId, yhsId)
  838. .orderByDesc(OrderDon::getId)
  839. .last("limit 1"));
  840. if (orderDon == null) {
  841. notifyMsg.append("点击购买立即上车");
  842. return notifyMsg.toString();
  843. }
  844. startTime = orderDon.getPayTime() != null ? orderDon.getPayTime() : orderDon.getCreatedTime();
  845. } else if (type == 2) {
  846. RegisterOrderDon orderDon = registerOrderDonMapper.selectOne(Wrappers.lambdaQuery(RegisterOrderDon.class)
  847. .eq(RegisterOrderDon::getGoodsId, goodsId)
  848. .notIn(RegisterOrderDon::getStatus, Constant.noOrderStatus).select(RegisterOrderDon::getCreatedTime, RegisterOrderDon::getPayTime, RegisterOrderDon::getId)
  849. .orderByDesc(RegisterOrderDon::getId)
  850. .last("limit 1"));
  851. if (orderDon == null) {
  852. return null;
  853. }
  854. startTime = orderDon.getPayTime() != null ? orderDon.getPayTime() : orderDon.getCreatedTime();
  855. }
  856. long hour = DateUtil.between(startTime, now, DateUnit.HOUR);
  857. if (hour == 0) {
  858. long minute = DateUtil.between(startTime, now, DateUnit.MINUTE);
  859. if (minute == 0) {
  860. notifyMsg.append("1分钟");
  861. } else {
  862. notifyMsg.append(minute);
  863. notifyMsg.append("分钟");
  864. }
  865. } else {
  866. if (hour > 3) {
  867. int hours = RandomUtil.randomInt(3) + 1;
  868. notifyMsg.append(hours);
  869. } else {
  870. notifyMsg.append(hour);
  871. }
  872. notifyMsg.append("小时");
  873. }
  874. notifyMsg.append("前有人购买");
  875. return notifyMsg.toString();
  876. }
  877. /**
  878. * 是否购买过特定平台
  879. */
  880. public Boolean isPaySpecificGoodsIds(String specificGoodsIdsStr, Long userId) {
  881. if (StrUtil.isNotBlank(specificGoodsIdsStr)) {
  882. try {
  883. Set<Long> specificGoodsIds = Jsons.parseSet(specificGoodsIdsStr, Long.class);
  884. Number number = beanSearcher.searchCount(OrderDon.class, MapUtils.builder().field(OrderDon::getGoodsId, specificGoodsIds).op(Operator.InList)
  885. .field(OrderDon::getUserId, userId)
  886. .field(OrderDon::getStatus, Constant.noOrderAllStatus).op(Operator.NotIn)
  887. .build());
  888. if (number.intValue() == 0) {
  889. return false;
  890. }
  891. return true;
  892. } catch (Exception e) {
  893. }
  894. }
  895. return false;
  896. }
  897. /**
  898. * 设置平台 特定价格
  899. */
  900. public void setSpecificPrice(GoodsFrontListView data, Long fUid, Long dsUserId) {
  901. if (fUid != null) {
  902. if (data.getIsSp()) {
  903. //查找所有特定规格最小价格 满足条件
  904. String specificGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "specific:" + data.getId();
  905. Object speciObj = redisService.get(specificGoodsKey);
  906. List<GoodsDonSku> specificSkus = null;
  907. if (speciObj != null) {
  908. specificSkus = Jsons.parseList(speciObj, GoodsDonSku.class);
  909. } else {
  910. specificSkus = goodsDonSkuMapper.selectList(
  911. Wrappers.lambdaQuery(GoodsDonSku.class)
  912. .eq(GoodsDonSku::getGoodsId, data.getId())
  913. .eq(GoodsDonSku::getStatus, true)
  914. .gt(GoodsDonSku::getSpecificPrice, 0));
  915. redisService.setNx(specificGoodsKey, specificSkus, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  916. }
  917. specificSkus.forEach(specificSku -> {
  918. if (StrUtil.isNotEmpty(specificSku.getUserOwns()) && (dsUserId == null || !specificSku.getUserOwns().contains(dsUserId.toString()))) {
  919. return;
  920. }
  921. String specificGoodsIdsStr = specificSku.getSpecificGoodsIds();
  922. if (isPaySpecificGoodsIds(specificGoodsIdsStr, fUid)) {
  923. BigDecimal specificPrice = data.getSpecificPrice();
  924. BigDecimal skuSpecificPrice = specificSku.getSpecificPrice();
  925. Long specificSkuId = specificSku.getId();
  926. if (specificPrice == null) {
  927. data.setSpecificPrice(skuSpecificPrice);
  928. data.setSpecificSkuId(specificSkuId);
  929. } else if (specificPrice.compareTo(skuSpecificPrice) > 0) {
  930. data.setSpecificPrice(skuSpecificPrice);
  931. data.setSpecificSkuId(specificSkuId);
  932. }
  933. }
  934. });
  935. List<GoodsDonSkuFrontView> skuList = data.getSkuList();
  936. if (CollUtil.isNotEmpty(skuList)) {
  937. skuList.forEach(sku -> {
  938. //设置特定价格
  939. if (!isPaySpecificGoodsIds(sku.getSpecificGoodsIds(), fUid)) {
  940. sku.setSpecificGoodsIds(null);
  941. sku.setSpecificPrice(null);
  942. }
  943. });
  944. }
  945. }
  946. }
  947. }
  948. /**
  949. * 设置平台 特定价格
  950. */
  951. public void setGcSpecificPrice(GoodsFrontListCategoryView data, Long fUid, Long dsUserId) {
  952. if (fUid != null) {
  953. if (data.getIsSp()) {
  954. //查找所有特定规格最小价格 满足条件
  955. String specificGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "specific:" + data.getId();
  956. Object speciObj = redisService.get(specificGoodsKey);
  957. List<GoodsDonSku> specificSkus = null;
  958. if (speciObj != null) {
  959. specificSkus = Jsons.parseList(speciObj, GoodsDonSku.class);
  960. } else {
  961. specificSkus = goodsDonSkuMapper.selectList(
  962. Wrappers.lambdaQuery(GoodsDonSku.class)
  963. .eq(GoodsDonSku::getGoodsId, data.getId())
  964. .eq(GoodsDonSku::getStatus, true)
  965. .gt(GoodsDonSku::getSpecificPrice, 0));
  966. redisService.setNx(specificGoodsKey, specificSkus, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  967. }
  968. specificSkus.forEach(specificSku -> {
  969. if (StrUtil.isNotEmpty(specificSku.getUserOwns()) && (dsUserId == null || !specificSku.getUserOwns().contains(dsUserId.toString()))) {
  970. return;
  971. }
  972. String specificGoodsIdsStr = specificSku.getSpecificGoodsIds();
  973. if (isPaySpecificGoodsIds(specificGoodsIdsStr, fUid)) {
  974. BigDecimal specificPrice = data.getSpecificPrice();
  975. BigDecimal skuSpecificPrice = specificSku.getSpecificPrice();
  976. Long specificSkuId = specificSku.getId();
  977. if (specificPrice == null) {
  978. data.setSpecificPrice(skuSpecificPrice);
  979. data.setSpecificSkuId(specificSkuId);
  980. } else if (specificPrice.compareTo(skuSpecificPrice) > 0) {
  981. data.setSpecificPrice(skuSpecificPrice);
  982. data.setSpecificSkuId(specificSkuId);
  983. }
  984. }
  985. });
  986. List<GoodsDonSkuFrontView> skuList = data.getSkuList();
  987. if (CollUtil.isNotEmpty(skuList)) {
  988. skuList.forEach(sku -> {
  989. //设置特定价格
  990. if (!isPaySpecificGoodsIds(sku.getSpecificGoodsIds(), fUid)) {
  991. sku.setSpecificGoodsIds(null);
  992. sku.setSpecificPrice(null);
  993. }
  994. });
  995. }
  996. }
  997. }
  998. }
  999. /**
  1000. * 获取特定规格
  1001. */
  1002. public List<GoodsDonSku> getSpecificSku(Long goodsId) {
  1003. //查找所有特定规格最小价格 满足条件
  1004. String specificGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "specific:" + goodsId;
  1005. Object speciObj = redisService.get(specificGoodsKey);
  1006. List<GoodsDonSku> specificSkus = null;
  1007. if (speciObj != null) {
  1008. specificSkus = Jsons.parseList(speciObj, GoodsDonSku.class);
  1009. } else {
  1010. specificSkus = goodsDonSkuMapper.selectList(
  1011. Wrappers.lambdaQuery(GoodsDonSku.class)
  1012. .eq(GoodsDonSku::getGoodsId, goodsId)
  1013. .eq(GoodsDonSku::getStatus, true)
  1014. .gt(GoodsDonSku::getSpecificPrice, 0));
  1015. redisService.setNx(specificGoodsKey, specificSkus, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  1016. }
  1017. specificSkus = Optional.ofNullable(specificSkus).orElse(new ArrayList<>());
  1018. return specificSkus;
  1019. }
  1020. /**
  1021. * 设置首页配置 涉及平台特定价格
  1022. */
  1023. public void setHomeManageSpecific(List<HomeManagementFrontView> list, Long fUid, Long dsUserId, BigDecimal discount) {
  1024. DateTime now = DateTime.now();
  1025. String lang = request.getHeader("lang");
  1026. list.forEach(data -> {
  1027. if (data.getIsSp() != null && data.getIsSp() && fUid != null) {
  1028. //查找所有特定规格最小价格 满足条件
  1029. List<GoodsDonSku> specificSkus = getSpecificSku(data.getGoodsId());
  1030. specificSkus.forEach(specificSku -> {
  1031. if (StrUtil.isNotEmpty(specificSku.getUserOwns()) && (dsUserId == null || !specificSku.getUserOwns().contains(dsUserId.toString()))) {
  1032. return;
  1033. }
  1034. String specificGoodsIdsStr = specificSku.getSpecificGoodsIds();
  1035. if (isPaySpecificGoodsIds(specificGoodsIdsStr, fUid)) {
  1036. BigDecimal specificPrice = data.getSpecificPrice();
  1037. BigDecimal skuSpecificPrice = specificSku.getSpecificPrice();
  1038. Long specificSkuId = specificSku.getId();
  1039. if (specificPrice == null) {
  1040. data.setSpecificPrice(skuSpecificPrice);
  1041. data.setSpecificSkuId(specificSkuId);
  1042. } else if (specificPrice.compareTo(skuSpecificPrice) > 0) {
  1043. data.setSpecificPrice(skuSpecificPrice);
  1044. data.setSpecificSkuId(specificSkuId);
  1045. }
  1046. }
  1047. });
  1048. }
  1049. data.setSold(getGoodsSold(data.getGoodsId(), data.getSold()));
  1050. data.setNotifyMsg(getPayMsgTime(data.getGoodsId(), now, 1, null));
  1051. //设置用户存在优惠券的优惠商品价格
  1052. data.setChannelDiscount(discount);
  1053. setHomeManageUserCouponGoodsMoney(fUid, data);
  1054. });
  1055. }
  1056. public void setHomeConfigGoods(Map<String, List<HomeManagementFrontView>> map, HomeManagementConfig.Type type, List<HomeManagementFrontView> list, AtomicLong dsUserId, AtomicBoolean existsChannel, Long fUid, BigDecimal discount) throws Exception {
  1057. //设置特定价格
  1058. List<Long> gptBuyGids = getGptMirrorGiveawayBuyGids();
  1059. if (CollUtil.isNotEmpty(list)) {
  1060. list = list.stream().filter(data -> {
  1061. if (CollUtil.isNotEmpty(gptBuyGids) && gptBuyGids.contains(data.getGoodsId())) {
  1062. data.setIsGptGift(true);
  1063. }
  1064. if (data.getIsOwns()) {
  1065. if (dsUserId.get() == 0 && fUid != null && existsChannel.get()) {
  1066. Long sharedId = userService.getSharedIdByUserId(fUid);
  1067. if (sharedId == 0) {
  1068. existsChannel.set(false);
  1069. }
  1070. dsUserId.set(sharedId);
  1071. }
  1072. GoodsDonSku minPriceUserOwnsSku = goodsDonSkuMapper.checkAndReturnMinPriceUserOwnsSku(data.getGoodsId(), dsUserId.get());
  1073. if (minPriceUserOwnsSku == null) {
  1074. return false;
  1075. }
  1076. data.setPrice(minPriceUserOwnsSku.getPrice());
  1077. data.setMonths(minPriceUserOwnsSku.getMonths());
  1078. data.setDays(minPriceUserOwnsSku.getDays());
  1079. }
  1080. //未登录
  1081. if (fUid == null) {
  1082. //推荐优惠券商品最低折扣
  1083. setHomeConfigGoodsCouponMoney(data);
  1084. }
  1085. //已登录 查看是否开启通知订阅
  1086. if (fUid != null && data.getIsSubscribeAlert()) {
  1087. data.setUserSubscribeStatus(userSubscribeAlertGoodsRecordService.getUserSubscribeGoodsStatus(fUid, data.getGoodsId(), null));
  1088. }
  1089. return true;
  1090. }).collect(Collectors.toList());
  1091. setHomeManageSpecific(list, fUid, dsUserId.get(), discount);
  1092. map.put(type.name(), list);
  1093. }
  1094. }
  1095. /**
  1096. * 店铺平台列表
  1097. */
  1098. @GetMapping("/get/shop/goods/{yhsId}")
  1099. public Result<List<YhShopGoodsFrontView>> getShopGoods(@PathVariable Long yhsId) {
  1100. try {
  1101. String yhShopCacheKey = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getName() + yhsId;
  1102. Object value = redisService.get(yhShopCacheKey);
  1103. List<YhShopGoodsFrontView> list = null;
  1104. if (value != null) {
  1105. try {
  1106. list = Jsons.parseList(value, YhShopGoodsFrontView.class);
  1107. } catch (Exception e) {
  1108. }
  1109. }
  1110. if (CollUtil.isEmpty(list)) {
  1111. // 加锁防止缓存击穿
  1112. String lockKey = yhShopCacheKey + ":lock";
  1113. if (redisService.setNx(lockKey, "1", 10L)) {
  1114. try {
  1115. value = redisService.get(yhShopCacheKey);
  1116. if (value != null) {
  1117. try {
  1118. list = Jsons.parseList(value, YhShopGoodsFrontView.class);
  1119. } catch (Exception e) {
  1120. }
  1121. }
  1122. if (CollUtil.isEmpty(list)) {
  1123. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  1124. builder.put("condition", String.format("and yhs_id = %s", yhsId));
  1125. list = beanSearcher.searchAll(YhShopGoodsFrontView.class, builder.build());
  1126. redisService.set(yhShopCacheKey, list, RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getTimeout());
  1127. }
  1128. } finally {
  1129. redisService.del(lockKey);
  1130. }
  1131. } else {
  1132. // 未获取锁,返回空列表
  1133. return GatewayResponse.SUCCESS.newBuilder().toResult(Collections.emptyList());
  1134. }
  1135. }
  1136. DateTime now = DateTime.now();
  1137. list.forEach(data -> {
  1138. data.setSold(getGoodsSold(data.getId(), data.getSold()));
  1139. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, yhsId));
  1140. Optional.ofNullable(yhShopGoodsSkuMapper.selectOne(Wrappers.lambdaQuery(YhShopGoodsSku.class)
  1141. .eq(YhShopGoodsSku::getYhsId, yhsId)
  1142. .eq(YhShopGoodsSku::getGoodsId, data.getId())
  1143. .eq(YhShopGoodsSku::getPrice, data.getPrice())
  1144. .eq(YhShopGoodsSku::getStatus, 1)
  1145. .eq(YhShopGoodsSku::getDeleted, 1)
  1146. .last("limit 1"))).ifPresent(yhSku -> {
  1147. GoodsDonSku sku = goodsDonSkuMapper.selectById(yhSku.getSkuId());
  1148. if (sku != null) {
  1149. data.setMonths(sku.getMonths());
  1150. data.setDays(sku.getDays());
  1151. }
  1152. });
  1153. });
  1154. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  1155. } catch (Exception e) {
  1156. // Redis异常,返回空列表保护数据库
  1157. return GatewayResponse.SUCCESS.newBuilder().toResult(Collections.emptyList());
  1158. }
  1159. }
  1160. /**
  1161. * 店铺平台详情
  1162. */
  1163. @GetMapping("/get/shop/goods/detail/{yhsId}/{goodsId}")
  1164. public Result<YhShopGoodsFrontView> getGoodsDetailByYhsId(@PathVariable Long yhsId, @PathVariable Long goodsId) {
  1165. String yhShopDetailCacheKey = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getName() + yhsId + ":goodsId:" + goodsId;
  1166. Object value = redisService.get(yhShopDetailCacheKey);
  1167. if (value == null) {
  1168. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  1169. String condition = String.format("and yhs_id = %s and ys.goods_id = %s", yhsId, goodsId);
  1170. builder.put("condition", condition);
  1171. YhShopGoodsFrontView goodsDetailView = beanSearcher.searchFirst(YhShopGoodsFrontView.class, builder.build());
  1172. if (goodsDetailView == null) {
  1173. throw BusinessRuntimeException.getInstance("该平台不存在或已下架,请刷新页面");
  1174. }
  1175. goodsDetailView.setSkuList(beanSearcher.searchAll(YhShopGoodsSkuFrontView.class, MapUtils.builder().put("condition", condition).orderBy(YhShopGoodsSkuFrontView::getSorted).asc().build()));
  1176. goodsDetailView.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, goodsDetailView.getId()).build()));
  1177. OrderCommentFrontView comments = beanSearcher.searchFirst(OrderCommentFrontView.class, MapUtils.builder()
  1178. .field(OrderCommentFrontView::getGoodsId, goodsDetailView.getId())
  1179. .orderBy(OrderCommentFrontView::getCommentTime).desc()
  1180. .orderBy(OrderCommentFrontView::getId).desc()
  1181. .build());
  1182. goodsDetailView.setComments(comments);
  1183. redisService.setNx(yhShopDetailCacheKey, goodsDetailView, 60 * 60 * 24L);
  1184. value = redisService.get(yhShopDetailCacheKey);
  1185. }
  1186. YhShopGoodsFrontView goodsDetailView = Jsons.parseObject(value, YhShopGoodsFrontView.class);
  1187. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsDetailView);
  1188. }
  1189. /**
  1190. * 根据分类获取对应平台
  1191. */
  1192. @GetMapping("/get/categoryGoods")
  1193. public Result<? extends Object> getGoodsByCategory(Long cgy, String customId, String dsCode, BigDecimal discount, Boolean isFilter) {
  1194. try {
  1195. if (StrUtil.isNotBlank(customId)) {
  1196. Result<List<YhShopGoodsFrontCategoryView>> shopGoods = getShopCategoryGoods(yhShopFrontService.getYhsIdByCustomId(customId), cgy);
  1197. return shopGoods;
  1198. }
  1199. Long fUid = StpUserUtil.getUserIdAfterLogin();
  1200. String categoryGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "category:goods";
  1201. if (cgy != null) {
  1202. categoryGoodsKey += ":" + cgy;
  1203. }
  1204. Boolean isFilterGoodsIds = isFilterGoodsIds(fUid, isFilter);
  1205. if (isFilterGoodsIds) {
  1206. categoryGoodsKey += ":filter-ip";
  1207. }
  1208. String lang = request.getHeader("lang");
  1209. AtomicBoolean language = new AtomicBoolean(false);
  1210. if (lang != null && UserPageLanguage.Language.en_US.name().equals(lang)) {
  1211. categoryGoodsKey += ":" + lang;
  1212. language.set(true);
  1213. }
  1214. Object o = redisService.get(categoryGoodsKey);
  1215. if (o == null) {
  1216. // 加锁防止缓存击穿
  1217. String lockKey = categoryGoodsKey + ":lock";
  1218. if (redisService.setNx(lockKey, "1", 10L)) {
  1219. try {
  1220. o = redisService.get(categoryGoodsKey);
  1221. if (o == null) {
  1222. DateTime now = DateTime.now();
  1223. MapBuilder mapBuilder = MapUtils.flatBuilder(request.getParameterMap());
  1224. //把开启订阅提醒的商品展示出来
  1225. String condition = " and (g.status or (g.status = 0 and g.is_subscribe_alert))";
  1226. if (cgy != null) {
  1227. condition += String.format(" and gcr.category = %s", cgy);
  1228. }
  1229. if (isFilterGoodsIds) {
  1230. mapBuilder.field(GoodsFrontListCategoryView::getFilter, Boolean.FALSE);
  1231. }
  1232. if (StrUtil.isNotEmpty(condition)) {
  1233. mapBuilder.put("condition", condition);
  1234. }
  1235. List<GoodsFrontListCategoryView> goodsFrontListViews = beanSearcher.searchAll(GoodsFrontListCategoryView.class, mapBuilder.build());
  1236. List<Long> gptBuyGids = getGptMirrorGiveawayBuyGids();
  1237. goodsFrontListViews.forEach(data -> {
  1238. data.setSold(getGoodsSold(data.getId(), data.getSold()));
  1239. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, null));
  1240. if (CollUtil.isNotEmpty(gptBuyGids) && gptBuyGids.contains(data.getId())) {
  1241. data.setIsGptGift(true);
  1242. }
  1243. if (language.get()) {
  1244. backInfoTranslationsFrontService.setCategoryGoodsLanguage(data, lang);
  1245. }
  1246. });
  1247. redisService.set(categoryGoodsKey, goodsFrontListViews, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 10 * 60);
  1248. o = goodsFrontListViews;
  1249. }
  1250. } finally {
  1251. redisService.del(lockKey);
  1252. }
  1253. } else {
  1254. // 未获取锁,返回空列表
  1255. return GatewayResponse.SUCCESS.newBuilder().toResult(Collections.emptyList());
  1256. }
  1257. }
  1258. List<GoodsFrontListCategoryView> goodsFrontListViews = Jsons.parseList(o, GoodsFrontListCategoryView.class);
  1259. AtomicLong dsUserId = new AtomicLong();
  1260. AtomicBoolean existsChannel = new AtomicBoolean(true);
  1261. if (StrUtil.isNotBlank(dsCode)) {
  1262. UserSharedDto userSharedDto = userService.getUserShredDtoByDsCode(dsCode);
  1263. dsUserId.set(userSharedDto.getSharedId());
  1264. }
  1265. AtomicBoolean filterCourseGoods = new AtomicBoolean(false);
  1266. if (fUid != null && cgy != null && (cgy == 3 || cgy == 4)) {
  1267. List<Long> userIdList = userBindRelationService.getRelationUserIdList(fUid, null);
  1268. Integer orders = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  1269. .in(OrderDon::getUserId, userIdList)
  1270. .eq(OrderDon::getSkuId, 444)
  1271. .notIn(OrderDon::getStatus, Constant.noOrderStatus));
  1272. if (orders > 0) {
  1273. filterCourseGoods.set(true);
  1274. }
  1275. }
  1276. goodsFrontListViews = goodsFrontListViews.stream().filter(data -> {
  1277. if (data.getIsOwns()) {
  1278. if (dsUserId.get() == 0 && fUid != null && existsChannel.get()) {
  1279. Long sharedId = userService.getSharedIdByUserId(fUid);
  1280. if (sharedId == 0) {
  1281. existsChannel.set(false);
  1282. }
  1283. dsUserId.set(sharedId);
  1284. }
  1285. GoodsDonSku minPriceUserOwnsSku = goodsDonSkuMapper.checkAndReturnMinPriceUserOwnsSku(data.getId(), dsUserId.get());
  1286. if (minPriceUserOwnsSku == null) {
  1287. return false;
  1288. }
  1289. data.setPrice(minPriceUserOwnsSku.getPrice());
  1290. data.setMonths(minPriceUserOwnsSku.getMonths());
  1291. data.setDays(minPriceUserOwnsSku.getDays());
  1292. }
  1293. if (filterCourseGoods.get() && data.getSpecialType() != null && data.getSpecialType() == GoodsDon.SpecialType.courseware) {
  1294. return false;
  1295. }
  1296. setGcSpecificPrice(data, fUid, dsUserId.get());
  1297. data.setChannelDiscount(discount);
  1298. setUserCouponGoodsMoney(fUid, data);
  1299. if (fUid == null) {
  1300. setGoodsCouponMoney(data);
  1301. }
  1302. if (fUid != null && data.getIsSubscribeAlert()) {
  1303. data.setUserSubscribeStatus(userSubscribeAlertGoodsRecordService.getUserSubscribeGoodsStatus(fUid, data.getId(), null));
  1304. }
  1305. return true;
  1306. }).collect(Collectors.toList());
  1307. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsFrontListViews);
  1308. } catch (Exception e) {
  1309. // Redis异常,返回空列表保护数据库
  1310. return GatewayResponse.SUCCESS.newBuilder().toResult(Collections.emptyList());
  1311. }
  1312. }
  1313. @GetMapping("/get/shop/categoryGoods/{yhsId}")
  1314. public Result<List<YhShopGoodsFrontCategoryView>> getShopCategoryGoods(@PathVariable Long yhsId, Long category) {
  1315. String categoryGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getName() + yhsId + ":category";
  1316. if (category != null) {
  1317. categoryGoodsKey += ":" + category;
  1318. }
  1319. Object value = redisService.get(categoryGoodsKey);
  1320. if (value == null) {
  1321. DateTime now = DateTime.now();
  1322. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  1323. builder.put("yhsId", String.format(" and yhs_id = %s", yhsId));
  1324. String condition = StrUtil.EMPTY;
  1325. if (category != null) {
  1326. condition += String.format(" and gcr.category = %s", category);
  1327. }
  1328. builder.put("condition", condition);
  1329. List<YhShopGoodsFrontCategoryView> list = beanSearcher.searchAll(YhShopGoodsFrontCategoryView.class, builder.build());
  1330. list.forEach(data->{
  1331. data.setSold(getGoodsSold(data.getId(), data.getSold()));
  1332. data.setNotifyMsg(getPayMsgTime(data.getId(), now, 1, yhsId));
  1333. //重新设置最低价格规格
  1334. YhShopGoodsSku sku = yhShopGoodsSkuMapper.selectMinPriceSkuByYhsId(yhsId, data.getId());
  1335. data.setPrice(sku.getPrice());
  1336. data.setSkuId(sku.getSkuId());
  1337. });
  1338. redisService.setNx(categoryGoodsKey, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 5 * 60);
  1339. value = redisService.get(categoryGoodsKey);
  1340. }
  1341. List<YhShopGoodsFrontCategoryView> list = Jsons.parseList(value, YhShopGoodsFrontCategoryView.class);
  1342. list.forEach(data->{
  1343. Optional.ofNullable(yhShopGoodsSkuMapper.selectOne(Wrappers.lambdaQuery(YhShopGoodsSku.class)
  1344. .eq(YhShopGoodsSku::getYhsId, yhsId)
  1345. .eq(YhShopGoodsSku::getGoodsId, data.getId())
  1346. .eq(YhShopGoodsSku::getPrice, data.getPrice())
  1347. .eq(YhShopGoodsSku::getStatus, 1)
  1348. .eq(YhShopGoodsSku::getDeleted, 1)
  1349. .last("limit 1"))).ifPresent(yhSku -> {
  1350. GoodsDonSku sku = goodsDonSkuMapper.selectById(yhSku.getSkuId());
  1351. if (sku != null) {
  1352. data.setMonths(sku.getMonths());
  1353. data.setDays(sku.getDays());
  1354. }
  1355. });
  1356. });
  1357. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  1358. }
  1359. /**
  1360. * 获取设备活动详情
  1361. */
  1362. @GetMapping("/get/equipment/activity")
  1363. public Result<EquipmentActivityReduce> getEpActivity() {
  1364. DateTime now = DateTime.now();
  1365. EquipmentActivityReduce equipmentActivityReduce = beanSearcher.searchFirst(EquipmentActivityReduce.class, MapUtils.builder()
  1366. .field(EquipmentActivityReduce::getSt, now).op(Operator.LessEqual)
  1367. .field(EquipmentActivityReduce::getEt, now).op(Operator.GreaterThan)
  1368. .field(EquipmentActivityReduce::getDeleted, 1)
  1369. .onlySelect(EquipmentActivityReduce::getF, EquipmentActivityReduce::getQ, EquipmentActivityReduce::getTq, EquipmentActivityReduce::getSt, EquipmentActivityReduce::getEt)
  1370. .build());
  1371. return GatewayResponse.SUCCESS.newBuilder().toResult(equipmentActivityReduce);
  1372. }
  1373. /**
  1374. * 获取租赁商品列表
  1375. */
  1376. @GetMapping("/get/deposit")
  1377. public Result<? extends Object> getDepositGoods() {
  1378. String depositGoodsKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "deposit";
  1379. Object o = redisService.get(depositGoodsKey);
  1380. if (o == null) {
  1381. List<GoodsDon> depositList = beanSearcher.searchAll(GoodsDon.class, MapUtils.builder()
  1382. .field(GoodsDon::getType, 4)
  1383. .field(GoodsDon::getStatus, true)
  1384. .field(GoodsDon::getDeleted, true)
  1385. .onlySelect(GoodsDon::getId, GoodsDon::getTitle, GoodsDon::getLogo)
  1386. .build());
  1387. if (CollUtil.isEmpty(depositList)) {
  1388. return GatewayResponse.SUCCESS.newBuilder().toResult();
  1389. }
  1390. redisService.setNx(depositGoodsKey, depositList, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 60 * RandomUtil.randomInt(10));
  1391. o = redisService.get(depositGoodsKey);
  1392. }
  1393. List<GoodsDon> depositList = Jsons.parseList(o, GoodsDon.class);
  1394. return GatewayResponse.SUCCESS.newBuilder().toResult(depositList);
  1395. }
  1396. /**
  1397. * 获取pc广告配置
  1398. */
  1399. @GetMapping("/get/pc/homeManage")
  1400. public Result<Map<String, List<HomeManagementFrontView>>> getPcHomeManagementFrontPage() {
  1401. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "pc:homeManage";
  1402. //移除奈飞客厅 临时
  1403. Long filterConfigId = null;
  1404. // if (isHzIp()) {
  1405. // key += ":hz_ip";
  1406. // filterConfigId = 55l;
  1407. // }
  1408. Object o = redisService.get(key);
  1409. if (o == null) {
  1410. try {
  1411. MapBuilder builder = MapUtils.builder()
  1412. .field(HomeManagementFrontView::getAdType, List.of(HomeManagementConfig.AdType.pc.name(), HomeManagementConfig.AdType.pcBanner.name(), HomeManagementConfig.AdType.pcSec.name(), HomeManagementConfig.AdType.pcDis.name())).op(Operator.InList);
  1413. if (filterConfigId != null) {
  1414. builder.field(HomeManagementFrontView::getHomeManageConfigId, filterConfigId).op(Operator.NotEqual);
  1415. }
  1416. List<HomeManagementFrontView> homeManagementFrontViews = beanSearcher.searchAll(HomeManagementFrontView.class, builder.build());
  1417. Map<HomeManagementConfig.AdType, List<HomeManagementFrontView>> map = homeManagementFrontViews.stream().collect(Collectors.groupingBy(HomeManagementFrontView::getAdType));
  1418. redisService.setNx(key, map, 60 * 60l * 24 * 7);
  1419. o = redisService.get(key);
  1420. }catch (Exception e) {
  1421. redisService.setNx(key, new HashMap<HomeManagementConfig.AdType, List<HomeManagementFrontView>>(), 10L);
  1422. o = redisService.get(key);
  1423. }
  1424. }
  1425. Map<String, List<HomeManagementFrontView>> map = Jsons.parseObject(o, Map.class);
  1426. return GatewayResponse.SUCCESS.newBuilder().toResult(map);
  1427. }
  1428. /**
  1429. * 获取pc实物配置
  1430. */
  1431. @GetMapping("/get/pc/realGoodsConfig")
  1432. public Result<? extends Object> getPcRealGoodsConfig() throws Exception {
  1433. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "pc:realHomePage";
  1434. Object o = redisService.get(key);
  1435. List<HomeManagementFrontView> list = null;
  1436. if (o == null) {
  1437. list = goodsDonFrontService.getRealGoodsConfig();
  1438. redisService.setNx(key, list, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 300);
  1439. }
  1440. if (list == null && o != null) {
  1441. list = Jsons.parseList(o, HomeManagementFrontView.class);
  1442. }
  1443. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  1444. }
  1445. /**
  1446. * 百亿补贴
  1447. * 补贴规格列表
  1448. */
  1449. @GetMapping("/get/subsidy")
  1450. public Result<List<GoodsDonSkuSubsidyFrontView>> getSubsidyGoods(Boolean isFilter) {
  1451. try {
  1452. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "subsidy";
  1453. Long fUid = StpUserUtil.getUserIdAfterLogin();
  1454. Boolean filterGoodsIds = isFilterGoodsIds(fUid, isFilter);
  1455. if (filterGoodsIds) {
  1456. key += ":filter-ip";
  1457. }
  1458. Object o = redisService.get(key);
  1459. if (o == null) {
  1460. // 加锁防止缓存击穿
  1461. String lockKey = key + ":lock";
  1462. if (redisService.setNx(lockKey, "1", 10L)) {
  1463. try {
  1464. o = redisService.get(key);
  1465. if (o == null) {
  1466. MapBuilder builder = MapUtils.builder();
  1467. if (filterGoodsIds) {
  1468. builder.field(GoodsDonSkuSubsidyFrontView::getFilter, Boolean.FALSE);
  1469. }
  1470. List<GoodsDonSkuSubsidyFrontView> subsidyList = beanSearcher.searchAll(GoodsDonSkuSubsidyFrontView.class, builder.build());
  1471. subsidyList.forEach(e -> {
  1472. Optional.ofNullable(beanSearcher.searchFirst(GoodsDonSkuSubsidyDetailView.class, MapUtils.builder().field(GoodsDonSkuSubsidyDetailView::getGoodsId, e.getId()).field(GoodsDonSkuSubsidyDetailView::getPrice, e.getPrice()).build())).ifPresent(sku -> {
  1473. e.setOriPrice(sku.getOriPrice());
  1474. e.setMonths(sku.getMonths());
  1475. e.setDays(sku.getDays());
  1476. e.setSkuId(sku.getId());
  1477. e.setPicture(sku.getSubsidyPicture());
  1478. });
  1479. });
  1480. redisService.set(key, subsidyList, 60 * 60 * 24 * 7L);
  1481. o = subsidyList;
  1482. }
  1483. } finally {
  1484. redisService.del(lockKey);
  1485. }
  1486. } else {
  1487. // 未获取锁,返回空列表
  1488. return GatewayResponse.SUCCESS.newBuilder().toResult(Collections.emptyList());
  1489. }
  1490. }
  1491. List<GoodsDonSkuSubsidyFrontView> subsidyList = Jsons.parseList(o, GoodsDonSkuSubsidyFrontView.class);
  1492. if (CollUtil.isEmpty(subsidyList)) {
  1493. return GatewayResponse.SUCCESS.newBuilder().toResult(subsidyList);
  1494. }
  1495. Long userId = StpUserUtil.getUserIdAfterLogin();
  1496. if (userId != null) {
  1497. List<Long> relationUserIdList = userBindRelationService.getRelationUserIdList(userId, null);
  1498. List<Long> goodsIds = orderDonMapper.getPayGoodsIds(relationUserIdList);
  1499. if (CollUtil.isNotEmpty(goodsIds)) {
  1500. subsidyList = subsidyList.stream().filter(data -> !goodsIds.contains(data.getId())).collect(Collectors.toList());
  1501. }
  1502. }
  1503. return GatewayResponse.SUCCESS.newBuilder().toResult(subsidyList);
  1504. } catch (Exception e) {
  1505. // Redis异常,返回空列表保护数据库
  1506. return GatewayResponse.SUCCESS.newBuilder().toResult(Collections.emptyList());
  1507. }
  1508. }
  1509. /**
  1510. * 百亿补贴规格详情
  1511. */
  1512. @GetMapping("/get/subsidy/detail/{id}")
  1513. public Result<GoodsDonSubsidyDetailView> getSubsidyDetailById(@PathVariable Long id) throws Exception {
  1514. String goodsDetailKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "subsidy:detail:" + id;
  1515. Object o = redisService.get(goodsDetailKey);
  1516. GoodsDonSubsidyDetailView subsidyDetail = null;
  1517. if (o == null) {
  1518. subsidyDetail = beanSearcher.searchFirst(GoodsDonSubsidyDetailView.class, MapUtils.builder().field(GoodsDonSubsidyDetailView::getId, id).build());
  1519. if (subsidyDetail == null) {
  1520. return GatewayResponse.SUCCESS.newBuilder().toResult();
  1521. }
  1522. subsidyDetail.setSpecs(beanSearcher.searchFirst(GoodsDonSpecFrontView.class, MapUtils.builder().field(GoodsDonSpecFrontView::getGoodsId, subsidyDetail.getId()).build()));
  1523. MapBuilder builder = MapUtils.builder().field(GoodsDonSkuSubsidyDetailView::getGoodsId, subsidyDetail.getId());
  1524. subsidyDetail.setSkuList(beanSearcher.searchAll(GoodsDonSkuSubsidyDetailView.class, builder.build()));
  1525. subsidyDetail.setSold(getGoodsSold(subsidyDetail.getId(), subsidyDetail.getSold()));
  1526. redisService.setNx(goodsDetailKey, Jsons.toJson(subsidyDetail), RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + 60 * RandomUtil.randomInt(10));
  1527. }
  1528. if (subsidyDetail == null) {
  1529. o = redisService.get(goodsDetailKey);
  1530. subsidyDetail = Jsons.parseObject(o.toString(), GoodsDonSubsidyDetailView.class);
  1531. }
  1532. return GatewayResponse.SUCCESS.newBuilder().toResult(subsidyDetail);
  1533. }
  1534. /**
  1535. * 可选百亿补贴规格列表
  1536. */
  1537. @GetMapping("/get/available/subsidy/sku/{goodsId}")
  1538. public Result<List<GoodsDonSkuSubsidyAvailableView>> getAvailableSubsidySku(@PathVariable Long goodsId) {
  1539. String key = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "subsidy:" + goodsId;
  1540. Object o = redisService.get(key);
  1541. if (o == null) {
  1542. List<GoodsDonSkuSubsidyAvailableView> goodsDonSkuSubsidyAvailableViews = beanSearcher.searchAll(GoodsDonSkuSubsidyAvailableView.class, MapUtils.builder().put("gid", String.format(" and goods_id = %s", goodsId)).build());
  1543. if (CollUtil.isEmpty(goodsDonSkuSubsidyAvailableViews)) {
  1544. return GatewayResponse.SUCCESS.newBuilder().toResult();
  1545. }
  1546. redisService.set(key, goodsDonSkuSubsidyAvailableViews, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout() + RandomUtil.randomInt(10000));
  1547. o = redisService.get(key);
  1548. }
  1549. List<GoodsDonSkuSubsidyAvailableView> goodsDonSkuSubsidyAvailableViews = Jsons.parseList(o, GoodsDonSkuSubsidyAvailableView.class);
  1550. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsDonSkuSubsidyAvailableViews);
  1551. }
  1552. /**
  1553. * 获取用户店铺首页配置列表
  1554. */
  1555. @GetMapping("/get/shop/homeManage")
  1556. public Result<Map<String, List<HomeManagementFrontView>>> getShopHomeManageConfig(String customId) {
  1557. //是否同意展示对应首页广告位
  1558. String key = RedisService.key.YH_HOME_PAGHE_CACHE_SHOP.getName() + customId + ":homeManage";
  1559. Object o = redisService.get(key);
  1560. if (o == null) {
  1561. MapBuilder builder = MapUtils.builder()
  1562. .field(HomeManagementFrontView::getType, HomeManagementConfig.Type.ad.name())
  1563. .field(HomeManagementFrontView::getIsShop, 1);
  1564. List<HomeManagementFrontView> homeManagementFrontViews = beanSearcher.searchAll(HomeManagementFrontView.class, builder.build());
  1565. if (homeManagementFrontViews == null) {
  1566. return GatewayResponse.SUCCESS.newBuilder().toResult();
  1567. }
  1568. Map<String, List<HomeManagementFrontView>> map = new ConcurrentHashMap<>();
  1569. map.putIfAbsent(HomeManagementConfig.Type.ad.name(), homeManagementFrontViews);
  1570. redisService.setNx(key, map, 50 * 60l);
  1571. o = redisService.get(key);
  1572. }
  1573. Map map = Jsons.parseObject(o, Map.class);
  1574. return GatewayResponse.SUCCESS.newBuilder().toResult(map);
  1575. }
  1576. /**
  1577. * 获取套餐列表
  1578. */
  1579. @GetMapping("/get/upgradePackage")
  1580. public Result<SearchResult<UpgradePackageView>> list(Long skuId) {
  1581. long userId = StpUserUtil.getLoginIdAsLong();
  1582. log.info("userId:{}获取套餐列表", userId);
  1583. Map<String, Object> build = MapUtils.flatBuilder(request.getParameterMap()).build();
  1584. if (skuId != null) {
  1585. build.put("sku", String.format(" and JSON_CONTAINS(up.sku_ids,'\"%s\"')", skuId));
  1586. }
  1587. SearchResult<UpgradePackageView> search = beanSearcher.search(UpgradePackageView.class, build);
  1588. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  1589. }
  1590. /**
  1591. * 获取所有平台列表
  1592. */
  1593. @GetMapping("/get/goods")
  1594. public Result<List<GoodsDon>> getAllGoodsList() {
  1595. List<GoodsDon> goodsDons = beanSearcher.searchAll(GoodsDon.class, MapUtils.flatBuilder(request.getParameterMap()).field(GoodsDon::getStatus, 1)
  1596. .onlySelect(GoodsDon::getId, GoodsDon::getTitle)
  1597. .build());
  1598. return GatewayResponse.SUCCESS.newBuilder().toResult(goodsDons);
  1599. }
  1600. /**
  1601. * 查询ip地址所属地
  1602. */
  1603. @PostMapping("/get/ipAddress")
  1604. public Result<String> getIpAddress(@RequestBody Map<String, Object> params) throws Exception {
  1605. Boolean b = apiVerifyService.verifyApiDo(Jsons.toJson(params));
  1606. if (b) {
  1607. String newestIpAddress = StringUtil.getNewestIpAddress(ServletUtil.getClientIP(request));
  1608. return GatewayResponse.SUCCESS.newBuilder().toResult(newestIpAddress);
  1609. }
  1610. return GatewayResponse.SUCCESS.newBuilder().toResult();
  1611. }
  1612. /**
  1613. * 用户开启到货提醒
  1614. */
  1615. @PostMapping("/user/subscribe/alert")
  1616. public Result<String> userSubscribeAlert(@RequestBody GoodsSubscribeAlertReq goodsSubscribeAlertReq) throws Exception {
  1617. long userId = StpUserUtil.getLoginIdAsLong();
  1618. goodsSubscribeAlertReq.setUserId(userId);
  1619. userSubscribeAlertGoodsRecordService.userSubscribeAlert(goodsSubscribeAlertReq);
  1620. return GatewayResponse.SUCCESS.newBuilder().toResult();
  1621. }
  1622. /**
  1623. * 获取平台销量
  1624. * @param goodsId
  1625. * @param virtualSold
  1626. * @return
  1627. */
  1628. private Integer getGoodsSold(Long goodsId, Integer virtualSold) {
  1629. try {
  1630. String cacheKey = RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "sold:" + goodsId;
  1631. Object soldObj = redisService.get(cacheKey);
  1632. if (soldObj == null) {
  1633. String lockKey = cacheKey + ":lock";
  1634. if (redisService.setNx(lockKey, "1", 10L)) {
  1635. try {
  1636. soldObj = redisService.get(cacheKey);
  1637. if (soldObj == null) {
  1638. Integer sold = orderDonMapper.selectCount(Wrappers.lambdaQuery(OrderDon.class)
  1639. .eq(OrderDon::getGoodsId, goodsId).notIn(OrderDon::getStatus, Constant.noOrderStatus));
  1640. soldObj = sold + virtualSold;
  1641. redisService.set(cacheKey, soldObj, RedisService.key.YH_HOME_PAGHE_CACHE.getTimeout());
  1642. }
  1643. } finally {
  1644. redisService.del(lockKey);
  1645. }
  1646. } else {
  1647. return virtualSold;
  1648. }
  1649. }
  1650. return Integer.parseInt(soldObj.toString());
  1651. } catch (Exception e) {
  1652. // Redis异常,返回虚拟销量保护数据库
  1653. return virtualSold;
  1654. }
  1655. }
  1656. /**
  1657. * 设置平台优惠价格
  1658. */
  1659. private void setUserCouponGoodsMoney(Long userId, GoodsFrontListCategoryView data) {
  1660. //登录后
  1661. if (userId != null) {
  1662. //特定价格
  1663. BigDecimal specificPrice = data.getSpecificPrice();
  1664. //渠道折扣
  1665. BigDecimal channelDiscount = data.getChannelDiscount();
  1666. //支付来源
  1667. Integer source = 0;
  1668. BigDecimal presentCouponMoney = null;
  1669. //寻找对应最低规格的优惠券
  1670. Coupon coupon = couponUserMapper.selectUserCouponByGidAndPrice(userId, data.getId(), data.getPrice());
  1671. if (coupon != null) {
  1672. source = 1;
  1673. data.setDiscount(coupon.getDiscount());
  1674. data.setReduceMoney(coupon.getReduceMoney());
  1675. data.setDiscountPrice(data.getPrice().subtract(coupon.getCouponMoney()));
  1676. data.setValidEndTime(coupon.getValidEndTime());
  1677. data.setCouponId(coupon.getId());
  1678. presentCouponMoney = data.getDiscountPrice();
  1679. }
  1680. if (channelDiscount != null) {
  1681. BigDecimal channelCouponMoney = data.getPrice().multiply(channelDiscount);
  1682. if (presentCouponMoney == null) {
  1683. presentCouponMoney = channelCouponMoney;
  1684. }
  1685. if (channelCouponMoney.compareTo(presentCouponMoney) <= 0) {
  1686. source = 2;
  1687. presentCouponMoney = channelCouponMoney;
  1688. data.setDiscount(channelDiscount);
  1689. data.setReduceMoney(null);
  1690. data.setDiscountPrice(channelCouponMoney);
  1691. data.setValidEndTime(null);
  1692. data.setCouponId(null);
  1693. }
  1694. }
  1695. if (specificPrice != null && specificPrice.compareTo(BigDecimal.ZERO) > 0) {
  1696. if (presentCouponMoney == null) {
  1697. presentCouponMoney = specificPrice;
  1698. }
  1699. if (specificPrice.compareTo(presentCouponMoney) <= 0) {
  1700. source = 3;
  1701. data.setDiscount(null);
  1702. data.setReduceMoney(null);
  1703. data.setDiscountPrice(specificPrice);
  1704. data.setValidEndTime(null);
  1705. data.setCouponId(null);
  1706. }
  1707. }
  1708. data.setSource(source);
  1709. }
  1710. }
  1711. /**
  1712. * 设置平台规格优惠价格
  1713. */
  1714. private void setUserCouponSkuMoney(Long userId, GoodsDonSkuFrontView sku) {
  1715. //登录后
  1716. if (userId != null) {
  1717. //特定价格
  1718. BigDecimal specificPrice = sku.getSpecificPrice();
  1719. //渠道折扣
  1720. BigDecimal channelDiscount = sku.getChannelDiscount();
  1721. //支付来源
  1722. Integer source = 0;
  1723. BigDecimal presentCouponMoney = null;
  1724. //寻找对应最低规格的优惠券
  1725. Coupon coupon = couponUserMapper.selectUserCouponBySkuId(userId, sku.getId());
  1726. if (coupon != null) {
  1727. source = 1;
  1728. sku.setDiscount(coupon.getDiscount());
  1729. sku.setReduceMoney(coupon.getReduceMoney());
  1730. sku.setDiscountPrice(sku.getPrice().subtract(coupon.getCouponMoney()));
  1731. sku.setValidEndTime(coupon.getValidEndTime());
  1732. sku.setCouponId(coupon.getId());
  1733. presentCouponMoney = sku.getDiscountPrice();
  1734. }
  1735. if (channelDiscount != null) {
  1736. BigDecimal channelCouponMoney = sku.getPrice().multiply(channelDiscount);
  1737. if (presentCouponMoney == null) {
  1738. presentCouponMoney = channelCouponMoney;
  1739. }
  1740. if (channelCouponMoney.compareTo(presentCouponMoney) <= 0) {
  1741. source = 2;
  1742. presentCouponMoney = channelCouponMoney;
  1743. sku.setDiscount(channelDiscount);
  1744. sku.setReduceMoney(null);
  1745. sku.setDiscountPrice(channelCouponMoney);
  1746. sku.setValidEndTime(null);
  1747. sku.setCouponId(null);
  1748. }
  1749. }
  1750. if (specificPrice != null && specificPrice.compareTo(BigDecimal.ZERO) > 0) {
  1751. if (presentCouponMoney == null) {
  1752. presentCouponMoney = specificPrice;
  1753. }
  1754. if (specificPrice.compareTo(presentCouponMoney) <= 0) {
  1755. source = 3;
  1756. sku.setDiscount(null);
  1757. sku.setReduceMoney(null);
  1758. sku.setDiscountPrice(specificPrice);
  1759. sku.setValidEndTime(null);
  1760. sku.setCouponId(null);
  1761. }
  1762. }
  1763. sku.setSource(source);
  1764. }
  1765. }
  1766. /**
  1767. * 设置推荐平台优惠价格
  1768. */
  1769. private void setHomeManageUserCouponGoodsMoney(Long userId, HomeManagementFrontView data) {
  1770. //登录后
  1771. if (userId != null) {
  1772. //特定价格
  1773. BigDecimal specificPrice = data.getSpecificPrice();
  1774. //渠道折扣
  1775. BigDecimal channelDiscount = data.getChannelDiscount();
  1776. //支付来源
  1777. Integer source = 0;
  1778. BigDecimal presentCouponMoney = null;
  1779. //寻找对应最低规格的优惠券
  1780. Coupon coupon = couponUserMapper.selectUserCouponByGidAndPrice(userId, data.getGoodsId(), data.getPrice());
  1781. if (coupon != null) {
  1782. source = 1;
  1783. data.setDiscount(coupon.getDiscount());
  1784. data.setReduceMoney(coupon.getReduceMoney());
  1785. data.setDiscountPrice(data.getPrice().subtract(coupon.getCouponMoney()));
  1786. data.setValidEndTime(coupon.getValidEndTime());
  1787. data.setCouponId(coupon.getId());
  1788. presentCouponMoney = data.getDiscountPrice();
  1789. }
  1790. if (channelDiscount != null) {
  1791. BigDecimal channelCouponMoney = data.getPrice().multiply(channelDiscount);
  1792. if (presentCouponMoney == null) {
  1793. presentCouponMoney = channelCouponMoney;
  1794. }
  1795. if (channelCouponMoney.compareTo(presentCouponMoney) <= 0) {
  1796. source = 2;
  1797. presentCouponMoney = channelCouponMoney;
  1798. data.setDiscount(channelDiscount);
  1799. data.setReduceMoney(null);
  1800. data.setDiscountPrice(channelCouponMoney);
  1801. data.setValidEndTime(null);
  1802. data.setCouponId(null);
  1803. }
  1804. }
  1805. if (specificPrice != null && specificPrice.compareTo(BigDecimal.ZERO) > 0) {
  1806. if (presentCouponMoney == null) {
  1807. presentCouponMoney = specificPrice;
  1808. }
  1809. if (specificPrice.compareTo(presentCouponMoney) <= 0) {
  1810. source = 3;
  1811. data.setDiscount(null);
  1812. data.setReduceMoney(null);
  1813. data.setDiscountPrice(specificPrice);
  1814. data.setValidEndTime(null);
  1815. data.setCouponId(null);
  1816. }
  1817. }
  1818. data.setSource(source);
  1819. }
  1820. }
  1821. /**
  1822. * 获取gpt镜像天卡活动参与平台ids
  1823. * @return
  1824. */
  1825. private List<Long> getGptMirrorGiveawayBuyGids() {
  1826. GptMirrorDayCardGiveawayActivity gptMirrorDayCardGiveawayActivity = beanSearcher.searchFirst(GptMirrorDayCardGiveawayActivity.class, MapUtils.builder().build());
  1827. if (gptMirrorDayCardGiveawayActivity != null) {
  1828. String buyGoodsIds = gptMirrorDayCardGiveawayActivity.getBuyGoodsIds();
  1829. try {
  1830. return Jsons.parseList(buyGoodsIds, Long.class);
  1831. } catch (Exception e) {
  1832. }
  1833. }
  1834. return null;
  1835. }
  1836. /**
  1837. * 未登录 分类下平台推荐优惠券
  1838. */
  1839. private void setGoodsCouponMoney(GoodsFrontListCategoryView data) {
  1840. //寻找对应最低规格的推荐优惠券
  1841. Coupon coupon = couponUserMapper.selectNoLoginRecommendCouponReduce(data.getId(), data.getPrice());
  1842. if (coupon != null) {
  1843. data.setDiscount(coupon.getDiscount());
  1844. data.setReduceMoney(coupon.getReduceMoney());
  1845. data.setDiscountPrice(data.getPrice().subtract(coupon.getCouponMoney()));
  1846. data.setValidEndTime(coupon.getValidEndTime());
  1847. data.setCouponId(coupon.getId());
  1848. data.setSource(1);
  1849. }
  1850. }
  1851. /**
  1852. * 未登录 首页推荐推荐优惠券
  1853. */
  1854. private void setHomeConfigGoodsCouponMoney(HomeManagementFrontView data) {
  1855. //寻找对应最低规格的推荐优惠券
  1856. Coupon coupon = couponUserMapper.selectNoLoginRecommendCouponReduce(data.getGoodsId(), data.getPrice());
  1857. if (coupon != null) {
  1858. data.setDiscount(coupon.getDiscount());
  1859. data.setReduceMoney(coupon.getReduceMoney());
  1860. data.setDiscountPrice(data.getPrice().subtract(coupon.getCouponMoney()));
  1861. data.setValidEndTime(coupon.getValidEndTime());
  1862. data.setCouponId(coupon.getId());
  1863. data.setSource(1);
  1864. }
  1865. }
  1866. }