package com.ydtech.modules.admin.service.impl; import com.ydtech.modules.admin.model.report.profitAnalysis.ProfitAnalysisDto; import com.ydtech.modules.admin.model.report.profitAnalysis.ProfitAnalysisQueryDto; import com.ydtech.modules.admin.service.ProfitAnalysisService; import com.ydtech.modules.admin.utils.SliceUpDateUtil; import com.ydtech.modules.console.dao.ProfitAnalysisMapper; import com.ydtech.modules.fnc.dao.InsPlyIncomeMapper; import com.ydtech.modules.inv.dao.InvAccountMapper; import com.ydtech.modules.inv.model.InvAccount; import com.ydtech.modules.inv.model.InvAccountCardVo; import com.ydtech.modules.inv.model.InvAccountExpenses; import com.ydtech.utils.BigDecimalUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; /** * @version * @author: hxl * @Date: 2024/7/1 11:36 * @Description: 控制台-运营数据 */ @Service public class ProfitAnalysisServiceImpl implements ProfitAnalysisService { @Autowired private InvAccountMapper invAccountMapper; @Autowired private InsPlyIncomeMapper insPlyIncomeMapper; @Autowired private ProfitAnalysisMapper profitAnalysisMapper; /** * @version * @author: hxl * @Date: 2024/7/31 17:42 * @Description: 查询利润数据 */ @Override public List queryPage(ProfitAnalysisQueryDto profitAnalysisQueryDto) { HashMap dateMapByType = getDateMapByType(profitAnalysisQueryDto.getDateType(), profitAnalysisQueryDto.getYear()); profitAnalysisQueryDto.setBeginDate(dateMapByType.get("startDate")); profitAnalysisQueryDto.setEndDate(dateMapByType.get("endDate")); List list = insPlyIncomeMapper.queryPage(profitAnalysisQueryDto); return list; } /** * @version * @author: whp * @Date: 2024/8/16 15:35 * @Description: 查询利润分析统计 */ @Override public List getProfitAnalysisQueryList(ProfitAnalysisQueryDto profitAnalysisQueryDto) { HashMap dateMapByType = getDateMapByType(profitAnalysisQueryDto.getDateType(), profitAnalysisQueryDto.getYear()); profitAnalysisQueryDto.setBeginDate(dateMapByType.get("startDate")); profitAnalysisQueryDto.setEndDate(dateMapByType.get("endDate")); List profitAnalysisQueryList = insPlyIncomeMapper.getProfitAnalysisQueryList(profitAnalysisQueryDto); if(!profitAnalysisQueryList.isEmpty()){ for (ProfitAnalysisDto profitAnalysisDto : profitAnalysisQueryList) { BigDecimal variableCost = BigDecimal.ZERO; String yearAndMonth = profitAnalysisDto.getYearAndMonth(); profitAnalysisQueryDto.setYearAndMonth(yearAndMonth); List profitAnalysisDto1 = insPlyIncomeMapper.changeAmount(profitAnalysisQueryDto); for (ProfitAnalysisDto analysisDto : profitAnalysisDto1) { String outCardNum = analysisDto.getOutCardNum(); String enterCardNum = analysisDto.getEnterCardNum(); InvAccountCardVo outAccount = invAccountMapper.getByCarNumber(outCardNum); InvAccountCardVo enterAccount = invAccountMapper.getByCarNumber(enterCardNum); if(StringUtils.isNotEmpty(outAccount.getOuterFlag()) && StringUtils.isNotEmpty(enterAccount.getOuterFlag())){ if(!enterAccount.getOuterFlag().equals(outAccount.getOuterFlag())){ variableCost=variableCost.add(analysisDto.getApplyAmount()); } } } profitAnalysisDto.setVariableCost(variableCost); } } return profitAnalysisQueryList; } @Override public List getProfitAnalysisQueryDetailsList(ProfitAnalysisQueryDto profitAnalysisQueryDto) { List resultList =new ArrayList<>(); if (StringUtils.isNotEmpty(profitAnalysisQueryDto.getExpensesType()) && "3".equals(profitAnalysisQueryDto.getExpensesType())) { List profitAnalysisDtos = profitAnalysisMapper.selectAccountExpenses(profitAnalysisQueryDto); resultList.addAll(profitAnalysisDtos) ; List addVariableCost = new ArrayList<>(); List profitAnalysisDto1 = insPlyIncomeMapper.changeAmount(profitAnalysisQueryDto); for (ProfitAnalysisDto analysisDto : profitAnalysisDto1) { String outCardNum = analysisDto.getOutCardNum(); String enterCardNum = analysisDto.getEnterCardNum(); InvAccountCardVo outAccount = invAccountMapper.getByCarNumber(outCardNum); InvAccountCardVo enterAccount = invAccountMapper.getByCarNumber(enterCardNum); if (StringUtils.isNotEmpty(outAccount.getOuterFlag()) && StringUtils.isNotEmpty(enterAccount.getOuterFlag())) { if (!enterAccount.getOuterFlag().equals(outAccount.getOuterFlag())) { addVariableCost.add(analysisDto); } } } Map profitAnalysisDtoMap = addVariableCost.stream() .collect(Collectors.toMap( ProfitAnalysisDto::getExpensesId, // keyMapper,用于提取键(expensesId) ProfitAnalysisDto::getApplyAmount, // valueMapper,用于提取初始值(applyAmount) BigDecimal::add // mergeFunction,用于处理键冲突(相加 applyAmount) )); for (ProfitAnalysisDto profitAnalysisDto : resultList) { String expensesId = profitAnalysisDto.getExpensesId(); if (profitAnalysisDtoMap.containsKey(expensesId)) { BigDecimal bigDecimal = profitAnalysisDtoMap.get(expensesId); profitAnalysisDto.setApplyAmount(bigDecimal); } } }else { resultList.addAll(profitAnalysisMapper.getAllAccountExpenses(profitAnalysisQueryDto)); } return getTree(resultList); } /** * 组装树形结构结果集方法 */ public List getTree(List list){ return list.stream().filter(accountExpenses -> accountExpenses.getParentId().equals("0")) .peek(accountExpenses -> accountExpenses.setChildren(getChildren(accountExpenses,list))).collect(Collectors.toList()); } public List getChildren(ProfitAnalysisDto user,List userList){ return userList.stream().filter(u -> Objects.equals(u.getParentId(),user.getExpensesId())).map( u -> { u.setChildren(getChildren(u,userList)); return u; } ).collect(Collectors.toList()); } /** * @version * @author: hxl * @Date: 2024/6/19 11:57 * @Description: 通过时间类型判断 */ private static HashMap getDateMapByType(String type, String year) { HashMap map =new HashMap<>(); String startDateStr=""; String endDateStr=""; if(StringUtils.isBlank(year) && StringUtils.isBlank(type)){ Calendar cale = Calendar.getInstance(); int yearNow = cale.get(Calendar.YEAR); startDateStr= SliceUpDateUtil.getYearBeginDateStr(yearNow)+" 00:00:00"; endDateStr= SliceUpDateUtil.getYearEndDateStr(yearNow)+" 23:59:59"; }else{ if(StringUtils.isNotBlank(year)){ startDateStr= SliceUpDateUtil.getYearBeginDateStr(Integer.parseInt(year))+" 00:00:00"; endDateStr= SliceUpDateUtil.getYearEndDateStr(Integer.parseInt(year))+" 23:59:59"; }else{ if(StringUtils.isNotBlank(type)){ if("1".equals(type)){ // 一月内 startDateStr=SliceUpDateUtil.getBeforeDay(new Date(),2)+" 00:00:00"; endDateStr=SliceUpDateUtil.getBeforeDay(new Date(),0)+" 23:59:59"; }else if("2".equals(type)){ // 三月内 startDateStr=SliceUpDateUtil.getBeforeDay(new Date(),3)+" 00:00:00"; endDateStr=SliceUpDateUtil.getBeforeDay(new Date(),0)+" 23:59:59"; }else if("3".equals(type)){ // 近半年 startDateStr=SliceUpDateUtil.getBeforeDay(new Date(),4)+" 00:00:00"; endDateStr=SliceUpDateUtil.getBeforeDay(new Date(),0)+" 23:59:59"; } } } } map.put("startDate",startDateStr); map.put("endDate",endDateStr); return map; } }