ProfitAnalysisServiceImpl.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package com.ydtech.modules.admin.service.impl;
  2. import com.ydtech.modules.admin.model.report.profitAnalysis.ProfitAnalysisDto;
  3. import com.ydtech.modules.admin.model.report.profitAnalysis.ProfitAnalysisQueryDto;
  4. import com.ydtech.modules.admin.service.ProfitAnalysisService;
  5. import com.ydtech.modules.admin.utils.SliceUpDateUtil;
  6. import com.ydtech.modules.console.dao.ProfitAnalysisMapper;
  7. import com.ydtech.modules.fnc.dao.InsPlyIncomeMapper;
  8. import com.ydtech.modules.inv.dao.InvAccountMapper;
  9. import com.ydtech.modules.inv.model.InvAccount;
  10. import com.ydtech.modules.inv.model.InvAccountCardVo;
  11. import com.ydtech.modules.inv.model.InvAccountExpenses;
  12. import com.ydtech.utils.BigDecimalUtils;
  13. import org.apache.commons.lang3.StringUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Service;
  16. import java.math.BigDecimal;
  17. import java.util.*;
  18. import java.util.stream.Collectors;
  19. /**
  20. * @version
  21. * @author: hxl
  22. * @Date: 2024/7/1 11:36
  23. * @Description: 控制台-运营数据
  24. */
  25. @Service
  26. public class ProfitAnalysisServiceImpl implements ProfitAnalysisService {
  27. @Autowired
  28. private InvAccountMapper invAccountMapper;
  29. @Autowired
  30. private InsPlyIncomeMapper insPlyIncomeMapper;
  31. @Autowired
  32. private ProfitAnalysisMapper profitAnalysisMapper;
  33. /**
  34. * @version
  35. * @author: hxl
  36. * @Date: 2024/7/31 17:42
  37. * @Description: 查询利润数据
  38. */
  39. @Override
  40. public List<ProfitAnalysisDto> queryPage(ProfitAnalysisQueryDto profitAnalysisQueryDto) {
  41. HashMap<String, String> dateMapByType = getDateMapByType(profitAnalysisQueryDto.getDateType(), profitAnalysisQueryDto.getYear());
  42. profitAnalysisQueryDto.setBeginDate(dateMapByType.get("startDate"));
  43. profitAnalysisQueryDto.setEndDate(dateMapByType.get("endDate"));
  44. List<ProfitAnalysisDto> list = insPlyIncomeMapper.queryPage(profitAnalysisQueryDto);
  45. return list;
  46. }
  47. /**
  48. * @version
  49. * @author: whp
  50. * @Date: 2024/8/16 15:35
  51. * @Description: 查询利润分析统计
  52. */
  53. @Override
  54. public List<ProfitAnalysisDto> getProfitAnalysisQueryList(ProfitAnalysisQueryDto profitAnalysisQueryDto) {
  55. HashMap<String, String> dateMapByType = getDateMapByType(profitAnalysisQueryDto.getDateType(), profitAnalysisQueryDto.getYear());
  56. profitAnalysisQueryDto.setBeginDate(dateMapByType.get("startDate"));
  57. profitAnalysisQueryDto.setEndDate(dateMapByType.get("endDate"));
  58. List<ProfitAnalysisDto> profitAnalysisQueryList = insPlyIncomeMapper.getProfitAnalysisQueryList(profitAnalysisQueryDto);
  59. if(!profitAnalysisQueryList.isEmpty()){
  60. for (ProfitAnalysisDto profitAnalysisDto : profitAnalysisQueryList) {
  61. BigDecimal variableCost = BigDecimal.ZERO;
  62. String yearAndMonth = profitAnalysisDto.getYearAndMonth();
  63. profitAnalysisQueryDto.setYearAndMonth(yearAndMonth);
  64. List<ProfitAnalysisDto> profitAnalysisDto1 = insPlyIncomeMapper.changeAmount(profitAnalysisQueryDto);
  65. for (ProfitAnalysisDto analysisDto : profitAnalysisDto1) {
  66. String outCardNum = analysisDto.getOutCardNum();
  67. String enterCardNum = analysisDto.getEnterCardNum();
  68. InvAccountCardVo outAccount = invAccountMapper.getByCarNumber(outCardNum);
  69. InvAccountCardVo enterAccount = invAccountMapper.getByCarNumber(enterCardNum);
  70. if(StringUtils.isNotEmpty(outAccount.getOuterFlag()) && StringUtils.isNotEmpty(enterAccount.getOuterFlag())){
  71. if(!enterAccount.getOuterFlag().equals(outAccount.getOuterFlag())){
  72. variableCost=variableCost.add(analysisDto.getApplyAmount());
  73. }
  74. }
  75. }
  76. profitAnalysisDto.setVariableCost(variableCost);
  77. }
  78. }
  79. return profitAnalysisQueryList;
  80. }
  81. @Override
  82. public List<ProfitAnalysisDto> getProfitAnalysisQueryDetailsList(ProfitAnalysisQueryDto profitAnalysisQueryDto) {
  83. List<ProfitAnalysisDto> resultList =new ArrayList<>();
  84. if (StringUtils.isNotEmpty(profitAnalysisQueryDto.getExpensesType()) && "3".equals(profitAnalysisQueryDto.getExpensesType())) {
  85. List<ProfitAnalysisDto> profitAnalysisDtos = profitAnalysisMapper.selectAccountExpenses(profitAnalysisQueryDto);
  86. resultList.addAll(profitAnalysisDtos) ;
  87. List<ProfitAnalysisDto> addVariableCost = new ArrayList<>();
  88. List<ProfitAnalysisDto> profitAnalysisDto1 = insPlyIncomeMapper.changeAmount(profitAnalysisQueryDto);
  89. for (ProfitAnalysisDto analysisDto : profitAnalysisDto1) {
  90. String outCardNum = analysisDto.getOutCardNum();
  91. String enterCardNum = analysisDto.getEnterCardNum();
  92. InvAccountCardVo outAccount = invAccountMapper.getByCarNumber(outCardNum);
  93. InvAccountCardVo enterAccount = invAccountMapper.getByCarNumber(enterCardNum);
  94. if (StringUtils.isNotEmpty(outAccount.getOuterFlag()) && StringUtils.isNotEmpty(enterAccount.getOuterFlag())) {
  95. if (!enterAccount.getOuterFlag().equals(outAccount.getOuterFlag())) {
  96. addVariableCost.add(analysisDto);
  97. }
  98. }
  99. }
  100. Map<String, BigDecimal> profitAnalysisDtoMap = addVariableCost.stream()
  101. .collect(Collectors.toMap(
  102. ProfitAnalysisDto::getExpensesId, // keyMapper,用于提取键(expensesId)
  103. ProfitAnalysisDto::getApplyAmount, // valueMapper,用于提取初始值(applyAmount)
  104. BigDecimal::add // mergeFunction,用于处理键冲突(相加 applyAmount)
  105. ));
  106. for (ProfitAnalysisDto profitAnalysisDto : resultList) {
  107. String expensesId = profitAnalysisDto.getExpensesId();
  108. if (profitAnalysisDtoMap.containsKey(expensesId)) {
  109. BigDecimal bigDecimal = profitAnalysisDtoMap.get(expensesId);
  110. profitAnalysisDto.setApplyAmount(bigDecimal);
  111. }
  112. }
  113. }else {
  114. resultList.addAll(profitAnalysisMapper.getAllAccountExpenses(profitAnalysisQueryDto));
  115. }
  116. return getTree(resultList);
  117. }
  118. /**
  119. * 组装树形结构结果集方法
  120. */
  121. public List<ProfitAnalysisDto> getTree(List<ProfitAnalysisDto> list){
  122. return list.stream().filter(accountExpenses -> accountExpenses.getParentId().equals("0"))
  123. .peek(accountExpenses -> accountExpenses.setChildren(getChildren(accountExpenses,list))).collect(Collectors.toList());
  124. }
  125. public List<ProfitAnalysisDto> getChildren(ProfitAnalysisDto user,List<ProfitAnalysisDto> userList){
  126. return userList.stream().filter(u -> Objects.equals(u.getParentId(),user.getExpensesId())).map(
  127. u -> {
  128. u.setChildren(getChildren(u,userList));
  129. return u;
  130. }
  131. ).collect(Collectors.toList());
  132. }
  133. /**
  134. * @version
  135. * @author: hxl
  136. * @Date: 2024/6/19 11:57
  137. * @Description: 通过时间类型判断
  138. */
  139. private static HashMap<String,String> getDateMapByType(String type, String year) {
  140. HashMap<String,String> map =new HashMap<>();
  141. String startDateStr="";
  142. String endDateStr="";
  143. if(StringUtils.isBlank(year) && StringUtils.isBlank(type)){
  144. Calendar cale = Calendar.getInstance();
  145. int yearNow = cale.get(Calendar.YEAR);
  146. startDateStr= SliceUpDateUtil.getYearBeginDateStr(yearNow)+" 00:00:00";
  147. endDateStr= SliceUpDateUtil.getYearEndDateStr(yearNow)+" 23:59:59";
  148. }else{
  149. if(StringUtils.isNotBlank(year)){
  150. startDateStr= SliceUpDateUtil.getYearBeginDateStr(Integer.parseInt(year))+" 00:00:00";
  151. endDateStr= SliceUpDateUtil.getYearEndDateStr(Integer.parseInt(year))+" 23:59:59";
  152. }else{
  153. if(StringUtils.isNotBlank(type)){
  154. if("1".equals(type)){
  155. // 一月内
  156. startDateStr=SliceUpDateUtil.getBeforeDay(new Date(),2)+" 00:00:00";
  157. endDateStr=SliceUpDateUtil.getBeforeDay(new Date(),0)+" 23:59:59";
  158. }else if("2".equals(type)){
  159. // 三月内
  160. startDateStr=SliceUpDateUtil.getBeforeDay(new Date(),3)+" 00:00:00";
  161. endDateStr=SliceUpDateUtil.getBeforeDay(new Date(),0)+" 23:59:59";
  162. }else if("3".equals(type)){
  163. // 近半年
  164. startDateStr=SliceUpDateUtil.getBeforeDay(new Date(),4)+" 00:00:00";
  165. endDateStr=SliceUpDateUtil.getBeforeDay(new Date(),0)+" 23:59:59";
  166. }
  167. }
  168. }
  169. }
  170. map.put("startDate",startDateStr);
  171. map.put("endDate",endDateStr);
  172. return map;
  173. }
  174. }