whycxzp
2023-12-01 516a6fabac1eca611a8bbd0b79bdde3f0d39e843
src/main/java/com/whyc/service/ProductService.java
@@ -3,9 +3,11 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.dto.FileUrlDTO;
import com.whyc.dto.Response;
import com.whyc.mapper.ProductHistoryMapper;
import com.whyc.mapper.ProductMapper;
import com.whyc.pojo.*;
import com.whyc.util.DateUtil;
@@ -34,6 +36,9 @@
    @Autowired(required = false)
    private ProductMapper mapper;
    @Autowired(required = false)
    private ProductHistoryMapper hisMapper;
    @Autowired
    private ProductHistoryService phService;
@@ -52,10 +57,13 @@
    @Autowired
    private ProductLockLogService productLockLogService;
    @Autowired
    private MaterialHistoryService materialHistoryService;
    //查询出所有的产品信息(分页加模糊查询<产品的编码,型号,名字,定制表编号>
    public Response getAllProduct(String subCode,String parentCode, String parentName, String parentModel, String customCode, int pageCurr, int pageSize) {
        /*PageHelper.startPage(pageCurr,pageSize);
        QueryWrapper wrapper=new QueryWrapper();
    public Response getAllProduct(String subCode,String parentCode, String parentName, String parentModel, String customCode,Integer enabled, int pageCurr, int pageSize) {
        PageHelper.startPage(pageCurr,pageSize);
        /*QueryWrapper wrapper=new QueryWrapper();
        if(parentCode!=null){
            wrapper.like("parent_code",parentCode);
        }
@@ -74,7 +82,15 @@
        }
        wrapper.orderByAsc("id");
        List list=mapper.selectList(wrapper);*/
        List list=mapper.getAllProduct(subCode,parentCode,parentName,parentModel,customCode);
        List<Product> list=mapper.getAllProduct(subCode,parentCode,parentName,parentModel,customCode,enabled);
        /*list.stream().forEach(product -> {
            //1.查询是否存在该记录
            QueryWrapper qwrapper=new QueryWrapper();
            qwrapper.eq("id",product.getId());
            qwrapper.last("limit 1");
            ProductHistory pHis=hisMapper.selectOne(qwrapper);
            product.setEnabled(pHis.getEnabled());
        });*/
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list.size()>0,pageInfo,"返回产品信息");
    }
@@ -233,6 +249,9 @@
                for (int k = 2; k < lastRowNum + 1; k++) {
                    if(k==2){
                        product.setParentCode(sheet.getRow(2).getCell(1).getStringCellValue());
                        if(!originalFilename.contains(product.getParentCode())){
                            return response.set(1,false,"上传的产品压缩包必须包含产品编码");
                        }
                        product.setParentName(sheet.getRow(2).getCell(2).getStringCellValue());
                        String parentModel = sheet.getRow(2).getCell(3).getStringCellValue();
                        parentModel = parentModel.trim();
@@ -433,8 +452,17 @@
        List<ProductBom> bomList = product.getBomList();
        String zipFilePath = product.getFileUrl();
        Date date = new Date();
        String dateStr = DateUtil.YYYY_MM_DD_HH_MM_SS_UNION.format(date);
        Long userId = ActionUtil.getUser().getId();
        boolean isCopyCustom = false;
        Product relatedProduct = null;
        //校验凡是有定制表单号的产品,不能使用已上传过的标准产品编码
        if(customCode!=null && !customCode.trim().equals("")){
            ProductHistory standard = phService.getStandard(parentCode);
            if(standard !=null){
                return new Response().set(1,false,"系统存在此次母物料编码的标准产品,拒绝提交");
            }
        }
        //判断是否为依据产品复制定制的产品
        if(bomList == null){
            bomList = pbService.getBomByProductId(product.getId());
@@ -657,14 +685,15 @@
            his.setVersionTime(product.getVersionTime());
            his.setVersion(1);
            his.setSubVersionMax(1);
            his.setEnabled(1);
            //上传后的最初始使能状态为-1
            //第一次解锁后,后续的锁定和解锁在0和1之间切换
            his.setEnabled(-1);
            phService.insertAndUpdateEnabled(his);
            //phService.insert(his);
            // -> product
            // -> product 现有产品和产品bom需要插入,version默认为-1,同时要做删除原来的旧版本
            product.setId(his.getId());
            product.setCreateTime(date);
            product.setVersion(1);
            //insert(product);
            product.setVersion(-1);
            deleteAndInsert(product);
            // -> bom
            bomList.forEach(bom-> {
@@ -714,13 +743,14 @@
                for (ProductBom bom : finalBomList) {
                    String filename = excelExcludeUr.substring(excelExcludeUr.lastIndexOf(File.separator) + 1, excelExcludeUr.lastIndexOf("."));
                    String fileFullName = excelExcludeUr.substring(excelExcludeUr.lastIndexOf(File.separator) + 1);
                    String fileSuffix = fileFullName.substring(fileFullName.lastIndexOf("."));
                    if (bom.getSubModel().toUpperCase().equals(filename.toUpperCase()) && excelExcludeUr.substring(excelExcludeUr.lastIndexOf(".")+1).equals("dwg")) {
                        materialUrlList.add(excelExcludeUr);
                        existFlag = true;
                        bom.setDwgUrl("doc_file" + File.separator + "material"
                                + File.separator + fileFullName);
                        bom.setDwgUrl("doc_file" + File.separator + "material" + File.separator + bom.getSubCode() + "-" + bom.getSubModel()
                                + File.separator + filename + "_" + dateStr + fileSuffix);
                        break;
                    }
                    else if((bom.getSubModel().toUpperCase()+"-BOM").equals(filename.toUpperCase()) && (excelExcludeUr.substring(excelExcludeUr.lastIndexOf(".")+1).equals("png") ||excelExcludeUr.substring(excelExcludeUr.lastIndexOf(".")+1).equals("jpeg"))
@@ -729,8 +759,8 @@
                        existFlag = true;
                        bom.setPictureUrl("doc_file" + File.separator + "material"
                                + File.separator + fileFullName);
                        bom.setPictureUrl("doc_file" + File.separator + "material" + File.separator + bom.getSubCode() + "-" + bom.getSubModel()
                                + File.separator + filename + "_" + dateStr + fileSuffix);
                        break;
                    }
                }
@@ -787,32 +817,75 @@
                }
            });
            materialUrlList.forEach(materialUrl -> {
                String dwgName = materialUrl.substring(materialUrl.lastIndexOf(File.separator) + 1, materialUrl.length() - 4);
            //物料dwg图纸存到对应的 物料编码+型号 下面
            for (String materialUrl : materialUrlList) {
                try {
                    File dwgFile = new File(materialDir + File.separator + dwgName + ".dwg");
                    if(!dwgFile.exists()) {
                    String dwgName = materialUrl.substring(materialUrl.lastIndexOf(File.separator) + 1, materialUrl.length() - 4);
                    //确定物料的具体型号
                    String subModel = null;
                    String subCode = null;
                    for (ProductBom bom : bomList) {
                        String bomSubMode = bom.getSubModel();
                        if (bomSubMode.toUpperCase().equals(dwgName.toUpperCase())) {
                            subModel = bomSubMode;
                            subCode = bom.getSubCode();
                            break;
                        }
                    }
                    File dwgFile;
                    if (subModel != null) {
                        dwgFile = new File(materialDir + File.separator + subCode + "-" + subModel + File.separator + dwgName + "_" + dateStr + ".dwg");
                        File dwgDir = new File(materialDir + File.separator + subCode + "-" + subModel);
                        if(!dwgDir.exists()){
                            dwgDir.mkdirs();
                        }
                    } else {
                        dwgFile = new File(materialDir + File.separator + dwgName + "_" + dateStr + ".dwg");
                    }
                    if (!dwgFile.exists()) {
                        FileCopyUtils.copy(new File(materialUrl), dwgFile);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            }
            picUrlList.forEach(picUrl -> {
            for (String picUrl : picUrlList) {
                String picFullName = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1);
                String picSubModel = picFullName.substring(0, picFullName.lastIndexOf("."));
                String picSuffix = picFullName.substring(picFullName.lastIndexOf("."));
                try {
                    File picFile = new File(materialDir + File.separator + picFullName);
                    if(!picFile.exists()) {
                    //确定物料的具体型号和编码
                    String subModel = null;
                    String subCode = null;
                    for (ProductBom bom : bomList) {
                        String bomSubMode = bom.getSubModel();
                        if ((bomSubMode + "-bom").toUpperCase().equals(picSubModel.toUpperCase())) {
                            subModel = bomSubMode;
                            subCode = bom.getSubCode();
                            break;
                        }
                    }
                    File picFile;
                    if (subModel != null) {
                        picFile = new File(materialDir + File.separator + subCode + "-" + subModel + File.separator + picSubModel + "_" + dateStr + picSuffix);
                        File picDir = new File(materialDir + File.separator + subCode + "-" + subModel);
                        if(!picDir.exists()){
                            picDir.mkdirs();
                        }
                    } else {
                        picFile = new File(materialDir + File.separator + picSubModel + "_" + dateStr + picSuffix);
                    }
                    if (!picFile.exists()) {
                        FileCopyUtils.copy(new File(picUrl), picFile);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            }
            //物料表中不存在的(依据:物料编码+物料型号),则添加到物料表中去(包含product这个物料)
            //没有图纸和图片的,更新图纸和图片;有图纸和图片的,不更新图纸和图片
            //更新图纸和图片
            List<Material> materialExistList = mService.getListByCodeAndModelList2(bomList);
            //List<String> subCodeList = materialExistList.stream().map(Material::getSubCode).collect(Collectors.toList());
            List<String> subCodeList = materialExistList.stream().map(material -> material.getSubCode()+"/"+material.getSubModel()).collect(Collectors.toList());
@@ -825,13 +898,13 @@
                    material.setDwgUrl(bom.getDwgUrl());
                    material.setFileUrl(bom.getFileUrl());
                    material.setPictureUrl(bom.getPictureUrl());
                    // TODO 是否要更新同物料编码的老物料状态为0?
                    material.setStatus(1);
                    material.setSubCode(bom.getSubCode());
                    material.setSubModel(bom.getSubModel());
                    material.setSubName(bom.getSubName());
                    material.setType(bom.getType());
                    material.setUnit(bom.getUnit());
                    material.setUpUserId(userId);
                    materialList.add(material);
                } else { //物料已经存在的
@@ -861,6 +934,7 @@
                material.setSubModel(product.getParentModel());
                material.setCreateDate(date);
                material.setStatus(1);
                material.setUpUserId(userId);
                materialList.add(material);
            }
@@ -900,12 +974,13 @@
            productHistory.setVersion(nextVersion);
            //版本新增,初始bom子件版本为1
            productHistory.setSubVersionMax(1);
            productHistory.setEnabled(1);
            productHistory.setEnabled(-1);
            phService.insertAndUpdateEnabled(productHistory);
            // -> product
            // -> product 现有产品和产品bom需要插入,version默认为-1,同时要做删除原来的旧版本
            product.setId(productHistory.getId());
            product.setCreateTime(date);
            product.setVersion(nextVersion);
            product.setVersion(-1);
            deleteAndInsert(product);
            // -> product_bom
            List<ProductBom> productBomList = new LinkedList<>();
@@ -949,6 +1024,26 @@
            });
            pbhService.insertBatch(bomHistoryList);
            if(materialUpdateList.size()>0) {
                //更新物料图纸图片的同时,添加新的图纸图片信息到物料历史表中
                List<MaterialHistory> materialHistoryList = new LinkedList<>();
                for (Material material : materialUpdateList) {
                    MaterialHistory materialHistory = new MaterialHistory();
                    materialHistory.setPictureUrl(material.getPictureUrl());
                    materialHistory.setProductId(productHistory.getId());
                    materialHistory.setDwgUrl(material.getDwgUrl());
                    materialHistory.setUpUserId(userId.intValue());
                    materialHistory.setCreateTime(date);
                    for (Material materialInDB : materialExistList) {
                        if ((material.getSubCode() + "/" + material.getSubModel()).equals(materialInDB.getSubCode() + "/" + materialInDB.getSubModel())) {
                            materialHistory.setMaterialId(materialInDB.getId());
                            break;
                        }
                    }
                    materialHistoryList.add(materialHistory);
                }
                materialHistoryService.addBatch(materialHistoryList);
            }
        }
        //添加新增日志到tb_product_lock_log表
        ProductLockLog lockLog = new ProductLockLog();
@@ -963,7 +1058,7 @@
        lockLog.setLockFlag(-1);
        productLockLogService.insert(lockLog);
        return new Response().setII(1,"新增完成");
        return new Response().set(1,true,"新增完成");
    }
    private void deleteAndInsert(Product product) {
@@ -973,6 +1068,14 @@
            pbService.deleteByProductId(productDB.getId());
        }
        mapper.insert(product);
    }
    private void delete(Product product) {
        Product productDB = getByProductCodeAndCustomCode(product.getParentCode(), product.getCustomCode());
        if(productDB!=null) {
            mapper.deleteById(productDB.getId());
            pbService.deleteByProductId(productDB.getId());
        }
    }
    private Product getByProductCodeAndCustomCode(String parentCode, String customCode) {
@@ -1009,5 +1112,9 @@
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return object -> seen.putIfAbsent(keyExtractor.apply(object), Boolean.TRUE) == null;
    }
    //验证母料编码是否存在产品中
    public Response judgeParentCode(String parentCode,String customCode) {
        int count = mapper.judgeParentCode(parentCode,customCode);
        return  new Response().set(1,count>0,"验证母料编码是否存在产品中");
    }
}