package com.zksy.property.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.zksy.property.domain.ARefund; import com.zksy.property.domain.vo.ARefundReportDayVo; import com.zksy.property.domain.vo.ARefundReportVo; import com.zksy.property.domain.vo.ARefundVo; import com.zksy.property.mapper.ARefundMapper; import com.zksy.property.service.ARefundService; import com.zksy.service.MinioFileStorageService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.time.LocalDate; import java.util.*; import java.util.stream.Collectors; /** * @author Administrator * @description 针对表【a_refund(退款信息表)】的数据库操作Service实现 * @createDate 2025-07-29 15:30:56 */ @Service public class ARefundServiceImpl extends ServiceImpl implements ARefundService{ @Autowired private ARefundMapper aRefundMapper; @Override public Page findByPage(long pageNum, long pageSize, String unit, String tenant, String generationDate) { Page page = new Page<>(pageNum,pageSize); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper(); queryWrapper.like(unit != null,ARefund::getUnit,unit); queryWrapper.like(tenant != null,ARefund::getTenant,tenant); queryWrapper.like(generationDate != null,ARefund::getGenerationDate,generationDate); Page page1 = this.page(page, queryWrapper); return page1; } @Override public List getARefundList(String unit, String tenant, String generationDate) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper(); queryWrapper.like(unit != null,ARefund::getUnit,unit); queryWrapper.like(tenant != null,ARefund::getTenant,tenant); queryWrapper.like(generationDate != null,ARefund::getGenerationDate,generationDate); List list = this.list(queryWrapper); return list; } @Override public Map calculateMonthlyRefundStatistics(int year) { // 从数据库查询指定年份的所有退款记录 List refundList = list(new QueryWrapper() .ge("generation_date", LocalDate.of(year, 1, 1)) .le("generation_date", LocalDate.of(year, 12, 31))); Map monthlyData = new LinkedHashMap<>(); for (int month = 1; month <= 12; month++) { monthlyData.put(String.format("%d-%02d", year, month), BigDecimal.ZERO); } for (ARefund refund : refundList) { if (refund.getGenerationDate() != null && refund.getTotalAmount() != null) { String monthKey = String.format("%d-%02d", year, refund.getGenerationDate().getMonthValue() ); monthlyData.merge(monthKey, refund.getTotalAmount(), BigDecimal::add); } } return monthlyData; } @Override public Map calculateQuarterlyRefundStatistics(int year) { List refundList = list(new QueryWrapper() .ge("generation_date", LocalDate.of(year, 1, 1)) .le("generation_date", LocalDate.of(year, 12, 31))); // 初始化4个季度的容器(使用LinkedHashMap保证顺序) Map quarterlyData = new LinkedHashMap<>(); for (int quarter = 1; quarter <= 4; quarter++) { quarterlyData.put(year + "Q" + quarter, BigDecimal.ZERO); } for (ARefund refund : refundList) { LocalDate date = refund.getGenerationDate(); if (date != null && refund.getTotalAmount() != null) { int quarter = (date.getMonthValue() - 1) / 3 + 1; String quarterKey = year + "Q" + quarter; quarterlyData.merge(quarterKey, refund.getTotalAmount(), BigDecimal::add); } } return quarterlyData; } @Override public Map calculateYearlyRefundStatistics() { List refundList = list(); Map yearlyData = new TreeMap<>(); // 统计数据 for (ARefund refund : refundList) { LocalDate date = refund.getGenerationDate(); if (date != null && refund.getTotalAmount() != null) { String yearKey = String.valueOf(date.getYear()); yearlyData.merge(yearKey, refund.getTotalAmount(), BigDecimal::add); } } return yearlyData; } @Override public List getExistingRefundYears() { List refundList = list(); return refundList.stream() .map(ARefund::getGenerationDate) .filter(Objects::nonNull) .map(date -> date.getYear()) .distinct() .sorted() .collect(Collectors.toList()); } @Autowired private MinioFileStorageService minioFileStorageService; @Override @Transactional public boolean removeBatchByIdsWithUrl(List ids) { List aReceiptInfoList = listByIds(ids); List list = aReceiptInfoList.stream().map(ARefund::getAttachmentUrl).collect(Collectors.toList()); minioFileStorageService.deleteFileBatch(list); return removeBatchByIds(ids); } @Override public Page findByPageWithContract(long pageNum, long pageSize, String unit, String tenant, String contractNumber) { Page page = new Page<>(pageNum, pageSize); return aRefundMapper.selectRefundWithContractByPage(page, unit, tenant, contractNumber); } @Override public List getRefundReport(Integer year, Integer month) { // 校验年份必填 if (year == null) { throw new IllegalArgumentException("年份参数不能为空"); } List resultList = new ArrayList<>(); // 按年查询(仅传年份) if (month == null) { resultList.add(sumByYear(year)); } else { resultList = sumByMonth(year); } return resultList; } @Override public List statRefundDetailByYear(Integer year,String assetType) { return baseMapper.statRefundByYear(year,assetType); } @Override public List statRefundDetailByMonth(Integer year, Integer month,String assetType) { if (month < 1 || month > 12) { throw new IllegalArgumentException("月份必须在1-12之间"); } return baseMapper.statRefundByMonth(year, month,assetType); } @Override public List statRefundDetailByDay(Integer year, Integer month,String assetType) { if (month < 1 || month > 12) { throw new IllegalArgumentException("月份必须在1-12之间"); } return baseMapper.statRefundByDay(year, month,assetType); } /** * 按年汇总退款数据(MySQL适配) * @param year 年份(必填) * @return 年度汇总VO */ private ARefundReportVo sumByYear(Integer year) { //构建MySQL适配的查询条件 QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("YEAR(generation_date)", year) .select( "DATE_FORMAT(generation_date, '%Y') as refundTime", "IFNULL(SUM(deposit), 0) as deposit", "IFNULL(SUM(rent), 0) as rent", "IFNULL(SUM(property_fee), 0) as propertyFee" ) .groupBy("DATE_FORMAT(generation_date, '%Y')"); // 执行查询 List> resultList = baseMapper.selectMaps(wrapper); // 转换为VO ARefundReportVo yearVo = new ARefundReportVo(); if (CollectionUtils.isNotEmpty(resultList)) { Map dataMap = resultList.get(0); yearVo.setRefundTime((String) dataMap.get("refundTime")); yearVo.setDeposit((BigDecimal) dataMap.get("deposit")); yearVo.setRent((BigDecimal) dataMap.get("rent")); yearVo.setPropertyFee((BigDecimal) dataMap.get("propertyFee")); } else { // 无数据时默认值 yearVo.setRefundTime(year.toString()); yearVo.setDeposit(BigDecimal.ZERO); yearVo.setRent(BigDecimal.ZERO); yearVo.setPropertyFee(BigDecimal.ZERO); } return yearVo; } /** * 按月汇总退款数据 * @param year 年份(必填) * @return 12个月的汇总VO列表 */ private List sumByMonth(Integer year) { // 构建MySQL适配的查询条件 QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("YEAR(generation_date)", year) .select( "DATE_FORMAT(generation_date, '%Y-%m') as refundTime", "MONTH(generation_date) as month", "IFNULL(SUM(deposit), 0) as deposit", "IFNULL(SUM(rent), 0) as rent", "IFNULL(SUM(property_fee), 0) as propertyFee" ) .groupBy("DATE_FORMAT(generation_date, '%Y-%m')", "MONTH(generation_date)"); //执行查询 List> monthDataList = baseMapper.selectMaps(wrapper); //构建月份数据映射 Map monthDataMap = new HashMap<>(); if (!CollectionUtils.isEmpty(monthDataList)) { for (Map data : monthDataList) { Integer month = ((Number) data.get("month")).intValue(); ARefundReportVo vo = new ARefundReportVo(); vo.setRefundTime((String) data.get("refundTime")); vo.setDeposit((BigDecimal) data.get("deposit")); vo.setRent((BigDecimal) data.get("rent")); vo.setPropertyFee((BigDecimal) data.get("propertyFee")); monthDataMap.put(month, vo); } } // 生成12个月完整数据 List resultList = new ArrayList<>(); for (int i = 1; i <= 12; i++) { ARefundReportVo monthVo = monthDataMap.getOrDefault(i, new ARefundReportVo()); if (monthVo.getRefundTime() == null) { monthVo.setRefundTime(String.format("%d-%02d", year, i)); monthVo.setDeposit(BigDecimal.ZERO); monthVo.setRent(BigDecimal.ZERO); monthVo.setPropertyFee(BigDecimal.ZERO); } resultList.add(monthVo); } return resultList; } }