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.Comparator;
|
import java.util.Date;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
import java.util.stream.Collectors;
|
|
import javax.annotation.Resource;
|
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
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.convenient.ConvenientProductDTO;
|
import com.panzhihua.common.model.dtos.community.convenient.ConvenientProductSpecificationDTO;
|
import com.panzhihua.common.model.dtos.community.convenient.DeleteConvenientProductDTO;
|
import com.panzhihua.common.model.dtos.community.convenient.OnShelfOrOffShelfProductDTO;
|
import com.panzhihua.common.model.dtos.community.convenient.PageConvenientProductDTO;
|
import com.panzhihua.common.model.dtos.community.convenient.PageSearchDTO;
|
import com.panzhihua.common.model.vos.R;
|
import com.panzhihua.common.model.vos.community.convenient.ConvenientProductLevelInfoVO;
|
import com.panzhihua.common.model.vos.community.convenient.ConvenientProductShelfVO;
|
import com.panzhihua.common.model.vos.community.convenient.ConvenientProductSpecificationVO;
|
import com.panzhihua.common.model.vos.community.convenient.ConvenientProductVO;
|
import com.panzhihua.service_community.dao.ConvenientProductCategoryDAO;
|
import com.panzhihua.service_community.dao.ConvenientProductDAO;
|
import com.panzhihua.service_community.dao.ConvenientProductSpecificationDAO;
|
import com.panzhihua.service_community.model.dos.ConvenientProductCategoryDO;
|
import com.panzhihua.service_community.model.dos.ConvenientProductDO;
|
import com.panzhihua.service_community.model.dos.ConvenientProductSpecificationDO;
|
import com.panzhihua.service_community.service.ConvenientProductService;
|
|
/**
|
* @title: ConvenientProductServiceImpl
|
* @projectName: 成都呐喊信息技术有限公司-智慧社区项目
|
* @description: 便民服务商家后台产品服务实现类
|
* @author: hans
|
* @date: 2021/09/21 21:33
|
*/
|
@Service
|
public class ConvenientProductServiceImpl extends ServiceImpl<ConvenientProductDAO, ConvenientProductDO> implements ConvenientProductService {
|
|
@Resource
|
private ConvenientProductCategoryDAO convenientProductCategoryDAO;
|
@Resource
|
private ConvenientProductSpecificationDAO convenientProductSpecificationDAO;
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public R addProduct(ConvenientProductDTO convenientProductDTO) {
|
ConvenientProductCategoryDO convenientProductCategoryDO = convenientProductCategoryDAO.selectById(convenientProductDTO.getCategoryId());
|
if (isNull(convenientProductCategoryDO)) {
|
return R.fail("产品分类不存在");
|
}
|
ConvenientProductDO convenientProductDO = new ConvenientProductDO();
|
BeanUtils.copyProperties(convenientProductDTO, convenientProductDO);
|
Date nowDate = new Date();
|
if (convenientProductDTO.getOnShelf()) {
|
convenientProductDO.setOnShelfAt(nowDate);
|
}
|
convenientProductDO.setCreatedAt(nowDate);
|
this.baseMapper.insert(convenientProductDO);
|
Long productId = convenientProductDO.getId();
|
if (nonNull(productId)) {
|
//规格
|
List<ConvenientProductSpecificationDTO> productSpecificationDTOList = convenientProductDTO.getProductSpecificationDTOList();
|
List<ConvenientProductSpecificationDO> productSpecificationDOList = productSpecificationDTOList.stream().map(specificationDTO -> {
|
ConvenientProductSpecificationDO convenientProductSpecificationDO = new ConvenientProductSpecificationDO();
|
BeanUtils.copyProperties(specificationDTO, convenientProductSpecificationDO);
|
convenientProductSpecificationDO.setProductId(productId);
|
convenientProductSpecificationDO.setCreatedAt(nowDate);
|
convenientProductSpecificationDO.setCreatedBy(convenientProductDTO.getCreatedBy());
|
return convenientProductSpecificationDO;
|
}).collect(Collectors.toList());
|
convenientProductSpecificationDAO.batchInsert(productSpecificationDOList);
|
}
|
return R.ok();
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public R putProduct(ConvenientProductDTO convenientProductDTO) {
|
Long productId = convenientProductDTO.getId();
|
Long updatedBy = convenientProductDTO.getUpdatedBy();
|
ConvenientProductDO convenientProductDO = this.baseMapper.selectById(productId);
|
Boolean isOnShelf = convenientProductDO.getOnShelf();
|
if (isNull(convenientProductDO)) {
|
return R.fail("产品不存在");
|
}
|
BeanUtils.copyProperties(convenientProductDTO, convenientProductDO);
|
Date nowDate = new Date();
|
if (!isOnShelf && convenientProductDTO.getOnShelf()) {
|
convenientProductDO.setOnShelfAt(nowDate);
|
}
|
convenientProductDO.setUpdatedBy(updatedBy);
|
this.baseMapper.updateById(convenientProductDO);
|
List<ConvenientProductSpecificationDTO> productSpecificationDTOList = convenientProductDTO.getProductSpecificationDTOList();
|
List<Long> notNeedDelIds = new ArrayList<>();
|
productSpecificationDTOList.forEach(specificationDTO -> {
|
Long specificationId = specificationDTO.getId();
|
if (isNull(specificationId)) {
|
//新增
|
ConvenientProductSpecificationDO convenientProductSpecificationDO = new ConvenientProductSpecificationDO();
|
BeanUtils.copyProperties(specificationDTO, convenientProductSpecificationDO);
|
convenientProductSpecificationDO.setProductId(productId);
|
convenientProductSpecificationDO.setCreatedAt(nowDate);
|
convenientProductSpecificationDO.setCreatedBy(updatedBy);
|
convenientProductSpecificationDAO.insert(convenientProductSpecificationDO);
|
notNeedDelIds.add(convenientProductSpecificationDO.getId());
|
} else {
|
//更新
|
ConvenientProductSpecificationDO convenientProductSpecificationDO = convenientProductSpecificationDAO.selectById(specificationId);
|
BeanUtils.copyProperties(specificationDTO, convenientProductSpecificationDO);
|
convenientProductSpecificationDO.setUpdatedBy(updatedBy);
|
convenientProductSpecificationDAO.updateById(convenientProductSpecificationDO);
|
notNeedDelIds.add(specificationId);
|
}
|
});
|
//删除已失去关联的规格
|
convenientProductSpecificationDAO.deleteLoseRelationSpecifications(notNeedDelIds, productId);
|
return R.ok();
|
}
|
|
@Override
|
public R getProduct(Long productId) {
|
if (isNull(productId)) {
|
return R.fail("产品ID不能为空");
|
}
|
ConvenientProductDO convenientProductDO = this.baseMapper.selectById(productId);
|
if (isNull(convenientProductDO)) {
|
return R.fail("产品不存在");
|
}
|
ConvenientProductVO convenientProductVO = new ConvenientProductVO();
|
BeanUtils.copyProperties(convenientProductDO, convenientProductVO);
|
//查找产品规格
|
List<ConvenientProductSpecificationDO> convenientProductSpecificationDOList = convenientProductSpecificationDAO.selectList(new LambdaQueryWrapper<ConvenientProductSpecificationDO>()
|
.eq(ConvenientProductSpecificationDO::getProductId, productId).eq(ConvenientProductSpecificationDO::getIsDel, false));
|
List<ConvenientProductSpecificationVO> convenientProductSpecificationVOList = new ArrayList<>();
|
if (!convenientProductSpecificationDOList.isEmpty()) {
|
convenientProductSpecificationDOList.forEach(specificationDO -> {
|
ConvenientProductSpecificationVO convenientProductSpecificationVO = new ConvenientProductSpecificationVO();
|
BeanUtils.copyProperties(specificationDO, convenientProductSpecificationVO);
|
convenientProductSpecificationVOList.add(convenientProductSpecificationVO);
|
});
|
}
|
convenientProductVO.setProductSpecificationVOList(convenientProductSpecificationVOList);
|
return R.ok(convenientProductVO);
|
}
|
|
@Override
|
public R pageProduct(PageConvenientProductDTO pageConvenientProductDTO) {
|
Page page = new Page<>();
|
page.setSize(pageConvenientProductDTO.getPageSize());
|
page.setCurrent(pageConvenientProductDTO.getPageNum());
|
IPage<ConvenientProductVO> productVOIPage = this.baseMapper.pageProduct(page, pageConvenientProductDTO);
|
List<ConvenientProductVO> convenientProductVOList = productVOIPage.getRecords();
|
//图片填充
|
if (!convenientProductVOList.isEmpty()) {
|
convenientProductVOList.forEach(product -> {
|
List<ConvenientProductSpecificationDO> specificationDOList = convenientProductSpecificationDAO.selectList(new LambdaQueryWrapper<ConvenientProductSpecificationDO>()
|
.eq(ConvenientProductSpecificationDO::getProductId, product.getId()).isNotNull(ConvenientProductSpecificationDO::getImage));
|
product.setImage(specificationDOList.isEmpty() ? null : specificationDOList.get(0).getImage());
|
});
|
}
|
return R.ok(productVOIPage);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public R deleteProduct(DeleteConvenientProductDTO deleteConvenientProductDTO) {
|
List<Long> needDelIds = deleteConvenientProductDTO.getIds();
|
//删除产品
|
int result = this.baseMapper.batchDeleteByIds(needDelIds);
|
if (result > 0) {
|
//删除规格
|
convenientProductSpecificationDAO.batchDeleteByProductIds(needDelIds);
|
}
|
return R.ok();
|
}
|
|
@Override
|
public R onShelfOrOffShelfProduct(OnShelfOrOffShelfProductDTO onShelfOrOffShelfProductDTO) {
|
Integer type = onShelfOrOffShelfProductDTO.getType();
|
Long updatedBy = onShelfOrOffShelfProductDTO.getUpdatedBy();
|
List<Long> needDelIds = onShelfOrOffShelfProductDTO.getIds();
|
if (type.intValue() == 1) {
|
//上架
|
this.baseMapper.batchOnShelfOrOffShelfByIds(needDelIds, updatedBy, true);
|
} else if (type.intValue() == 2) {
|
//下架
|
this.baseMapper.batchOnShelfOrOffShelfByIds(needDelIds, updatedBy, false);
|
} else {
|
return R.fail("处理类型错误");
|
}
|
return R.ok();
|
}
|
|
/**
|
* 小程序获取商家产品
|
* @param merchantId
|
* @return
|
*/
|
@Override
|
public R getMerchantProduct(Long merchantId) {
|
List<ConvenientProductVO> productVOList = this.baseMapper.getMerchantProduct(merchantId);
|
List<ConvenientProductSpecificationVO> specificationVOList = this.baseMapper.getProductSpecifications(merchantId);
|
List<ConvenientProductLevelInfoVO> levelInfoVOList = new ArrayList<>();
|
List<String> categoryList = new ArrayList<>();
|
if (!productVOList.isEmpty()) {
|
productVOList.forEach(productVO -> {
|
List<ConvenientProductSpecificationVO> currentProductSpecificationVOList = new ArrayList<>();
|
specificationVOList.forEach(specificationVO -> {
|
if (productVO.getId().equals(specificationVO.getProductId())) {
|
currentProductSpecificationVOList.add(specificationVO);
|
}
|
});
|
productVO.setProductSpecificationVOList(currentProductSpecificationVOList);
|
if (!categoryList.contains(productVO.getCategoryName())) {
|
categoryList.add(productVO.getCategoryName());
|
}
|
});
|
//分类
|
Map<String, List<ConvenientProductVO>> levelGroup = productVOList.stream()
|
.collect(Collectors.groupingBy(ConvenientProductVO::getCategoryName));
|
categoryList.forEach(category -> {
|
ConvenientProductLevelInfoVO levelInfoVOLevelInfoVO = new ConvenientProductLevelInfoVO();
|
levelInfoVOLevelInfoVO.setCategoryName(category);
|
List<ConvenientProductVO> productVOS = levelGroup.get(category);
|
List<ConvenientProductVO> sortedProductVOS = productVOS.stream()
|
.sorted(Comparator.comparing(ConvenientProductVO::getOnShelfAt).reversed()).collect(Collectors.toList());
|
levelInfoVOLevelInfoVO.setProductVOList(sortedProductVOS);
|
levelInfoVOList.add(levelInfoVOLevelInfoVO);
|
});
|
}
|
return R.ok(levelInfoVOList);
|
}
|
|
@Override
|
public R pageSearchProduct(PageSearchDTO pageSearchDTO) {
|
Page page = new Page<>();
|
page.setSize(pageSearchDTO.getPageSize());
|
page.setCurrent(pageSearchDTO.getPageNum());
|
IPage<ConvenientProductVO> productVOIPage = this.baseMapper.pageSearchProduct(page, pageSearchDTO);
|
List<ConvenientProductVO> records = productVOIPage.getRecords();
|
if (!records.isEmpty()) {
|
List<Long> productIds = records.stream().map(ConvenientProductVO::getId).collect(Collectors.toList());
|
List<ConvenientProductSpecificationDO> specificationDOS = convenientProductSpecificationDAO
|
.selectList(new LambdaQueryWrapper<ConvenientProductSpecificationDO>()
|
.eq(ConvenientProductSpecificationDO::getIsDel, false).in(ConvenientProductSpecificationDO::getProductId, productIds));
|
List<ConvenientProductSpecificationVO> specificationVOList = new ArrayList<>();
|
if (!specificationDOS.isEmpty()) {
|
specificationDOS.forEach(specificationDO -> {
|
ConvenientProductSpecificationVO specificationVO = new ConvenientProductSpecificationVO();
|
BeanUtils.copyProperties(specificationDO, specificationVO);
|
specificationVOList.add(specificationVO);
|
});
|
Map<Long, List<ConvenientProductSpecificationVO>> collect = specificationVOList
|
.stream().collect(Collectors.groupingBy(ConvenientProductSpecificationVO::getProductId));
|
Set<Long> keySet = collect.keySet();
|
Iterator<Long> iterator = keySet.iterator();
|
while (iterator.hasNext()) {
|
Long productId = iterator.next();
|
records.forEach(record -> {
|
if (record.getId().equals(productId)) {
|
record.setProductSpecificationVOList(collect.get(productId));
|
}
|
});
|
}
|
}
|
}
|
return R.ok(productVOIPage);
|
}
|
|
@Override
|
public R incrProductView(Long productId) {
|
this.baseMapper.incrProductView(productId);
|
return R.ok();
|
}
|
|
@Override
|
public R getProductShelfNum(Long merchantId) {
|
int onShelfTotalNum = this.baseMapper.selectCount(new LambdaQueryWrapper<ConvenientProductDO>().eq(ConvenientProductDO::getMerchantId, merchantId)
|
.eq(ConvenientProductDO::getIsDel, false).eq(ConvenientProductDO::getOnShelf, true));
|
int offShelfTotalNum = this.baseMapper.selectCount(new LambdaQueryWrapper<ConvenientProductDO>().eq(ConvenientProductDO::getMerchantId, merchantId)
|
.eq(ConvenientProductDO::getIsDel, false).eq(ConvenientProductDO::getOnShelf, false));
|
ConvenientProductShelfVO convenientProductShelfVO = new ConvenientProductShelfVO();
|
convenientProductShelfVO.setOnShelfTotalNum(onShelfTotalNum);
|
convenientProductShelfVO.setOffShelfTotalNum(offShelfTotalNum);
|
return R.ok(convenientProductShelfVO);
|
}
|
}
|