package com.ruoyi.system.service.impl; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.write.metadata.WriteSheet; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; 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.google.common.collect.Lists; import com.ruoyi.common.basic.PageDTO; import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.core.domain.model.LoginUser; import com.ruoyi.common.enums.CalculateTypeEnum; import com.ruoyi.common.enums.ReportingStatusEnum; import com.ruoyi.common.enums.ShowStatusEnum; import com.ruoyi.common.enums.UserTypeEnum; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.BeanUtils; import com.ruoyi.common.utils.CalculateUtil; import com.ruoyi.common.utils.CollUtils; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.EasyExcelUtil; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.system.domain.TbBasicData; import com.ruoyi.system.domain.TbBasicDataConfig; import com.ruoyi.system.domain.TbBasicDataConfigDetail; import com.ruoyi.system.domain.TbBasicDataField; import com.ruoyi.system.domain.TbDept; import com.ruoyi.system.domain.TbField; import com.ruoyi.system.domain.TbScore; import com.ruoyi.system.dto.BasicDataDTO; import com.ruoyi.system.handler.CustomCellWriteHandler; import com.ruoyi.system.listener.BasicDataListener; import com.ruoyi.system.listener.HistoryDataListener; import com.ruoyi.system.mapper.TbBasicDataMapper; import com.ruoyi.system.mapper.TbDeptMapper; import com.ruoyi.system.query.CurrentFieldsQuery; import com.ruoyi.system.query.ScoreCalculateQuery; import com.ruoyi.system.query.ScoreQuery; import com.ruoyi.system.service.ISysUserService; import com.ruoyi.system.service.TbBasicDataConfigDetailService; import com.ruoyi.system.service.TbBasicDataConfigService; import com.ruoyi.system.service.TbBasicDataFieldService; import com.ruoyi.system.service.TbBasicDataService; import com.ruoyi.system.service.TbFieldService; import com.ruoyi.system.service.TbScoreService; import com.ruoyi.system.utils.FieldBuildUtil; import com.ruoyi.system.vo.BasicDataReportingVO; import com.ruoyi.system.vo.CurrentFieldsAllVO; import com.ruoyi.system.vo.CurrentFieldsDetailVO; import com.ruoyi.system.vo.CurrentFieldsVO; import com.ruoyi.system.vo.FieldsTreeVO; import com.ruoyi.system.vo.ScoreCalculateVO; import com.ruoyi.system.vo.ScoreVO; import java.io.IOException; import java.net.URLEncoder; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; import javax.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; /** * 基础数据表 服务实现类 * * @author mitao * @since 2024-03-13 */ @Slf4j @Service @RequiredArgsConstructor public class TbBasicDataServiceImpl extends ServiceImpl implements TbBasicDataService { private final TbDeptMapper tbDeptMapper; private final HttpServletResponse response; private final TbFieldService tbFieldService; private final TbBasicDataFieldService tbBasicDataFieldService; private final TbBasicDataConfigService tbBasicDataConfigService; private final TbBasicDataConfigDetailService tbBasicDataConfigDetailService; private final TbScoreService tbScoreService; private final ISysUserService sysUserService; public static void setFieldValues( List fields, Map fieldMap) { for (FieldsTreeVO field : fields) { TbBasicDataField tbBasicDataField = fieldMap.get(field.getId()); if (tbBasicDataField != null) { field.setValue( FieldBuildUtil.formatNumberWithCommas(tbBasicDataField.getFieldValue())); } if (field.getChildren() != null && !field.getChildren().isEmpty()) { setFieldValues(field.getChildren(), fieldMap); } } } @Override public R getBasicFields() throws Exception { SysUser user = SecurityUtils.getLoginUser().getUser(); String deptAreaCode = user.getAreaCode(); BasicDataReportingVO vo = new BasicDataReportingVO(); // 校验区划代码 TbDept dept = tbDeptMapper.selectOne( Wrappers.lambdaQuery().eq(TbDept::getAreaCode, deptAreaCode)); if (Objects.isNull(dept)) { throw new ServiceException(String.format("区划代码%s不存在", deptAreaCode)); } Date date = new Date(); Map quarterDate = DateUtils.getQuarterDate(date); // 当前季度开始 Date quarterStart = quarterDate.get("first"); // 当前季度结束 Date quarterEnd = quarterDate.get("last"); // 判断当前时间是否在季度初1-15号 Instant instant = quarterStart.toInstant(); LocalDate quarterStartLocalDate = instant.atZone(ZoneId.systemDefault()).toLocalDate(); LocalDate fifteenDaysLimit = quarterStartLocalDate.plusDays(15); LocalDate now = LocalDate.now(); vo.setQuarter(DateUtils.getNowQuarter()); vo.setStatus(ReportingStatusEnum.UNFILLED); // 如果当前时间不在规定范围内:季度初1-15号 if (now.isBefore(quarterStartLocalDate) || now.isAfter(fifteenDaysLimit)) { return R.ok(vo, "请于季度初1-15号上传季度数据。"); } // 查询是否有当前季度的填报记录 TbBasicData basicData = this.getOne( Wrappers.lambdaQuery() .eq(TbBasicData::getDeptAreaCode, dept.getAreaCode()) .between(TbBasicData::getCreateTime, quarterStart, quarterEnd)); // 查询需要填写的字段 List list = tbFieldService.lambdaQuery().eq(TbField::getStatus, ShowStatusEnum.SHOW).list(); List roots = new ArrayList<>(); FieldsTreeVO fieldsTreeVO = new FieldsTreeVO(); fieldsTreeVO.setId(-1L); fieldsTreeVO.setName("转移支付规模"); fieldsTreeVO.setCategory(Boolean.FALSE); FieldsTreeVO fieldsTreeVO2 = new FieldsTreeVO(); fieldsTreeVO2.setId(-2L); fieldsTreeVO2.setName("当期GDP"); fieldsTreeVO2.setCategory(Boolean.FALSE); roots.add(fieldsTreeVO); roots.add(fieldsTreeVO2); if (CollUtils.isNotEmpty(list)) { FieldBuildUtil.buildTreeFromTbFieldList(list, roots); FieldsTreeVO remark = new FieldsTreeVO(); remark.setId(-3L); remark.setName("备注"); roots.add(remark); vo.setFields(roots); } if (Objects.isNull(basicData)) { vo.setQuarter(DateUtils.getNowQuarter()); vo.setStatus(ReportingStatusEnum.UNFILLED); return R.ok(vo); } else { vo.setStatus(basicData.getStatus()); // 查询已填报数据 包含数据缺失和已填报 List basicDataFields = tbBasicDataFieldService .lambdaQuery() .eq(TbBasicDataField::getBasicDataId, basicData.getId()) .list(); if (CollUtils.isNotEmpty(basicDataFields)) { Map fieldMap = basicDataFields.stream() .collect(Collectors.toMap(TbBasicDataField::getFieldId, Function.identity())); setFieldValues(vo.getFields(), fieldMap); return R.ok(vo); } } return R.ok(vo); } @Override @Transactional(rollbackFor = Exception.class) public void saveBasicData(BasicDataDTO dto) throws Exception { LoginUser loginUser = SecurityUtils.getLoginUser(); String areaCode = loginUser.getUser().getAreaCode(); // 数据校验 if (Objects.isNull(dto) || CollUtils.isEmpty(dto.getFields())) { return; } String nowQuarter = DateUtils.getNowQuarter(); TbBasicData tbBasicData = BeanUtils.copyBean(dto, TbBasicData.class); tbBasicData.setDeptAreaCode(areaCode); tbBasicData.setStatus(ReportingStatusEnum.MISSING_DATA); tbBasicData.setQuarter(nowQuarter); tbBasicData.setReportingTime(DateUtils.getQuarterDate(nowQuarter)); this.saveOrUpdate(tbBasicData); // 保存基础数据动态字段数据 List tbBasicDataFields = BeanUtils.copyList(dto.getFields(), TbBasicDataField.class); // 查询需要填写的动态字段 List fieldList = tbFieldService.lambdaQuery().eq(TbField::getStatus, ShowStatusEnum.SHOW).list(); Map fieldMap = fieldList.stream().collect(Collectors.toMap(TbField::getId, e -> e)); tbBasicDataFields.forEach( item -> { item.setBasicDataId(tbBasicData.getId()); TbField tbField = fieldMap.get(item.getFieldId()); if (Objects.nonNull(tbField)) { item.setFieldName(tbField.getFieldName()); } }); // 添加固定字段 转移支付规模、当期GDP TbBasicDataField transferPaymentScale = new TbBasicDataField(); transferPaymentScale.setBasicDataId(tbBasicData.getId()); transferPaymentScale.setFieldId(-1L); transferPaymentScale.setFieldValue(tbBasicData.getTransferPaymentScale()); tbBasicDataFields.add(transferPaymentScale); TbBasicDataField currentGdp = new TbBasicDataField(); currentGdp.setBasicDataId(tbBasicData.getId()); currentGdp.setFieldId(-2L); currentGdp.setFieldValue(tbBasicData.getCurrentGdp()); tbBasicDataFields.add(currentGdp); TbBasicDataField remark = new TbBasicDataField(); remark.setBasicDataId(tbBasicData.getId()); remark.setFieldId(-3L); remark.setFieldValue(tbBasicData.getRemark()); tbBasicDataFields.add(remark); // 将该基础数据的动态字段数据全部删除 tbBasicDataFieldService.remove( Wrappers.lambdaQuery() .eq(TbBasicDataField::getBasicDataId, tbBasicData.getId())); tbBasicDataFieldService.saveBatch(tbBasicDataFields); // 需要填写的动态字段 Set fieldIds = fieldList.stream().map(TbField::getId).collect(Collectors.toSet()); // 用户填写的动态字段 Set deptFieldIds = tbBasicDataFields.stream().map(TbBasicDataField::getFieldId) .collect(Collectors.toSet()); boolean flag = deptFieldIds.containsAll(fieldIds) && (StringUtils.isNotEmpty(dto.getTransferPaymentScale()) && StringUtils.isNotEmpty(dto.getCurrentGdp())); if (flag) { tbBasicData.setStatus(ReportingStatusEnum.FILLED); this.updateById(tbBasicData); } if (ReportingStatusEnum.FILLED.equals(tbBasicData.getStatus())) { calculateScore(tbBasicData); } } @Override @Transactional(rollbackFor = Exception.class) public void importBasicData(MultipartFile file) throws Exception { LoginUser loginUser = SecurityUtils.getLoginUser(); String areaCode = loginUser.getUser().getAreaCode(); EasyExcel.read( file.getInputStream(), new BasicDataListener( this, tbFieldService, areaCode, tbBasicDataFieldService, tbBasicDataConfigService, tbBasicDataConfigDetailService, tbScoreService)) .sheet() .doRead(); } private void calculateScore(TbBasicData tbBasicData) { // 计算得分 List list = tbBasicDataConfigService .lambdaQuery() .eq(TbBasicDataConfig::getStatus, ShowStatusEnum.SHOW) .list(); if (CollectionUtils.isEmpty(list)) { throw new ServiceException("计算得分失败,平台未配置得分计算规则"); } List numCalculates = list.stream() .filter(item -> CalculateTypeEnum.NUMBER.equals(item.getCalculateType())) .collect(Collectors.toList()); List textAndPercentages = list.stream() .filter(item -> !CalculateTypeEnum.NUMBER.equals(item.getCalculateType())) .collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(numCalculates)) { numCalculates.forEach( item -> { Map valueMap = new HashMap<>(); String numberCalculateFormula = item.getNumberCalculateFormula(); Map fieldsAndValue = CalculateUtil.getFieldsAndValue(numberCalculateFormula); for (Map.Entry stringIntegerEntry : fieldsAndValue.entrySet()) { Optional tbBasicDataField = tbBasicDataFieldService .lambdaQuery() .eq(TbBasicDataField::getBasicDataId, tbBasicData.getId()) .eq(TbBasicDataField::getFieldId, stringIntegerEntry.getValue()) .oneOpt(); tbBasicDataField.ifPresent( basicDataField -> valueMap.put(stringIntegerEntry.getKey(), basicDataField.getFieldValue())); } double score = CalculateUtil.calculate(numberCalculateFormula, valueMap); TbScore tbScore = new TbScore(); tbScore.setBasicDataId(tbBasicData.getId()); tbScore.setScore(score); tbScore.setBasicDataConfigId(item.getId()); tbScoreService.save(tbScore); }); } if (CollectionUtils.isNotEmpty(textAndPercentages)) { for (TbBasicDataConfig textAndPercentage : textAndPercentages) { TbScore tbScore = new TbScore(); List details = tbBasicDataConfigDetailService .lambdaQuery() .eq(TbBasicDataConfigDetail::getBasicDataConfigId, textAndPercentage.getId()) .list(); Map scoreMap = details.stream() .collect( Collectors.toMap( TbBasicDataConfigDetail::getKey, TbBasicDataConfigDetail::getValue)); if (CollectionUtils.isNotEmpty(details)) { Optional tbBasicDataFieldOptional = tbBasicDataFieldService .lambdaQuery() .eq(TbBasicDataField::getBasicDataId, tbBasicData.getId()) .eq(TbBasicDataField::getFieldId, textAndPercentage.getFieldIdStr()) .oneOpt(); if (tbBasicDataFieldOptional.isPresent()) { TbBasicDataField tbBasicDataField = tbBasicDataFieldOptional.get(); if (CalculateTypeEnum.TEXT.equals(textAndPercentage.getCalculateType())) { String score = scoreMap.get(tbBasicDataField.getFieldValue()); tbScore.setBasicDataId(tbBasicData.getId()); tbScore.setScore(Double.parseDouble(score)); tbScore.setBasicDataConfigId(textAndPercentage.getId()); tbScoreService.save(tbScore); } if (CalculateTypeEnum.PERCENTAGE.equals( textAndPercentage.getCalculateType())) { for (Map.Entry stringStringEntry : scoreMap.entrySet()) { String[] split = stringStringEntry.getKey().split("-"); double v = Double.parseDouble(tbBasicDataField.getFieldValue()); double min = Double.parseDouble(split[0]); double max = Double.parseDouble(split[1]); if (v >= min && v <= max) { tbScore.setScore( Double.parseDouble(stringStringEntry.getValue())); } } tbScore.setBasicDataId(tbBasicData.getId()); tbScore.setBasicDataConfigId(textAndPercentage.getId()); tbScoreService.save(tbScore); } } } } } } @Override public PageDTO pageScore(ScoreQuery query) { Page page = new Page<>(query.getPageNum(), query.getPageSize()); SysUser user = SecurityUtils.getLoginUser().getUser(); String areaCode = user.getAreaCode(); Map quarterDate = DateUtils.getQuarterDate(new Date()); // 当前季度开始 Date quarterStart = quarterDate.get("first"); // 当前季度结束 Date quarterEnd = quarterDate.get("last"); // 查询是否有当前季度的填报记录 TbBasicData basicData = this.getOne( Wrappers.lambdaQuery() .eq(TbBasicData::getDeptAreaCode, areaCode) .between(TbBasicData::getCreateTime, quarterStart, quarterEnd)); if (Objects.isNull(basicData)) { return PageDTO.empty(page); } query.setBasicDataId(basicData.getId()); // 查询对应的基础数据配置 Page pageVO = tbScoreService.pageScore(query, page); return PageDTO.of(pageVO); } @Override public R fieldsDetails(Long id) { // 从数据库中获取基本数据 TbBasicData basicData = this.getById(id); if (Objects.isNull(basicData)) { throw new ServiceException("非法参数"); } CurrentFieldsDetailVO vo = BeanUtils.copyBean(basicData, CurrentFieldsDetailVO.class); // 查询用户信息 sysUserService .lambdaQuery() .eq(SysUser::getAreaCode, basicData.getDeptAreaCode()) .eq(SysUser::getUserType, UserTypeEnum.DEPARTMENT.getCode()) .oneOpt() .ifPresent( item -> { vo.setAreaName(item.getAreaName()); vo.setPhoneNumber(item.getPhoneNumber()); vo.setPersonInCharge(item.getPersonInCharge()); }); // 查询动态字段 List basicDataFields = tbBasicDataFieldService .lambdaQuery() .eq(TbBasicDataField::getBasicDataId, basicData.getId()) .list(); // 获取所有字段ID Set fieldIds = basicDataFields.stream().map(TbBasicDataField::getFieldId) .collect(Collectors.toSet()); // 根据字段ID查询字段信息并构建字段ID到字段对象的映射 Map fieldMap = tbFieldService .lambdaQuery() .in(!fieldIds.isEmpty(), TbField::getId, fieldIds) .list() .stream() .collect(Collectors.toMap(TbField::getId, e -> e)); // 根节点 List root = new ArrayList<>(); basicDataFields.stream() .filter(item -> item.getFieldId() == -1) .findFirst() .ifPresent( item -> { FieldsTreeVO fieldsTreeVO = new FieldsTreeVO(); fieldsTreeVO.setId(item.getFieldId()); fieldsTreeVO.setName("转移支付规模"); fieldsTreeVO.setValue(item.getFieldValue()); fieldsTreeVO.setCategory(Boolean.FALSE); root.add(fieldsTreeVO); }); basicDataFields.stream() .filter(item -> item.getFieldId() == -2) .findFirst() .ifPresent( item -> { FieldsTreeVO fieldsTreeVO = new FieldsTreeVO(); fieldsTreeVO.setId(item.getFieldId()); fieldsTreeVO.setName("当期GDP"); fieldsTreeVO.setValue(item.getFieldValue()); fieldsTreeVO.setCategory(Boolean.FALSE); root.add(fieldsTreeVO); }); FieldBuildUtil.buildTreeStructure(basicDataFields, fieldMap, root); basicDataFields.stream() .filter(item -> item.getFieldId() == -3) .findFirst() .ifPresent( item -> { FieldsTreeVO fieldsTreeVO = new FieldsTreeVO(); fieldsTreeVO.setId(item.getFieldId()); fieldsTreeVO.setName("备注"); fieldsTreeVO.setValue(item.getFieldValue()); fieldsTreeVO.setCategory(Boolean.FALSE); root.add(fieldsTreeVO); }); vo.setFields(root); return R.ok(vo); } @Override public R> fieldsStatics(CurrentFieldsQuery dto) { Page page = new Page<>(dto.getPageNum(), dto.getPageSize()); return R.ok(PageDTO.of(baseMapper.fieldsStatics(page, dto))); } @Override public CurrentFieldsAllVO fieldsStaticsAll() throws Exception { CurrentFieldsAllVO vo = new CurrentFieldsAllVO(); List roots = new ArrayList<>(); // 获取表头 FieldsTreeVO area = new FieldsTreeVO(); area.setCategory(Boolean.FALSE); area.setName("地区"); FieldsTreeVO quarter = new FieldsTreeVO(); quarter.setCategory(Boolean.FALSE); quarter.setName("填报季度"); FieldsTreeVO transferPaymentScale = new FieldsTreeVO(); transferPaymentScale.setName("转移支付规模"); FieldsTreeVO currentGdp = new FieldsTreeVO(); currentGdp.setCategory(Boolean.FALSE); currentGdp.setName("当期GDP"); roots.add(area); roots.add(quarter); roots.add(transferPaymentScale); roots.add(currentGdp); // 查询当前季度填写字段 List fieldList = tbFieldService.lambdaQuery().eq(TbField::getStatus, ShowStatusEnum.SHOW).list(); FieldBuildUtil.buildTreeFromTbFieldList(fieldList, roots); FieldsTreeVO remark = new FieldsTreeVO(); remark.setCategory(Boolean.FALSE); remark.setName("备注"); roots.add(remark); vo.setFields(roots); String nowQuarter = DateUtils.getNowQuarter(); // 查询上报的基础数据 List basicDataList = this.lambdaQuery() .eq(TbBasicData::getQuarter, nowQuarter) .eq(TbBasicData::getStatus, ReportingStatusEnum.FILLED) .list(); if (CollUtils.isNotEmpty(basicDataList)) { Set areaCodeList = basicDataList.stream().map(TbBasicData::getDeptAreaCode) .collect(Collectors.toSet()); Map userMap = sysUserService.lambdaQuery().in(SysUser::getAreaCode, areaCodeList).list() .stream() .collect(Collectors.toMap(SysUser::getAreaCode, e -> e)); Set basicDataIds = basicDataList.stream().map(TbBasicData::getId).collect(Collectors.toSet()); Map basicDataFieldMap = tbBasicDataFieldService .lambdaQuery() .in(TbBasicDataField::getBasicDataId, basicDataIds) .list() .stream() .collect(Collectors.toMap(TbBasicDataField::getFieldId, e -> e)); // 值 List result = new ArrayList<>(); for (TbBasicData tbBasicData : basicDataList) { SysUser user = userMap.get(tbBasicData.getDeptAreaCode()); List item = new ArrayList<>(); item.add(Objects.nonNull(user) ? user.getAreaName() : ""); item.add(tbBasicData.getQuarter()); item.add(tbBasicData.getTransferPaymentScale()); item.add(tbBasicData.getCurrentGdp()); for (TbField tbField : fieldList) { TbBasicDataField tbBasicDataField = basicDataFieldMap.get(tbField.getId()); item.add( Objects.nonNull(tbBasicDataField) ? FieldBuildUtil.formatNumberWithCommas( tbBasicDataField.getFieldValue()) : ""); } item.add(tbBasicData.getRemark()); result.add(item); } vo.setValue(result); } return vo; } @Override public R> historyFieldsStatics(CurrentFieldsQuery dto) { Page page = new Page<>(dto.getPageNum(), dto.getPageSize()); // 当前所在季度 return R.ok(PageDTO.of(baseMapper.fieldsStatics(page, dto))); } @Override public PageDTO scoreCalculatePage(ScoreCalculateQuery query) { Page page = new Page<>(query.getPageNum(), query.getPageSize()); Page pageVO = tbBasicDataConfigService.scoreCalculatePage(query, page); return PageDTO.of(pageVO); } @Override public CurrentFieldsAllVO fieldsStaticsAll(String quarterStr) { CurrentFieldsAllVO vo = new CurrentFieldsAllVO(); List roots = new ArrayList<>(); // 获取表头 FieldsTreeVO area = new FieldsTreeVO(); area.setCategory(Boolean.FALSE); area.setName("地区"); FieldsTreeVO quarter = new FieldsTreeVO(); quarter.setCategory(Boolean.FALSE); quarter.setName("填报季度"); FieldsTreeVO transferPaymentScale = new FieldsTreeVO(); transferPaymentScale.setName("转移支付规模"); FieldsTreeVO currentGdp = new FieldsTreeVO(); currentGdp.setCategory(Boolean.FALSE); currentGdp.setName("当期GDP"); roots.add(area); roots.add(quarter); roots.add(transferPaymentScale); roots.add(currentGdp); List basicDataList = this.lambdaQuery() .eq(TbBasicData::getQuarter, quarterStr) .eq(TbBasicData::getStatus, ReportingStatusEnum.FILLED) .list(); if (CollUtils.isEmpty(basicDataList)) { throw new ServiceException("非法参数"); } Long basicDataId = basicDataList.get(0).getId(); Set fieldIds = tbBasicDataFieldService .lambdaQuery() .eq(TbBasicDataField::getBasicDataId, basicDataId) .ne(TbBasicDataField::getFieldId, -1L) .ne(TbBasicDataField::getFieldId, -2L) .list() .stream() .map(TbBasicDataField::getFieldId) .collect(Collectors.toSet()); List fieldList = tbFieldService.lambdaQuery().in(TbField::getId, fieldIds).list(); if (CollUtils.isEmpty(fieldList)) { FieldsTreeVO remark = new FieldsTreeVO(); remark.setCategory(Boolean.FALSE); remark.setName("备注"); roots.add(remark); vo.setFields(roots); return vo; } FieldBuildUtil.buildTreeFromTbFieldList(fieldList, roots); FieldsTreeVO remark = new FieldsTreeVO(); remark.setCategory(Boolean.FALSE); remark.setName("备注"); roots.add(remark); vo.setFields(roots); Set areaCodeList = basicDataList.stream().map(TbBasicData::getDeptAreaCode) .collect(Collectors.toSet()); Map userMap = sysUserService.lambdaQuery().in(SysUser::getAreaCode, areaCodeList).list().stream() .collect(Collectors.toMap(SysUser::getAreaCode, e -> e)); Set basicDataIds = basicDataList.stream().map(TbBasicData::getId).collect(Collectors.toSet()); List basicDataFieldList = tbBasicDataFieldService .lambdaQuery() .in(TbBasicDataField::getBasicDataId, basicDataIds) .list(); // 值 List result = new ArrayList<>(); for (TbBasicData tbBasicData : basicDataList) { Map basicDataFieldMap = basicDataFieldList.stream() .filter(item -> tbBasicData.getId().equals(item.getBasicDataId())) .collect(Collectors.toMap(TbBasicDataField::getFieldId, e -> e)); SysUser user = userMap.get(tbBasicData.getDeptAreaCode()); List item = new ArrayList<>(); item.add(Objects.nonNull(user) ? user.getAreaName() : ""); item.add(tbBasicData.getQuarter()); item.add(tbBasicData.getTransferPaymentScale()); item.add(tbBasicData.getCurrentGdp()); for (TbField tbField : fieldList) { TbBasicDataField tbBasicDataField = basicDataFieldMap.get(tbField.getId()); item.add( Objects.nonNull(tbBasicDataField) ? FieldBuildUtil.formatNumberWithCommas( tbBasicDataField.getFieldValue()) : ""); } item.add(tbBasicData.getRemark()); result.add(item); } vo.setValue(result); return vo; } @Override @Transactional(rollbackFor = Exception.class) public void importData(MultipartFile file, String quarter) throws IOException { //校验季度 Date quarterDate = DateUtils.getQuarterDate(quarter); Map quarterMap = DateUtils.getQuarterDate(new Date()); Date date = quarterMap.get("first"); LocalDate quarterLocalDate = DateUtils.dateToLocalDate(quarterDate); LocalDate dateLocalDate = DateUtils.dateToLocalDate(date); if (quarterLocalDate.isAfter(dateLocalDate) || quarterLocalDate.equals(dateLocalDate)) { throw new ServiceException("请导入过去季度的数据。"); } EasyExcel.read( file.getInputStream(), new HistoryDataListener( this, tbFieldService, tbBasicDataFieldService, tbBasicDataConfigService, tbBasicDataConfigDetailService, tbScoreService, sysUserService, quarter)) .sheet() .doRead(); } @Override public void exportData(List quarterList) throws Exception { response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); String fileName = "地方财政运行及“三保”情况统计表"; response.setHeader( "Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx"); //季度数据,用于反查字段 List tbBasicDataList = this.lambdaQuery() .in(TbBasicData::getQuarter, quarterList) .eq(TbBasicData::getStatus, ReportingStatusEnum.FILLED) .groupBy(TbBasicData::getQuarter).list(); //符合导出条件的基础数据 List tbBasicDataListAll = this.lambdaQuery() .in(TbBasicData::getQuarter, quarterList) .eq(TbBasicData::getStatus, ReportingStatusEnum.FILLED) .list(); //基础数据id列表 Set basicDataIdList = tbBasicDataListAll.stream().map(TbBasicData::getId) .collect(Collectors.toSet()); //涉及到的部门编码 Set userAreaCodeList = tbBasicDataListAll.stream().map(TbBasicData::getDeptAreaCode) .collect(Collectors.toSet()); //用户信息map Map userMap = sysUserService.lambdaQuery() .in(SysUser::getAreaCode, userAreaCodeList).list().stream() .collect(Collectors.toMap(SysUser::getAreaCode, e -> e)); //基础数据对应的字段值列表 List tbBasicDataFieldList = tbBasicDataFieldService.lambdaQuery() .in(TbBasicDataField::getBasicDataId, basicDataIdList) .list(); //构建Excel写对象 try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()) .registerWriteHandler(new CustomCellWriteHandler()) .registerWriteHandler(EasyExcelUtil.getStyleStrategy()).build()) { WriteSheet writeSheet; //遍历季度基础数据 for (TbBasicData tbBasicData : tbBasicDataList) { //该季度的动态字段id列表 Set fieldIdList = tbBasicDataFieldList.stream() .filter(item -> tbBasicData.getId().equals(item.getBasicDataId())) .map(TbBasicDataField::getFieldId) .collect(Collectors.toSet()); //字段列表 List fieldList = tbFieldService.lambdaQuery() .in(TbField::getId, fieldIdList) .list(); //表头 List> head = head(fieldList); //过滤基础数据为遍历元素的季度 List list = tbBasicDataListAll.stream() .filter(item -> tbBasicData.getQuarter().equals(item.getQuarter())) .collect(Collectors.toList()); List> dataList = dataList(fieldList, list, tbBasicDataFieldList, userMap, head); // 构建sheet对象 writeSheet = EasyExcel.writerSheet(tbBasicData.getQuarter()).head(head).build(); // 写出sheet数据 excelWriter.write(dataList, writeSheet); } } } private List> dataList(List fieldList, List list, List tbBasicDataFieldList, Map userMap, List> head) { //所有数据集合 List> excellist = new ArrayList<>(); //构建栏号行 List columnNo = Lists.newArrayList("栏号"); for (int i = 1; i < head.size(); i++) { columnNo.add(String.valueOf(i)); } excellist.add(columnNo); //动态字段数据行 for (TbBasicData tbBasicData : list) { //转换为map,方便遍历的时候取 Map basicDataFieldMap = tbBasicDataFieldList.stream() .filter(item -> tbBasicData.getId().equals(item.getBasicDataId())) .collect(Collectors.toMap(TbBasicDataField::getFieldId, e -> e)); SysUser sysUser = userMap.get(tbBasicData.getDeptAreaCode()); if (CollUtils.isEmpty(basicDataFieldMap) || Objects.isNull(sysUser)) { continue; } List valueList = Lists.newArrayList(sysUser.getAreaName(), tbBasicData.getQuarter()); TbBasicDataField transferPaymentScale = basicDataFieldMap.get(-1L); valueList.add( Objects.nonNull(transferPaymentScale) ? transferPaymentScale.getFieldValue() : ""); TbBasicDataField currentGdp = basicDataFieldMap.get(-2L); valueList.add( Objects.nonNull(currentGdp) ? currentGdp.getFieldValue() : ""); for (TbField tbField : fieldList) { TbBasicDataField tbBasicDataField = basicDataFieldMap.get(tbField.getId()); valueList.add( Objects.nonNull(tbBasicDataField) ? tbBasicDataField.getFieldValue() : ""); } excellist.add(valueList); valueList.add(tbBasicData.getRemark()); } return excellist; } private List> head(List list) { List> headTitles = Lists.newArrayList(); // 固定字段 headTitles.add(Lists.newArrayList("地区")); headTitles.add(Lists.newArrayList("填报季度")); headTitles.add(Lists.newArrayList("转移支付规模")); headTitles.add(Lists.newArrayList("当期GDP")); list.forEach( item -> { String levelOneCategory = item.getLevelOneCategory(); String levelTwoCategory = item.getLevelTwoCategory(); String levelThreeCategory = item.getLevelThreeCategory(); String fieldName = item.getFieldName(); headTitles.add( Lists.newArrayList( levelOneCategory, StringUtils.isBlank(levelTwoCategory) ? fieldName : levelTwoCategory, StringUtils.isBlank(levelThreeCategory) ? fieldName : levelThreeCategory, fieldName)); }); headTitles.add(Lists.newArrayList("备注")); return headTitles; } }