郑永安
2023-06-19 2fcd97552d16718cc7997629fd637a73a5a4483f
src/main/java/com/gk/firework/Service/ServiceImpl/LicenseServiceImpl.java
对比新文件
@@ -0,0 +1,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);
    }
}