This commit is contained in:
mashuai 2025-02-20 22:56:17 +08:00
parent a15db088a1
commit 213d250d34
2 changed files with 46 additions and 16 deletions

View File

@ -3,10 +3,7 @@ package com.bonus.material.scrap.service.impl;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
import cn.hutool.core.collection.CollectionUtil;
@ -851,7 +848,45 @@ public class ScrapApplyDetailsServiceImpl implements IScrapApplyDetailsService {
*/
@Override
public List<ScrapTotalListVo> getScrapList(ScrapApplyDetails scrapApplyDetails) {
return scrapApplyDetailsMapper.getScrapList(scrapApplyDetails);
List<ScrapTotalListVo> scrapTotalListVos = new ArrayList<>();
List<ScrapTotalListVo> list = scrapApplyDetailsMapper.getScrapList(scrapApplyDetails); // 获取数据
// 如果列表不为空则进行处理
if (CollectionUtils.isNotEmpty(list)) {
// 如果用户传入了具体月份先进行过滤
if (StringUtils.isNotBlank(scrapApplyDetails.getMonth())) {
list = list.stream()
.filter(totalListVo -> totalListVo.getMonth().equals(scrapApplyDetails.getMonth()))
.collect(Collectors.toList());
}
// 按月分组
Map<String, List<ScrapTotalListVo>> groupedByMonth = list.stream()
.collect(Collectors.groupingBy(ScrapTotalListVo::getMonth));
// 遍历按月分组的结果
groupedByMonth.forEach((month, monthList) -> {
ScrapTotalListVo scrapTotalListVo = new ScrapTotalListVo();
scrapTotalListVo.setMonth(month); // 设置当前月份
// 重新初始化 totalCost避免多个月份累加
BigDecimal monthTotalCost = BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
BigDecimal numTotalCost = BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP);
// 累加每个月的 totalCost
for (ScrapTotalListVo totalListVo : monthList) {
monthTotalCost = monthTotalCost.add(
totalListVo.getScrapNum()
.multiply(totalListVo.getBuyPrice())
.divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP)
);
numTotalCost = numTotalCost.add(totalListVo.getScrapNum());
}
scrapTotalListVo.setTotalCost(monthTotalCost); // 设置总成本
scrapTotalListVo.setScrapNum(numTotalCost);
scrapTotalListVos.add(scrapTotalListVo); // 添加到结果列表
});
}
return scrapTotalListVos; // 返回结果
}
/**

View File

@ -402,9 +402,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
sad.scrap_type like concat('%', #{keyWord}, '%')
)
</if>
<if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
<if test="month != null and month != ''">
<![CDATA[
AND DATE_FORMAT( sad.create_time, '%Y-%m' ) BETWEEN DATE_FORMAT(#{startTime}, '%Y-%m') AND DATE_FORMAT(#{endTime}, '%Y-%m')
AND DATE_FORMAT( sad.create_time, '%Y-%m' ) = DATE_FORMAT(#{month}, '%Y-%m')
]]>
</if>
GROUP BY
@ -490,12 +490,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
mt.type_name AS typeModelName,
mt.unit_name AS unitName,
mt.manage_type AS manageType,
sum( sad.scrap_num ) AS scrapNum,
sad.scrap_num AS scrapNum,
mt.buy_price AS buyPrice,
GROUP_CONCAT( DISTINCT sad.create_by ) AS createName,
sad.create_by AS createName,
DATE_FORMAT( sad.ledger_time, '%Y-%m' ) AS month,
sad.ledger_status AS ledgerStatus,
TRIM(TRAILING '0' FROM CAST(SUM(ROUND(mt.buy_price) * ROUND(sad.scrap_num)) / 10000 AS CHAR)) AS totalCost
sad.ledger_status AS ledgerStatus
FROM
scrap_apply_details sad
LEFT JOIN ma_type mt ON sad.type_id = mt.type_id
@ -506,10 +505,6 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
AND DATE_FORMAT( sad.create_time, '%Y-%m' ) BETWEEN DATE_FORMAT(#{startTime}, '%Y-%m') AND DATE_FORMAT(#{endTime}, '%Y-%m')
]]>
</if>
GROUP BY
DATE_FORMAT(
sad.ledger_time,
'%Y-%m')
</select>
<select id="getScrapDetailsList" resultType="com.bonus.material.scrap.domain.vo.ScrapDetailsListVo">
@ -542,7 +537,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</if>
<if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
<![CDATA[
AND DATE_FORMAT( sad.ledger_time, '%Y-%m' ) BETWEEN DATE_FORMAT(#{startTime}, '%Y-%m') AND DATE_FORMAT(#{endTime}, '%Y-%m')
AND DATE_FORMAT( sad.ledger_time, '%Y-%m-%d' ) BETWEEN DATE_FORMAT(#{startTime}, '%Y-%m-%d') AND DATE_FORMAT(#{endTime}, '%Y-%m-%d')
]]>
</if>
GROUP BY