|
@@ -0,0 +1,324 @@
|
|
|
+package com.caimei365.commodity.service.impl;
|
|
|
+
|
|
|
+import com.caimei365.commodity.mapper.*;
|
|
|
+import com.caimei365.commodity.model.ResponseJson;
|
|
|
+import com.caimei365.commodity.model.po.HeheCollageProductPo;
|
|
|
+import com.caimei365.commodity.model.po.ProductDetailInfoPo;
|
|
|
+import com.caimei365.commodity.model.po.ProductParameterPo;
|
|
|
+import com.caimei365.commodity.model.po.SearchHistoryPo;
|
|
|
+import com.caimei365.commodity.model.vo.*;
|
|
|
+import com.caimei365.commodity.service.HeheService;
|
|
|
+import com.caimei365.commodity.service.PageService;
|
|
|
+import com.caimei365.commodity.utils.AppletsLinkUtil;
|
|
|
+import com.caimei365.commodity.utils.ImageUtils;
|
|
|
+import com.caimei365.commodity.utils.MathUtil;
|
|
|
+import com.github.pagehelper.PageHelper;
|
|
|
+import com.github.pagehelper.PageInfo;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+import static com.alibaba.fastjson.JSON.parseArray;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Description
|
|
|
+ *
|
|
|
+ * @author : Charles
|
|
|
+ * @date : 2021/4/9
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class HeheServiceImpl implements HeheService {
|
|
|
+ @Value("${caimei.wwwDomain}")
|
|
|
+ private String domain;
|
|
|
+ @Resource
|
|
|
+ private HeheMapper heheMapper;
|
|
|
+ @Resource
|
|
|
+ private SearchMapper searchMapper;
|
|
|
+ @Resource
|
|
|
+ private PageMapper pageMapper;
|
|
|
+ @Resource
|
|
|
+ private ShopMapper shopMapper;
|
|
|
+ @Resource
|
|
|
+ private SecondHandMapper secondHandMapper;
|
|
|
+ @Autowired
|
|
|
+ private PageService pageService;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseJson<List<BigTypeVo>> getHomeTypeList() {
|
|
|
+ return heheMapper.getHomeTypeList();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseJson<PaginationVo<HeheProductVo>> getProductList(Integer listType, Integer homeTypeId, Integer homeFloorId, Integer smallTypeId, Integer userId, String productName, String productIds, Integer sortType, int pageNum, int pageSize) {
|
|
|
+ PageHelper.startPage(pageNum, pageSize);
|
|
|
+ List<HeheProductVo> productList = heheMapper.getProductList(listType, homeTypeId, homeFloorId, smallTypeId, null, productName, sortType, productIds);
|
|
|
+ productList.forEach(product -> setProductInfo(product, userId));
|
|
|
+ PaginationVo<HeheProductVo> page = new PaginationVo<>(productList);
|
|
|
+ if (1 == listType) {
|
|
|
+ // 保存搜索记录
|
|
|
+ if (StringUtils.isNotEmpty(productName) && userId != null && userId > 0) {
|
|
|
+ //保存并删除大于10条的历史记录
|
|
|
+ SearchHistoryPo historyRecord = new SearchHistoryPo();
|
|
|
+ historyRecord.setUserId(userId);
|
|
|
+ historyRecord.setSearchDate(new Date());
|
|
|
+ historyRecord.setSearchWord(productName);
|
|
|
+ historyRecord.setDelFlag(0);
|
|
|
+ Integer recordId = searchMapper.getHistoryIdByWord(productName, userId);
|
|
|
+ if (recordId != null && recordId > 0) {
|
|
|
+ //更新
|
|
|
+ historyRecord.setId(recordId);
|
|
|
+ searchMapper.updateSearchHistoryById(historyRecord);
|
|
|
+ } else {
|
|
|
+ // 新增
|
|
|
+ searchMapper.insertSearchHistory(historyRecord);
|
|
|
+ }
|
|
|
+ int count = searchMapper.getHistoryCount(userId);
|
|
|
+ if (count > 10) {
|
|
|
+ // 删除大于10条的历史记录
|
|
|
+ searchMapper.deleteSearchHistory(userId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ResponseJson.success(page);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Map<String, Object>> getHomeData(Integer userId) {
|
|
|
+ // 页面数据
|
|
|
+ CmPageVo page = pageMapper.findHeheHomePage();
|
|
|
+ if (page == null) {
|
|
|
+ return ResponseJson.error("首页页面不存在", null);
|
|
|
+ }
|
|
|
+ // 来源:小程序
|
|
|
+ Integer source = 2;
|
|
|
+ //轮播图设置链接类型
|
|
|
+ if (StringUtils.isNotBlank(page.getHeadLink())) {
|
|
|
+ Integer linkType = AppletsLinkUtil.getLinkType(page.getHeadLink());
|
|
|
+ page.setLinkType(linkType);
|
|
|
+ page.setLinkParam(AppletsLinkUtil.getLinkParam(linkType, page.getHeadLink()));
|
|
|
+ }
|
|
|
+ List<PageFloorVo> floorList = pageMapper.getFloorByPageId(page.getId(), source);
|
|
|
+ ListIterator<PageFloorVo> iterator = floorList.listIterator();
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ PageFloorVo floor = iterator.next();
|
|
|
+ FloorContentVo floorContent = pageMapper.getFloorContentByCentreId(floor.getId());
|
|
|
+ if (null == floorContent) {
|
|
|
+ iterator.remove();
|
|
|
+ } else {
|
|
|
+ pageService.setFloorLinkType(floorContent);
|
|
|
+ List<FloorImageVo> floorImageList = pageMapper.getFloorImageByCentreId(floor.getId(), source);
|
|
|
+ setFloorImageProduct(userId, floorImageList);
|
|
|
+ floor.setFloorImageList(floorImageList);
|
|
|
+ floor.setFloorContent(floorContent);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Map<String, Object> map = new HashMap<>(2);
|
|
|
+ map.put("page", page);
|
|
|
+ map.put("floorList", floorList);
|
|
|
+ return ResponseJson.success(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseJson<PaginationVo<HeHeActivityVo>> getActivityList(Integer userId, Integer pageNum, Integer pageSize) {
|
|
|
+ PageHelper.startPage(pageNum, pageSize);
|
|
|
+ List<HeHeActivityVo> activityList = heheMapper.getActivityList(userId);
|
|
|
+ PaginationVo<HeHeActivityVo> pageInfo = new PaginationVo<>(activityList);
|
|
|
+ return ResponseJson.success(pageInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Map<String, Object>> getActivityDetails(Integer userId, Integer activityId, Integer pageNum, Integer pageSize) {
|
|
|
+ Map<String, Object> map = new HashMap<>(2);
|
|
|
+ String image = heheMapper.getActivityImageById(activityId);
|
|
|
+ PageHelper.startPage(pageNum, pageSize);
|
|
|
+ List<HeheProductVo> productList = heheMapper.getProductList(5, null, null, null, activityId, null, 4, null);
|
|
|
+ productList.forEach(product -> setProductInfo(product, userId));
|
|
|
+ PageInfo<HeheProductVo> pageInfo = new PageInfo<>(productList);
|
|
|
+ map.put("image", image);
|
|
|
+ map.put("pageInfo", pageInfo);
|
|
|
+ return ResponseJson.success(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseJson<HeheProductVo> getProductDetails(Integer productId, Integer userId) {
|
|
|
+ HeheProductVo product = heheMapper.getProduct(productId);
|
|
|
+ if (product == null) {
|
|
|
+ //商品不存在
|
|
|
+ return ResponseJson.error("商品不存在", null);
|
|
|
+ }
|
|
|
+ String[] split = null;
|
|
|
+ //商品标签
|
|
|
+ if (product.getTags() != null) {
|
|
|
+ String tags = product.getTags();
|
|
|
+ tags = tags.replace(" ", ",").replace("、", ",").replace(",", ",");
|
|
|
+ split = tags.split(",");
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ for (String s : split) {
|
|
|
+ if (org.apache.commons.lang3.StringUtils.isNotBlank(s)) {
|
|
|
+ list.add(s);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ split = list.toArray(new String[list.size()]);
|
|
|
+ }
|
|
|
+ product.setTagsList(split);
|
|
|
+ //商品图片
|
|
|
+ List<String> imageList = secondHandMapper.getImageByProductId(product.getProductId());
|
|
|
+ imageList.forEach(img -> img = ImageUtils.getImageURL("product", img, 0, domain));
|
|
|
+ product.setImageList(imageList);
|
|
|
+ //商品详情
|
|
|
+ ProductDetailInfoPo productDetail = shopMapper.getProductDetailInfo(productId);
|
|
|
+ product.setProductDetail(productDetail);
|
|
|
+ //相关参数
|
|
|
+ List<ProductParameterPo> parametersList = shopMapper.getProductParameters(productId);
|
|
|
+ product.setParametersList(parametersList);
|
|
|
+ // 商城活动
|
|
|
+ Integer activityId = heheMapper.getActivityIdByProductId(product.getProductId());
|
|
|
+ //内部优惠折扣
|
|
|
+ Integer discount = heheMapper.getProductDiscount(product.getProductId(), userId);
|
|
|
+ //税费
|
|
|
+ boolean addTaxFlag = ("0".equals(product.getIncludedTax()) && ("1".equals(product.getInvoiceType()) || "2".equals(product.getInvoiceType())));
|
|
|
+ if (activityId != null && activityId > 0) {
|
|
|
+ //活动阶梯
|
|
|
+ List<LadderPriceVo> ladderList = heheMapper.getActivityLadderList(activityId, product.getProductId());
|
|
|
+ if (null != discount && discount > 0 && null != ladderList && ladderList.size() > 0) {
|
|
|
+ ladderList.forEach(ladder -> ladder.setBuyPrice(MathUtil.div(MathUtil.mul(ladder.getBuyPrice(), discount), 100).doubleValue()));
|
|
|
+ }
|
|
|
+ if (addTaxFlag && null != ladderList && ladderList.size() > 0) {
|
|
|
+ ladderList.forEach(ladder->{
|
|
|
+ BigDecimal addedValueTax = MathUtil.div(MathUtil.mul(ladder.getBuyPrice(), product.getClubTaxPoint()), BigDecimal.valueOf(100), 2);
|
|
|
+ BigDecimal price = MathUtil.add(ladder.getBuyPrice(), addedValueTax);
|
|
|
+ ladder.setBuyPrice(price.doubleValue());
|
|
|
+ });
|
|
|
+ }
|
|
|
+ product.setLadderList(ladderList);
|
|
|
+ }
|
|
|
+ setProductInfo(product, userId);
|
|
|
+ return ResponseJson.success(product);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setProductInfo(HeheProductVo product, Integer userId) {
|
|
|
+ product.setMainImage(ImageUtils.getImageURL("product", product.getMainImage(), 0, domain));
|
|
|
+ // 活动id
|
|
|
+ Integer activityId = heheMapper.getActivityIdByProductId(product.getProductId());
|
|
|
+ // 拼团商品
|
|
|
+ HeheCollageProductPo collageProduct = heheMapper.getCollageProduct(product.getProductId());
|
|
|
+ // 限时特价
|
|
|
+ BigDecimal discountPrice = heheMapper.getDiscountPrice(product.getProductId());
|
|
|
+ if (activityId != null && activityId > 0) {
|
|
|
+ product.setActiveStatus(1);
|
|
|
+ //活动阶梯
|
|
|
+ List<LadderPriceVo> ladderList = heheMapper.getActivityLadderList(activityId, product.getProductId());
|
|
|
+ if (ladderList != null && ladderList.size() > 0) {
|
|
|
+ LadderPriceVo ladder = ladderList.get(0);
|
|
|
+ // 没有起订量概念,默认显示购买1个商品的价格,若第一阶梯为一个商品,则取第一阶梯时间
|
|
|
+ product.setPrice(1 == ladder.getBuyNum() ? new BigDecimal(ladder.getBuyPrice()) : product.getPrice());
|
|
|
+ }
|
|
|
+ } else if (collageProduct != null) {
|
|
|
+ // 拼团价
|
|
|
+ product.setPrice(collageProduct.getPrice());
|
|
|
+ product.setCollageStatus(1);
|
|
|
+ product.setCollageProduct(collageProduct);
|
|
|
+ } else if (discountPrice != null) {
|
|
|
+ // 限时特价
|
|
|
+ product.setPrice(discountPrice);
|
|
|
+ product.setDiscountStatus(1);
|
|
|
+ }
|
|
|
+ Integer discount = heheMapper.getProductDiscount(product.getProductId(), userId);
|
|
|
+ if (null != discount && discount > 0) {
|
|
|
+ product.setPrice(MathUtil.div(MathUtil.mul(product.getPrice(), discount), 100));
|
|
|
+ product.setNormalPrice(MathUtil.div(MathUtil.mul(product.getNormalPrice(), discount), 100));
|
|
|
+ }
|
|
|
+ //税费
|
|
|
+ boolean addTaxFlag = ("0".equals(product.getIncludedTax()) && ("1".equals(product.getInvoiceType()) || "2".equals(product.getInvoiceType())));
|
|
|
+ if (addTaxFlag) {
|
|
|
+ BigDecimal addedValueTax = MathUtil.div(MathUtil.mul(product.getPrice(), product.getClubTaxPoint()), BigDecimal.valueOf(100), 2);
|
|
|
+ BigDecimal price = MathUtil.add(product.getPrice(), addedValueTax);
|
|
|
+ product.setPrice(price);
|
|
|
+ if (null != product.getNormalPrice()) {
|
|
|
+ addedValueTax = MathUtil.div(MathUtil.mul(product.getNormalPrice(), product.getClubTaxPoint()), BigDecimal.valueOf(100), 2);
|
|
|
+ price = MathUtil.add(product.getNormalPrice(), addedValueTax);
|
|
|
+ product.setNormalPrice(price);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 查询用户的注册时间
|
|
|
+ Date registerTime = heheMapper.getUserRegisterTime(userId);
|
|
|
+ String[] productIdArr = {product.getProductId().toString()};
|
|
|
+ // 用户在该商品上可领取的优惠券列表
|
|
|
+ List<HeheCouponVo> couponList = heheMapper.getCouponList(userId, productIdArr, registerTime, null);
|
|
|
+ if (null != userId) {
|
|
|
+ // 用户在该商品上已领取未使用的优惠券列表
|
|
|
+ List<HeheCouponVo> receiveCouponList = heheMapper.getReceiveCouponList(userId, productIdArr, null, 1);
|
|
|
+ couponList.addAll(receiveCouponList);
|
|
|
+ }
|
|
|
+ // 单价满足优惠条件的优惠券列表
|
|
|
+ List<HeheCouponVo> ableCouponList = new ArrayList<>();
|
|
|
+ // 单价不满足优惠条件的优惠券列表
|
|
|
+ List<HeheCouponVo> unableCouponList = new ArrayList<>();
|
|
|
+ if (couponList.size() > 0) {
|
|
|
+ // 有可用优惠券且商品未参与其他活动
|
|
|
+ couponList.forEach(coupon->{
|
|
|
+ if (1 == coupon.getNoThresholdFlag() || MathUtil.compare(product.getPrice(),coupon.getTouchPrice())>0) {
|
|
|
+ ableCouponList.add(coupon);
|
|
|
+ } else {
|
|
|
+ unableCouponList.add(coupon);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if (ableCouponList.size() > 0) {
|
|
|
+ // 根据优惠金额排序,优惠大的排前面
|
|
|
+ ableCouponList.sort(Comparator.comparing(HeheCouponVo::getCouponAmount));
|
|
|
+ HeheCouponVo biggestCoupon = ableCouponList.get(0);
|
|
|
+ // 现价-最大优惠金额=券后价
|
|
|
+ product.setCouponPrice(MathUtil.sub(product.getPrice(), biggestCoupon.getCouponAmount()));
|
|
|
+ // 原价-最大优惠金额=原价券后价
|
|
|
+ product.setNormalCouponPrice(MathUtil.sub(product.getNormalPrice(), biggestCoupon.getCouponAmount()));
|
|
|
+ // 优惠券id
|
|
|
+ product.setCouponId(biggestCoupon.getCouponId());
|
|
|
+ // 券后价标签
|
|
|
+ product.setCouponStatus(1);
|
|
|
+ } else {
|
|
|
+ // 根据优惠条件排序,条件小的排前面
|
|
|
+ unableCouponList.sort((o1, o2) -> o2.getTouchPrice().compareTo(o1.getTouchPrice()));
|
|
|
+ // 优惠券信息
|
|
|
+ product.setCouponStatus(2);
|
|
|
+ HeheCouponVo smallestCoupon = unableCouponList.get(0);
|
|
|
+ product.setCouponInfo("券|满" + smallestCoupon.getTouchPrice() + "元减" + smallestCoupon.getCouponAmount());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置楼层相关图片的商品信息
|
|
|
+ */
|
|
|
+ private void setFloorImageProduct(Integer userId, List<FloorImageVo> floorImageList) {
|
|
|
+ Iterator<FloorImageVo> iterator = floorImageList.iterator();
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ FloorImageVo image = iterator.next();
|
|
|
+ Integer linkType = AppletsLinkUtil.getLinkType(image.getLink());
|
|
|
+ Map<String, Object> linkParam = AppletsLinkUtil.getLinkParam(linkType, image.getLink());
|
|
|
+ image.setLinkType(linkType);
|
|
|
+ image.setLinkParam(linkParam);
|
|
|
+ if (image.getProductId() != null) {
|
|
|
+ // 获取商品及价格
|
|
|
+ HeheProductVo product = heheMapper.getProduct(image.getProductId());
|
|
|
+ if (product != null) {
|
|
|
+ setProductInfo(product, userId);
|
|
|
+ // 设置商品主图
|
|
|
+ image.setImage(ImageUtils.getImageURL("product", product.getMainImage(), 0, domain));
|
|
|
+ image.setHeheProduct(product);
|
|
|
+ } else {
|
|
|
+ iterator.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|