yanghui
2022-11-26 8da7ca5f5a5466c76b0d0abe23c7935e13e09136
springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComShopFlowerGoodsServiceImpl.java
@@ -1,5 +1,6 @@
package com.panzhihua.service_community.service.impl;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -13,20 +14,26 @@
import com.panzhihua.common.model.vos.community.convenient.ConvenientMerchantVO;
import com.panzhihua.common.model.vos.shop.*;
import com.panzhihua.common.utlis.DifferentLongListUtil;
import com.panzhihua.service_community.dao.ComShopFlowerGoodsAttrDAO;
import com.panzhihua.service_community.dao.ComShopFlowerGoodsDAO;
import com.panzhihua.service_community.dao.ConvenientMerchantDAO;
import com.panzhihua.common.utlis.Snowflake;
import com.panzhihua.common.utlis.StringUtils;
import com.panzhihua.service_community.dao.*;
import com.panzhihua.service_community.model.dos.*;
import com.panzhihua.service_community.service.ComShopFlowerGoodsAttrService;
import com.panzhihua.service_community.service.ComShopFlowerGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
/**
 * @ClassName: ComShopFlowerGoodsServiceImpl
@@ -44,24 +51,30 @@
    private ComShopFlowerGoodsAttrDAO shopFlowerGoodsAttrDAO;
    @Resource
    private ConvenientMerchantDAO convenientMerchantDAO;
    @Resource
    private ConvenientGoodsCategoryDAO convenientGoodsCategoryDAO;
    @Resource
    private ComShopFlowerOrderGoodsDAO comShopFlowerOrderGoodsDAO;
    @Resource
    private ComShopFlowerEvaluateDAO comShopFlowerEvaluateDAO;
    @Resource
    private ComShopFlowerGoodsViewStatisticsDAO comShopFlowerGoodsViewStatisticsDAO;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveShopFlowerGoods(AddShopFlowerGoodsVO addShopFlowerGoodsVO) {
        if (addShopFlowerGoodsVO == null) {
            return R.ok("500", "数据为空!");
        }
        String[] images = addShopFlowerGoodsVO.getGoodsPic().split(",");
        ComShopFlowerGoodsDO shopGoodsDO = new ComShopFlowerGoodsDO();
        BeanUtils.copyProperties(addShopFlowerGoodsVO, shopGoodsDO);
        shopGoodsDO.setOriginalPrice(addShopFlowerGoodsVO.getPrice());
        if (images.length > 1) {
            shopGoodsDO.setGoodsPic(images[0]);
        }
        shopGoodsDO.setImages(addShopFlowerGoodsVO.getGoodsPic());
        shopGoodsDO.setSale(0);
        shopGoodsDO.setDetails(addShopFlowerGoodsVO.getDetails());
        int goodsId = this.baseMapper.insert(shopGoodsDO);
        if (goodsId > 0) {
            //保存规格
            ArrayList<ComShopFlowerGoodsAttrDO> goodsAttrDOS = Lists.newArrayList();
            ComShopFlowerGoodsDO goodDO = this.baseMapper.selectById(shopGoodsDO.getId());
            addShopFlowerGoodsVO.getGoodsAttrVOList().forEach(goodsAttr -> {
@@ -86,7 +99,21 @@
            if (goodsAttrDOS.size() > 0) {
                comShopFlowerGoodsAttrService.saveBatch(goodsAttrDOS);
            }
            //保存商品分类
            Long goodId = shopGoodsDO.getId();
            if (nonNull(goodId)) {
                //添加分类
                List<Long> categoryIds = addShopFlowerGoodsVO.getCategoryIds();
                categoryIds.forEach(categoryId -> {
                    ConvenientGoodsCategoryDO convenientGoodsCategoryDO = convenientGoodsCategoryDAO.selectById(categoryId);
                    convenientGoodsCategoryDAO.createGoodsCategoryRelation(Snowflake.getId(),goodId, categoryId,
                            convenientGoodsCategoryDO.getName(), convenientGoodsCategoryDO.getCreatedBy());
                });
            }
        }
        return R.ok();
    }
@@ -111,9 +138,13 @@
                for (ComShopFlowerGoodsAttrDO oneGoodsAttrDO : goodsAttrDOs) {
                    AddShopFlowerGoodsAttrVO goodsAttrVO = new AddShopFlowerGoodsAttrVO();
                    BeanUtils.copyProperties(oneGoodsAttrDO, goodsAttrVO);
                    goodsAttrVO.setGoodsAttrId(oneGoodsAttrDO.getId());
                    goodsAttrVOList.add(goodsAttrVO);
                }
                shopGoods.setGoodsAttrs(goodsAttrVOList);
                if (CollUtil.isNotEmpty(goodsAttrDOs)){
                    shopGoods.setGoodsPic(goodsAttrDOs.get(0).getAttrPic());
                }
            });
        }
        return R.ok(comShopStoreVOIPage);
@@ -132,15 +163,18 @@
        IPage<ComShopFlowerGoodsVO> goodsVOIPage = this.baseMapper.pageShopGoodByApps(page, comShopFlowerGoodsDTO);
        if (!goodsVOIPage.getRecords().isEmpty()) {
            goodsVOIPage.getRecords().forEach(goodsVo -> {
                List<ComShopFlowerGoodsAttrVO> goodsAttrList = shopFlowerGoodsAttrDAO.getGoodsAttr(goodsVo.getId());
                Long goodsId = goodsVo.getId();
                List<ComShopFlowerGoodsAttrVO> goodsAttrList = shopFlowerGoodsAttrDAO.getGoodsAttr(goodsId);
                if (!goodsAttrList.isEmpty()) {
                    ComShopFlowerGoodsAttrVO comShopFlowerGoodsAttrVO = goodsAttrList.get(0);
                    goodsVo.setGoodsAttrList(goodsAttrList);
                    goodsVo.setGoodsPic(comShopFlowerGoodsAttrVO.getAttrPic());
                    goodsVo.setOriginalPrice(comShopFlowerGoodsAttrVO.getPrice());
                    goodsVo.setPrice(comShopFlowerGoodsAttrVO.getCollatePrice());
                    goodsVo.setCollatePrice(comShopFlowerGoodsAttrVO.getCollatePrice());
                    goodsVo.setFreeShippingPrice(comShopFlowerGoodsAttrVO.getFreeShippingPrice());
                }
                //设置商品总浏览量
                goodsVo.setViewNum(comShopFlowerGoodsViewStatisticsDAO.selectTotalViewNum(goodsId));
            });
        }
        return R.ok(goodsVOIPage);
@@ -161,12 +195,16 @@
        List<ComShopFlowerGoodsAttrVO> goodsAttrList = new ArrayList<>();
        List<ComShopFlowerGoodsAttrDO> goodsAttrDOS =
                shopFlowerGoodsAttrDAO.selectList(new QueryWrapper<ComShopFlowerGoodsAttrDO>().eq("goods_id", goodsId));
        goodsAttrDOS.forEach(attrDO -> {
        List<String> images = new ArrayList<>();
        int stock = 0;
        for (ComShopFlowerGoodsAttrDO attrDO : goodsAttrDOS) {
            ComShopFlowerGoodsAttrVO goodsAttrVO = new ComShopFlowerGoodsAttrVO();
            BeanUtils.copyProperties(attrDO, goodsAttrVO);
            String attrPic = attrDO.getAttrPic();
            images.add(attrPic);
            goodsAttrList.add(goodsAttrVO);
        });
            stock = stock +attrDO.getStock();
        }
        // 查询商品店铺信息
        ConvenientMerchantDO convenientMerchantDO = convenientMerchantDAO.selectById(goodsDO.getStoreId());
        ConvenientMerchantVO convenientMerchantVO = new ConvenientMerchantVO();
@@ -177,10 +215,26 @@
        BeanUtils.copyProperties(goodsDO, shopGoods);
        shopGoods.setGoodsAttrList(goodsAttrList);
        shopGoods.setConvenientMerchantVO(convenientMerchantVO);
        shopGoods.setImages(StringUtils.join(images,","));
        Integer orderNum = comShopFlowerOrderGoodsDAO.selectCount(new LambdaQueryWrapper<ComShopFlowerOrderGoodsDO>().eq(ComShopFlowerOrderGoodsDO::getGoodsId, goodsId));
        shopGoods.setOrderNum(orderNum);
        List<ComShopFlowerEvaluateVO> comShopFlowerEvaluateVOList = comShopFlowerEvaluateDAO.selectListLimit(goodsId);
        shopGoods.setShopFlowerEvaluateVOList(comShopFlowerEvaluateVOList);
        List<Long> categoryIds = convenientGoodsCategoryDAO.selectCategoryIdsForGoods(goodsId);
        shopGoods.setCategoryIds(categoryIds);
        ComShopFlowerGoodsAttrVO comShopFlowerGoodsAttrVO = goodsAttrList.get(0);
        shopGoods.setOriginalPrice(comShopFlowerGoodsAttrVO.getPrice());
        shopGoods.setCollatePrice(comShopFlowerGoodsAttrVO.getCollatePrice());
        shopGoods.setFreeShippingPrice(comShopFlowerGoodsAttrVO.getFreeShippingPrice());
        shopGoods.setStock(stock);
        return R.ok(shopGoods);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R editFlowerShopGoods(Long id, AddShopFlowerGoodsVO addShopFlowerGoodsVO) {
        if (addShopFlowerGoodsVO == null) {
            return R.ok("500", "数据为空!");
@@ -231,7 +285,7 @@
                    shopFlowerGoodsAttrDAO.updateById(comShopGoodsAttrDO);
                } else {
                    comShopGoodsAttrDO = new ComShopFlowerGoodsAttrDO();
                    comShopGoodsAttrDO.setGoodsId(shopGoodsDO.getId());
                    comShopGoodsAttrDO.setGoodsId(id);
                    comShopGoodsAttrDO.setStoreId(shopGoodsDO.getStoreId());
                    comShopGoodsAttrDO.setGoodsName(shopGoodsDO.getName());
                    comShopGoodsAttrDO.setGoodsAttrName(goodsAttr.getGoodsAttrName());
@@ -250,17 +304,16 @@
    }
    @Override
    public R deleteShopFlowerGoods(Long[] id) {
        LambdaQueryWrapper<ComShopFlowerGoodsDO> query = new LambdaQueryWrapper<ComShopFlowerGoodsDO>().in(ComShopFlowerGoodsDO::getId, id);
        List<ComShopFlowerGoodsDO> comShopGoodsDOS = this.baseMapper.selectList(query);
        for (ComShopFlowerGoodsDO one : comShopGoodsDOS) {
            one.setDeleteStatus(ComShopFlowerGoodsDO.deleteStatus.yes);
            this.baseMapper.updateById(one);
        }
    @Transactional(rollbackFor = Exception.class)
    public R deleteShopFlowerGoods(Long id) {
        ComShopFlowerGoodsDO comShopFlowerGoodsDO = this.baseMapper.selectById(id);
        comShopFlowerGoodsDO.setDeleteStatus(ComShopFlowerGoodsDO.deleteStatus.yes);
        this.baseMapper.updateById(comShopFlowerGoodsDO);
        return R.ok();
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R changeStatus(Long id, Integer status) {
        ComShopFlowerGoodsDO shopGoodsDO = this.baseMapper.selectById(id);
        if (shopGoodsDO == null) {
@@ -270,4 +323,22 @@
        this.baseMapper.updateById(shopGoodsDO);
        return R.ok();
    }
    @Override
    public R incrGoodsView(Long goodsId) {
        ComShopFlowerGoodsDO comShopFlowerGoodsDO = this.baseMapper.selectById(goodsId);
        if (isNull(comShopFlowerGoodsDO)) {
            return R.fail("商品不存在");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String nowDate = simpleDateFormat.format(new Date());
        ComShopFlowerGoodsViewStatisticsDO statisticsDO = comShopFlowerGoodsViewStatisticsDAO.selectOne(new LambdaQueryWrapper<ComShopFlowerGoodsViewStatisticsDO>()
                .eq(ComShopFlowerGoodsViewStatisticsDO::getGoodsId, goodsId).eq(ComShopFlowerGoodsViewStatisticsDO::getStatisticDate, nowDate));
        if (isNull(statisticsDO)) {
            comShopFlowerGoodsViewStatisticsDAO.createTodayStatistic(goodsId);
        } else {
            comShopFlowerGoodsViewStatisticsDAO.incrViewNum(goodsId, nowDate);
        }
        return R.ok();
    }
}