| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- 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<ARefundMapper, ARefund>
- implements ARefundService{
- @Autowired
- private ARefundMapper aRefundMapper;
- @Override
- public Page<ARefund> findByPage(long pageNum, long pageSize, String unit, String tenant, String generationDate) {
- Page<ARefund> page = new Page<>(pageNum,pageSize);
- LambdaQueryWrapper<ARefund> 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<ARefund> page1 = this.page(page, queryWrapper);
- return page1;
- }
- @Override
- public List<ARefund> getARefundList(String unit, String tenant, String generationDate) {
- LambdaQueryWrapper<ARefund> 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<ARefund> list = this.list(queryWrapper);
- return list;
- }
- @Override
- public Map<String, BigDecimal> calculateMonthlyRefundStatistics(int year) {
- // 从数据库查询指定年份的所有退款记录
- List<ARefund> refundList = list(new QueryWrapper<ARefund>()
- .ge("generation_date", LocalDate.of(year, 1, 1))
- .le("generation_date", LocalDate.of(year, 12, 31)));
- Map<String, BigDecimal> 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<String, BigDecimal> calculateQuarterlyRefundStatistics(int year) {
- List<ARefund> refundList = list(new QueryWrapper<ARefund>()
- .ge("generation_date", LocalDate.of(year, 1, 1))
- .le("generation_date", LocalDate.of(year, 12, 31)));
- // 初始化4个季度的容器(使用LinkedHashMap保证顺序)
- Map<String, BigDecimal> 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<String, BigDecimal> calculateYearlyRefundStatistics() {
- List<ARefund> refundList = list();
- Map<String, BigDecimal> 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<Integer> getExistingRefundYears() {
- List<ARefund> 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<String> ids) {
- List<ARefund> aReceiptInfoList = listByIds(ids);
- List<String> list = aReceiptInfoList.stream().map(ARefund::getAttachmentUrl).collect(Collectors.toList());
- minioFileStorageService.deleteFileBatch(list);
- return removeBatchByIds(ids);
- }
- @Override
- public Page<ARefundVo> findByPageWithContract(long pageNum, long pageSize, String unit, String tenant, String contractNumber) {
- Page<ARefundVo> page = new Page<>(pageNum, pageSize);
- return aRefundMapper.selectRefundWithContractByPage(page, unit, tenant, contractNumber);
- }
- @Override
- public List<ARefundReportVo> getRefundReport(Integer year, Integer month) {
- // 校验年份必填
- if (year == null) {
- throw new IllegalArgumentException("年份参数不能为空");
- }
- List<ARefundReportVo> resultList = new ArrayList<>();
- // 按年查询(仅传年份)
- if (month == null) {
- resultList.add(sumByYear(year));
- } else {
- resultList = sumByMonth(year);
- }
- return resultList;
- }
- @Override
- public List<ARefundReportVo> statRefundDetailByYear(Integer year,String assetType) {
- return baseMapper.statRefundByYear(year,assetType);
- }
- @Override
- public List<ARefundReportVo> 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<ARefundReportDayVo> 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<ARefund> 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<Map<String, Object>> resultList = baseMapper.selectMaps(wrapper);
- // 转换为VO
- ARefundReportVo yearVo = new ARefundReportVo();
- if (CollectionUtils.isNotEmpty(resultList)) {
- Map<String, Object> 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<ARefundReportVo> sumByMonth(Integer year) {
- // 构建MySQL适配的查询条件
- QueryWrapper<ARefund> 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<Map<String, Object>> monthDataList = baseMapper.selectMaps(wrapper);
- //构建月份数据映射
- Map<Integer, ARefundReportVo> monthDataMap = new HashMap<>();
- if (!CollectionUtils.isEmpty(monthDataList)) {
- for (Map<String, Object> 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<ARefundReportVo> 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;
- }
- }
|