zss
2023-11-07 3c34085a21511610477f555cebf3f6fa42272402
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
/*
 *    Copyright (c) 2018-2025, ztt All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: ztt
 */
package com.chinaztt.mes.technology.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaztt.ifs.api.feign.IfsFeignClient;
import com.chinaztt.mes.basic.entity.Part;
import com.chinaztt.mes.basic.mapper.PartMapper;
import com.chinaztt.mes.basic.util.DictUtils;
import com.chinaztt.mes.common.numgen.NumberGenerator;
import com.chinaztt.mes.technology.dto.StructureComponentDTO;
import com.chinaztt.mes.technology.dto.StructureDTO;
import com.chinaztt.mes.technology.dto.StructureTree;
import com.chinaztt.mes.technology.entity.Operation;
import com.chinaztt.mes.technology.entity.Structure;
import com.chinaztt.mes.technology.entity.StructureComponent;
import com.chinaztt.mes.technology.excel.StructureData;
import com.chinaztt.mes.technology.mapper.OperationMapper;
import com.chinaztt.mes.technology.mapper.StructureComponentMapper;
import com.chinaztt.mes.technology.mapper.StructureMapper;
import com.chinaztt.mes.technology.service.StructureService;
import com.chinaztt.ztt.common.core.util.R;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;
 
 
/**
 * 产品结构
 *
 * @author zhangxy
 * @date 2020-08-19 10:48:07
 */
@AllArgsConstructor
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class StructureServiceImpl extends ServiceImpl<StructureMapper, Structure> implements StructureService {
 
    private OperationMapper operationMapper;
    private StructureComponentMapper structureComponentMapper;
    private StructureMapper structureMapper;
    private PartMapper partMapper;
    private NumberGenerator<Structure> numberGenerator;
    private DictUtils dictUtils;
    private IfsFeignClient ifsFeignClient;
 
    /**
     * @Author: Hans
     * @Description: 保存产品结构数据
     * @Date: 2022-05-31
     */
    private void saveStructureDTO(StructureDTO structureDTO) {
        if (StringUtils.isEmpty(structureDTO.getBomTypeDb())) {
            structureDTO.setBomTypeDb("M");
        }
        //根据零件号和版本号查询数据
        Part part = partMapper.selectOne(Wrappers.<Part>lambdaQuery()
                .eq(Part::getPartNo, structureDTO.getPartNo())
                .eq(Part::getEngChgLevel, structureDTO.getVersion()));
        if (null == part) {
            throw new RuntimeException("找不到当前版本零件,请确认! 零件号:" + structureDTO.getPartNo() + " ! 版本号:" + structureDTO.getVersion());
        }
        structureDTO.setPartId(part.getId());
        int count = getBaseMapper().selectCount(Wrappers.<Structure>lambdaQuery()
                .eq(Structure::getVersion, structureDTO.getVersion())
                .eq(Structure::getAlternativeNo, structureDTO.getAlternativeNo())
                .eq(Structure::getBomTypeDb, structureDTO.getBomTypeDb())
                .eq(Structure::getPartId, structureDTO.getPartId()));
        if (count > 0) {
            throw new RuntimeException(" 零件号:" + structureDTO.getPartNo() + " ! " +
                    "版本号:" + structureDTO.getVersion() + " " +
                    "替代号:" + structureDTO.getAlternativeNo() + "" +
                    "类型:" + structureDTO.getBomTypeDb() + "" +
                    "重复!");
        }
        baseMapper.insert(structureDTO);
        Long structureId = structureDTO.getId();
        for (StructureComponentDTO structureComponentDTO : structureDTO.getComponents()) {
            structureComponentDTO.setStructureId(structureId);
            if (StringUtils.isEmpty(structureComponentDTO.getOperationName())) {
                throw new RuntimeException("缺少工序");
            }
            // 查询工序 如果有多个获取第一个
            List<Operation> operationList = operationMapper.selectList(Wrappers.<Operation>lambdaQuery()
                    .eq(Operation::getName, structureComponentDTO.getOperationName())
                    .eq(Operation::getActive, true).orderByAsc(Operation::getId));
            if (CollectionUtil.isEmpty(operationList)) {
                throw new RuntimeException("工序" + structureComponentDTO.getOperationName() + "不存在");
            }
            // 填入工序的id
            structureComponentDTO.setOperationId(operationList.get(0).getId());
            // 零件信息
            Part partComponent = partMapper.selectOne(Wrappers.<Part>lambdaQuery()
                    .eq(Part::getPartNo, structureComponentDTO.getPartNo())
                    .eq(Part::getEngChgLevel, structureComponentDTO.getVersion()));
            if (null == partComponent) {
                throw new RuntimeException("找不到当前版本零件,请确认! 零件号:" + structureComponentDTO.getPartNo() + " ! 版本号:" + structureComponentDTO.getVersion());
            }
            structureComponentDTO.setPartId(partComponent.getId());
            if (StringUtils.isEmpty(structureComponentDTO.getPlanningMethod())) {
                structureComponentDTO.setPlanningMethod(partComponent.getPlanningMethod());
            }
            structureComponentMapper.insert(structureComponentDTO);
        }
    }
 
    /**
     * @Author: Hans
     * @Description: 修改产品结构导入方法
     * @Date: 2022-05-31
     */
    @Override
    public void importExcelExt(Map<Integer, String> headMap, List<Map<Integer, String>> dataList) {
        log.info("--start---导入判断----");
        // 封装数据
        List<StructureDTO> structureDTOList = new ArrayList<>();
        try {
            for (Map<Integer, String> map : dataList) {
                // 初始化产品结构主表数据
                StructureDTO structureDTO = initStructureDTO(map);
                List<StructureComponentDTO> structureComponentDTOList = initStructureComponentDTO(headMap, map);
                structureDTO.setComponents(structureComponentDTOList);
                structureDTOList.add(structureDTO);
            }
        } catch (Exception e) {
            throw new RuntimeException("请核对必填数据是否为空或者数据格式是否正确");
        }
        // 保存产品结构数据
        for (StructureDTO structureDTO : structureDTOList) {
            saveStructureDTO(structureDTO);
        }
    }
 
    /**
     * @Author: Hans
     * @Description: 初始化产品结构主表数据
     * @Date: 2022-05-31
     */
    private StructureDTO initStructureDTO(Map<Integer, String> map) {
        Map<String, String> dicbomtype = dictUtils.getDicKey("bom_type_db");
        StructureDTO structureDTO = new StructureDTO();
        //第一列是零件号
        String partNo = map.get(0);
        structureDTO.setPartNo(partNo);
        //第二列是零件名称
        String partName = map.get(1);
        structureDTO.setPartName(partName);
        //第三列是版本号
        String version = map.get(2);
        structureDTO.setVersion(version);
        //第四列是结构类型
        String structureType = map.get(3);
        structureDTO.setBomTypeDb(StringUtils.isEmpty(structureType) ? "M" :
                (StringUtils.isEmpty(dicbomtype.get(structureType)) ? "M" : dicbomtype.get(structureType)));
        //第五列是替代
        String alternativeNo = map.get(4);
        structureDTO.setAlternativeNo(StringUtils.isEmpty(alternativeNo) ? "*" : alternativeNo);
        //第六列是替代描述
        String alternativeDesc = map.get(5);
        structureDTO.setAlternativeDesc(alternativeDesc);
        structureDTO.setIfsSync(false);
 
        return structureDTO;
    }
 
    /**
     * @Author: Hans
     * @Description: 初始化产品结构明细表数据
     * @Date: 2022-05-31
     */
    private List<StructureComponentDTO> initStructureComponentDTO(Map<Integer, String> headMap, Map<Integer, String> map) {
        List<StructureComponentDTO> structureComponentDTOList = new ArrayList<>();
        // 存储工序的下标
        List<Integer> operationIndexList = new ArrayList<>();
        // 存储盘数(盘)的下标
        List<Integer> operationIndex2List = new ArrayList<>();
        // 获取下标
        for (int i = 0; i < headMap.size(); i++) {
            String columnName = headMap.get(i);
            if ("行项号".equals(columnName)) {
                operationIndexList.add(i);
            }
            if ("盘数(盘)".equals(columnName)) {
                operationIndex2List.add(i);
            }
        }
        //根据头标和角标开始查询子表数据
        for (int i = 0; i < operationIndexList.size(); i++) {
            int start = operationIndexList.get(i);
            int end = operationIndex2List.get(i);
            StructureComponentDTO structureComponentDTO = new StructureComponentDTO();
            String lineItemNo = map.get(start);
            if (StringUtils.isEmpty(lineItemNo)) {
                continue;
            }
            //行号
            structureComponentDTO.setLineItemNo(Integer.valueOf(lineItemNo));
            //工序
            structureComponentDTO.setOperationName(map.get(start + 1));
            //零件号
            structureComponentDTO.setPartNo(map.get(start + 2));
            //零件名称
            structureComponentDTO.setPartName(map.get(start + 3));
            //零件版本
            structureComponentDTO.setVersion(map.get(start + 4));
            //数量
            structureComponentDTO.setQpa(new BigDecimal(map.get(start + 5)));
            //单位
            structureComponentDTO.setUnit(map.get(start + 6));
            //颜色
            structureComponentDTO.setColor(map.get(start + 7));
            //计划方法
            structureComponentDTO.setPlanningMethod(map.get(start + 8));
            //盘数可以为空
            if (!StringUtils.isEmpty(map.get(end))) {
                structureComponentDTO.setDiscNum(Long.valueOf(map.get(end)));
            }
            structureComponentDTOList.add(structureComponentDTO);
        }
        return structureComponentDTOList;
    }
 
    @Override
    public void importExcel(List<StructureData> list) {
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        for (StructureData data : list) {
            List<Part> parts = partMapper.selectList(Wrappers.<Part>lambdaQuery().eq(Part::getPartNo, data.getPartNo()));
            Part part = parts.get(0);
            if (part == null) {
                throw new RuntimeException("零件号:" + data.getPartNo() + "不存在");
            }
            Structure structure = baseMapper.selectOne(Wrappers.<Structure>lambdaQuery().eq(Structure::getPartId, part.getId()));
            if (structure == null) {
                structure = new Structure();
                BeanUtils.copyProperties(data, structure);
                structure.setPartId(part.getId());
                structure.setActive(true);
                structure.setBomTypeDb("M");
                structure.setIfsSync(false);
                structure.setAlternativeNo("*");
                baseMapper.insert(structure);
            }
            List<Part> childParts = partMapper.selectList(Wrappers.<Part>lambdaQuery().eq(Part::getPartNo, data.getSonPartNo()));
            Part childPart = childParts.get(0);
            if (childPart == null) {
                throw new RuntimeException("零件号:" + data.getSonPartNo() + "不存在");
            }
            List<Operation> operations = operationMapper.selectList(Wrappers.<Operation>lambdaQuery().eq(Operation::getName, data.getOperationName()));
            Operation operation = operations.get(0);
            StructureComponent component = new StructureComponent();
            component.setStructureId(structure.getId());
            component.setPartId(childPart.getId());
            component.setPlanningMethod(childPart.getPlanningMethod());
            component.setQpa(new BigDecimal(1));// todo: 产品结构导入数量,还缺一个行号
            component.setOperationId(operation.getId());
            structureComponentMapper.insert(component);
        }
    }
 
    @Override
    public StructureTree getMasterTreeByPartId(Long partId) {
        Structure structure = baseMapper.selectOne(Wrappers.<Structure>lambdaQuery().eq(Structure::getMaster, true).eq(Structure::getPartId, partId));
        if (structure != null) {
            return this.getTree(structure.getId());
        }
        return null;
    }
 
    @Override
    public StructureDTO getFullById(Long id) {
        StructureDTO structure = baseMapper.getDtoById(id);
        if (structure == null) {
            return null;
        }
        structure.setComponents(baseMapper.getComponentDtoByStructureId(id));
        return structure;
    }
 
 
    /**
     * 检验是否引用自己,是否存在死循环
     * 该节点是否在父节点及父上级里
     *
     * @param mainPartId
     * @param compPartId
     */
    private void validate(Long mainPartId, Long compPartId) {
        if (mainPartId != null && mainPartId.equals(compPartId)) {
            throw new RuntimeException("不能引用自身");
        }
        Set<Long> parentIds = baseMapper.getParentPartIds(mainPartId);
        if (CollectionUtil.isEmpty(parentIds)) {
            return;
        }
        if (parentIds.contains(compPartId)) {
            throw new RuntimeException("存在循环引用");
        }
        for (Long id : parentIds) {
            validate(id, compPartId);
        }
    }
 
    @Override
    public R fullSave(StructureDTO structure) {
        System.out.println(structure);
        System.out.println("===============================");
        int noCount = baseMapper.selectCount(Wrappers.<Structure>lambdaQuery()
                .eq(Structure::getAlternativeNo, structure.getAlternativeNo())
                .eq(Structure::getBomTypeDb, structure.getBomTypeDb())
                .eq(Structure::getPartId, structure.getPartId()));
        if (noCount > 0) {
            return R.failed("该零件已存在产品结构");
        }
        resetMaster(structure);
        baseMapper.insert(structure);
        return R.ok(structure.getId());
    }
 
    private void resetMaster(StructureDTO structure) {
        if (structure.getMaster()) {
            UpdateWrapper<Structure> updateWrapper = new UpdateWrapper();
            updateWrapper.lambda().eq(Structure::getPartId, structure.getPartId()).set(Structure::getMaster, false);
            this.update(updateWrapper);
        }
    }
 
    @Override
    public R<Boolean> fullUpdate(StructureDTO structure) {
        int noCount = baseMapper.selectCount(Wrappers.<Structure>lambdaQuery()
                .eq(Structure::getAlternativeNo, structure.getAlternativeNo())
                .eq(Structure::getBomTypeDb, structure.getBomTypeDb())
                .eq(Structure::getPartId, structure.getPartId())
                .ne(Structure::getId, structure.getId()));
        if (noCount > 0) {
            return R.failed("该零件已存在产品结构");
        }
        structure.setIfsSync(false);
        resetMaster(structure);
        baseMapper.updateById(structure);
        //// 对接ifs
        //List<StructureDTO> structureDTOS = structureMapper.getStructureDtoByIds(Arrays.asList(structure.getId()));
        //structureIfsSyncByDto(structureDTOS);
        return R.ok();
    }
 
    @Override
    public StructureTree getTree(Long id) {
        StructureDTO structure = baseMapper.getDtoById(id);
        StructureTree root = new StructureTree();
        root.setPartName(structure.getPartName());
        root.setPartId(structure.getPartId());
        root.setQpa(BigDecimal.ONE);
        List<StructureComponentDTO> comps = baseMapper.getComponentDtoByStructureId(id);
        repeatComponents(root, comps);
        return root;
    }
 
    @Override
    public R<Boolean> changeState(List<Long> ids, String event) {
        return R.ok();
    }
 
    @Override
    public IPage<List<StructureDTO>> getPage(Page page, QueryWrapper<StructureDTO> ew) {
        return baseMapper.getStructurePage(page, ew);
    }
 
    @Override
    public List<Structure> getBuildBom() {
        return structureMapper.selectList(Wrappers.<Structure>lambdaQuery().eq(Structure::getActive, true)
                .notInSql(Structure::getPartId, "SELECT part_id FROM technology_bom")
                .inSql(Structure::getPartId, "SELECT ID FROM basic_part WHERE planning_method = 'A'"));
    }
 
    /**
     * 复制产品结构
     */
    @Override
    public R<Boolean> copyStructureSave(List<StructureDTO> structure) {
        if (CollectionUtil.isEmpty(structure)) {
            return R.failed("请选择产品结构");
        }
        for (StructureDTO structureDTO : structure) {
            List<StructureComponent> structureComponentList = structureComponentMapper.selectList(Wrappers.<StructureComponent>lambdaQuery().eq(StructureComponent::getStructureId, structureDTO.getId()));
            structureDTO.setId(null);
            structureDTO.setVersion(numberGenerator.generateNumberWithPrefix(Structure.DIGIT, Structure.PREFIX, Structure::getVersion));
            structureDTO.setMaster(false);
            baseMapper.insert(structureDTO);
            for (StructureComponent structureComponent : structureComponentList) {
                structureComponent.setId(null);
                structureComponent.setStructureId(structureDTO.getId());
                structureComponentMapper.insert(structureComponent);
            }
        }
        return R.ok();
    }
 
    @Override
    public R addComponent(StructureComponent component) {
        if (null == component.getOperationId() || null == component.getPartId() || null == component.getQpa()) {
            throw new RuntimeException("工序、零件和数量不可为空");
        }
        Structure structure = baseMapper.selectById(component.getStructureId());
        structure.setIfsSync(false);
        validate(structure.getPartId(), component.getPartId());
        structureComponentMapper.insert(component);
        // 对接ifs
//        List<StructureDTO> structureDTOS = structureMapper.getStructureDtoByIds(Arrays.asList(component.getStructureId()));
//        structureIfsSyncByDto(structureDTOS);
        return R.ok(component.getId());
    }
 
    @Override
    public R batchComponent(List<StructureComponent> structureComponents) {
        structureComponents.forEach(structureComponent -> {
            StructureComponent structureComponentUpdate = structureComponentMapper.selectById(structureComponent.getId());
            structureComponentUpdate.setLineItemNo(structureComponent.getLineItemNo());
            structureComponentMapper.updateById(structureComponentUpdate);
        });
        // 对接ifs
        List<StructureDTO> structureDTOS = structureMapper.getStructureDtoByIds(Arrays.asList(structureComponents.get(0).getStructureId()));
        structureIfsSyncByDto(structureDTOS);
        return R.ok();
    }
 
    @Override
    public R updateComponent(StructureComponent component) {
        if (null == component.getOperationId() || null == component.getPartId() || null == component.getQpa()) {
            throw new RuntimeException("工序、零件和数量不可为空");
        }
        Structure structure = baseMapper.selectById(component.getStructureId());
        structure.setIfsSync(false);
        validate(structure.getPartId(), component.getPartId());
        structureComponentMapper.updateById(component);
        // 对接ifs
//        List<StructureDTO> structureDTOS = structureMapper.getStructureDtoByIds(Arrays.asList(component.getStructureId()));
//        structureIfsSyncByDto(structureDTOS);
        return R.ok();
    }
 
    @Override
    public R removeComponent(Long compId) {
        StructureComponent component = structureComponentMapper.selectById(compId);
        structureComponentMapper.deleteById(compId);
        Structure structure = baseMapper.selectById(component.getStructureId());
        structure.setIfsSync(false);
        baseMapper.updateById(structure);
        return R.ok();
    }
 
 
    private void repeatComponents(StructureTree parent, List<StructureComponentDTO> comps) {
        if (CollectionUtil.isEmpty(comps)) {
            return;
        }
        BigDecimal parentQpa = parent.getQpa();
        List<StructureTree> children = new ArrayList<>();
        for (StructureComponentDTO c : comps) {
            StructureTree child = new StructureTree();
            child.setPartName(c.getPartName());
            child.setPartId(c.getPartId());
            child.setQpa(c.getQpa().multiply(parentQpa));
            children.add(child);
            buildChildren(child, c.getPartId());
        }
        parent.setChildren(children);
    }
 
 
    private void buildChildren(StructureTree parent, Long partId) {
        List<StructureComponentDTO> comps = baseMapper.getMasterComponentsByPartId(partId);
        repeatComponents(parent, comps);
    }
 
    /**
     * 导入模板下载
     *
     * @param response
     * @throws IOException
     */
    @Override
    public void exportModel(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("structureImportModel", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        try {
            //新建ExcelWriter
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
            //获取sheet0对象
            WriteSheet mainSheet = EasyExcel.writerSheet(0, "导入模板").head(StructureData.class).build();
            //向sheet0写入数据 传入空list这样只导出表头
            List<StructureData> structureData = new ArrayList<>();
            excelWriter.write(structureData, mainSheet);
            //关闭流
            excelWriter.finish();
        } catch (IOException e) {
            throw new RuntimeException("导出失败");
        }
    }
 
    /**
     * 产品结构对接ifs
     *
     * @param structureIds
     * @return
     */
    @Override
    public R structureIfsSync(List<Long> structureIds) {
        List<StructureDTO> structureDTOS = structureMapper.getStructureDtoByIds(structureIds);
        return R.ok(structureIfsSyncByDto(structureDTOS));
    }
 
    /**
     * 对接ifs
     *
     * @param structureDTOS
     * @return
     */
    @Override
    public boolean structureIfsSyncByDto(List<StructureDTO> structureDTOS) {
        JSONObject jsonObject = new JSONObject()
                .fluentPut("RECORD_ID", UUID.randomUUID().toString())
                .fluentPut("SYSCODE", "LMES")
                .fluentPut("SYSMODEL", "产品结构");
        JSONArray batchInfoJsonArray = new JSONArray();
        List<Long> ids = new ArrayList<>();
        structureDTOS.forEach(structureDTO -> {
            JSONObject batchInfoJsonObj = new JSONObject()
                    //版本
                    .fluentPut("ENG_CHG_LEVEL", structureDTO.getVersion())
                    //零件号
                    .fluentPut("PART_NO", structureDTO.getPartNo())
                    //bom类型
                    .fluentPut("BOM_TYPE", structureDTO.getBomTypeDb())
                    //生效日期
                    .fluentPut("EFF_PHASE_IN_DATE", "2000-01-01")
                    //結束時間
                    .fluentPut("EFF_PHASE_OUT_DATE", "");
            JSONArray alinfoArray = new JSONArray();
            JSONObject alinfoObj = new JSONObject()
                    //替代
                    .fluentPut("ALTERNATIVE_NO", structureDTO.getAlternativeNo())
                    //替代描述
                    .fluentPut("ALTERNATIVE_DESC", structureDTO.getAlternativeDesc())
                    //是否建立
                    .fluentPut("IS_BUILD", "TRUE");
            JSONArray compInfoJsonArray = new JSONArray();
            alinfoObj.put("COMP_INFO", compInfoJsonArray);
            structureDTO.getComponents().forEach(structureComponentDTO -> {
                compInfoJsonArray.add(new JSONObject()
                        .fluentPut("LINE_ITEM_NO", structureComponentDTO.getLineItemNo())
                        .fluentPut("COMPONENT_PART", structureComponentDTO.getPartNo())
                        .fluentPut("QTY_PER_ASSEMBLY", structureComponentDTO.getQpa()));
            });
            alinfoArray.add(alinfoObj);
            ids.add(structureDTO.getId());
            batchInfoJsonObj.put("ALTER_INFO", alinfoArray);
            batchInfoJsonArray.add(batchInfoJsonObj);
        });
        jsonObject.put("BATCH_INFO", batchInfoJsonArray);
        if (batchInfoJsonArray.size() > 0) {
            R result = ifsFeignClient.importProdStructureStd(jsonObject, true);
            if (result.getCode() == 1) {
                throw new RuntimeException("IFS错误——" + result.getMsg());
            } else {
                structureMapper.updateIfsTrue(ids);
            }
        }
        return true;
    }
}