lxw
2022-09-01 742f00e9798bda7f8e34f6879e580785e94dba70
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.dto.Response;
import com.whyc.mapper.MaterialMapper;
import com.whyc.pojo.Material;
import com.whyc.pojo.ProductBom;
import com.whyc.util.CommonUtil;
import com.whyc.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileCopyUtils;
 
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
public class MaterialService {
    @Autowired(required = false)
    private MaterialMapper mapper;
    //查询所有的散装件信息加设置了替换关系的子件信息
    /*public Response getAllMaterial(String category,String subCode,String subName,String subModel,int pageCurr,int pageSize) {
        PageHelper.startPage(pageCurr,pageSize);
        *//*QueryWrapper wrapper=new QueryWrapper();
        wrapper.orderByAsc("id");
        List<Material> list=mapper.selectList(wrapper);*//*
        List<Material> list=mapper.getAllMaterial(category,subCode,subName,subModel);
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list.size()>0?true:false,pageInfo,"返回所有的散装件");
    }*/
    //查询所有的散装件信息不分页
    /*public Response getMaterialWithoutSub(String parentModel, String subName,int version) {
        List<Material> list=mapper.getMaterialWithoutSub(parentModel,subName,version);
        return new Response().setII(1,list.size()>0?true:false,list,"返回子件没有添加过联系的散装件");
    }*/
    //删除散装件(将散装件标识为不可用)
    /*public Response deleteMaterial(int id) {
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.set("status",0);
        wrapper.eq("id",id);
        int bl=mapper.update(null,wrapper);
        return new Response().setII(1,bl>0?true:false,bl,"将散装件标识为不可用");
    }*/
 
    public List<Material> getList() {
        return mapper.selectList(null);
    }
 
    public void insertBatch(List<Material> MaterialList) {
        mapper.insertBatchSomeColumn(MaterialList);
    }
 
    /**mybatis-plus逻辑删除*/
    public void deleteBatch(List<Integer> MaterialIdList) {
        mapper.deleteBatchIds(MaterialIdList);
    }
 
    //查询所有物料(分页,模糊查询)
    public Response getMaterialLimit(String subCode, String subName, String subModel, int pageCurr, int pageSize) {
       PageHelper.startPage(pageCurr,pageSize);
        List<Material> list=mapper.getMaterialLimit(subCode,subName,subModel);
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list.size()>0?true:false,pageInfo,"查询所有物料(分页,模糊查询)");
    }
    //建立关联时查询所有的物料(不分页)
    public Response getAllMaterialNoLimit() {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.ne("status",0);
        wrapper.orderByAsc("id");
        List<Material> list=mapper.selectList(wrapper);
        return new Response().setII(1,list.size()>0?true:false,list,"查询所有物料(不分页)");
    }
 
    public List<String> getListByCodeList(List<String> codeList) {
        return mapper.getListByCodeList(codeList);
    }
 
    public void updateDwgUrl(List<Material> materialDwgUrlNameList) {
        mapper.updateDwgUrl(materialDwgUrlNameList);
    }
 
    @Transactional
    public Response add(List<Material> materialList) {
        String fileUrl = materialList.get(0).getFileUrl();
        //校验物料合理性,非空校验:物料名称/物料编号/物料型号
        for (int i = 0; i < materialList.size(); i++) {
            Material material = materialList.get(i);
            if(material.getSubCode()==null ||material.getSubCode().equals("")
            || material.getSubName()==null ||material.getSubName().equals("")
            || material.getSubModel()==null ||material.getSubModel().equals("")){
                return new Response().set(1,false,"拒绝新增,物料名称/物料编号/物料型号存在空值");
            }
        }
        //检验物料重复性,已存在的物料,拒绝重复提交,判断依据:物料编码+物料型号
        List<Material> existList = getListByCodeAndModelList(materialList);
        if(existList!=null && existList.size()!=0){
            return new Response().setII(1,false,existList,"拒绝新增,物料重复提交");
        }
 
        //获取路径下的所有文件
        List<String> fileUrlList = new LinkedList<>();
        List<String> dwgUrlList = null;
        List<String> materialUrlList = new LinkedList<>();
 
        fileUrlList = FileUtil.getStaticFilePath(new File(fileUrl),fileUrlList);
        dwgUrlList = fileUrlList.stream().filter(url->url.contains(".dwg")).collect(Collectors.toList());
        //图纸的url预设
        dwgUrlList.forEach(dwgUrl-> {
            materialList.forEach(material -> {
                String filename = dwgUrl.substring(dwgUrl.lastIndexOf(File.separator) + 1, dwgUrl.length() - 4);
                String fileFullName = dwgUrl.substring(dwgUrl.lastIndexOf(File.separator) + 1);
                if (material.getSubModel().toUpperCase().equals(filename.toUpperCase())) {
                    materialUrlList.add(dwgUrl);
 
                    material.setDwgUrl("doc_file" + File.separator + "material"
                            + File.separator + fileFullName);
                }
            });
        });
        //图纸文件转移到正式文件夹
        String projectDir = CommonUtil.getProjectDir();
        String materialDir = projectDir + File.separator + "doc_file" + File.separator + "material";
        File materialFile = new File(materialDir);
        if(!materialFile.exists()){
            materialFile.mkdirs();
        }
        materialUrlList.forEach(materialUrl->{
            String dwgFullName = materialUrl.substring(materialUrl.lastIndexOf(File.separator) + 1);
            try {
                FileCopyUtils.copy(new File(materialUrl),new File(materialDir + File.separator + dwgFullName));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
 
        //新增,同时对于物料编码相同,物料型号不同的,禁用旧物料状态
        mapper.disableStatus(materialList);
        materialList.forEach(material -> {
            material.setStatus(1);
            material.setFileUrl(null);
        });
        mapper.insertBatchSomeColumn(materialList);
        return new Response().set(1,true,"新增完成");
    }
 
    private List<Material> getListByCodeAndModelList(List<Material> materialList) {
        return mapper.getListByCodeAndModelList(materialList);
    }
 
    public List<Material> getListByCodeAndModelList2(List<ProductBom> bomList) {
        return mapper.getListByCodeAndModelList2(bomList);
    }
 
    public Material getByCodeAndModel(String subCode, String subModel) {
        QueryWrapper<Material> query = Wrappers.query();
        query.eq("sub_code",subCode).eq("sub_model",subModel).last(" limit 1");
        return mapper.selectOne(query);
    }
}