郑永安
2023-06-19 f65443d8abeaedc9d102324565e8368e7c9d90c8
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
package com.gk.firework.Service.ServiceImpl;
 
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.firework.Domain.*;
import com.gk.firework.Domain.Enum.CertificateStatus;
import com.gk.firework.Domain.Exception.BusinessException;
import com.gk.firework.Domain.Utils.Properties;
import com.gk.firework.Domain.Utils.StringUtils;
import com.gk.firework.Domain.Utils.UploadUtil;
import com.gk.firework.Domain.Vo.TransportCertVo;
import com.gk.firework.Mapper.TransportCertMapper;
import com.gk.firework.Service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import sun.rmi.transport.Transport;
 
import java.util.*;
 
@Service("transportCertService")
public class TransportCertServiceImpl extends ServiceImpl<TransportCertMapper, TransportCert> implements TransportCertService {
 
    @Autowired
    private UserService userService;
    @Autowired
    private TransportCertProductService transportCertProductService;
    @Autowired
    private TransportCertificatePersonService transportCertificatePersonService;
    @Autowired
    private TransportCertMapper transportCertMapper;
    @Autowired
    private TransportCertFileService transportCertFileService;
 
    @Override
    @Transactional
    public void addCert(TransportCertVo transportCertVo, UserInfo user) throws Exception {
 
        if (StringUtils.isBlank(transportCertVo.getCode())) {
            throw new BusinessException("运输证编号不能为空");
        }
        //判断重复
        if (this.countByCode(transportCertVo.getCode()) > 0) {
            throw new BusinessException("运输证编号已经重复");
        }
        JSONArray personJson = JSONArray.parseArray(transportCertVo.getTransportPerson());
        JSONArray productJson = JSONArray.parseArray(transportCertVo.getTransportProduct());
 
        if (personJson == null) {
            throw new BusinessException("产品信息不能不填");
        }
        if (productJson == null) {
            throw new BusinessException("驾驶员|押运员不能不填");
        }
 
 
        UserInfo userInfo = userService.getById(user.getId());
        if (StringUtils.isBlank(userInfo.getCompanynumber())){
            throw new BusinessException("没有权限创建运输证");
        }
        //新增运输证基本信息
        TransportCert transportCert = new TransportCert();
        {
//            String code = "TC-" + System.currentTimeMillis();
            //运输证上传改手动上传-》编号已存在(手填)
            transportCert.setCode(transportCertVo.getCode());
            transportCert.setCreateby(user.getId());
            transportCert.setCreatebyname(user.getUsername());
            transportCert.setEnterprisenumber(userInfo.getCompanynumber());
            transportCert.setCreatetime(new Date());
            transportCert.setValidflag(true);
            transportCert.setStatus(CertificateStatus.PENDING);
            transportCert.setCarnumber(transportCertVo.getCarnumber());
            transportCert.setContractcode(transportCertVo.getContractcode());
            transportCert.setReceiveraddress(transportCertVo.getReceiveraddress());
            transportCert.setReceivercompanyname(transportCertVo.getReceivercompanyname());
            transportCert.setReceiverprincipalname(transportCertVo.getReceiverprincipalname());
            transportCert.setReceiveraddress(transportCertVo.getReceiveraddress());
            transportCert.setReceiverprincipalphone(transportCertVo.getReceiverprincipalphone());
            transportCert.setTransportPerson(personJson.toJavaList(TransportCertificatePerson.class));
            transportCert.setTransportProduct(productJson.toJavaList(TransportCertProduct.class));
            transportCert.setprocesstime(transportCertVo.getProcesstime());
 
            this.save(transportCert);
        }
 
        //新增产品
        {
            List<TransportCertProduct> products = transportCert.getTransportProduct();
            if (products != null && products.size() > 0) {
                for (TransportCertProduct product : products) {
                    product.setCertificatecode(transportCert.getCode());
                    product.setValidflag(true);
                }
                transportCertProductService.saveBatch(products);
            }
        }
 
        //新增 驾驶员|押运员
        {
            List<TransportCertificatePerson> persons = transportCert.getTransportPerson();
            if (persons != null && persons.size() > 0) {
                for (TransportCertificatePerson person : persons) {
                    person.setCertificatecode(transportCert.getCode());
                    person.setValidflag(true);
                }
                transportCertificatePersonService.saveBatch(persons);
            }
        }
 
        //新增文件信息
        {
            MultipartFile[] files = transportCertVo.getFile();
            List<TransportCertFile> adds;
            if (files != null && files.length > 0) {
                adds = new ArrayList<>();
                for (MultipartFile file : files) {
                    String name = UploadUtil.uploadFile(file, com.gk.firework.Domain.Utils.Properties.transportCertificatePath);
                    TransportCertFile certFile = new TransportCertFile();
                    certFile.setCertificatecode(transportCert.getCode());
                    certFile.setFilename(file.getOriginalFilename());
                    certFile.setType(file.getContentType());
                    certFile.setUrl(Properties.transportCertificate + name);
                    certFile.setValidflag(true);
                    adds.add(certFile);
                }
                transportCertFileService.saveBatch(adds);
 
            }
 
        }
    }
 
 
    /**
    * @Description: 分页查询运输证
    * @date 2021/4/25 15:11
    */
    @Override
    public IPage selectPages(Page<TransportCert> page, Map filter, UserInfo userInfo) {
 
        UserInfo user = userService.getById(userInfo.getId());
        Map<String, Object> params = new HashMap<>();
        //可见权限
        {
            params.put("enterprisenumber", user.getCompanynumber());
            params.put("province", user.getProvince());
            params.put("city", user.getCity());
            params.put("district", user.getArea());
            params.put("street", user.getTown());
            params.put("committee", user.getCommunity());
        }
        //基本
        params.put("filterProvince", filter.get("province"));
        params.put("filterCity", filter.get("city"));
        params.put("filterDistrict",filter.get("district"));
        params.put("filterStreet", filter.get("street"));
        params.put("filterCommittee", filter.get("committee"));
        params.put("code", filter.get("code"));
        List<TransportCert> list = transportCertMapper.selectPages(page, params);
        return page.setRecords(list);
    }
 
 
    /**
     * @Description: 修改运输证
     * @date 2021/4/25 15:29
     */
    @Override
    @Transactional
    public void modCert(TransportCertVo transportCertVo, UserInfo userInfo) throws Exception {
 
        if (StringUtils.isBlank(transportCertVo.getCode())) {
            throw new BusinessException("运输证编号不能为空");
        }
        JSONArray personJson = JSONArray.parseArray(transportCertVo.getTransportPerson());
        JSONArray productJson = JSONArray.parseArray(transportCertVo.getTransportProduct());
 
        if (personJson == null) {
            throw new BusinessException("产品信息不能不填");
        }
        if (productJson == null) {
            throw new BusinessException("驾驶员|押运员不能不填");
        }
        //修改运输证基本信息
        TransportCert transportCert = new TransportCert();
        UserInfo user = userService.getById(userInfo.getId());
        {
            transportCert.setCode(transportCertVo.getCode());
            transportCert.setId(transportCertVo.getId());
            transportCert.setCarnumber(transportCertVo.getCarnumber());
            transportCert.setContractcode(transportCertVo.getContractcode());
            transportCert.setReceiveraddress(transportCertVo.getReceiveraddress());
            transportCert.setReceivercompanyname(transportCertVo.getReceivercompanyname());
            transportCert.setReceiverprincipalname(transportCertVo.getReceiverprincipalname());
            transportCert.setReceiveraddress(transportCertVo.getReceiveraddress());
            transportCert.setReceiverprincipalphone(transportCertVo.getReceiverprincipalphone());
            transportCert.setUpdateby(user.getId());
            transportCert.setUpdatebyname(user.getUsername());
            transportCert.setUpdatetime(new Date());
            transportCert.setTransportPerson(personJson.toJavaList(TransportCertificatePerson.class));
            transportCert.setTransportProduct(productJson.toJavaList(TransportCertProduct.class));
            transportCert.setprocesstime(transportCertVo.getProcesstime());
 
            this.updateById(transportCert);
        }
        //修改产品
        {
            List<TransportCertProduct> adds = new ArrayList<>();
            List<TransportCertProduct> dels = new ArrayList<>();
            List<TransportCertProduct> upds = new ArrayList<>();
            List<TransportCertProduct> products = transportCert.getTransportProduct();
            List<TransportCertProduct> productList = transportCertProductService.selectProduct(transportCert.getCode());
            if (products.size() > 0) {
                for (TransportCertProduct product : products) {
                    if (product.getId() == null) {
                        product.setValidflag(true);
                        product.setCertificatecode(transportCert.getCode());
                        adds.add(product);
                    }
                }
            }
            //B-A
            if (productList.size() > 0) {
                for (int i = 0; i < productList.size(); i++) {
                    boolean flag = true;
                    for (int j = 0; j < products.size(); j++) {
                        if (productList.get(i).getId().equals(products.get(j).getId())) {
                            upds.add(products.get(j));
                            flag = false;
                        }
                    }
                    if (flag) {
                        productList.get(i).setValidflag(false);
                        dels.add(productList.get(i));
                    }
 
 
                }
            }
 
            if (adds.size() > 0) {
                transportCertProductService.saveBatch(adds);
            }
            if (dels.size() > 0) {
                transportCertProductService.updateBatchById(dels);
            }
            if (upds.size() > 0) {
                transportCertProductService.updateBatchById(upds);
            }
        }
 
        //修改人员
        {
 
            List<TransportCertificatePerson> adds = new ArrayList<>();
            List<TransportCertificatePerson> dels = new ArrayList<>();
            List<TransportCertificatePerson> upds = new ArrayList<>();
            List<TransportCertificatePerson> persons = transportCert.getTransportPerson();
            List<TransportCertificatePerson> personList = transportCertificatePersonService.selectProduct(transportCert.getCode());
            if (persons.size() > 0) {
                for (TransportCertificatePerson person : persons) {
                    if (person.getId() == null) {
                        person.setValidflag(true);
                        person.setCertificatecode(transportCert.getCode());
                        adds.add(person);
                    }
                }
            }
            //B-A
            if (personList.size() > 0) {
                for (int i = 0; i < personList.size(); i++) {
                    boolean flag = true;
                    for (int j = 0; j < persons.size(); j++) {
                        if (personList.get(i).getId().equals(persons.get(j).getId())) {
                            upds.add(persons.get(j));
                            flag = false;
                        }
                    }
                    if (flag) {
                        personList.get(i).setValidflag(false);
                        dels.add(personList.get(i));
                    }
 
 
                }
            }
 
            if (adds.size() > 0) {
                transportCertificatePersonService.saveBatch(adds);
            }
            if (dels.size() > 0) {
                transportCertificatePersonService.updateBatchById(dels);
            }
            if (upds.size() > 0) {
                transportCertificatePersonService.updateBatchById(upds);
            }
        }
 
 
        //修改文件(每次上传都是覆盖)
        {
 
            MultipartFile[] files = transportCertVo.getFile();
            List<TransportCertFile> adds;
            if (files != null && files.length > 0) {
                //1.删除所有文件
                transportCertFileService.deleteByCode(transportCert.getCode());
 
                //2.新增新上传文件
                adds = new ArrayList<>();
                for (MultipartFile file : files) {
                    String name = UploadUtil.uploadFile(file, com.gk.firework.Domain.Utils.Properties.transportCertificatePath);
                    TransportCertFile certFile = new TransportCertFile();
                    certFile.setCertificatecode(transportCert.getCode());
                    certFile.setFilename(file.getOriginalFilename());
                    certFile.setType(file.getContentType());
                    certFile.setUrl(Properties.transportCertificate + name);
                    certFile.setValidflag(true);
                    adds.add(certFile);
                }
                transportCertFileService.saveBatch(adds);
 
            }
        }
    }
 
 
    /**
    * @Description: 删除单子
    * @date 2021/4/28 8:33
    */
    @Override
    public void delCert(Long id, UserInfo user) {
        TransportCert cert = new TransportCert();
        cert.setId(id);
        cert.setUpdatetime(new Date());
        cert.setUpdateby(user.getId());
        cert.setUpdatebyname(user.getUsername());
        cert.setValidflag(false);
        this.updateById(cert);
 
        TransportCert transport = this.getById(cert);
        //删除产品
        transportCertProductService.deleteProductByCode(transport.getCode());
        //删除人员
        transportCertificatePersonService.deleteByCertificateCode(transport.getCode());
        //删除文件
        transportCertFileService.deleteByCode(transport.getCode());
    }
 
    @Override
    public int countByCode(String code) {
        if (StringUtils.isBlank(code)) {
            throw new BusinessException("运输证编号不能为空");
        }
        LambdaQueryWrapper<TransportCert> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TransportCert::getValidflag, true)
                .eq(TransportCert::getCode, code);
        return transportCertMapper.selectCount(queryWrapper);
    }
 
    @Override
    public List<TransportCert> selectWarnList(Date start, Date end) {
        return transportCertMapper.selectWarnList(start,end);
    }
 
 
}