PayFeeMonthImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package com.java110.fee.feeMonth;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.java110.core.factory.GenerateCodeFactory;
  4. import com.java110.core.log.LoggerFactory;
  5. import com.java110.core.smo.IComputeFeeSMO;
  6. import com.java110.dto.fee.FeeDetailDto;
  7. import com.java110.dto.fee.FeeDto;
  8. import com.java110.dto.payFeeDetailMonth.PayFeeDetailMonthDto;
  9. import com.java110.dto.payFeeDetailMonth.PayFeeMonthOwnerDto;
  10. import com.java110.intf.fee.*;
  11. import com.java110.po.payFeeDetailMonth.PayFeeDetailMonthPo;
  12. import com.java110.utils.util.Assert;
  13. import com.java110.utils.util.DateUtil;
  14. import org.slf4j.Logger;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.scheduling.annotation.Async;
  17. import org.springframework.stereotype.Service;
  18. import java.util.*;
  19. /**
  20. * 费用离散为月 实现类
  21. */
  22. @Service
  23. public class PayFeeMonthImpl implements IPayFeeMonth {
  24. private static Logger logger = LoggerFactory.getLogger(PayFeeMonthImpl.class);
  25. @Autowired
  26. private IFeeInnerServiceSMO feeInnerServiceSMOImpl;
  27. @Autowired
  28. private IPayFeeConfigV1InnerServiceSMO payFeeConfigV1InnerServiceSMOImpl;
  29. @Autowired
  30. private IPayFeeDetailMonthInnerServiceSMO payFeeDetailMonthInnerServiceSMOImpl;
  31. @Autowired
  32. private IFeeDetailInnerServiceSMO feeDetailInnerServiceSMOImpl;
  33. @Autowired
  34. private IPayFeeMonthHelp payFeeMonthHelp;
  35. @Autowired
  36. private IComputeFeeSMO computeFeeSMOImpl;
  37. public static final int DEFAULT_DEAL_COUNT = 200;
  38. /**
  39. * 生成单个费用 并 离散到月
  40. *
  41. * @param feeId
  42. * @param communityId
  43. */
  44. @Override
  45. public void doGeneratorOrRefreshFeeMonth(String feeId, String communityId) {
  46. // todo 查询费用
  47. FeeDto feeDto = new FeeDto();
  48. feeDto.setCommunityId(communityId);
  49. feeDto.setFeeId(feeId);
  50. List<FeeDto> tmpFeeDtos = feeInnerServiceSMOImpl.queryFees(feeDto);
  51. Assert.listOnlyOne(tmpFeeDtos, "费用不存在");
  52. doGeneratorOrRefreshFeeMonth(tmpFeeDtos.get(0), communityId);
  53. }
  54. public void doGeneratorOrRefreshFeeMonth(FeeDto feeDto, String communityId) {
  55. //todo 计算每月单价
  56. Double feePrice = payFeeMonthHelp.getMonthFeePrice(feeDto);
  57. // todo 准备离散的基础数据
  58. PayFeeMonthOwnerDto payFeeMonthOwnerDto = payFeeMonthHelp.generatorOwnerRoom(feeDto);
  59. //todo 离散start_time 或者 pay_fee_detail_month 最大月份 到 deadlineTime 的数据
  60. maxMonthDateToDeadlineTimeData(feeDto, payFeeMonthOwnerDto, feePrice);
  61. }
  62. /**
  63. * 离散最大 离散月到 deadlineTime 的数据
  64. *
  65. * @param feeDto
  66. * @param payFeeMonthOwnerDto
  67. * @param feePrice
  68. */
  69. private void maxMonthDateToDeadlineTimeData(FeeDto feeDto, PayFeeMonthOwnerDto payFeeMonthOwnerDto, Double feePrice) {
  70. PayFeeDetailMonthDto payFeeDetailMonthDto = new PayFeeDetailMonthDto();
  71. payFeeDetailMonthDto.setCommunityId(feeDto.getCommunityId());
  72. payFeeDetailMonthDto.setFeeId(feeDto.getFeeId());
  73. List<PayFeeDetailMonthDto> payFeeDetailMonthDtos = payFeeDetailMonthInnerServiceSMOImpl.queryPayFeeDetailMaxMonths(payFeeDetailMonthDto);
  74. Date startTime = null;
  75. Date deadlineTime = computeFeeSMOImpl.getDeadlineTime(feeDto);
  76. if (payFeeDetailMonthDtos == null || payFeeDetailMonthDtos.size() < 1) {
  77. startTime = feeDto.getStartTime();
  78. } else {
  79. Calendar calendar = Calendar.getInstance();
  80. calendar.setTime(DateUtil.getDateFromStringA(payFeeDetailMonthDtos.get(0).getCurMonthTime()));
  81. calendar.add(Calendar.MONTH, 1);
  82. startTime = calendar.getTime();
  83. }
  84. // todo 生成一段时间内的数据
  85. doGeneratorTimeMonthData(feeDto, payFeeMonthOwnerDto, feePrice, startTime, deadlineTime);
  86. }
  87. private void doGeneratorTimeMonthData(FeeDto feeDto, PayFeeMonthOwnerDto payFeeMonthOwnerDto, Double feePrice, Date startTime, Date endTime) {
  88. double maxMonth = Math.ceil(computeFeeSMOImpl.dayCompare(startTime, endTime));
  89. if (maxMonth < 1) {
  90. return;
  91. }
  92. //todo 查询 缴费明细
  93. FeeDetailDto feeDetailDto = new FeeDetailDto();
  94. feeDetailDto.setCommunityId(feeDto.getCommunityId());
  95. feeDetailDto.setFeeId(feeDto.getFeeId());
  96. List<FeeDetailDto> feeDetailDtos = feeDetailInnerServiceSMOImpl.queryFeeDetails(feeDetailDto);
  97. //todo 生成 月离散数据
  98. PayFeeDetailMonthPo tmpPayFeeDetailMonthPo;
  99. List<PayFeeDetailMonthPo> payFeeDetailMonthPos = new ArrayList<>();
  100. for (int month = 0; month < maxMonth; month++) {
  101. Calendar calendar = Calendar.getInstance();
  102. calendar.setTime(startTime);
  103. calendar.add(Calendar.MONTH, month);
  104. //calendar.set(Calendar.DAY_OF_MONTH, 1);
  105. tmpPayFeeDetailMonthPo = new PayFeeDetailMonthPo();
  106. tmpPayFeeDetailMonthPo.setFeeId(feeDto.getFeeId());
  107. tmpPayFeeDetailMonthPo.setCommunityId(feeDto.getCommunityId());
  108. tmpPayFeeDetailMonthPo.setDetailId(payFeeMonthHelp.getFeeDetailId(feeDetailDtos, calendar.getTime()));
  109. tmpPayFeeDetailMonthPo.setDetailYear(calendar.get(Calendar.YEAR) + "");
  110. tmpPayFeeDetailMonthPo.setDetailMonth((calendar.get(Calendar.MONTH) + 1) + "");
  111. tmpPayFeeDetailMonthPo.setReceivableAmount(payFeeMonthHelp.getReceivableAmount(feeDetailDtos, feePrice, calendar.getTime(), feeDto) + "");
  112. tmpPayFeeDetailMonthPo.setReceivedAmount(payFeeMonthHelp.getReceivedAmount(feeDetailDtos, feePrice, calendar.getTime(), feeDto) + "");
  113. tmpPayFeeDetailMonthPo.setDiscountAmount(
  114. payFeeMonthHelp.getDiscountAmount(Double.parseDouble(tmpPayFeeDetailMonthPo.getReceivableAmount()),
  115. Double.parseDouble(tmpPayFeeDetailMonthPo.getReceivedAmount()),
  116. calendar.getTime(), feeDto) + "");
  117. tmpPayFeeDetailMonthPo.setMonthId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_monthId));
  118. tmpPayFeeDetailMonthPo.setRemark("程序计算生成");
  119. tmpPayFeeDetailMonthPo.setObjName(payFeeMonthOwnerDto.getObjName());
  120. tmpPayFeeDetailMonthPo.setObjId(payFeeMonthOwnerDto.getObjId());
  121. tmpPayFeeDetailMonthPo.setOwnerId(payFeeMonthOwnerDto.getOwnerId());
  122. tmpPayFeeDetailMonthPo.setOwnerName(payFeeMonthOwnerDto.getOwnerName());
  123. tmpPayFeeDetailMonthPo.setLink(payFeeMonthOwnerDto.getLink());
  124. tmpPayFeeDetailMonthPo.setCurMonthTime(DateUtil.getFormatTimeStringB(calendar.getTime()));
  125. tmpPayFeeDetailMonthPo.setPayFeeTime(payFeeMonthHelp.getFeeFeeTime(feeDetailDtos, tmpPayFeeDetailMonthPo.getDetailId()));
  126. tmpPayFeeDetailMonthPo.setState("W"); // todo 这里暂时写死,目前用不到,算是预留字段
  127. tmpPayFeeDetailMonthPo.setFeeName(feeDto.getFeeName());
  128. tmpPayFeeDetailMonthPo.setConfigId(feeDto.getConfigId());
  129. payFeeDetailMonthPos.add(tmpPayFeeDetailMonthPo);
  130. }
  131. payFeeDetailMonthInnerServiceSMOImpl.savePayFeeDetailMonths(payFeeDetailMonthPos);
  132. }
  133. /**
  134. * 小区数据 离散为 月数据
  135. *
  136. * @param communityId
  137. */
  138. @Async
  139. @Override
  140. public void doGeneratorOrRefreshAllFeeMonth(String communityId) {
  141. FeeDto feeDto = new FeeDto();
  142. feeDto.setCommunityId(communityId);
  143. int count = feeInnerServiceSMOImpl.queryFeesCount(feeDto);
  144. int page = 1;
  145. int max = 15;
  146. if (count < DEFAULT_DEAL_COUNT) {
  147. page = 1;
  148. max = count;
  149. } else {
  150. page = (int) Math.ceil((double) count / (double) DEFAULT_DEAL_COUNT);
  151. max = DEFAULT_DEAL_COUNT;
  152. }
  153. //todo 每次按200条处理
  154. for (int pageIndex = 0; pageIndex < page; pageIndex++) {
  155. feeDto.setPage(pageIndex * max + 1);
  156. feeDto.setRow(max);
  157. List<FeeDto> tmpFeeDtos = feeInnerServiceSMOImpl.queryFees(feeDto);
  158. // 离散费用
  159. doTmpFeeDtoMonths(communityId, tmpFeeDtos);
  160. }
  161. }
  162. /**
  163. * 物业缴费时离散 报表数据
  164. *
  165. * @param feeId
  166. * @param detailId
  167. * @param communityId
  168. */
  169. @Async
  170. @Override
  171. public void payFeeDetailRefreshFeeMonth(String feeId, String detailId, String communityId) {
  172. // todo 查询费用
  173. FeeDto feeDto = new FeeDto();
  174. feeDto.setCommunityId(communityId);
  175. feeDto.setFeeId(feeId);
  176. List<FeeDto> tmpFeeDtos = feeInnerServiceSMOImpl.queryFees(feeDto);
  177. Assert.listOnlyOne(tmpFeeDtos, "费用不存在");
  178. //todo 查询 缴费明细
  179. FeeDetailDto feeDetailDto = new FeeDetailDto();
  180. feeDetailDto.setCommunityId(feeDto.getCommunityId());
  181. feeDetailDto.setFeeId(feeDto.getFeeId());
  182. feeDetailDto.setDetailId(detailId);
  183. List<FeeDetailDto> feeDetailDtos = feeDetailInnerServiceSMOImpl.queryFeeDetails(feeDetailDto);
  184. Assert.listOnlyOne(feeDetailDtos, "缴费记录不存在");
  185. //todo 计算每月单价
  186. Double feePrice = payFeeMonthHelp.getMonthFeePrice(feeDto);
  187. // todo 准备离散的基础数据
  188. PayFeeMonthOwnerDto payFeeMonthOwnerDto = payFeeMonthHelp.generatorOwnerRoom(feeDto);
  189. // todo 删除缴费时间范围内的数据
  190. doDeletePayFeeDetailInMonth(feeDto, feeDetailDtos.get(0));
  191. // todo 生成一段时间内的数据
  192. doGeneratorTimeMonthData(feeDto, payFeeMonthOwnerDto, feePrice, feeDetailDtos.get(0).getStartTime(), feeDetailDtos.get(0).getEndTime());
  193. }
  194. /**
  195. * 删除缴费范围内的数据
  196. *
  197. * @param feeDto
  198. * @param feeDetailDto
  199. */
  200. private void doDeletePayFeeDetailInMonth(FeeDto feeDto, FeeDetailDto feeDetailDto) {
  201. PayFeeDetailMonthPo payFeeDetailMonthPo = new PayFeeDetailMonthPo();
  202. payFeeDetailMonthPo.setFeeId(feeDto.getFeeId());
  203. Calendar calendar = Calendar.getInstance();
  204. calendar.setTime(feeDetailDto.getStartTime());
  205. calendar.set(Calendar.DAY_OF_MONTH, 1);
  206. payFeeDetailMonthPo.setCurMonthTime(DateUtil.getFormatTimeStringB(calendar.getTime()));
  207. payFeeDetailMonthPo.setCurMonthEndTime(DateUtil.getFormatTimeStringB(feeDetailDto.getEndTime()));
  208. payFeeDetailMonthInnerServiceSMOImpl.deletePayFeeDetailMonth(payFeeDetailMonthPo);
  209. }
  210. private void doTmpFeeDtoMonths(String communityId, List<FeeDto> tmpFeeDtos) {
  211. for (FeeDto tmpFeeDto : tmpFeeDtos) {
  212. try {
  213. doGeneratorOrRefreshFeeMonth(tmpFeeDto, communityId);
  214. } catch (Exception e) {
  215. e.printStackTrace();
  216. logger.error("生成费用报表失败" + JSONObject.toJSONString(tmpFeeDto), e);
  217. }
  218. }
  219. }
  220. }