package com.panzhihua.service_community.service.impl; import static java.util.Objects.isNull; import static java.util.Objects.nonNull; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; 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.DeleteProductDTO; 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)表服务实现类 * * @author makejava * @since 2021-12-28 14:37:12 */ @Service("mcsProductService") public class McsProductServiceImpl extends ServiceImpl 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().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 labelIds = mcsProductDTO.getLabelIds(); if (nonNull(labelIds) && !labelIds.isEmpty()) { List mcsLabels = mcsLabelDAO.selectBatchIds(labelIds); if (nonNull(mcsLabels) && !mcsLabels.isEmpty()) { List 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 labelIds = mcsProductDTO.getLabelIds(); mcsProductLabelDAO.delete(new QueryWrapper().lambda().eq(McsProductLabel::getProductId, mcsProduct.getId())); if (nonNull(labelIds) && !labelIds.isEmpty()) { List mcsLabels = mcsLabelDAO.selectBatchIds(labelIds); if (nonNull(mcsLabels) && !mcsLabels.isEmpty()) { List 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 deleteProductDTO * @return */ @Override public R deleteMcsProduct(DeleteProductDTO deleteProductDTO) { List mcsProducts = this.baseMapper.selectBatchIds(deleteProductDTO.getProductIds()); if (nonNull(mcsProducts) && !mcsProducts.isEmpty()) { mcsProducts.forEach(e -> { e.setIsDel(true); e.setUpdatedBy(deleteProductDTO.getUpdatedBy()); }); this.updateBatchById(mcsProducts); } return R.ok(); } /** * 上架/下架产品信息 * @param setShelfForProductDTO * @return */ @Override public R setShelfForMcsProduct(SetShelfForProductDTO setShelfForProductDTO) { Integer type = setShelfForProductDTO.getType(); List 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 mcsProduct = this.baseMapper.pageMcsProduct(page, pageMcsProductDTO); return R.ok(mcsProduct); } /** * 获取产品信息详情 * @param productId * @return */ @Override public R getMcsProduct(Long productId) { McsProduct mcsProduct = this.baseMapper.selectById(productId); if (isNull(mcsProduct)) { return R.fail("资源不存在"); } McsProductVO mcsProductVO = new McsProductVO(); BeanUtils.copyProperties(mcsProduct, mcsProductVO); List mcsProductLabelList = mcsProductLabelDAO.selectList(new QueryWrapper().lambda() .eq(McsProductLabel::getProductId, productId)); if (nonNull(mcsProductLabelList) && !mcsProductLabelList.isEmpty()) { List labelIds = mcsProductLabelList.stream().map(McsProductLabel::getLabelId).collect(Collectors.toList()); mcsProductVO.setLabelIds(labelIds); } return R.ok(mcsProductVO); } }