whyclxw
3 天以前 29aed8c1fde9c1fc6d248ba8028914e038442306
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
package com.whyc.service;
 
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.constant.UserOperation;
import com.whyc.dto.FileDirPath;
import com.whyc.dto.Response;
import com.whyc.dto.ZipUtils;
import com.whyc.mapper.AttachLockMapper;
import com.whyc.mapper.MaterialMapper;
import com.whyc.mapper.ProductBomMapper;
import com.whyc.mapper.ProductMapper;
import com.whyc.pojo.*;
import com.whyc.util.ActionUtil;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
 
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
 
@Service
public class ProductBomService {
 
 
    @Autowired(required = false)
    private ProductBomMapper mapper;
 
    @Autowired(required = false)
    private MaterialMapper cponentMapper;
 
    @Autowired(required = false)
    private ProductMapper productMapper;
 
    @Autowired(required = false)
    private AttachLockMapper attachLockMapper;
 
    @Autowired
    private DocLogService logService;
    //图纸分类检索
    public Response searchCadDrawer(ProductBom productBom,int pageCurr,int pageSize) {
        PageHelper.startPage(pageCurr,pageSize);
        List list=mapper.searchCadDrawer(productBom);
        PageInfo pageInfo=new PageInfo(list);
        return  new Response().setII(1,list.size()>0,pageInfo,"数据返回");
    }
   /* //图纸文件下载
    public void downloadCadDrawer(HttpServletRequest req, HttpServletResponse resp, List<ProductBom> boms) {
        HSSFWorkbook wb = new HSSFWorkbook();
        //字体格式-加粗
        HSSFCellStyle cellStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        cellStyle.setFont(font);
        //生成excel并将dwg文件放在同一报下压缩
        creatBomExcel(req,resp,boms,wb,"pic");
        //记录日志
        DocUser docUser= ActionUtil.getUser();
        String operationDetail="具体图纸为数量:"+boms.size();
        String opreationMsg="执行了文件打包下载操作";
        String terminalIp=req.getRemoteAddr();
        logService.recordOperationLog(docUser.getId(),docUser.getName(), UserOperation.TYPE_DOWNLOAD.getType(),new Date(),terminalIp,opreationMsg,operationDetail);
 
    }*/
    //根据子件code获取最终的信息
    public Response getBomBySubcode(String scode) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("sub_code",scode);
        wrapper.last("limit 1");
        ProductBom productBom=mapper.selectOne(wrapper);
        return new Response().setII(1,productBom!=null,productBom,"返回数据");
    }
 
    /**获取产品的信息(不包含子料)*/
    public ProductBom getProduct(String parentModel){
        QueryWrapper<ProductBom> query = Wrappers.query();
        query.eq("parent_model",parentModel).last(" limit 1");
        return mapper.selectOne(query);
    }
 
    /** 删除旧的bom,增加新的bom*/
    @Transactional
    public void updateNewBom(List<ProductBomHistory> newBomHistoryList) {
        UpdateWrapper<ProductBom> update = Wrappers.update();
        //lxw注释
        //update.eq("parent_model",newBomHistoryList.get(0).getParentModel());
        mapper.delete(update);
 
        List<ProductBom> newBomList = new LinkedList<>();
        newBomHistoryList.forEach(newBomHis->{
            ProductBom newBom = new ProductBom();
            newBom.setCategory(newBomHis.getCategory());
            newBom.setCreateDate(new Date());
            newBom.setDwgUrl(newBomHis.getDwgUrl());
            newBom.setFileUrl(newBomHis.getFileUrl());
            newBom.setMaterial(newBomHis.getMaterial());
            newBom.setNotes(newBomHis.getNotes());
            //newBom.setParentCode(newBomHis.getParentCode());
            //newBom.setParentModel(newBomHis.getParentModel());
            //newBom.setParentName(newBomHis.getParentName());
            //newBom.setParentVersion(newBomHis.getParentVersion());
            newBom.setPictureUrl(newBomHis.getPictureUrl());
            newBom.setProducer(newBomHis.getProducer());
            newBom.setQuantity(newBomHis.getQuantity());
            newBom.setSubCode(newBomHis.getSubCode());
            newBom.setSubModel(newBomHis.getSubModel());
            newBom.setSubName(newBomHis.getSubName());
            newBom.setSurfaceDetail(newBomHis.getSurfaceDetail());
            newBom.setThickness(newBomHis.getThickness());
            newBom.setType(newBomHis.getType());
            newBom.setUnit(newBomHis.getUnit());
            //newBom.setUpUserId(newBomHis.getUpUserId());
            //newBom.setVersion(newBomHis.getEVersion());
 
            newBomList.add(newBom);
        });
        mapper.insertBatchSomeColumn(newBomList);
    }
 
    //将选中的文件存入指定目录下
    public void copyFile(File source,String dest ){
        //创建目的地文件夹
        File destfile = new File(dest);
        if(!destfile.exists()) {
            destfile.mkdir();
        }/*else{
            if(i==0){
                ZipUtils.delDir(dest);
            }
        }*/
        //source是文件,则用字节输入输出流复制文件
        try {
            if(source.isFile()){
                FileInputStream fis = new FileInputStream(source);
                //创建新的文件,保存复制内容,文件名称与源文件名称一致
                File dfile = new File(dest+File.separator+source.getName());
                if(!dfile.exists()){
                    dfile.createNewFile();
                }
                FileOutputStream fos = new FileOutputStream(dfile);
                // 读写数据
                // 定义数组
                byte[] b = new byte[1024];
                // 定义长度
                int len;
                // 循环读取
                while ((len = fis.read(b))!=-1) {
                    // 写出数据
                    fos.write(b, 0 , len);
                }
                //关闭资源
                fos.close();
                fis.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //文件夹的拷贝
    public  void copyDir(String sourcePathDir, String newPathDir) {
        File start = new File(sourcePathDir);
        File end = new File(newPathDir);
        if(!start.exists()) {
            return;
        }
        String[] filePath = start.list();//获取该文件夹下的所有文件以及目录的名字
        if(!end.exists()) {
            end.mkdir();
        }
        if(filePath.length>=0){
            for(String temp : filePath) {
                //添加满足情况的条件
                if(new File(sourcePathDir + File.separator + temp ).isFile()) {
                    //为文件则进行拷贝
                    copyFile(new File(sourcePathDir + File.separator + temp ), newPathDir );
                }
            }
        }
 
    }
    //文件夹的拷贝移除productId对应的被锁定的
    public  void copyDirWithOutProductId(String sourcePathDir, String newPathDir,int productId) {
        File start = new File(sourcePathDir);
        File end = new File(newPathDir);
        if(!start.exists()) {
            return;
        }
        String[] filePath = start.list();//获取该文件夹下的所有文件以及目录的名字
        if(!end.exists()) {
            end.mkdir();
        }
        if(filePath.length>=0){
            for(String temp : filePath) {
                //添加满足情况的条件
                if(new File(sourcePathDir + File.separator + temp ).isFile()) {
                    QueryWrapper wrapper=new QueryWrapper();
                    wrapper.eq("product_id",productId);
                    wrapper.eq("attach_name",temp);
                    wrapper.eq("lock_flag",1);
                    wrapper.last("limit 1");
                    AttachLock attachLock=attachLockMapper.selectOne(wrapper);
                    if(attachLock==null){
                        //为文件则进行拷贝
                        copyFile(new File(sourcePathDir + File.separator + temp ), newPathDir );
                    }
                }
            }
        }
    }
    public void updateUrl(List<ProductBomApproving> fileBomApprovingList) {
        mapper.updateUrl(fileBomApprovingList);
    }
    /**
     * 获取所有产品信息*/
    /*public Response getAllBom(String parentCode,String parentName,String parentModel, int pageCurr,int pageSize) {
        PageHelper.startPage(pageCurr,pageSize);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.select("distinct parent_code","parent_name","parent_model","version").orderByAsc("id");
        if(parentCode!=null&&!parentCode.isEmpty()){
            wrapper.like("parent_code",parentCode);
        }
        if(parentName!=null&&!parentName.isEmpty()){
            wrapper.like("parent_name",parentName);
        }
        if(parentModel!=null&&!parentModel.isEmpty()){
            wrapper.like("parent_model",parentModel);
        }
        List list=mapper.selectList(wrapper);
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list!=null,pageInfo,"返回数据");
    }*/
    //产品下载(产品id和版本)
    public  void downloadProduct(HttpServletRequest req, HttpServletResponse resp, int productId , int version
       , @RequestParam String oprateReason, @RequestParam String oprateInfo) {
        HSSFWorkbook wb = new HSSFWorkbook();
        //读取产品信息
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",productId);
        wrapper.eq("version",version);
        wrapper.last("limit 1");
        Product product=productMapper.selectOne(wrapper);
        //读取产品的子件和关联的物料信息
        List<ProductBom> list=mapper.getBomAndMaterial(productId,version);
        //处理存在关联关系的物料
        List<ProductBom> endList=new ArrayList<>();
        list.stream().forEach(bom -> {
            bom.setConnFlag(0);
            if(bom.getDwgUrl()!=null){
                QueryWrapper qwrapper=new QueryWrapper();
                qwrapper.eq("material_id",bom.getMaterialId());
                qwrapper.eq("attach_name",bom.getDwgUrl().substring(bom.getDwgUrl().lastIndexOf("\\")+1));
                qwrapper.eq("lock_flag",1);
                qwrapper.last("limit 1");
                AttachLock attachLock=attachLockMapper.selectOne(qwrapper);
                if(attachLock!=null){
                    bom.setDwgUrl("");
                }
            }
            endList.add(bom);
            if(bom.getMaterials()!=null&&bom.getMaterials().size()>0){
                for (Material m:bom.getMaterials()) {
                    //将物料存放为一个bom为下载做准备
                    ProductBom copyBom=copyMaterialToBom(m);
                    endList.add(copyBom);
                }
            }
        });
        //将没有管理BOM的dwg文件拷贝下载
        String withOutDwg="doc_file/product"+File.separator+product.getParentModel();
        if(product.getCustomCode()!=null&&!product.getCustomCode().isEmpty()){
            withOutDwg+=File.separator+product.getCustomCode()+File.separator+version;
        }else{
            withOutDwg+=File.separator+"standard"+File.separator+version;
        }
        //生成excel并将dwg文件放在同一报下压缩
        creatBomExcel(req,resp,product,endList,wb,withOutDwg,ActionUtil.sdfwithALL.format(product.getVersionTime()),oprateReason,oprateInfo);
    }
 
 
    //根据产品信息创建excel表格并存放在指定目录
    private void creatBomExcel(HttpServletRequest req, HttpServletResponse resp,Product product,List list, HSSFWorkbook wb,String withOutDwg
            , String oprateVersion, @RequestParam String oprateReason, @RequestParam String oprateInfo){
        String fileDirName = FileDirPath.getFileDirName();
        String rootFace="";
        String excelName="";
        String[] titleNames=new String[]{"序列","母物料编码","母物料名称","母物料型号","类别","子件编码","子件名称","子件型号"
                                        ,"基本单位","子件数量","生产商","封装类型/材质","元件编号/料厚","表面处理/物料详情","备注","图片"};
        //字体格式-加粗
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillForegroundColor(IndexedColors.GOLD.getIndex());//添加前景色,内容看的清楚
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        HSSFFont font = wb.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //font.setColor(HSSFFont.COLOR_RED);
        cellStyle.setFont(font);
        //创建单个sheet
        HSSFSheet sheet = wb.createSheet("产品信息");
        sheet.setColumnWidth(1,5000);
        sheet.setColumnWidth(2,5000);
        sheet.setColumnWidth(3,5000);
        sheet.setColumnWidth(4,5000);
        sheet.setColumnWidth(6,5000);
        sheet.setColumnWidth(7,5000);
        sheet.setColumnWidth(8,5000);
        sheet.setColumnWidth(16,6000);
        sheet.setDefaultRowHeight((short)(1000));
        //图片元素
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
        int rownum = 1;
        HSSFRow row=sheet.createRow(rownum);
        HSSFCell cell=row.createCell(0);
        if(titleNames.length>0){
            for (int i=0;i<titleNames.length;i++) {
                cell=row.createCell(i+1);
                cell.setCellValue(titleNames[i]);
            }
        }
        //构建excel名称和创建文件夹
        excelName=product.getParentCode()+"_"+product.getParentModel();
        String timeStr= ActionUtil.sdfwithFTP.format(new Date());
        rootFace=fileDirName+File.separator+excelName;
        File destfile = new File(rootFace);
        if(!destfile.exists()) {
            destfile.mkdir();
        }
        //将没有管理BOM的dwg文件拷贝下载
        withOutDwg=fileDirName+File.separator+withOutDwg;
        copyDirWithOutProductId(withOutDwg,rootFace,product.getId());
        //将选中的文件存入指定目录下打包下载
        if(list!=null&&list.size()>0){
            for (int i=0;i<list.size();i++) {
                ProductBom bom= (ProductBom) list.get(i);
                String dwgUrl=bom.getDwgUrl();
                if((dwgUrl!=null)&&(!dwgUrl.isEmpty())){
                    File sourceFile=new File(fileDirName+ File.separator+dwgUrl);
                    copyFile(sourceFile,rootFace);
                }
                rownum=rownum+1;
                row=sheet.createRow(rownum);
                if(bom.getConnFlag()==1){
                    cell=row.createCell(0);
                    cell.setCellValue("替换件");
                    cell.setCellStyle(cellStyle);
 
                    cell=row.createCell(1);
                    cell.setCellValue(i+1);
                    cell.setCellStyle(cellStyle);
 
                    cell=row.createCell(2);
                    cell.setCellValue(product.getParentCode());
                    cell.setCellStyle(cellStyle);
 
                    cell=row.createCell(3);
                    cell.setCellValue(product.getParentName());
                    cell.setCellStyle(cellStyle);
 
                    cell=row.createCell(4);
                    cell.setCellValue(product.getParentModel());
                    cell.setCellStyle(cellStyle);
 
                    cell=row.createCell(5);
                    cell.setCellValue(bom.getCategory());
                    cell.setCellStyle(cellStyle);
 
                    cell=row.createCell(6);
                    cell.setCellValue(bom.getSubCode());
                    cell.setCellStyle(cellStyle);
 
                    cell=row.createCell(7);
                    cell.setCellValue(bom.getSubName());
                    cell.setCellStyle(cellStyle);
 
                    cell=row.createCell(8);
                    cell.setCellValue(bom.getSubModel());
                    cell.setCellStyle(cellStyle);
 
                    cell=row.createCell(9);
                    cell.setCellValue(bom.getUnit()==null?"":bom.getUnit());
                    cell.setCellStyle(cellStyle);
 
                    cell=row.createCell(10);
                    cell.setCellValue(bom.getQuantity()==null?"": bom.getQuantity().toString());
                    cell.setCellStyle(cellStyle);
 
                    cell=row.createCell(11);
                    cell.setCellValue(bom.getProducer()==null?"":bom.getProducer());
                    cell.setCellStyle(cellStyle);
 
                    cell=row.createCell(12);
                    cell.setCellValue(bom.getMaterial());
                    cell.setCellStyle(cellStyle);
 
                    cell=row.createCell(12);
                    cell.setCellValue(bom.getThickness());
                    cell.setCellStyle(cellStyle);
 
                    cell=row.createCell(13);
                    cell.setCellValue(bom.getThickness());
                    cell.setCellStyle(cellStyle);
 
                    cell=row.createCell(14);
                    cell.setCellValue(bom.getSurfaceDetail());
                    cell.setCellStyle(cellStyle);
 
                    cell=row.createCell(15);
                    cell.setCellValue(bom.getNotes());
                    cell.setCellStyle(cellStyle);
                }else{
                    row.createCell(1).setCellValue(i+1);
                    row.createCell(2).setCellValue(product.getParentCode());
                    row.createCell(3).setCellValue(product.getParentName());
                    row.createCell(4).setCellValue(product.getParentModel());
                    row.createCell(5).setCellValue(bom.getCategory());
                    row.createCell(6).setCellValue(bom.getSubCode());
                    row.createCell(7).setCellValue(bom.getSubName());
                    row.createCell(8).setCellValue(bom.getSubModel());
                    row.createCell(9).setCellValue(bom.getUnit()==null?"":bom.getUnit());
                    row.createCell(10).setCellValue(bom.getQuantity()==null?"": bom.getQuantity().toString());
                    row.createCell(11).setCellValue(bom.getProducer()==null?"":bom.getProducer());
                    row.createCell(12).setCellValue(bom.getMaterial());
                    row.createCell(13).setCellValue(bom.getThickness());
                    row.createCell(14).setCellValue(bom.getSurfaceDetail());
                    row.createCell(15).setCellValue(bom.getNotes());
                }
                if((bom.getPictureUrl()!=null)&&(!bom.getPictureUrl().isEmpty())){
                    ByteArrayOutputStream byteArrayOut = null;
                    try {
                        byteArrayOut = new ByteArrayOutputStream();
                        BufferedImage bufferImg = ImageIO.read(new FileInputStream(new File(fileDirName+File.separator+bom.getPictureUrl())));
                        ImageIO.write(bufferImg, "png", byteArrayOut);
                        //anchor主要用于设置图片的属性
                        HSSFClientAnchor anchor = new HSSFClientAnchor(50, 20, 1000, 230,(short) 16, rownum, (short) 16, rownum);
                        anchor.setAnchorType(ClientAnchor.MOVE_AND_RESIZE);
                        //插入图片
                        patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        if (byteArrayOut != null) {
                            try {
                                byteArrayOut.close();
                            } catch (IOException e) {
                                System.out.println("关闭ByteArrayOutputStream失败");
                            }
                        }
                    }
                }
                FileOutputStream fileOut =null;
                try {
                    fileOut = new FileOutputStream(rootFace+File.separator+excelName+".xls");
                    // 写入excel文件
                    wb.write(fileOut);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if(fileOut != null){
                        try {
                            fileOut.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
       try {
            File file=new File(rootFace+".zip");
            FileOutputStream forootFace = new FileOutputStream(file);
            ZipUtils.toZip(rootFace, forootFace,true);
 
            // 转码防止乱码
            /*resp.addHeader("Content-Disposition", "attachment;filename="
                    + new String(excelName.getBytes("UTF-8"), "ISO8859-1")
                    + ".zip");*/
            resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode (excelName+".zip", "utf-8"));
            OutputStream out = resp.getOutputStream();
            FileInputStream in = new FileInputStream(rootFace+".zip");
            int len=0;
            byte[] buffer =new byte[1024];
            //7. 将缓冲区中的数据输出
            while ((len=in.read(buffer))>0){
                out.write(buffer,0,len);
            }
            in.close();
            out.close();
            file.delete();
            ZipUtils.delDir(rootFace);
        } catch (FileNotFoundException | UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //记录日志
        logService.recordOperationLogDownLoad(ActionUtil.getUser().getId(),ActionUtil.getUser().getName(), UserOperation.TYPE_DOWNLOAD_PRODUCT.getType(),new Date(),req.getRemoteAddr()
                ,excelName+".zip",rootFace+".zip",oprateReason,oprateInfo,oprateVersion);
    }
    //查询所有产品中没有与指定散装件关联的子件
    public Response getAllSubWithOutMaterial(int materialId) {
        List<ProductBom> list=mapper.getAllSubWithOutMaterial(materialId);
        return new Response().setII(1,list.size()>0,list,"返回数据");
    }
   //根据母料型号查询子件信息及有最新版本关联的散装件信息
     /*public Response getSubByMaterialProduct(String parentModel) {
        List<ProductBom> list=mapper.getSubByMaterialProduct(parentModel);
        //查询出存在替换关系得散装件
        List<ProductBom> replaceBoms=cponentMapper.getReplaceMaterial(parentModel);
        if(replaceBoms!=null&&replaceBoms.size()>0){
            replaceBoms.stream().forEach(replaceBom->{
                        //如果存在替换关系,查询出原子件的关联关系(将原子件名称放在oldSubName中)
                        if(replaceBom!=null&&!replaceBom.getSubName().isEmpty()) {
                            //replaceBom表示为替换件
                            replaceBom.setReplaceStatus(1);
                            if (replaceBom.getOldSubName() != null && !replaceBom.getOldSubName().isEmpty()) {
                                //查询出存在的旧的关联子件,还要与原BOM版本一致
                                List<Material> oldCponent = mapper.getOldMaterial(replaceBom.getOldSubName());
                                replaceBom.setMaterials(oldCponent);
                            }
                        }
                        list.add(replaceBom);
                    }
            );
        }
        return new Response().setII(1,list.size()>0,list,"返回数据");
    }*/
    //根据产品id查询子件及其关联的物料信息
    public Response getBomAndMaterial(int productId,int version) {
        List<ProductBom> list=mapper.getBomAndMaterial(productId,version);
        if(list!=null&&list.size()>0){
            list.stream().forEach(bom -> {
                if(bom.getDwgUrl()!=null){
                    QueryWrapper wrapper=new QueryWrapper();
                    wrapper.eq("material_id",bom.getMaterialId());
                    wrapper.eq("attach_name",bom.getDwgUrl().substring(bom.getDwgUrl().lastIndexOf("\\")+1));
                    wrapper.last("limit 1");
                    AttachLock attachLock=attachLockMapper.selectOne(wrapper);
                    if(attachLock!=null){
                        bom.setDwgUrl("");
                    }
                }
            });
        }
        return new Response().setII(1,list.size()>0,list,"返回物料信息及关联物料");
    }
 
    //将物料存放为一个bom为下载做准备
    private ProductBom copyMaterialToBom(Material m) {
        ProductBom bom=new ProductBom();
        bom.setType(m.getType());
        //bom.setCategory(m.getCategory());
        bom.setSubCode(m.getSubCode());
        bom.setSubName(m.getSubName());
        bom.setSubModel(m.getSubModel());
        bom.setUnit(m.getUnit());
        //bom.setQuantity(m.getQuantity());
        //bom.setProducer(m.getProducer());
        //bom.setMaterial(m.getMaterial());
        //bom.setThickness(m.getThickness());
        //bom.setSurfaceDetail(m.getSurfaceDetail());
        //bom.setNotes(m.getNotes());
        bom.setPictureUrl(m.getPictureUrl());
        bom.setDwgUrl(m.getDwgUrl());
        bom.setFileUrl(m.getFileUrl());
        bom.setCreateDate(m.getCreateDate());
        bom.setConnFlag(1);
        return  bom;
    }
 
    public void insertBatch(List<ProductBom> productBomList) {
        mapper.insertBatchSomeColumn(productBomList);
    }
 
    public List<ProductBom> getBomByProductId(Integer productId) {
        QueryWrapper<ProductBom> query = Wrappers.query();
        query.eq("product_id",productId);
        return mapper.selectList(query);
    }
 
    public void deleteByProductId(Integer productId) {
        UpdateWrapper<ProductBom> update = Wrappers.update();
        update.eq("product_id",productId);
        mapper.delete(update);
    }
 
    public List<ProductBom> getListByCodeAndModelList2(List<ProductBom> bomList) {
        return mapper.getListByCodeAndModelList2(bomList);
    }
 
    public List<ProductBom> getEnabledBomListByParentCodeAndCustomCode(String parentCode, String customCode) {
        return mapper.getEnabledBomListByParentCodeAndCustomCode(parentCode,customCode);
    }
 
    public void updateMaterialField2Bom() {
        List<ProductBom> bomList = mapper.selectListWithMaterialField();
        mapper.updateMaterialField2BomBatch(bomList);
    }
}