liuxm
2024-06-04 eeee3d8fa04aff41db6ee2418468e7371ecad57d
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
package com.dy.pmsPlatform.product;
 
import cn.hutool.core.codec.Base64;
import com.alibaba.excel.util.StringUtils;
import com.dy.common.webUtil.QueryResultVo;
import com.dy.pmsGlobal.daoOth.OthFileMapper;
import com.dy.pmsGlobal.daoPlt.PltProParamsMapper;
import com.dy.pmsGlobal.daoPlt.PltProductFileMapper;
import com.dy.pmsGlobal.daoPlt.PltProductMapper;
import com.dy.pmsGlobal.daoPlt.PltProductQualityInspectionItemsMapper;
import com.dy.pmsGlobal.dyFile.FileOperate;
import com.dy.pmsGlobal.dyFile.FileRestVo;
import com.dy.pmsGlobal.pojoOth.OthFile;
import com.dy.pmsGlobal.pojoPlt.PltProduct;
import com.dy.pmsGlobal.pojoPlt.PltProductFile;
import com.dy.pmsGlobal.util.QrCodeUtil;
import com.google.zxing.WriterException;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.common.utils.PojoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
 
@Slf4j
@Service
public class ProductSv {
    private PltProductMapper dao;
    private PltProductFileMapper pfDao;
 
    private FileOperate fileOperate;
    private OthFileMapper othFileMapper;
    private PltProductQualityInspectionItemsMapper itemDao;
    private PltProParamsMapper paramDao;
 
    @Value("${dy.webFile.fmUrl}")
    private String fmUrl ;
 
    private static final String DEFAULT_CODE = "001";
    private static final String CODE_FORMAT = "%03d";
 
    @Autowired
    public void setFileOperate(FileOperate fileOperate){
        this.fileOperate = fileOperate;
    }
    @Autowired
    public void setOthFileMapper(OthFileMapper othFileMapper){
        this.othFileMapper = othFileMapper;
    }
 
    @Autowired
    public void setDao(PltProductMapper dao) {
        this.dao = dao;
    }
    @Autowired
    public void setPfDao(PltProductFileMapper pfDao) {
        this.pfDao = pfDao;
    }
    @Autowired
    public void setItemDao(PltProductQualityInspectionItemsMapper itemDao) {
        this.itemDao = itemDao;
    }
    @Autowired
    public void setParamDao(PltProParamsMapper paramDao) {
        this.paramDao = paramDao;
    }
 
 
    @Transactional
    public int save(PltProduct p) {
        //判断产品不能重名
        if (dao.exists(p.name, p.id)) {
            throw new RuntimeException("产品名称重复");
        }
        int flag=0;
        do {
            p.code = getNextCode();
            try {
                int count = dao.insertSelective(p);
                if (count > 0) {
                    saveProRel(p);
                    return count;
                }
            } catch (DuplicateKeyException e) {
                // 如果出现唯一约束违反异常,尝试获取新的code并重试
                log.warn("插入产品时遇到唯一约束异常,尝试获取新的code", e);
            }
            flag++;
        } while (flag<5);
        return 0;
    }
    private String getNextCode(){
        log.info("获取产品编号");
        String maxCode = dao.selectMaxCode();
        if (StringUtils.isBlank(maxCode)) {
            return DEFAULT_CODE;
        } else {
            int nextCode = Integer.parseInt(maxCode) + 1;
            // 检查溢出
            if (nextCode > 999) {
                throw new RuntimeException("产品编号溢出");
            }
            return String.format(CODE_FORMAT, nextCode);
        }
    }
 
    @Transactional
    public int update(PltProduct p) {
        if (dao.exists(p.name, p.id)) {
            throw new RuntimeException("产品名称重复");
        }
        int count = dao.updateByPrimaryKeySelective(p);
        //删除旧数据,重新插入
        pfDao.deleteByProId(p.id);
        saveProRel(p);
        return count;
    }
 
 
    private void saveProRel(PltProduct p) {
        p.params.forEach(param->{
            param.proId=p.id;
            if(param.id !=null){
                paramDao.updateByPrimaryKeySelective(param);
            }else{
                param.deleted=false;
                paramDao.insert(param);
            }
        });
        saveRel(p, p.proFiles);
    }
 
    private void saveRel(PltProduct p, List<PltProductFile> docs) {
        docs.forEach(doc -> {
            doc.proId = p.id;
            pfDao.insertSelective(doc);
        });
    }
 
 
 
    /**
     * 逻辑删除实体
     * @param id 实体ID
     * @return 影响记录数量
     */
    @Transactional
    public int delete(Long id) {
        return dao.deleteLogicById(id);
    }
 
    public PltProduct selectById(String proId) {
        PltProduct pro=dao.selectByPrimaryKey(Long.valueOf(proId));
        pro=addWebUrl(pro);
        return pro;
    }
 
    private PltProduct addWebUrl(PltProduct pro) {
        if (pro != null) {
            if (pro.image != null) {
                String filePathWithWebUrl = getFilePathWithWebUrl(pro.image);
                pro.imageWebPath = filePathWithWebUrl;
                pro.imageWebPathZip = fileOperate.getImgFileZipPath(filePathWithWebUrl);
            }
 
            pro.proFiles.stream().forEach(doc -> {
                 OthFile file = othFileMapper.selectByPrimaryKey(doc.fileId);
                 if (file == null) {
                     return;
                 }
                 FileRestVo fileRestVo = fileOperate.parseHashcode(fmUrl, file.hash);
                 doc.webUrl = fileRestVo.fileSysRestUrl + fileRestVo.fileWebDownloadPath + doc.fileId;
                 doc.orgName = file.orgName;
                 doc.extName = file.extName;
             });
        }
        return pro;
    }
 
    private String getFilePathWithWebUrl(Long fileId) {
        OthFile file = othFileMapper.selectByPrimaryKey(fileId);
        FileRestVo fileRestVo = fileOperate.parseHashcode(fmUrl, file.hash);
        return fileRestVo.fileWebUrl + file.filePath;
    }
 
 
    /**
     * 获取产品列表
     */
    public QueryResultVo<List<PltProduct>> selectSome(QueryVo queryVo) {
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo);
        //查询符合条件的记录总数
        Long itemTotal = dao.selectSomeCount(params);
        QueryResultVo<List<PltProduct>> rsVo = new QueryResultVo<>(queryVo.pageSize, queryVo.pageCurr) ;
        //计算分页等信息
        rsVo.calculateAndSet(itemTotal, params);
        //查询符合条件的记录
        rsVo.obj = dao.selectSome(params) ;
        if(CollectionUtils.isNotEmpty(rsVo.obj)){
            rsVo.obj.parallelStream().forEach(item->{
                try {
                    byte[] codes = QrCodeUtil.genQrCode(item.code);
                    item.qrCode = "data:image/jpeg;base64," + Base64.encode(codes);
                    item = addWebUrl(item);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (WriterException e) {
                    e.printStackTrace();
                }
            });
        }
        return rsVo ;
    }
 
    /**
     * 添加产品文档关联
     * @param proId 产品id
     * @param fileId 文档id
     * @return 更新数量
     */
    public int addDoc(long proId,long fileId,String fileType){
        PltProductFile pf=new PltProductFile();
        pf.fileId = fileId;
        pf.proId = proId;
        pf.fileType = fileType;
        return pfDao.insertSelective(pf);
    }
 
    /**
     * 查询产品关联文档
     * @param proId 产品id
     * @return 返回关联文档集合
     */
    public List<OthFile> selectDoc(long proId){
        return othFileMapper.selectByProId(proId);
    }
 
 
    public List<PltProduct> selectAll(QueryVo queryVo) {
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo);
        return dao.selectAll(params);
    }
 
 
    public void downloadDoc(HttpServletResponse response) {
        PltProduct pro=dao.selectByPrimaryKey(Long.valueOf(1));
    }
}