李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
package com.chinaztt.mes.quality.service.impl;
 
import cn.hutool.core.date.DateUtil;
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.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.mes.common.wrapper.QueryWrapperUtil;
import com.chinaztt.mes.quality.dto.QualityHw19CodeDTO;
import com.chinaztt.mes.quality.dto.TestStandardDTO;
import com.chinaztt.mes.quality.entity.QualityHw19Code;
import com.chinaztt.mes.quality.excel.QualityHw19CodeData;
import com.chinaztt.mes.quality.excel.QualityHw19CodeImportData;
import com.chinaztt.mes.quality.mapper.QualityHw19CodeMapper;
import com.chinaztt.mes.quality.service.QualityHw19CodeService;
import lombok.Synchronized;
import com.chinaztt.ztt.common.core.constant.SecurityConstants;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.WeekFields;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
/**
 * 19条码生成信息
 *
 * @author pigx code generator
 * @date 2022-12-01 17:11:07
 */
@Service
public class QualityHw19CodeServiceImpl extends ServiceImpl<QualityHw19CodeMapper, QualityHw19Code> implements QualityHw19CodeService {
 
 
    @Override
    @Synchronized
    public void insert(QualityHw19Code qualityHw19Code) {
 
        //19固定前缀 + 物料编码 + ‘141146’供应商编码 + 年 + 周次 + Q + 长度 + S + autoId
 
        LocalDateTime barTime = qualityHw19Code.getBarTime();
 
        //获取年 周次
        String year = (barTime.getYear()+"").substring(2, 4);
        WeekFields weekFields = WeekFields.ISO;
        int weekCycle = barTime.get(weekFields.weekOfWeekBasedYear());
        String weekCycleStr = String.format("%0" + 2 + "d",weekCycle);
 
        //长度
        BigDecimal length = qualityHw19Code.getLength();
        if (length.compareTo(new BigDecimal(1000)) >= 0) {
            throw new RuntimeException("长度超出三位数,不符合规定");
        }
        DecimalFormat decimalFormat = new DecimalFormat("0000.000000");
        String lengthFormat = new BigDecimal(decimalFormat.format(length)).stripTrailingZeros().toPlainString();
        lengthFormat = String.format("%0" + 4 + "d",Integer.parseInt(lengthFormat));//长度部分格式化成4位
 
        String preCode = "19"+qualityHw19Code.getMaterialCode() +"/141146"+year+weekCycleStr+"Q"+lengthFormat+"S";
 
        Integer genCount = qualityHw19Code.getGenCount();
        for (int i = 0; i < genCount; i++) {
            QualityHw19Code entity = new QualityHw19Code();
            BeanUtils.copyProperties(qualityHw19Code,entity);
            entity.setWeekCycle(weekCycle);
            //获取生码批次
            String newestBarCode= this.baseMapper.getAutoIdByPre(preCode);
            String autoId = null;
            if (StringUtils.isBlank(newestBarCode)) {
                autoId = "000001";
            } else {
                autoId = String.format("%0" + 6 + "d", Integer.parseInt(newestBarCode.substring(newestBarCode.length()-6))+1);
            }
            entity.setBarCode(preCode+autoId);
            this.baseMapper.insert(entity);
        }
 
    }
 
    @Override
    public IPage<QualityHw19Code> pageInfo(Page page, QualityHw19CodeDTO dto) {
        return this.baseMapper.pageInfo(page,dto);
    }
 
    @Override
    public List<QualityHw19CodeData> selectExcelList(QualityHw19CodeDTO dto) {
        return this.baseMapper.selectExcelList(dto);
    }
 
    @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("19条码生成模板", "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, "19条码").head(QualityHw19CodeImportData.class).build();
            //向sheet0写入数据 传入空list这样只导出表头
            List<QualityHw19CodeImportData> respList = new ArrayList<>();
            excelWriter.write(respList, mainSheet);
            //关闭流
            excelWriter.finish();
        } catch (IOException e) {
            throw new RuntimeException("导出失败");
        }
    }
 
    @Override
    public void importExcel(List<QualityHw19CodeImportData> list) {
        long distinctNum = list.stream().map(QualityHw19CodeImportData::getBarCode).distinct().count();
        if (distinctNum < list.size()) {
            throw new RuntimeException("导入记录中19条码重复");
        }
        List<QualityHw19Code> qualityHw19CodeList = new ArrayList<>();
        for (QualityHw19CodeImportData qualityHw19CodeImportData : list) {
            //19条码非空判断
            if (StringUtils.isBlank(qualityHw19CodeImportData.getBarCode())) {
                throw new RuntimeException("19条码不能为空");
            }
            //物料编码非空判断
            if (StringUtils.isBlank(qualityHw19CodeImportData.getMaterialCode())) {
                throw new RuntimeException("物料编码不能为空");
            }
            //是否标长非空判断
            if (qualityHw19CodeImportData.getIsStandardLen()==null) {
                throw new RuntimeException("是否标长不能为空");
            }
            //生成数量非空判断
            if (qualityHw19CodeImportData.getGenCount()==null) {
                throw new RuntimeException("生成数量不能为空");
            }
            //条码日期非空及格式判断
            if (StringUtils.isBlank(qualityHw19CodeImportData.getBarTime())) {
                throw new RuntimeException("条码日期不能为空");
            }
            LocalDateTime barTime = null;
            String[] barTimeSplit = qualityHw19CodeImportData.getBarTime().split(" ");
            String barTimeString = barTimeSplit[0];
            try {
                LocalDate localDate = LocalDate.parse(barTimeString, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                barTime = localDate.atStartOfDay();
            }catch (Exception e){
                throw new RuntimeException("日期格式错误,请设置成yyyy-MM-dd格式");
            }
            //长度校验
            BigDecimal length = qualityHw19CodeImportData.getLength();
            if (length.compareTo(new BigDecimal(1000)) >= 0) {
                throw new RuntimeException("长度超出三位数,不符合规定");
            }
            Integer count = baseMapper.selectCount(Wrappers.<QualityHw19Code>lambdaQuery().eq(QualityHw19Code::getBarCode, qualityHw19CodeImportData.getBarCode()));
            if(count>0){
                throw new RuntimeException("19条码重复");
            }
            QualityHw19Code qualityHw19Code = new QualityHw19Code();
            BeanUtils.copyProperties(qualityHw19CodeImportData,qualityHw19Code);
            qualityHw19Code.setBarTime(barTime);
            qualityHw19CodeList.add(qualityHw19Code);
        }
        saveBatch(qualityHw19CodeList);
    }
}