puhanshu
2022-01-05 eb80b83a3d4a0b59325e90405dc6c687c2904d3a
springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/McsProductServiceImpl.java
@@ -1,10 +1,35 @@
package com.panzhihua.service_community.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panzhihua.service_community.dao.McsProductDAO;
import com.panzhihua.service_community.entity.McsProduct;
import com.panzhihua.service_community.service.McsProductService;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.McsProductDTO;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.PageMcsProductDTO;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.SetShelfForProductDTO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.microCommercialStreet.McsProductVO;
import com.panzhihua.common.utlis.Snowflake;
import com.panzhihua.service_community.dao.McsLabelDAO;
import com.panzhihua.service_community.dao.McsMerchantDAO;
import com.panzhihua.service_community.dao.McsProductDAO;
import com.panzhihua.service_community.dao.McsProductLabelDAO;
import com.panzhihua.service_community.entity.McsLabel;
import com.panzhihua.service_community.entity.McsMerchant;
import com.panzhihua.service_community.entity.McsProduct;
import com.panzhihua.service_community.entity.McsProductLabel;
import com.panzhihua.service_community.service.McsProductService;
/**
 * (McsProduct)表服务实现类
@@ -15,4 +40,149 @@
@Service("mcsProductService")
public class McsProductServiceImpl extends ServiceImpl<McsProductDAO, McsProduct> implements McsProductService {
    @Resource
    private McsMerchantDAO mcsMerchantDAO;
    @Resource
    private McsLabelDAO mcsLabelDAO;
    @Resource
    private McsProductLabelDAO mcsProductLabelDAO;
    /**
     * 新增产品信息
     * @param mcsProductDTO
     * @return
     */
    @Override
    public R addMcsProduct(McsProductDTO mcsProductDTO) {
        Long userId = mcsProductDTO.getCreatedBy();
        McsMerchant mcsMerchant = mcsMerchantDAO.selectOne(new QueryWrapper<McsMerchant>().lambda().eq(McsMerchant::getUserId, userId));
        if (isNull(mcsMerchant)) {
            return R.fail("商家信息不存在");
        }
        McsProduct mcsProduct = new McsProduct();
        BeanUtils.copyProperties(mcsProductDTO, mcsProduct);
        mcsProduct.setMerchantId(mcsMerchant.getId());
        mcsProduct.setStatus(McsProduct.Status.yxj);
        mcsProduct.setId(Snowflake.getId());
        int num = this.baseMapper.insert(mcsProduct);
        if (num > 0) {
            List<Long> labelIds = mcsProductDTO.getLabelIds();
            if (nonNull(labelIds) && !labelIds.isEmpty()) {
                List<McsLabel> mcsLabels = mcsLabelDAO.selectBatchIds(labelIds);
                if (nonNull(mcsLabels) && !mcsLabels.isEmpty()) {
                    List<McsProductLabel> mcsProductLabelList = new ArrayList<>();
                    mcsLabels.forEach(e -> {
                        McsProductLabel productLabel = new McsProductLabel();
                        productLabel.setLabelId(e.getId());
                        productLabel.setProductId(mcsProduct.getId());
                        productLabel.setLabelName(e.getName());
                        mcsProductLabelList.add(productLabel);
                    });
                    mcsProductLabelDAO.insertBatch(mcsProductLabelList);
                }
            }
            return R.ok();
        }
        return R.fail("新增失败,请重新尝试");
    }
    /**
     * 编辑产品信息
     * @param mcsProductDTO
     * @return
     */
    @Override
    public R putMcsProduct(McsProductDTO mcsProductDTO) {
        McsProduct mcsProduct = this.baseMapper.selectById(mcsProductDTO.getId());
        if (isNull(mcsProduct)) {
            return R.fail("资源不存在");
        }
        BeanUtils.copyProperties(mcsProductDTO, mcsProduct);
        int num = this.baseMapper.updateById(mcsProduct);
        if (num > 0) {
            List<Long> labelIds = mcsProductDTO.getLabelIds();
            mcsProductLabelDAO.delete(new QueryWrapper<McsProductLabel>().lambda().eq(McsProductLabel::getProductId, mcsProduct.getId()));
            if (nonNull(labelIds) && !labelIds.isEmpty()) {
                List<McsLabel> mcsLabels = mcsLabelDAO.selectBatchIds(labelIds);
                if (nonNull(mcsLabels) && !mcsLabels.isEmpty()) {
                    List<McsProductLabel> mcsProductLabelList = new ArrayList<>();
                    mcsLabels.forEach(e -> {
                        McsProductLabel productLabel = new McsProductLabel();
                        productLabel.setLabelId(e.getId());
                        productLabel.setProductId(mcsProduct.getId());
                        productLabel.setLabelName(e.getName());
                        mcsProductLabelList.add(productLabel);
                    });
                    mcsProductLabelDAO.insertBatch(mcsProductLabelList);
                }
            }
            return R.ok();
        }
        return R.fail("编辑失败,请重新尝试");
    }
    /**
     * 删除产品信息
     * @param productId
     * @param userId
     * @return
     */
    @Override
    public R deleteMcsProduct(Long productId, Long userId) {
        McsProduct mcsProduct = this.baseMapper.selectById(productId);
        if (isNull(mcsProduct)) {
            return R.fail("资源不存在");
        }
        mcsProduct.setIsDel(true);
        mcsProduct.setUpdatedBy(userId);
        int num = this.baseMapper.updateById(mcsProduct);
        if (num > 0) {
            return R.ok();
        }
        return R.fail("删除失败,请重新尝试");
    }
    /**
     * 上架/下架产品信息
     * @param setShelfForProductDTO
     * @return
     */
    @Override
    public R setShelfForMcsProduct(SetShelfForProductDTO setShelfForProductDTO) {
        Integer type = setShelfForProductDTO.getType();
        List<McsProduct> mcsProducts = this.baseMapper.selectBatchIds(setShelfForProductDTO.getProductIds());
        if (nonNull(mcsProducts) && !mcsProducts.isEmpty()) {
            if (type.equals(1)) {
                //上架
                mcsProducts.forEach(e -> {
                    e.setStatus(McsProduct.Status.sjz);
                    e.setUpdatedBy(setShelfForProductDTO.getUpdatedBy());
                });
            } else if (type.equals(2)) {
                //下架
                mcsProducts.forEach(e -> {
                    e.setStatus(McsProduct.Status.yxj);
                    e.setUpdatedBy(setShelfForProductDTO.getUpdatedBy());
                });
            } else {
                return R.fail("未知错误");
            }
            this.updateBatchById(mcsProducts);
        }
        return R.ok();
    }
    /**
     * 分页查询产品信息
     * @param pageMcsProductDTO
     * @return
     */
    @Override
    public R pageMcsProduct(PageMcsProductDTO pageMcsProductDTO) {
        Page page = new Page<>();
        page.setSize(pageMcsProductDTO.getPageSize());
        page.setCurrent(pageMcsProductDTO.getPageNum());
        IPage<McsProductVO> mcsProduct = this.baseMapper.pageMcsProduct(page, pageMcsProductDTO);
        return R.ok(mcsProduct);
    }
}