郑永安
2023-06-19 2fcd97552d16718cc7997629fd637a73a5a4483f
src/main/java/com/gk/firework/Service/ServiceImpl/ProductServiceImpl.java
对比新文件
@@ -0,0 +1,330 @@
package com.gk.firework.Service.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.firework.Domain.DO.ProductDO;
import com.gk.firework.Domain.Exception.BusinessException;
import com.gk.firework.Domain.ProductCodeInfo;
import com.gk.firework.Domain.ProductInfo;
import com.gk.firework.Domain.StockInfo;
import com.gk.firework.Domain.UserInfo;
import com.gk.firework.Domain.Utils.BeanUtils;
import com.gk.firework.Domain.Utils.PageInfo;
import com.gk.firework.Domain.Utils.StringUtils;
import com.gk.firework.Domain.Vo.DirectionDetail;
import com.gk.firework.Domain.Vo.FireworkDeal;
import com.gk.firework.Domain.Vo.Product2JsonVo;
import com.gk.firework.Domain.Vo.ProductVo;
import com.gk.firework.Mapper.ProductInfoMapper;
import com.gk.firework.Service.ExcelExportService;
import com.gk.firework.Service.ProductCodeService;
import com.gk.firework.Service.ProductService;
import com.gk.firework.Service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * @author : jingjy
 * @date : 2021/3/16 10:21
 */
@Service("ProductService")
public class ProductServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements ProductService{
    @Autowired
    private ProductInfoMapper productInfoMapper;
    @Autowired
    private ExcelExportService excelExportService;
    @Autowired
    private StockService stockService;
    @Autowired
    private ProductCodeService productCodeService;
    @Override
    public List<ProductInfo> selectProductInfos(Map<String, Object>condition) {
        List<ProductInfo> productInfos = productInfoMapper.selectProductInfos(condition);
        return productInfos;
    }
    @Override
    public void selectDataGrid(PageInfo pageInfo) {
        Page<ProductInfo> page = new Page<>(pageInfo.getPageIndex(), pageInfo.getPageSize());
        List<OrderItem> orderItems = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtils.isNotBlank(pageInfo.getSort()) && StringUtils.isNotBlank(pageInfo.getOrder())) {
            orderItem.setAsc(pageInfo.getOrder().equalsIgnoreCase("ascending"));
            orderItem.setColumn(pageInfo.getSort());
        }else {
            orderItem.setAsc(false);
            orderItem.setColumn("createddate");
        }
        orderItems.add(orderItem);
        page.setOrders(orderItems);
        if (StringUtils.isBlank(pageInfo.getSort())){
            pageInfo.setSort("createddate");
        }
        if (StringUtils.isBlank(pageInfo.getOrder())){
            pageInfo.setOrder("desc");
        }
        List<ProductInfo> productInfos = productInfoMapper.selectProductDataGrid(pageInfo.getCondition(),page);
        pageInfo.setResult(productInfos);
        pageInfo.setTotalCount(page.getTotal());
    }
    @Override
    public List<ProductInfo> selectByProduct(ProductInfo productInfo) {
        if (productInfo == null || StringUtils.isBlank(productInfo.getDirectionCode())) {
            return null;
        }
        return productInfoMapper.selectProductsByDirectionCode(productInfo.getDirectionCode(),productInfo.getCompanyNumber());
    }
    @Override
    public boolean hasProductByDire(String directionCode) {
        ProductInfo productInfo = productInfoMapper.selectProductByDirectionCode(directionCode);
        return productInfo != null;
    }
    @Override
    public List<String> hasNoProductByCodes(List<String> directionCodes) {
        List<String>list = new ArrayList<>();
        for (String dire : directionCodes){
            if (!hasProductByDire(dire)){
                list.add(dire);
            }
        }
        return list;
    }
    @Override
    public ProductInfo selectByDirection(String dire) {
        if (StringUtils.isBlank(dire)|| dire.length() < 10){
            return null;
        }
        dire = dire.substring(0,10);
        return productInfoMapper.selectProductByDirectionCode(dire);
    }
    @Override
    public ProductVo selectVoByDirection(String dire) {
        DirectionDetail directionDetail = FireworkDeal.dealDirectionCode(dire);
        ProductInfo productInfo = productInfoMapper.selectProductByDirectionCode(directionDetail.getItemCode());
        if (productInfo == null) return null;
        ProductVo productVo = BeanUtils.copy(productInfo, ProductVo.class);
        productVo.setDirectionCode(dire);
        productVo.setItemCode(directionDetail.getItemCode());
        productVo.setDateCode(directionDetail.getDateCode());
        productVo.setSerialNo(directionDetail.getSerialNo());
        return productVo;
    }
    @Override
    public String getSlice(String directionCode){
        if (StringUtils.isBlank(directionCode) || directionCode.length() < 10){
            return "";
        }
        Integer slice = productInfoMapper.getSliceByDirectionCode(directionCode.substring(0,10));
        if (slice == null){
            return null;
        }
        return "_slice"+slice;
    }
    @Override
    public String getSlice(Long productId){
        if (productId == null){
            return "";
        }
        ProductInfo productInfo = productInfoMapper.selectById(productId);
        if (productInfo == null){
            return null;
        }
        Integer slice = productInfoMapper.getSliceByDirectionCode(productInfo.getDirectionCode());
        if (slice == null){
            return null;
        }
        return "_slice"+slice;
    }
    @Override
    public void deleteByEnterpriseName(String enterpriseName,String name) {
        productInfoMapper.deleteByEnterpriseName(enterpriseName,name);
    }
    @Override
    public List<Product2JsonVo> transform2Json(String enterprisenumber, MultipartFile file, UserInfo userInfo) {
        if (StringUtils.isBlank(enterprisenumber)) {
            throw new BusinessException("参数传递错误");
        }
        if (file == null || file.getSize() < 1 || file.getOriginalFilename() == null) {
            throw new BusinessException("文件上传为空");
        }
        String originalFilename = file.getOriginalFilename();
        try {
            byte [] byteArr=file.getBytes();
            InputStream inputStream = new ByteArrayInputStream(byteArr);
            boolean isExcel2007 = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).endsWith("xlsx");
            //解析
            List<Product2JsonVo> product2JsonVos = excelExportService.parsingProduct(inputStream, userInfo, isExcel2007, enterprisenumber);
            //再做一遍筛查
            assert product2JsonVos.size() > 0;
            List<String> stringList = product2JsonVos.stream().map(Product2JsonVo::getDirectionCode)
                    .collect(Collectors.toList());
            long count = stringList.stream().distinct().count();
            if (stringList.size() == count) {
                return product2JsonVos;
            } else {
                throw new BusinessException("文件中有重复产品在不同行");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new BusinessException("找不到文件");
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("发生错误,请联系管理员");
        }
    }
    /**
    * @Description: 根据产品编号和生产企业编号 查询个数
    * @date 2021/5/24 15:45
    */
    @Override
    public int countByEnterpriseNumberAndDirectionCode(String enterprisenumber, String directionCode) {
        if (StringUtils.isBlank(enterprisenumber)) {
            throw new BusinessException("企业编号不能为空");
        }
        if (StringUtils.isBlank(directionCode)) {
            throw new BusinessException("产品编号不能为空");
        }
        LambdaQueryWrapper<ProductInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductInfo::getIsDel, (byte) 0).
                eq(ProductInfo::getCompanyNumber, enterprisenumber)
                .eq(ProductInfo::getDirectionCode, directionCode);
        return productInfoMapper.selectCount(queryWrapper);
    }
    @Override
    public List<Product2JsonVo> transform2JsonSimple(MultipartFile file, UserInfo userInfo) {
        if (file == null || file.getSize() < 1 || file.getOriginalFilename() == null) {
            throw new BusinessException("文件上传为空");
        }
        String originalFilename = file.getOriginalFilename();
        try {
            byte [] byteArr=file.getBytes();
            InputStream inputStream = new ByteArrayInputStream(byteArr);
            boolean isExcel2007 = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).endsWith("xlsx");
            //解析
            List<Product2JsonVo> product2JsonVos = excelExportService.parsingProduct(inputStream, userInfo, isExcel2007);
            //再做一遍筛查
            assert product2JsonVos.size() > 0;
            List<String> stringList = product2JsonVos.stream().map(Product2JsonVo::getDirectionCode)
                    .collect(Collectors.toList());
            long count = stringList.stream().distinct().count();
            if (stringList.size() == count) {
                return product2JsonVos;
            } else {
                throw new BusinessException("文件中有重复产品在不同行");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new BusinessException("找不到文件");
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("发生错误,请联系管理员");
        }
    }
    @Override
    public List<ProductInfo> selectProductInfo(Map<String, Object> condition) {
        return productInfoMapper.selectProductInfo(condition);
    }
    @Override
    public List<String> getAllProductCodes() {
        return productInfoMapper.getAllProductCodes();
    }
    @Override
    @Transactional
    public void importDataByExcel(MultipartFile file, UserInfo user) {
        if (file == null || file.getSize() < 1 || file.getOriginalFilename() == null) {
            throw new BusinessException("文件上传为空");
        }
        String originalFilename = file.getOriginalFilename();
        try {
            byte [] byteArr=file.getBytes();
            InputStream inputStream = new ByteArrayInputStream(byteArr);
            boolean isExcel2007 = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).endsWith("xlsx");
            //解析导入
            List<ProductInfo> dataList= excelExportService.parseProductFromOldSystem(inputStream, user, isExcel2007);
            assert dataList.size() > 0;
            List<String> directionCodes = dataList.stream().map(ProductInfo::getDirectionCode)
                    .collect(Collectors.toList());
            long count = directionCodes.stream().distinct().count();
            if (directionCodes.size() != count)
                throw new BusinessException("文件中有重复产品流向码");
            for (ProductInfo productInfo : dataList) {
                productInfo.setIsOld((byte)1);
                this.save(productInfo);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new BusinessException("找不到文件");
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("发生错误,请联系管理员");
        }
    }
    @Override
    public boolean isProductUsed(Long id) {
        ProductInfo productInfo = productInfoMapper.selectById(id);
        if (productInfo == null){
            return false;
        }
        List<StockInfo> stockInfos = stockService.selectStockByProductId(id);
        if (stockInfos.size() > 0 ){
            return true;
        }
        List<ProductCodeInfo> productCodeInfos =  productCodeService.selectByItemCode(productInfo.getDirectionCode());
        return productCodeInfos.size() > 0;
    }
    @Override
    public List<String> selectTypes() {
        return productInfoMapper.selectTypes();
    }
    @Override
    public List<ProductDO> selectDoByDirections(List<String> direction10Codes) {
        if (direction10Codes == null || direction10Codes.size() == 0) {
            throw new BusinessException("系统入参为空");
        }
        return productInfoMapper.selectDoByDirections(direction10Codes);
    }
}