郑永安
2023-06-19 59e91a4e9ddaf23cebb12993c774aa899ab22d16
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
package com.gk.firework.Service.ServiceImpl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
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.LicenseStatus;
import com.gk.firework.Domain.Enum.ProvinceCode;
import com.gk.firework.Domain.Exception.BusinessException;
import com.gk.firework.Domain.Utils.BeanUtils;
import com.gk.firework.Domain.Utils.PageInfo;
import com.gk.firework.Domain.Utils.StringUtils;
import com.gk.firework.Domain.Vo.DealingRangeVo;
import com.gk.firework.Domain.Vo.LicenseVo;
import com.gk.firework.Mapper.LicenseInfoMapper;
import com.gk.firework.Service.*;
import com.spire.doc.interfaces.IField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
 
/**
 * @author : jingjy
 * @date : 2021/5/11 16:43
 */
@Service("LicenseService")
public class LicenseServiceImpl extends ServiceImpl<LicenseInfoMapper, LicenseInfo> implements LicenseService {
    @Autowired
    private LicenseInfoMapper licenseInfoMapper;
    @Autowired
    private LicenseStorageService licenseStorageService;
    @Autowired
    private UserService userService;
    @Autowired
    private EnterpriseLicenseService enterpriseLicenseService;
    @Autowired
    private EnterpriseService enterpriseService;
 
    @Override
    public void selectDataGrid(PageInfo pageInfo) {
        Page<LicenseInfo> page = new Page<>(pageInfo.getPageIndex(), pageInfo.getPageSize());
 
        List<OrderItem> orderItems = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtils.isNotBlank(pageInfo.getSort()) && StringUtils.isNotBlank(pageInfo.getOrder())) {
            orderItem.setAsc(pageInfo.getOrder().equalsIgnoreCase("ascending"));
            orderItem.setColumn(pageInfo.getSort());
        } else {
            orderItem.setAsc(false);
            orderItem.setColumn("createdat");
        }
        orderItems.add(orderItem);
        page.setOrders(orderItems);
        if (StringUtils.isBlank(pageInfo.getSort())) {
            pageInfo.setSort("createdat");
        }
        if (StringUtils.isBlank(pageInfo.getOrder())) {
            pageInfo.setOrder("desc");
        }
        List<LicenseVo> licenseVoList = licenseInfoMapper.selectLicenseDataGrid(pageInfo.getCondition(), page);
 
        List<LicenseVo> licenseVos = new ArrayList<>();
        for (LicenseVo licenseVo : licenseVoList) {
            List<LicenseStorage> licenseStorageList = licenseStorageService.getListByLicenseCode(licenseVo.getLicensecode());
            String licenseStorage = JSON.toJSONString(licenseStorageList);
            licenseVo.setLicenseStorage(licenseStorage);
            StringBuilder dealingRangeStr = dealDealingRange(licenseVo.getDealingrange());
            licenseVo.setDealingRangeStr(dealingRangeStr.toString());
            licenseVos.add(licenseVo);
        }
 
        pageInfo.setResult(licenseVos);
        pageInfo.setTotalCount(page.getTotal());
    }
 
    @Override
    public StringBuilder dealDealingRange(String dealingRange) {
        //处理dealingRange,供前台展示
        StringBuilder dealingRangeStr = new StringBuilder();
        JSONArray jsonArray = JSONArray.parseArray(dealingRange);
        List<DealingRangeVo> dealingRangeVos = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            int k = i + 1;
            JSONObject object = jsonArray.getJSONObject(i);
            Boolean checked = object.getBoolean("checked" + k);
            if (checked == null){
                return dealingRangeStr;
            }
            List<String> checkMore = new ArrayList<>();
            if (object.getJSONArray("checkMore"+k) != null){
                checkMore = object.getJSONArray("checkMore" + k).toJavaList(String.class);
            }
            DealingRangeVo dealingRangeVo = new DealingRangeVo();
            dealingRangeVo.setSn(k);
            dealingRangeVo.setChecked(checked);
            dealingRangeVo.setCheckMore(checkMore);
            switch (k) {
                case 1:
                    dealingRangeVo.setName("爆竹类");
                    break;
                case 2:
                    dealingRangeVo.setName("喷花类");
                    break;
                case 3:
                    dealingRangeVo.setName("旋转类");
                    break;
                case 4:
                    dealingRangeVo.setName("吐珠类");
                    break;
                case 5:
                    dealingRangeVo.setName("玩具类");
                    break;
                case 6:
                    dealingRangeVo.setName("组合烟花类");
                    break;
                default:
            }
            dealingRangeVos.add(dealingRangeVo);
 
        }
        for (DealingRangeVo dealingRangeVo : dealingRangeVos) {
            if (dealingRangeVo.getChecked()){
                dealingRangeStr.append(dealingRangeVo.getName());
                dealingRangeStr.append("(");
                if (dealingRangeVo.getCheckMore().size() == 1) {
                    dealingRangeStr.append(dealingRangeVo.getCheckMore().get(0).trim(), 0, 1);
                } else {
                    for (int i = 0; i < dealingRangeVo.getCheckMore().size(); i++) {
                        dealingRangeStr.append(dealingRangeVo.getCheckMore().get(i).trim(), 0, 1);
                        if (i != dealingRangeVo.getCheckMore().size() - 1) {
                            dealingRangeStr.append("、");
                        }
                    }
                }
                dealingRangeStr.append(")级、");
 
            }
 
        }
        if (dealingRangeStr.length() >= 1){
            dealingRangeStr.deleteCharAt(dealingRangeStr.length() - 1);
        }
        return dealingRangeStr;
    }
 
    @Override
    @Transactional
    public void rejectLicense(JSONObject data, UserInfo userInfo) {
 
//        UserInfo user = userService.getById(userInfo.getId());
//        assert  user != null;
        Long id = data.getLong("id");
        String rejectnote = data.getString("rejectnote");
        if (id == null) {
            throw new BusinessException("参数错误,请联系管理员");
        }
        LicenseInfo license = this.getLicenseById(id);
        if (!license.getReviewstatus().equals((byte) 2) && !license.getReviewstatus().equals((byte) 1)) {
            throw new BusinessException("只有等待审核和已审核的记录可以驳回");
        }
        if (StringUtils.isBlank(rejectnote)) {
            throw new BusinessException("驳回意见不能为空");
        }
        LicenseInfo info = new LicenseInfo();
        info.setId(id);
        info.setReviewstatus((byte) 3);
        info.setReviewnote(rejectnote);
        this.updateById(info);
        //删除企业信息的许可证
        enterpriseLicenseService.deleteByLicenseNumber(license.getLicensecode());
    }
 
 
    /**
    * @Description: 证书分析
    * @date 2021/7/1 15:24
    */
    @Override
    public IPage selectLicenseStatistic(Page<Map> page, Map filter, UserInfo user) {
 
        Map<String, Object> params = new HashMap<>();
 
        UserInfo userInfo = userService.getById(user.getId());
 
        //可见度
//        params.put("enterprisenumber", userInfo.getCompanynumber());
        //该菜单只有监管部门能看
        params.put("city", userInfo.getCity());
        params.put("filterCity", filter.get("city"));
        params.put("starttime", filter.get("starttime"));
        params.put("endtime", filter.get("endtime"));
        List<Map> result = licenseInfoMapper.selectLicenseStatisticPage(page, params);
        return page.setRecords(result);
    }
 
    @Override
    public List<LicenseVo> exportLicense(Map<String, Object> condition) {
        List<LicenseVo> licenseVoList = licenseInfoMapper.selectLicenseDataGrid(condition);
 
 
        List<LicenseVo> licenseVos = new ArrayList<>();
        for (LicenseVo licenseVo : licenseVoList) {
            List<LicenseStorage> licenseStorageList = licenseStorageService.getListByLicenseCode(licenseVo.getLicensecode());
            String licenseStorage = JSON.toJSONString(licenseStorageList);
            licenseVo.setLicenseStorage(licenseStorage);
            StringBuilder dealingRangeStr = dealDealingRange(licenseVo.getDealingrange());
            licenseVo.setDealingRangeStr(dealingRangeStr.toString());
            UserInfo one = userService.getOne(new LambdaQueryWrapper<UserInfo>()
                    .eq(UserInfo::getCompany, licenseVo.getIssuingunit()));
            if (one != null)
                licenseVo.setIssuingcode(one.getCode());
            licenseVos.add(licenseVo);
        }
        return licenseVos;
    }
 
    @Override
    @Transactional
    public void delayLicense(Long id, String issuingunit, Date issuingdate, Date validstarttime, Date validendtime,UserInfo userInfo) {
        if (id == null) throw new BusinessException("传参错误");
        LicenseInfo licenseInfo = this.getLicenseById(id);
        if (licenseInfo == null) throw new BusinessException("证书不存在");
        if (licenseInfo.getReviewstatus() != (byte)2) throw new BusinessException("只能操作审批通过的证书");
        if (StringUtils.isBlank(issuingunit)) throw new BusinessException("发证机关不能为空");
        if (validendtime == null || validstarttime == null) throw new BusinessException("有效期不能为空");
        if (issuingdate == null) throw new BusinessException("发证日期不能为空");
        if (validendtime.before(new Date())) throw new BusinessException("无意义日期");
        List<LicenseStorage> licenseStorages = licenseStorageService.getListByLicenseCode(licenseInfo.getLicensecode());
        //1.注销原本的证书
        licenseInfo.setValidstatus((byte) -1);
        this.updateById(licenseInfo);
        //2.创建新的证书
        licenseInfo.setValidstatus((byte) 1);
        this.generateLicenseDetail(licenseInfo);
        licenseInfo.setId(null);
        //证书状态:延期
        licenseInfo.setLicensestatus(LicenseStatus.POSTP);
        licenseInfo.setCreatedat(new Date());
        licenseInfo.setCreatedby(userInfo.getUsername());
        licenseInfo.setModifiedat(new Date());
        licenseInfo.setModifiedby(userInfo.getUsername());
        licenseInfo.setEffectdate(validstarttime);
        licenseInfo.setNoeffectdate(validendtime);
        licenseInfo.setIssuingdate(issuingdate);
        licenseInfo.setIssuingunit(issuingunit);
        this.save(licenseInfo);
        //3.复制新的storage表
        if (licenseStorages.size() > 0) {
            for (LicenseStorage licenseStorage : licenseStorages) {
                licenseStorage.setId(null);
                licenseStorage.setLicensecode(licenseInfo.getLicensecode());
                licenseStorageService.save(licenseStorage);
            }
        }
 
        Enterprise enterprise = enterpriseService.selectEnterpriseByName(licenseInfo.getName());
        enterprise.setValidstarttime(licenseInfo.getEffectdate());
        enterprise.setValidendtime(licenseInfo.getNoeffectdate());
        enterpriseService.updateById(enterprise);
        EnterpriseLicense enterpriseLicense = new EnterpriseLicense();
        if (licenseInfo.getType().equals((byte)2)){
            enterpriseLicense.setLicensename("烟花爆竹经营(批发)许可证");
        }else if (licenseInfo.getType().equals( (byte)3)){
            enterpriseLicense.setLicensename("烟花爆竹经营(零售)许可证");
        }
        enterpriseLicense.setLicensenumber(licenseInfo.getLicensecode());
        enterpriseLicense.setEnterpriseid(enterprise.getId());
        enterpriseLicense.setAuthority(licenseInfo.getIssuingunit());
        enterpriseLicense.setRanges(this.dealDealingRange(licenseInfo.getDealingrange()).toString());
        enterpriseLicense.setValidendtime(licenseInfo.getNoeffectdate());
        enterpriseLicense.setValidstarttime(licenseInfo.getEffectdate());
        enterpriseLicense.setValidflag(true);
        enterpriseLicense.setCreatetime(new Date());
        enterpriseLicense.setCreateby(userInfo.getId());
        enterpriseLicense.setCreatebyname(userInfo.getUsername());
        enterpriseLicenseService.save(enterpriseLicense);
        //issale = 1 修改 截止有效期
        List<UserInfo> userList = userService.selectByCompanyId(enterprise.getId(),0);
        if (userList.size() != 2) {
            throw new BusinessException("发生错误,请联系管理员");
        }
        for (UserInfo info : userList) {
            //修改许可证 同时修改issale=1的用户有效期
            if (info.getIssale() == (byte) 1) {
                Calendar instance = Calendar.getInstance();
                instance.setTime(validendtime);
                instance.set(Calendar.HOUR_OF_DAY, 23);
                instance.set(Calendar.MINUTE, 59);
                instance.set(Calendar.SECOND, 59);
                info.setExpiredate(instance.getTime());
                userService.updateById(info);
            }
        }
    }
 
    @Override
    public void generateLicenseDetail(LicenseInfo licenseInfo) {
        LicenseInfo licenseInfo1 = this.getLastOne();
        Integer sn = licenseInfo1.getSn();
        sn++;
        licenseInfo.setSn(sn);
        String abbr = "";
        String typeCode = "";
        for (ProvinceCode code : ProvinceCode.values()) {
            if (licenseInfo.getProvince().equals(code.getName())) {
                abbr = code.getAbbr();
            }
        }
        if (licenseInfo.getType().equals((byte)2)) {
            typeCode = "PF";
        } else if (licenseInfo.getType().equals((byte)3)) {
            typeCode = "LS";
        }
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
 
        String licenseCode = "(" + abbr + ")" + typeCode + "〔" + year + "〕" + String.format("%05d", sn);
        licenseInfo.setLicensecode(licenseCode);
    }
 
    @Override
    @Transactional
    public void modLicense(LicenseVo licenseVo, List<LicenseStorage> licenseStorageList, UserInfo user) {
        //1.普通判断
        if (licenseVo.getId() == null) throw new BusinessException("传参错误");
        LicenseInfo older = this.getLicenseById(licenseVo.getId());
        if (older == null) throw new BusinessException("证书不存在");
        if (StringUtils.isBlank(licenseVo.getIssuingunit())) throw new BusinessException("发证机关不能为空");
        if (licenseVo.getEffectdate() ==null|| licenseVo.getNoeffectdate() == null) throw new BusinessException("开始结束时间不能为空");
        if (licenseVo.getIssuingdate()== null) throw new BusinessException("发证日期不能为空");
        //2.注销旧版
        older.setValidstatus((byte) -1);
        this.updateById(older);
        //3.新建新版
        licenseVo.setLicensestatus(LicenseStatus.MOD);
        licenseVo.setValidstatus((byte) 1);
        licenseVo.setCreatedat(new Date());
        licenseVo.setCreatedby(user.getUsername());
        licenseVo.setModifiedat(new Date());
        licenseVo.setModifiedby(user.getUsername());
        licenseVo.setId(null);
        this.save(licenseVo);
        licenseStorageService.saveBatch(licenseStorageList);
 
        Enterprise enterprise = enterpriseService.selectEnterpriseByName(licenseVo.getName());
        enterprise.setValidstarttime(licenseVo.getEffectdate());
        enterprise.setValidendtime(licenseVo.getNoeffectdate());
        enterpriseService.updateById(enterprise);
        EnterpriseLicense enterpriseLicense = new EnterpriseLicense();
        if (licenseVo.getType().equals((byte)2)){
            enterpriseLicense.setLicensename("烟花爆竹经营(批发)许可证");
        }else if (licenseVo.getType().equals( (byte)3)){
            enterpriseLicense.setLicensename("烟花爆竹经营(零售)许可证");
        }
        enterpriseLicense.setLicensenumber(licenseVo.getLicensecode());
        enterpriseLicense.setEnterpriseid(enterprise.getId());
        enterpriseLicense.setAuthority(licenseVo.getIssuingunit());
        enterpriseLicense.setRanges(this.dealDealingRange(licenseVo.getDealingrange()).toString());
        enterpriseLicense.setValidendtime(licenseVo.getNoeffectdate());
        enterpriseLicense.setValidstarttime(licenseVo.getEffectdate());
        enterpriseLicense.setValidflag(true);
        enterpriseLicense.setCreatetime(new Date());
        enterpriseLicense.setCreateby(user.getId());
        enterpriseLicense.setCreatebyname(user.getUsername());
        enterpriseLicenseService.save(enterpriseLicense);
        //issale = 1 修改 截止有效期
        List<UserInfo> userList = userService.selectByCompanyId(enterprise.getId(),0);
        if (userList.size() != 2) {
            throw new BusinessException("发生错误,请联系管理员");
        }
        for (UserInfo info : userList) {
            //修改许可证 同时修改issale=1的用户有效期
            if (info.getIssale() == (byte) 1) {
                Calendar instance = Calendar.getInstance();
                instance.setTime(licenseVo.getNoeffectdate());
                instance.set(Calendar.HOUR_OF_DAY, 23);
                instance.set(Calendar.MINUTE, 59);
                instance.set(Calendar.SECOND, 59);
                info.setExpiredate(instance.getTime());
                userService.updateById(info);
            }
        }
 
    }
 
    @Override
    public LicenseInfo getLastOne() {
        LambdaQueryWrapper<LicenseInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(LicenseInfo::getSn);
        return licenseInfoMapper.selectOne(wrapper);
    }
 
    @Override
    public LicenseInfo getLicenseById(Long id) {
        LambdaQueryWrapper<LicenseInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LicenseInfo::getId, id);
        wrapper.eq(LicenseInfo::getFlag, 0);
        return licenseInfoMapper.selectOne(wrapper);
    }
}