package com.ningdatech.carapi.report.manage;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.basic.function.VUtils;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.basic.util.NdDateUtils;
import com.ningdatech.basic.util.StrPool;
import com.ningdatech.carapi.car.vehicle.contants.VehicleContant;
import com.ningdatech.carapi.common.constant.UserRoleTypeEnum;
import com.ningdatech.carapi.common.contants.DefValConstants;
import com.ningdatech.carapi.report.constant.DataUploadAuditStatusEnum;
import com.ningdatech.carapi.report.constant.DataUploadAuditTypeEnum;
import com.ningdatech.carapi.report.constant.UploadDataTypeEnum;
import com.ningdatech.carapi.report.model.entity.*;
import com.ningdatech.carapi.report.model.po.ReqDataUploadListPO;
import com.ningdatech.carapi.report.model.po.ReqMortarSupplyDataUploadPO;
import com.ningdatech.carapi.report.model.vo.ResDataUploadListVO;
import com.ningdatech.carapi.report.model.vo.ResMortarSupplyDataUploadDetailVO;
import com.ningdatech.carapi.report.model.vo.ResMortarSupplyRelatedDataVO;
import com.ningdatech.carapi.report.service.*;
import com.ningdatech.carapi.sys.entity.dto.CompanyDTO;
import com.ningdatech.carapi.sys.entity.dto.RegionDTO;
import com.ningdatech.carapi.sys.helper.CompaniesCacheHelper;
import com.ningdatech.carapi.sys.helper.RegionsCacheHelper;
import com.ningdatech.carapi.user.security.auth.model.UserInfoDetails;
import com.ningdatech.carapi.user.util.LoginUserUtil;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author CMM
 * @since 2023/09/28 11:15
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class MortarSupplyBulletinManage {

    private final IBulkCementDirectReportSystemAuditRecordService auditRecordService;
    private final IBulkCementDirectReportSystemAuditInfoService auditInfoService;
    private final RegionsCacheHelper regionsCacheHelper;
    private final IBulkCementDirectReportSystemDataRelevanceService dataRelevanceService;
    private final IMortarSupplyBulletinService mortarSupplyBulletinService;
    private final IBulkCementDirectReportSystemUniversalIndexService universalIndexService;
    private final CompaniesCacheHelper companiesCacheHelper;

    public PageVo<ResDataUploadListVO> dataUploadList(ReqDataUploadListPO req) {

        UserInfoDetails userDetail = LoginUserUtil.loginUserDetail();
        Long regionId = userDetail.getRegionId();
        Page<MortarSupplyBulletin> page = req.page();
        // 根据所属区域查出对应的散装水泥供应量数据上报记录
        LambdaQueryWrapper<MortarSupplyBulletin> wrapper = Wrappers.lambdaQuery(MortarSupplyBulletin.class);
        // 获取入参
        LocalDateTime reportStartMonth = req.getReportStartMonth();
        LocalDateTime reportEndMonth = req.getReportEndMonth();
        Integer auditStatus = req.getAuditStatus();
        wrapper.ge(Objects.nonNull(reportStartMonth), MortarSupplyBulletin::getReportMonth, reportStartMonth)
                .le(Objects.nonNull(reportEndMonth), MortarSupplyBulletin::getReportMonth, reportEndMonth)
                .eq(Objects.nonNull(auditStatus),MortarSupplyBulletin::getAuditStatus,auditStatus)
                .eq(MortarSupplyBulletin::getRegionId,regionId)
                .orderByDesc(MortarSupplyBulletin::getReportMonth);
                //.isNotNull(MortarSupplyBulletin::getAuditInfoId);
        mortarSupplyBulletinService.page(page,wrapper);
        List<MortarSupplyBulletin> records = page.getRecords();
        if (CollUtil.isEmpty(records)){
            return PageVo.empty();
        }
        // 获取审核信息
        List<Long> auditInfoIdList = records.stream().map(MortarSupplyBulletin::getAuditInfoId).collect(Collectors.toList());
        Map<Long, BulkCementDirectReportSystemAuditInfo> auditInfoMap = auditInfoService.list(Wrappers.lambdaQuery(BulkCementDirectReportSystemAuditInfo.class).in(BulkCementDirectReportSystemAuditInfo::getId, auditInfoIdList))
                .stream().collect(Collectors.toMap(BulkCementDirectReportSystemAuditInfo::getId, v -> v));
        List<ResDataUploadListVO> resVos = records.stream().map(r -> {
            ResDataUploadListVO vo = new ResDataUploadListVO();
            LocalDateTime month = r.getReportMonth();
            String reportMonthStr = NdDateUtils.format(month, NdDateUtils.DEFAULT_MONTH_FORMAT);
            LocalDateTime reportMonth = LocalDateTimeUtil.parse(reportMonthStr, DateTimeFormatter.ofPattern(DatePattern.NORM_MONTH_PATTERN));
            vo.setRegionId(r.getRegionId());
            vo.setReportMonth(reportMonth);
            vo.setAuditDataId(r.getId());
            BulkCementDirectReportSystemAuditInfo auditInfo = auditInfoMap.get(r.getAuditInfoId());
            if (Objects.nonNull(auditInfo)) {
                vo.setAuditOpinion(auditInfo.getAuditOpinion());
            }
            vo.setAuditStatus(r.getAuditStatus());
            String desc = DataUploadAuditStatusEnum.getDescByCode(r.getAuditStatus());
            vo.setAuditStatusName(desc);
            vo.setCreateOn(r.getCreateOn());
            return vo;
        }).collect(Collectors.toList());
        return PageVo.of(resVos, page.getTotal());
    }

    @Transactional(rollbackFor = Exception.class)
    public String mortarSupplyDataUpload(ReqMortarSupplyDataUploadPO req) {
        // 获取登录用户信息
        UserInfoDetails userDetail = LoginUserUtil.loginUserDetail();
        LocalDateTime reportMonth = req.getReportMonth();
        Long regionId = userDetail.getRegionId();

        // 如果登录用户为区县散办
        if (UserRoleTypeEnum.REGION_ADMIN_ROLE_ID.getId().equals(userDetail.getRoleId())) {

            VUtils.isTrue(Objects.isNull(reportMonth)).throwMessage("请选择上报月份!");

            // 提交市级审核申请
            BulkCementDirectReportSystemAuditInfo cityAuditInfo = new BulkCementDirectReportSystemAuditInfo();
            assemblyAuditInfo(cityAuditInfo, DataUploadAuditStatusEnum.UNDER_AUDIT, userDetail);
            cityAuditInfo.setAuditType(DataUploadAuditTypeEnum.CITY_AUDIT.getCode());
            auditInfoService.save(cityAuditInfo);

            // 保存上报数据信息
            MortarSupplyBulletin mortarSupplyBulletin = new MortarSupplyBulletin();
            // 区县的区域ID
            Long countryRegionId = userDetail.getRegionId();
            // 获取所在市的区域ID
            RegionDTO regionDto = regionsCacheHelper.getByRegionId(countryRegionId);
            Long parentId = regionDto.getParentId();
            // 判断上报月份所在市的关联数据不存在
            RegionDTO cityRegionDto = regionsCacheHelper.getByRegionId(parentId);
            BulkCementDirectReportSystemDataRelevance cityData = dataRelevanceService.getOne(Wrappers.lambdaQuery(BulkCementDirectReportSystemDataRelevance.class)
                    .eq(BulkCementDirectReportSystemDataRelevance::getRegionId, parentId)
                    .eq(BulkCementDirectReportSystemDataRelevance::getAuditType,DataUploadAuditTypeEnum.CITY_AUDIT.getCode())
                    .eq(BulkCementDirectReportSystemDataRelevance::getReportMonth, reportMonth)
                    .eq(BulkCementDirectReportSystemDataRelevance::getAuditDataType, UploadDataTypeEnum.MORTAR_SUPPLY_BULLETIN.getCode()));
            // 判断当前地区当前上报月份数据是否已经存在,如果已经存在，删除原数据并更新市、省关联数据列表
            checkUploadData(reportMonth, regionId, cityData);
            Long dataId = req.getId();
            // 上报数据不为空，说明为重新提交
            if (Objects.nonNull(dataId)) {
                MortarSupplyBulletin oldData = mortarSupplyBulletinService.getById(dataId);
                VUtils.isTrue(Objects.isNull(oldData)).throwMessage("重新提交数据不存在!");
                // 更新市级关联数据
                updateCityData(cityData,oldData);
                // 删除被退回的数据
                mortarSupplyBulletinService.removeById(oldData);
            }

            assemblyData(req, userDetail, mortarSupplyBulletin);
            mortarSupplyBulletin.setAuditInfoId(cityAuditInfo.getId());
            mortarSupplyBulletin.setAuditStatus(DataUploadAuditStatusEnum.UNDER_AUDIT.getCode());
            mortarSupplyBulletinService.saveOrUpdate(mortarSupplyBulletin);

            // 补充数据审核表信息
            BulkCementDirectReportSystemAuditRecord auditRecord = new BulkCementDirectReportSystemAuditRecord();
            assemblyAuditRecord(userDetail, mortarSupplyBulletin.getId(), cityAuditInfo.getId(), auditRecord);
            auditRecord.setReportMonth(reportMonth);
            auditRecord.setAuditType(DataUploadAuditTypeEnum.CITY_AUDIT.getCode());
            auditRecordService.save(auditRecord);

            // 补充所在市的关联数据信息
            if (Objects.isNull(cityData)) {
                BulkCementDirectReportSystemDataRelevance cityRelevanceData = new BulkCementDirectReportSystemDataRelevance();
                assemblyRelevanceData(userDetail, reportMonth, cityRelevanceData);
                cityRelevanceData.setRegionId(parentId);
                // 获取所在市散装中心所属的管理企业ID
                CompanyDTO companyDto = companiesCacheHelper.getByName(cityRegionDto.getRegionName() + VehicleContant.ADMIN_COMPANY_NAME);
                if (Objects.nonNull(companyDto)) {
                    cityRelevanceData.setCompanyId(companyDto.getId());
                }
                cityRelevanceData.setAuditType(DataUploadAuditTypeEnum.CITY_AUDIT.getCode());
                cityRelevanceData.setAuditDataType(UploadDataTypeEnum.MORTAR_SUPPLY_BULLETIN.getCode());
                // 有新上报的数据，就更新关联数据状态为未上报
                cityRelevanceData.setAuditStatus(DataUploadAuditStatusEnum.NOT_REPORT.getCode());
                dataRelevanceService.saveOrUpdate(cityRelevanceData);
            }
        }
        // 如果登录用户是市级散办,更新审核状态(未上报、被退回)为审核中，并提交省级审核信息和审核记录
        else if (UserRoleTypeEnum.BULK_CEMENT_ADMIN_ROLE_ID.getId().equals(userDetail.getRoleId())) {

            Long relevanceDataId = req.getId();
            VUtils.isTrue(Objects.isNull(relevanceDataId)).throwMessage("请传入关联数据ID!");

            // 提交省级审核申请
            BulkCementDirectReportSystemAuditInfo provinceAuditInfo = new BulkCementDirectReportSystemAuditInfo();
            assemblyAuditInfo(provinceAuditInfo, DataUploadAuditStatusEnum.UNDER_AUDIT, userDetail);
            provinceAuditInfo.setAuditType(DataUploadAuditTypeEnum.PROVINCE_AUDIT.getCode());
            auditInfoService.save(provinceAuditInfo);
            // 更新市级关联数据审核状态为审核中
            BulkCementDirectReportSystemDataRelevance cityRelevanceData = dataRelevanceService.getById(relevanceDataId);
            VUtils.isTrue(Objects.isNull(cityRelevanceData)).throwMessage("数据不存在!");
            // 关联审核信息ID
            cityRelevanceData.setUnitResponsiblePerson(req.getUnitResponsiblePerson());
            cityRelevanceData.setPreparer(req.getPreparer());
            cityRelevanceData.setReviewer(req.getReviewer());
            cityRelevanceData.setSubmissionDate(req.getSubmissionDate());
            cityRelevanceData.setAuditInfoId(provinceAuditInfo.getId());
            cityRelevanceData.setAuditStatus(DataUploadAuditStatusEnum.UNDER_AUDIT.getCode());
            cityRelevanceData.setUpdateOn(LocalDateTime.now());
            cityRelevanceData.setUpdateBy(userDetail.getUserId());
            cityRelevanceData.setUnitResponsiblePerson(req.getUnitResponsiblePerson());
            cityRelevanceData.setPreparer(req.getPreparer());
            cityRelevanceData.setReviewer(req.getReviewer());
            cityRelevanceData.setSubmissionDate(req.getSubmissionDate());
            dataRelevanceService.updateById(cityRelevanceData);

            // 判断上报月份省关联数据是否存在
            BulkCementDirectReportSystemDataRelevance provinceData = dataRelevanceService.getOne(Wrappers.lambdaQuery(BulkCementDirectReportSystemDataRelevance.class)
                    .eq(BulkCementDirectReportSystemDataRelevance::getRegionId, DefValConstants.ZJREGION_ID)
                    .eq(BulkCementDirectReportSystemDataRelevance::getAuditType,DataUploadAuditTypeEnum.PROVINCE_AUDIT.getCode())
                    .eq(BulkCementDirectReportSystemDataRelevance::getReportMonth, reportMonth)
                    .eq(BulkCementDirectReportSystemDataRelevance::getAuditDataType, UploadDataTypeEnum.MORTAR_SUPPLY_BULLETIN.getCode()));

            // 如果上报月份省级关联数据不存在
            // 补充省散办关联数据信息
            if (Objects.isNull(provinceData)) {
                BulkCementDirectReportSystemDataRelevance provinceRelevanceData = new BulkCementDirectReportSystemDataRelevance();
                assemblyRelevanceData(userDetail, reportMonth, provinceRelevanceData);
                provinceRelevanceData.setAuditInfoId(provinceAuditInfo.getId());
                provinceRelevanceData.setRegionId(DefValConstants.ZJREGION_ID);
                provinceRelevanceData.setAuditType(DataUploadAuditTypeEnum.PROVINCE_AUDIT.getCode());
                // 获取省散装中心所属的管理企业ID
                CompanyDTO companyDto = companiesCacheHelper.getByName(DefValConstants.PROVINCE + VehicleContant.ADMIN_COMPANY_NAME);
                if (Objects.nonNull(companyDto)) {
                    provinceRelevanceData.setCompanyId(companyDto.getId());
                }
                provinceRelevanceData.setAuditDataType(UploadDataTypeEnum.MORTAR_SUPPLY_BULLETIN.getCode());
                provinceRelevanceData.setAuditStatus(DataUploadAuditStatusEnum.UNDER_AUDIT.getCode());
                dataRelevanceService.save(provinceRelevanceData);
            }

            // 创建省级审核记录
            BulkCementDirectReportSystemAuditRecord provinceAuditRecord = new BulkCementDirectReportSystemAuditRecord();
            assemblyAuditRecord(userDetail, cityRelevanceData.getId(), provinceAuditInfo.getId(), provinceAuditRecord);
            provinceAuditRecord.setReportMonth(cityRelevanceData.getReportMonth());
            provinceAuditRecord.setAuditType(DataUploadAuditTypeEnum.PROVINCE_AUDIT.getCode());
            auditRecordService.save(provinceAuditRecord);
        }

        return "上报成功!";
    }

    private void assemblyRelevanceData(UserInfoDetails userDetail, LocalDateTime reportMonth, BulkCementDirectReportSystemDataRelevance relevanceData) {
        relevanceData.setReportMonth(reportMonth);
        relevanceData.setCreateOn(LocalDateTime.now());
        relevanceData.setCreateBy(userDetail.getUserId());
        relevanceData.setUpdateOn(LocalDateTime.now());
        relevanceData.setUpdateBy(userDetail.getUserId());
        relevanceData.setLastUpdateTime(LocalDateTime.now());
    }
    private void assemblyAuditRecord(UserInfoDetails userDetail, Long auditDataId, Long auditInfoId, BulkCementDirectReportSystemAuditRecord auditRecord) {
        auditRecord.setAuditDataId(auditDataId);
        auditRecord.setAuditInfoId(auditInfoId);
        auditRecord.setAuditStatus(DataUploadAuditStatusEnum.UNDER_AUDIT.getCode());
        auditRecord.setAuditDataType(UploadDataTypeEnum.MORTAR_SUPPLY_BULLETIN.getCode());
        auditRecord.setCompanyId(userDetail.getCompanyId());
        auditRecord.setRegionId(userDetail.getRegionId());
        auditRecord.setCreateOn(LocalDateTime.now());
        auditRecord.setCreateBy(userDetail.getUserId());
        auditRecord.setUpdateOn(LocalDateTime.now());
        auditRecord.setUpdateBy(userDetail.getUserId());
    }

    private void assemblyAuditInfo(BulkCementDirectReportSystemAuditInfo auditInfo, DataUploadAuditStatusEnum underAudit, UserInfoDetails userDetail) {
        auditInfo.setAuditStatus(underAudit.getCode());
        auditInfo.setAuditDataType(UploadDataTypeEnum.MORTAR_SUPPLY_BULLETIN.getCode());
        auditInfo.setCreateOn(LocalDateTime.now());
        auditInfo.setCreateBy(userDetail.getUserId());
        auditInfo.setUpdateOn(LocalDateTime.now());
        auditInfo.setUpdateBy(userDetail.getUserId());
    }

    private void assemblyData(ReqMortarSupplyDataUploadPO req, UserInfoDetails userDetail, MortarSupplyBulletin mortarSupplyBulletin) {
        RegionDTO regionDto = regionsCacheHelper.getByRegionId(userDetail.getRegionId());
        String regionName = regionDto.getRegionName();
        // 单位名称改成了地区
        mortarSupplyBulletin.setRegionName(regionName);
        mortarSupplyBulletin.setTwoCategoryGuidGoal(req.getTwoCategoryGuidGoal());
        mortarSupplyBulletin.setDryMixedMortarProductionCompanyNum(req.getDryMixedMortarProductionCompanyNum());
        mortarSupplyBulletin.setDryMixedMortarProductiveCapacity(req.getDryMixedMortarProductiveCapacity());
        mortarSupplyBulletin.setDryMixedMortarBulkCapacity(req.getDryMixedMortarBulkCapacity());

        mortarSupplyBulletin.setThisMonthTotalOutput(req.getThisMonthTotalOutput());
        mortarSupplyBulletin.setThisMonthAccumulativeOutput(req.getThisMonthAccumulativeOutput());
        mortarSupplyBulletin.setDryMixedMortarThisMonthOutput(req.getDryMixedMortarThisMonthOutput());
        mortarSupplyBulletin.setDryMixedMortarThisMonthAccumulativeOutput(req.getDryMixedMortarThisMonthAccumulativeOutput());
        mortarSupplyBulletin.setDryMixedMortarIncrement(req.getDryMixedMortarIncrement());
        mortarSupplyBulletin.setDryMixedMortarGrowthRate(req.getDryMixedMortarGrowthRate());
        mortarSupplyBulletin.setDryMixedMortarThisMonthBulkQuantity(req.getDryMixedMortarThisMonthBulkQuantity());
        mortarSupplyBulletin.setDryMixedMortarAccumulativeBulkQuantity(req.getDryMixedMortarAccumulativeBulkQuantity());
        mortarSupplyBulletin.setWetMortarThisMonthOutput(req.getWetMortarThisMonthOutput());
        mortarSupplyBulletin.setWetMortarAccumulativeOutput(req.getWetMortarAccumulativeOutput());
        mortarSupplyBulletin.setBulkCementThisMonthUsage(req.getBulkCementThisMonthUsage());
        mortarSupplyBulletin.setBulkCementAccumulativeUsage(req.getBulkCementAccumulativeUsage());
        mortarSupplyBulletin.setWasteThisMonthComprehensiveUtilization(req.getWasteThisMonthComprehensiveUtilization());
        mortarSupplyBulletin.setWasteAccumulativeComprehensiveUtilization(req.getWasteAccumulativeComprehensiveUtilization());
        mortarSupplyBulletin.setReportMonth(req.getReportMonth());
        mortarSupplyBulletin.setCompanyId(userDetail.getCompanyId());
        mortarSupplyBulletin.setUnitResponsiblePerson(req.getUnitResponsiblePerson());
        mortarSupplyBulletin.setPreparer(req.getPreparer());
        mortarSupplyBulletin.setReviewer(req.getReviewer());
        mortarSupplyBulletin.setSubmissionDate(req.getSubmissionDate());
        mortarSupplyBulletin.setRegionId(userDetail.getRegionId());
        mortarSupplyBulletin.setCreateOn(LocalDateTime.now());
        mortarSupplyBulletin.setCreateBy(userDetail.getUserId());
        mortarSupplyBulletin.setUpdateOn(LocalDateTime.now());
        mortarSupplyBulletin.setUpdateBy(userDetail.getUserId());
    }

    private void checkUploadData(LocalDateTime reportMonth, Long regionId, BulkCementDirectReportSystemDataRelevance cityData) {
        MortarSupplyBulletin mortarSupplyBulletin = mortarSupplyBulletinService.getOne(Wrappers.lambdaQuery(MortarSupplyBulletin.class)
                .eq(MortarSupplyBulletin::getRegionId, regionId)
                .eq(MortarSupplyBulletin::getReportMonth, reportMonth));
        // 判断审核状态，如果审核通过，更新审核通过的数据
        if (Objects.nonNull(mortarSupplyBulletin) &&
                DataUploadAuditStatusEnum.AUDIT_PASS.getCode().equals(mortarSupplyBulletin.getAuditStatus())){
            updateCityData(cityData,mortarSupplyBulletin);
            mortarSupplyBulletinService.removeById(mortarSupplyBulletin);
        }
    }

    private void updateCityData(BulkCementDirectReportSystemDataRelevance cityData, MortarSupplyBulletin mortarSupplyBulletin) {
        if (Objects.nonNull(cityData)) {
            String auditDataIdList = cityData.getAuditDataIdList();
            if (StringUtils.isNotBlank(auditDataIdList)){
                List<Long> idList = Arrays.stream(auditDataIdList.split(StrPool.COMMA)).map(Long::valueOf).collect(Collectors.toList());
                // 将已经存在的该地区该上报月份的数据数据从关联表中删除
                idList.remove(mortarSupplyBulletin.getId());
            }
        }
    }

    public ResMortarSupplyDataUploadDetailVO getMortarSupplyUploadData(ReqMortarSupplyDataUploadPO req) {

        // 获取登录用户信息
        UserInfoDetails userDetail = LoginUserUtil.loginUserDetail();
        // 上月填报的数据
        BigDecimal twoCategoryGuidGoal = req.getTwoCategoryGuidGoal();
        Integer dryMixedMortarProductionCompanyNum = req.getDryMixedMortarProductionCompanyNum();
        BigDecimal dryMixedMortarProductiveCapacity = req.getDryMixedMortarProductiveCapacity();
        BigDecimal dryMixedMortarBulkCapacity = req.getDryMixedMortarBulkCapacity();

        // 普通干混砂浆本月产量
        BigDecimal dryMixedMortarThisMonthOutput = req.getDryMixedMortarThisMonthOutput();
        // 普通干混砂浆本月散装量
        BigDecimal dryMixedMortarThisMonthBulkQuantity = req.getDryMixedMortarThisMonthBulkQuantity();
        // 湿拌砂浆本月产量
        BigDecimal wetMortarThisMonthOutput = req.getWetMortarThisMonthOutput();
        // 散装水泥本月使用量
        BigDecimal bulkCementThisMonthUsage = req.getBulkCementThisMonthUsage();
        // 废弃物本月综合利用量
        BigDecimal wasteThisMonthComprehensiveUtilization = req.getWasteThisMonthComprehensiveUtilization();

        LocalDateTime reportMonth = req.getReportMonth();
        Long regionId = userDetail.getRegionId();

        // 计算实际产量、使用散装水泥量、废弃物综合利用量
        // 获取上个月该地区的砂浆供应量数据
        LocalDateTime preReportMonth = reportMonth.minusMonths(1L);
        MortarSupplyBulletin preMortarSupplyBulletin = mortarSupplyBulletinService.getOne(Wrappers.lambdaQuery(MortarSupplyBulletin.class)
                .eq(MortarSupplyBulletin::getRegionId, regionId)
                .eq(MortarSupplyBulletin::getReportMonth, preReportMonth));
        VUtils.isTrue(Objects.isNull(preMortarSupplyBulletin)).throwMessage("系统上个月数据不存在!");
        // 获取去年当前上报月份该地区的散装水泥供应量数据
        LocalDateTime preYearReportMonth = preReportMonth.minusYears(1L).plusMonths(1L);
        MortarSupplyBulletin preYearMortarSupplyBulletin = mortarSupplyBulletinService.getOne(Wrappers.lambdaQuery(MortarSupplyBulletin.class)
                .eq(MortarSupplyBulletin::getRegionId, regionId)
                .eq(MortarSupplyBulletin::getReportMonth, preYearReportMonth));
        VUtils.isTrue(Objects.isNull(preYearMortarSupplyBulletin)).throwMessage("系统去年该月数据不存在!");

        ResMortarSupplyDataUploadDetailVO vo = new ResMortarSupplyDataUploadDetailVO();
        vo.setTwoCategoryGuidGoal(twoCategoryGuidGoal);
        vo.setDryMixedMortarProductionCompanyNum(dryMixedMortarProductionCompanyNum);
        vo.setDryMixedMortarProductiveCapacity(dryMixedMortarProductiveCapacity);
        vo.setDryMixedMortarBulkCapacity(dryMixedMortarBulkCapacity);

        if (Objects.nonNull(dryMixedMortarThisMonthOutput) && Objects.nonNull(wetMortarThisMonthOutput)){
            // 本月总产量：本月普通干混砂浆产量 + 本月湿拌砂浆产量
            BigDecimal thisMonthTotalOutput = dryMixedMortarThisMonthOutput.add(wetMortarThisMonthOutput);
            vo.setThisMonthTotalOutput(thisMonthTotalOutput);
            // 本月累计总产量：本月总产量 + 上个月的累计总产量
            BigDecimal preThisMonthAccumulativeOutput = preMortarSupplyBulletin.getThisMonthAccumulativeOutput();
            BigDecimal thisMonthAccumulativeOutput = thisMonthTotalOutput.add(preThisMonthAccumulativeOutput);
            vo.setThisMonthAccumulativeOutput(thisMonthAccumulativeOutput);
        }

        if (Objects.nonNull(dryMixedMortarThisMonthOutput)) {
            // 普通干混砂浆累计产量：普通干混砂浆本月产量 + 上个月普通干混砂浆累计产量
            BigDecimal preMonthDryMixedMortarThisMonthAccumulativeOutput = preMortarSupplyBulletin.getDryMixedMortarThisMonthAccumulativeOutput();
            BigDecimal preMonthDryMixedMortarThisMonthAccOutput = Objects.isNull(preMonthDryMixedMortarThisMonthAccumulativeOutput) ? BigDecimal.ZERO : preMonthDryMixedMortarThisMonthAccumulativeOutput;
            BigDecimal dryMixedMortarThisMonthAccumulativeOutput = dryMixedMortarThisMonthOutput.add(preMonthDryMixedMortarThisMonthAccOutput);
            vo.setDryMixedMortarThisMonthAccumulativeOutput(dryMixedMortarThisMonthAccumulativeOutput);

            // 普通干混砂浆同比增加量: 普通干混砂浆累计产量 - 去年普通干混砂浆累计产量
            BigDecimal preYearDryMixedMortarThisMonthAccumulativeOutput = preYearMortarSupplyBulletin.getDryMixedMortarThisMonthAccumulativeOutput();
            BigDecimal preYearDryMixedMortarThisMonthAccOutput = Objects.isNull(preYearDryMixedMortarThisMonthAccumulativeOutput) ? BigDecimal.ZERO : preYearDryMixedMortarThisMonthAccumulativeOutput;
            BigDecimal dryMixedMortarIncrement = dryMixedMortarThisMonthAccumulativeOutput.subtract(preYearDryMixedMortarThisMonthAccOutput);
            vo.setDryMixedMortarIncrement(dryMixedMortarIncrement);
            // 普通干混砂浆增长率：普通干混砂浆同比增加量 / 去年普通干混砂浆累计产量
            BigDecimal dryMixedMortarGrowthRate = dryMixedMortarIncrement.divide(preYearDryMixedMortarThisMonthAccumulativeOutput, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100L));
            vo.setDryMixedMortarGrowthRate(dryMixedMortarGrowthRate);
        }

        if (Objects.nonNull(dryMixedMortarThisMonthBulkQuantity)) {
            // 普通干混砂浆累计散装量：普通干混砂浆本月散装量 + 上个月普通干混砂浆累计散装量
            BigDecimal preDryMixedMortarAccumulativeBulkQuantity = preMortarSupplyBulletin.getDryMixedMortarAccumulativeBulkQuantity();
            BigDecimal dryMixedMortarAccumulativeBulkQuantity = dryMixedMortarThisMonthBulkQuantity.add(preDryMixedMortarAccumulativeBulkQuantity);
            vo.setDryMixedMortarAccumulativeBulkQuantity(dryMixedMortarAccumulativeBulkQuantity);
        }

        if (Objects.nonNull(wetMortarThisMonthOutput)) {
            // 湿拌砂浆累计产量：湿拌砂浆本月产量 + 上个月湿拌砂浆累计产量
            BigDecimal preMonthWetMortarAccumulativeOutput = preMortarSupplyBulletin.getWetMortarAccumulativeOutput();
            BigDecimal preMonthWetMortarAccOutput = Objects.isNull(preMonthWetMortarAccumulativeOutput) ? BigDecimal.ZERO : preMonthWetMortarAccumulativeOutput;
            BigDecimal wetMortarAccumulativeOutput = wetMortarThisMonthOutput.add(preMonthWetMortarAccOutput);
            vo.setWetMortarAccumulativeOutput(wetMortarAccumulativeOutput);
        }

        if (Objects.nonNull(bulkCementThisMonthUsage)) {
            // 累计使用散装水泥量：本月使用散装水泥量 + 上个月累计使用散装水泥量
            BigDecimal preMonthBulkCementAccumulativeUsage = preMortarSupplyBulletin.getBulkCementAccumulativeUsage();
            BigDecimal preMonthBulkCementAccUsage = Objects.isNull(preMonthBulkCementAccumulativeUsage) ? BigDecimal.ZERO : preMonthBulkCementAccumulativeUsage;
            BigDecimal bulkCementAccumulativeUsage = bulkCementThisMonthUsage.add(preMonthBulkCementAccUsage);
            vo.setBulkCementAccumulativeUsage(bulkCementAccumulativeUsage);
        }

        if (Objects.nonNull(wasteThisMonthComprehensiveUtilization)) {
            // 累计废弃物综合利用量：本月废弃物综合利用量 + 上个月累计废弃物综合利用量
            BigDecimal preMonthWasteAccumulativeComprehensiveUtilization = preMortarSupplyBulletin.getWasteAccumulativeComprehensiveUtilization();
            BigDecimal preMonthWasteAccComprehensiveUtilization = Objects.isNull(preMonthWasteAccumulativeComprehensiveUtilization) ? BigDecimal.ZERO : preMonthWasteAccumulativeComprehensiveUtilization;
            BigDecimal wasteAccumulativeComprehensiveUtilization = wasteThisMonthComprehensiveUtilization.add(preMonthWasteAccComprehensiveUtilization);
            vo.setWasteAccumulativeComprehensiveUtilization(wasteAccumulativeComprehensiveUtilization);
        }
        return vo;
    }

    public ResMortarSupplyRelatedDataVO getMortarSupplyRelatedData(ReqMortarSupplyDataUploadPO po) {
        LocalDateTime reportMonth = po.getReportMonth();
        VUtils.isTrue(Objects.isNull(reportMonth)).throwMessage("请传入上报月份!");
        Long regionId = po.getRegionId();
        VUtils.isTrue(Objects.isNull(regionId)).throwMessage("请传入区域ID!");
        LocalDateTime preReportMonth = reportMonth.minusMonths(1);
        MortarSupplyBulletin preMonthMortarSupplyBulletin = mortarSupplyBulletinService.getOne(Wrappers.lambdaQuery(MortarSupplyBulletin.class)
                .eq(MortarSupplyBulletin::getReportMonth, preReportMonth)
                .eq(MortarSupplyBulletin::getRegionId, regionId));
        int year = reportMonth.getYear();
        BulkCementDirectReportSystemUniversalIndex universalIndex = universalIndexService.getOne(Wrappers.lambdaQuery(BulkCementDirectReportSystemUniversalIndex.class)
                .eq(BulkCementDirectReportSystemUniversalIndex::getRegionId, regionId)
                .eq(BulkCementDirectReportSystemUniversalIndex::getYear, year));
        ResMortarSupplyRelatedDataVO vo = new ResMortarSupplyRelatedDataVO();
        if (Objects.nonNull(universalIndex)) {
            vo.setTwoCategoryGuidGoal(universalIndex.getMortarSupplyTwoCategoryGuidGoal());
        }else {
            vo.setTwoCategoryGuidGoal(BigDecimal.ZERO);
        }
        if (Objects.nonNull(preMonthMortarSupplyBulletin)){
            vo.setDryMixedMortarProductionCompanyNum(preMonthMortarSupplyBulletin.getDryMixedMortarProductionCompanyNum());
            vo.setDryMixedMortarProductiveCapacity(preMonthMortarSupplyBulletin.getDryMixedMortarProductiveCapacity());
            vo.setDryMixedMortarBulkCapacity(preMonthMortarSupplyBulletin.getDryMixedMortarBulkCapacity());
        }
        return vo;
    }
}
