ARefundServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package com.zksy.property.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.zksy.property.domain.ARefund;
  8. import com.zksy.property.domain.vo.ARefundReportDayVo;
  9. import com.zksy.property.domain.vo.ARefundReportVo;
  10. import com.zksy.property.domain.vo.ARefundVo;
  11. import com.zksy.property.mapper.ARefundMapper;
  12. import com.zksy.property.service.ARefundService;
  13. import com.zksy.service.MinioFileStorageService;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Transactional;
  17. import java.math.BigDecimal;
  18. import java.time.LocalDate;
  19. import java.util.*;
  20. import java.util.stream.Collectors;
  21. /**
  22. * @author Administrator
  23. * @description 针对表【a_refund(退款信息表)】的数据库操作Service实现
  24. * @createDate 2025-07-29 15:30:56
  25. */
  26. @Service
  27. public class ARefundServiceImpl extends ServiceImpl<ARefundMapper, ARefund>
  28. implements ARefundService{
  29. @Autowired
  30. private ARefundMapper aRefundMapper;
  31. @Override
  32. public Page<ARefund> findByPage(long pageNum, long pageSize, String unit, String tenant, String generationDate) {
  33. Page<ARefund> page = new Page<>(pageNum,pageSize);
  34. LambdaQueryWrapper<ARefund> queryWrapper = new LambdaQueryWrapper();
  35. queryWrapper.like(unit != null,ARefund::getUnit,unit);
  36. queryWrapper.like(tenant != null,ARefund::getTenant,tenant);
  37. queryWrapper.like(generationDate != null,ARefund::getGenerationDate,generationDate);
  38. Page<ARefund> page1 = this.page(page, queryWrapper);
  39. return page1;
  40. }
  41. @Override
  42. public List<ARefund> getARefundList(String unit, String tenant, String generationDate) {
  43. LambdaQueryWrapper<ARefund> queryWrapper = new LambdaQueryWrapper();
  44. queryWrapper.like(unit != null,ARefund::getUnit,unit);
  45. queryWrapper.like(tenant != null,ARefund::getTenant,tenant);
  46. queryWrapper.like(generationDate != null,ARefund::getGenerationDate,generationDate);
  47. List<ARefund> list = this.list(queryWrapper);
  48. return list;
  49. }
  50. @Override
  51. public Map<String, BigDecimal> calculateMonthlyRefundStatistics(int year) {
  52. // 从数据库查询指定年份的所有退款记录
  53. List<ARefund> refundList = list(new QueryWrapper<ARefund>()
  54. .ge("generation_date", LocalDate.of(year, 1, 1))
  55. .le("generation_date", LocalDate.of(year, 12, 31)));
  56. Map<String, BigDecimal> monthlyData = new LinkedHashMap<>();
  57. for (int month = 1; month <= 12; month++) {
  58. monthlyData.put(String.format("%d-%02d", year, month), BigDecimal.ZERO);
  59. }
  60. for (ARefund refund : refundList) {
  61. if (refund.getGenerationDate() != null && refund.getTotalAmount() != null) {
  62. String monthKey = String.format("%d-%02d",
  63. year,
  64. refund.getGenerationDate().getMonthValue()
  65. );
  66. monthlyData.merge(monthKey, refund.getTotalAmount(), BigDecimal::add);
  67. }
  68. }
  69. return monthlyData;
  70. }
  71. @Override
  72. public Map<String, BigDecimal> calculateQuarterlyRefundStatistics(int year) {
  73. List<ARefund> refundList = list(new QueryWrapper<ARefund>()
  74. .ge("generation_date", LocalDate.of(year, 1, 1))
  75. .le("generation_date", LocalDate.of(year, 12, 31)));
  76. // 初始化4个季度的容器(使用LinkedHashMap保证顺序)
  77. Map<String, BigDecimal> quarterlyData = new LinkedHashMap<>();
  78. for (int quarter = 1; quarter <= 4; quarter++) {
  79. quarterlyData.put(year + "Q" + quarter, BigDecimal.ZERO);
  80. }
  81. for (ARefund refund : refundList) {
  82. LocalDate date = refund.getGenerationDate();
  83. if (date != null && refund.getTotalAmount() != null) {
  84. int quarter = (date.getMonthValue() - 1) / 3 + 1;
  85. String quarterKey = year + "Q" + quarter;
  86. quarterlyData.merge(quarterKey, refund.getTotalAmount(), BigDecimal::add);
  87. }
  88. }
  89. return quarterlyData;
  90. }
  91. @Override
  92. public Map<String, BigDecimal> calculateYearlyRefundStatistics() {
  93. List<ARefund> refundList = list();
  94. Map<String, BigDecimal> yearlyData = new TreeMap<>();
  95. // 统计数据
  96. for (ARefund refund : refundList) {
  97. LocalDate date = refund.getGenerationDate();
  98. if (date != null && refund.getTotalAmount() != null) {
  99. String yearKey = String.valueOf(date.getYear());
  100. yearlyData.merge(yearKey, refund.getTotalAmount(), BigDecimal::add);
  101. }
  102. }
  103. return yearlyData;
  104. }
  105. @Override
  106. public List<Integer> getExistingRefundYears() {
  107. List<ARefund> refundList = list();
  108. return refundList.stream()
  109. .map(ARefund::getGenerationDate)
  110. .filter(Objects::nonNull)
  111. .map(date -> date.getYear())
  112. .distinct()
  113. .sorted()
  114. .collect(Collectors.toList());
  115. }
  116. @Autowired
  117. private MinioFileStorageService minioFileStorageService;
  118. @Override
  119. @Transactional
  120. public boolean removeBatchByIdsWithUrl(List<String> ids) {
  121. List<ARefund> aReceiptInfoList = listByIds(ids);
  122. List<String> list = aReceiptInfoList.stream().map(ARefund::getAttachmentUrl).collect(Collectors.toList());
  123. minioFileStorageService.deleteFileBatch(list);
  124. return removeBatchByIds(ids);
  125. }
  126. @Override
  127. public Page<ARefundVo> findByPageWithContract(long pageNum, long pageSize, String unit, String tenant, String contractNumber) {
  128. Page<ARefundVo> page = new Page<>(pageNum, pageSize);
  129. return aRefundMapper.selectRefundWithContractByPage(page, unit, tenant, contractNumber);
  130. }
  131. @Override
  132. public List<ARefundReportVo> getRefundReport(Integer year, Integer month) {
  133. // 校验年份必填
  134. if (year == null) {
  135. throw new IllegalArgumentException("年份参数不能为空");
  136. }
  137. List<ARefundReportVo> resultList = new ArrayList<>();
  138. // 按年查询(仅传年份)
  139. if (month == null) {
  140. resultList.add(sumByYear(year));
  141. } else {
  142. resultList = sumByMonth(year);
  143. }
  144. return resultList;
  145. }
  146. @Override
  147. public List<ARefundReportVo> statRefundDetailByYear(Integer year,String assetType) {
  148. return baseMapper.statRefundByYear(year,assetType);
  149. }
  150. @Override
  151. public List<ARefundReportVo> statRefundDetailByMonth(Integer year, Integer month,String assetType) {
  152. if (month < 1 || month > 12) {
  153. throw new IllegalArgumentException("月份必须在1-12之间");
  154. }
  155. return baseMapper.statRefundByMonth(year, month,assetType);
  156. }
  157. @Override
  158. public List<ARefundReportDayVo> statRefundDetailByDay(Integer year, Integer month,String assetType) {
  159. if (month < 1 || month > 12) {
  160. throw new IllegalArgumentException("月份必须在1-12之间");
  161. }
  162. return baseMapper.statRefundByDay(year, month,assetType);
  163. }
  164. /**
  165. * 按年汇总退款数据(MySQL适配)
  166. * @param year 年份(必填)
  167. * @return 年度汇总VO
  168. */
  169. private ARefundReportVo sumByYear(Integer year) {
  170. //构建MySQL适配的查询条件
  171. QueryWrapper<ARefund> wrapper = new QueryWrapper<>();
  172. wrapper.eq("YEAR(generation_date)", year)
  173. .select(
  174. "DATE_FORMAT(generation_date, '%Y') as refundTime",
  175. "IFNULL(SUM(deposit), 0) as deposit",
  176. "IFNULL(SUM(rent), 0) as rent",
  177. "IFNULL(SUM(property_fee), 0) as propertyFee"
  178. )
  179. .groupBy("DATE_FORMAT(generation_date, '%Y')");
  180. // 执行查询
  181. List<Map<String, Object>> resultList = baseMapper.selectMaps(wrapper);
  182. // 转换为VO
  183. ARefundReportVo yearVo = new ARefundReportVo();
  184. if (CollectionUtils.isNotEmpty(resultList)) {
  185. Map<String, Object> dataMap = resultList.get(0);
  186. yearVo.setRefundTime((String) dataMap.get("refundTime"));
  187. yearVo.setDeposit((BigDecimal) dataMap.get("deposit"));
  188. yearVo.setRent((BigDecimal) dataMap.get("rent"));
  189. yearVo.setPropertyFee((BigDecimal) dataMap.get("propertyFee"));
  190. } else {
  191. // 无数据时默认值
  192. yearVo.setRefundTime(year.toString());
  193. yearVo.setDeposit(BigDecimal.ZERO);
  194. yearVo.setRent(BigDecimal.ZERO);
  195. yearVo.setPropertyFee(BigDecimal.ZERO);
  196. }
  197. return yearVo;
  198. }
  199. /**
  200. * 按月汇总退款数据
  201. * @param year 年份(必填)
  202. * @return 12个月的汇总VO列表
  203. */
  204. private List<ARefundReportVo> sumByMonth(Integer year) {
  205. // 构建MySQL适配的查询条件
  206. QueryWrapper<ARefund> wrapper = new QueryWrapper<>();
  207. wrapper.eq("YEAR(generation_date)", year)
  208. .select(
  209. "DATE_FORMAT(generation_date, '%Y-%m') as refundTime",
  210. "MONTH(generation_date) as month",
  211. "IFNULL(SUM(deposit), 0) as deposit",
  212. "IFNULL(SUM(rent), 0) as rent",
  213. "IFNULL(SUM(property_fee), 0) as propertyFee"
  214. )
  215. .groupBy("DATE_FORMAT(generation_date, '%Y-%m')", "MONTH(generation_date)");
  216. //执行查询
  217. List<Map<String, Object>> monthDataList = baseMapper.selectMaps(wrapper);
  218. //构建月份数据映射
  219. Map<Integer, ARefundReportVo> monthDataMap = new HashMap<>();
  220. if (!CollectionUtils.isEmpty(monthDataList)) {
  221. for (Map<String, Object> data : monthDataList) {
  222. Integer month = ((Number) data.get("month")).intValue();
  223. ARefundReportVo vo = new ARefundReportVo();
  224. vo.setRefundTime((String) data.get("refundTime"));
  225. vo.setDeposit((BigDecimal) data.get("deposit"));
  226. vo.setRent((BigDecimal) data.get("rent"));
  227. vo.setPropertyFee((BigDecimal) data.get("propertyFee"));
  228. monthDataMap.put(month, vo);
  229. }
  230. }
  231. // 生成12个月完整数据
  232. List<ARefundReportVo> resultList = new ArrayList<>();
  233. for (int i = 1; i <= 12; i++) {
  234. ARefundReportVo monthVo = monthDataMap.getOrDefault(i, new ARefundReportVo());
  235. if (monthVo.getRefundTime() == null) {
  236. monthVo.setRefundTime(String.format("%d-%02d", year, i));
  237. monthVo.setDeposit(BigDecimal.ZERO);
  238. monthVo.setRent(BigDecimal.ZERO);
  239. monthVo.setPropertyFee(BigDecimal.ZERO);
  240. }
  241. resultList.add(monthVo);
  242. }
  243. return resultList;
  244. }
  245. }