李林
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
/*
 *    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.basic.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
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.basic.dto.ParamDTO;
import com.chinaztt.mes.basic.entity.Param;
import com.chinaztt.mes.basic.entity.ParamJoinTemplate;
import com.chinaztt.mes.basic.entity.Template;
import com.chinaztt.mes.basic.entity.TemplateTypeRelation;
import com.chinaztt.mes.basic.mapper.BasicParamTemplateMapper;
import com.chinaztt.mes.basic.mapper.ParamJoinTemplateMapper;
import com.chinaztt.mes.basic.mapper.ParamMapper;
import com.chinaztt.mes.basic.service.BasicParamTemplateService;
import com.chinaztt.mes.basic.util.DictUtils;
import com.chinaztt.mes.common.numgen.NumberGenerator;
import com.chinaztt.ztt.common.oss.service.OssTemplate;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 工序模板
 *
 * @author sunxl
 * @date 2021-03-25 13:25:43
 */
@Slf4j
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class BasicParamTemplateServiceImpl extends ServiceImpl<BasicParamTemplateMapper, Template> implements BasicParamTemplateService {
    private ParamMapper paramMapper;
    private ParamJoinTemplateMapper paramJoinTemplateMapper;
    private NumberGenerator<Template> templateNumberGenerator;
 
    private final OssTemplate minioTemplate;
 
    private DictUtils dictUtils;
 
    @Override
    public IPage<List<Param>> getParamById(Page page, QueryWrapper<ParamDTO> gen) {
        return paramMapper.getParamById(page, gen);
    }
 
 
    @Override
    public boolean updateParameters(ParamJoinTemplate paramJoinTemplate) {
        paramJoinTemplateMapper.updateParamValueJoinTemplate(paramJoinTemplate.getDefaultValue(), paramJoinTemplate.getTechnologyOperationParamId(), paramJoinTemplate.getTechnologyOperationTemplateId());
        return true;
    }
 
 
    @Override
    public List<ParamDTO> relateTypeTemplate(List<ParamJoinTemplate> operationParamJoinTemplate) {
        List<ParamJoinTemplate> newOperationParamJoinTemplateList = new ArrayList<>();
        List<ParamDTO> paramList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(operationParamJoinTemplate)) {
            List<ParamJoinTemplate> operationParamJoinTemplateList = paramJoinTemplateMapper.selectList(Wrappers.<ParamJoinTemplate>lambdaQuery().eq(ParamJoinTemplate::getTechnologyOperationTemplateId, operationParamJoinTemplate.get(0).getTechnologyOperationTemplateId()));
            A:
            for (ParamJoinTemplate newParamJoinTemplate : operationParamJoinTemplate) {
                for (int i = 0; i < operationParamJoinTemplateList.size(); i++) {
                    if (newParamJoinTemplate.getTechnologyOperationParamId().equals(operationParamJoinTemplateList.get(i).getTechnologyOperationParamId())) {
                        paramJoinTemplateMapper.updateParamJoinTemplate(newParamJoinTemplate);
                        continue A;
                    }
                }
                newOperationParamJoinTemplateList.add(newParamJoinTemplate);
            }
            for (ParamJoinTemplate newOperationParam : newOperationParamJoinTemplateList) {
                paramJoinTemplateMapper.insert(newOperationParam);
            }
            paramList = paramMapper.getTemplateParam(operationParamJoinTemplate.get(0).getTechnologyOperationTemplateId());
        }
        return paramList;
    }
 
    @Override
    public boolean deleteParamTemplate(ParamJoinTemplate operationParamJoinTemplate) {
        paramJoinTemplateMapper.delete(Wrappers.<ParamJoinTemplate>lambdaQuery().eq(ParamJoinTemplate::getTechnologyOperationParamId, operationParamJoinTemplate.getTechnologyOperationParamId()).eq(ParamJoinTemplate::getTechnologyOperationTemplateId, operationParamJoinTemplate.getTechnologyOperationTemplateId()));
        return false;
    }
 
    @Override
    public boolean fullSave(Template operationTemplate) {
        checkTemplateNo(operationTemplate);
        baseMapper.insert(operationTemplate);
        return true;
    }
 
    @Override
    public boolean fullUpdate(Template operationTemplate) {
        checkTemplateNo(operationTemplate);
        baseMapper.updateById(operationTemplate);
        return true;
    }
 
    private void checkTemplateNo(Template template) {
        if (StringUtils.isBlank(template.getTemplateNo())) {
            // 1. 自动生成编号
            template.setTemplateNo(templateNumberGenerator.generateNumberWithPrefix(Template.DIGIT, Template.PREFIX, Template::getTemplateNo));
        } else {
            // 2.判断是否重复
            int count = baseMapper.selectCount(Wrappers.<Template>query().lambda().eq(Template::getTemplateNo, template.getTemplateNo()).ne(Template::getId, template.getId()));
            if (count > 0) {
                throw new RuntimeException("存在重复的模板编号");
            }
        }
    }
 
    @Override
    public boolean delete(Long id) {
        baseMapper.deleteById(id);
        paramJoinTemplateMapper.delete(Wrappers.<ParamJoinTemplate>lambdaQuery().eq(ParamJoinTemplate::getTechnologyOperationTemplateId, id));
        return true;
    }
 
    @Override
    public IPage<List<TemplateTypeRelation>> getOperationTemplateByType(Page page, QueryWrapper<Template> gen) {
        return baseMapper.getOperationTemplateByType(page, gen);
    }
 
    @Override
    public void uploadTemplate(HttpServletResponse response, String fileName) {
        //获取字典的数组值
        //List<SysDictItem> dict = dictUtils.getDict("template_upload");
        //if (CollectionUtil.isNotEmpty(dict)) {
        //    dict.forEach(a -> {
        //        if (a.getLabel().equals(fileName)) {
        //            try (InputStream inputStream = minioTemplate.getObject("template", a.getValue())) {
        //                response.setContentType("application/octet-stream; charset=UTF-8");
        //                IoUtil.copy(inputStream, response.getOutputStream());
        //            } catch (Exception e) {
        //                log.error("文件读取异常: {}", e.getLocalizedMessage());
        //            }
        //        }
        //    });
        //}
    }
 
    @Override
    public List<ParamDTO> getLastParamById(ParamDTO paramDTO) {
        return paramMapper.getLastParamById(paramDTO);
    }
}