Compare commits

...

2 Commits

Author SHA1 Message Date
ljw badc3786ba Merge branch 'dev' of https://git.njzscloud.com/lzq/njzscloud into dev 2025-12-23 11:48:09 +08:00
ljw 9285618aa3 对账单 2025-12-23 11:47:46 +08:00
5 changed files with 104 additions and 58 deletions

View File

@ -3,6 +3,7 @@ package com.njzscloud.supervisory.money.pojo.entity;
import com.baomidou.mybatisplus.annotation.*;
import com.njzscloud.supervisory.money.contant.BillStatus;
import com.njzscloud.supervisory.money.contant.InvoiceStatus;
import com.njzscloud.supervisory.order.contant.OrderCategory;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@ -137,4 +138,9 @@ public class MoneyBillEntity {
*/
private String goodsName;
/**
* ; order_category
*/
private String orderCategory;
}

View File

@ -8,6 +8,9 @@ import lombok.experimental.Accessors;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* @author ljw
*/
@Getter
@Setter
@ToString
@ -23,6 +26,10 @@ public class MoneyBillResult {
private Long orderId;
private BigDecimal discountMoney;
private BigDecimal settleMoney;
/**
* ; order_category
*/
private String orderCategory;
/**
*
*/

View File

@ -1,6 +1,5 @@
package com.njzscloud.supervisory.money.quartz;
import com.njzscloud.supervisory.goods.contant.MoneyStrategy;
import com.njzscloud.supervisory.money.mapper.MoneyBillMapper;
import com.njzscloud.supervisory.money.pojo.entity.MoneyBillEntity;
import com.njzscloud.supervisory.money.pojo.result.MoneyBillResult;
@ -17,6 +16,9 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author ljw
*/
@Slf4j
@Component
@RequiredArgsConstructor
@ -24,8 +26,8 @@ public class MoneyBillQuartz {
private final MoneyBillMapper moneyBillMapper;
// @Scheduled(cron = "0 0/1 * * * ?")//每1分钟一次
@Scheduled(cron = "0 0 1 27 * ?")//每月27号凌晨1点执行
@Scheduled(cron = "0 0/5 * * * ?")//每1分钟一次
// @Scheduled(cron = "0 0 1 27 * ?")//每月27号凌晨1点执行
public void syncAssetsDiscover() {
generateMoneyBill();
}
@ -33,9 +35,11 @@ public class MoneyBillQuartz {
public void generateMoneyBill() {
// 计算上个月的时间范围
LocalDateTime now = LocalDateTime.now();
LocalDateTime lastMonthStart = now.minusMonths(1).withDayOfMonth(1).with(LocalTime.MIN); // 上个月1号0点0分0秒
// 上个月1号0点0分0秒
LocalDateTime lastMonthStart = now.minusMonths(1).withDayOfMonth(1).with(LocalTime.MIN);
// 上个月月末23点59分59秒
LocalDateTime lastMonthEnd = now.minusMonths(1).withDayOfMonth(now.minusMonths(1).toLocalDate().lengthOfMonth())
.with(LocalTime.MAX); // 上个月月末23点59分59秒
.with(LocalTime.MAX);
// 创建查询条件
MoneyBillResult queryCondition = new MoneyBillResult();
@ -44,79 +48,94 @@ public class MoneyBillQuartz {
// 执行查询
List<MoneyBillResult> billResults = moneyBillMapper.selectMoneyBillList(queryCondition);
// 仅保留订单类型为 PuTong 或 DuanBoRu 的记录
billResults = billResults.stream()
.filter(r -> "PuTong".equals(r.getOrderCategory()) || "DuanBoRu".equals(r.getOrderCategory()))
.collect(Collectors.toList());
log.info("查询到上个月账单数据 {} 条", billResults.size());
if (billResults.size() > 0) {
// 先按transCompanyId分组再按originExpenseItemId分组
Map<Long, Map<Long, List<MoneyBillResult>>> groupedResults = billResults.stream()
if (!billResults.isEmpty()) {
// 先按transCompanyId、originExpenseItemId、orderCategory三层分组区分不同订单类型如 DuanBoRu 与 PuTong
Map<Long, Map<Long, Map<String, List<MoneyBillResult>>>> groupedResults = billResults.stream()
.collect(Collectors.groupingBy(
MoneyBillResult::getTransCompanyId,
Collectors.groupingBy(MoneyBillResult::getOriginExpenseItemId)
Collectors.groupingBy(MoneyBillResult::getOriginExpenseItemId,
Collectors.groupingBy(MoneyBillResult::getOrderCategory))
));
log.info("按transCompanyId和originExpenseItemId二级分组后共 {} 个公司分组", groupedResults.size());
log.info("按transCompanyId、originExpenseItemId和orderCategory三级分组后共 {} 个公司分组", groupedResults.size());
// 处理每个公司分组的数据
List<MoneyBillEntity> entityList = Lists.newArrayList();
for (Map.Entry<Long, Map<Long, List<MoneyBillResult>>> companyEntry : groupedResults.entrySet()) {
for (Map.Entry<Long, Map<Long, Map<String, List<MoneyBillResult>>>> companyEntry : groupedResults.entrySet()) {
Long transCompanyId = companyEntry.getKey();
Map<Long, List<MoneyBillResult>> companyGroupResults = companyEntry.getValue();
Map<Long, Map<String, List<MoneyBillResult>>> companyGroupResults = companyEntry.getValue();
log.info("transCompanyId: {}, 该公司包含 {} 个originExpenseItemId分组",
transCompanyId, companyGroupResults.size());
// 处理该公司下的每个originExpenseItemId分组
for (Map.Entry<Long, List<MoneyBillResult>> itemEntry : companyGroupResults.entrySet()) {
for (Map.Entry<Long, Map<String, List<MoneyBillResult>>> itemEntry : companyGroupResults.entrySet()) {
Long originExpenseItemId = itemEntry.getKey();
List<MoneyBillResult> itemGroupResults = itemEntry.getValue();
Map<String, List<MoneyBillResult>> categoryGroupMap = itemEntry.getValue();
log.info("transCompanyId: {}, originExpenseItemId: {}, 该分组包含 {} 条数据",
transCompanyId, originExpenseItemId, itemGroupResults.size());
for (Map.Entry<String, List<MoneyBillResult>> categoryEntry : categoryGroupMap.entrySet()) {
String orderCategoryStr = categoryEntry.getKey();
List<MoneyBillResult> itemGroupResults = categoryEntry.getValue();
// 这里可以根据业务需求处理每个分组的数据
// 例如计算总金额、创建MoneyBillEntity等
MoneyBillEntity entity = new MoneyBillEntity();
entity.setStationId(transCompanyId);
entity.setStartTime(lastMonthStart.toLocalDate());
entity.setEndTime(lastMonthEnd.toLocalDate());
entity.setGoodsId(originExpenseItemId);
log.info("transCompanyId: {}, originExpenseItemId: {}, orderCategory: {}, 该分组包含 {} 条数据",
transCompanyId, originExpenseItemId, orderCategoryStr, itemGroupResults.size());
// 1. 订单数量通过orderId分组条数就是订单数
long orderCount = itemGroupResults.stream()
.map(MoneyBillResult::getOrderId)
.distinct()
.count();
entity.setOrderCount((int) orderCount);
// 创建账单实体并计算汇总字段
MoneyBillEntity entity = new MoneyBillEntity();
entity.setStationId(transCompanyId);
entity.setStartTime(lastMonthStart.toLocalDate());
entity.setEndTime(lastMonthEnd.toLocalDate());
entity.setGoodsId(originExpenseItemId);
// 2. 车数=订单数
entity.setCarCount((int) orderCount);
// 1. 订单数量通过orderId分组条数就是订单数
long orderCount = itemGroupResults.stream()
.map(MoneyBillResult::getOrderId)
.distinct()
.count();
entity.setOrderCount((int) orderCount);
// 3. 总质量
int totalWeight = itemGroupResults.stream()
.mapToInt(MoneyBillResult::getSettleWeight)
.sum();
entity.setTotalWeight(totalWeight);
// 2. 车数=订单数
entity.setCarCount((int) orderCount);
// 4. 优惠金额discountMoney求和可能是负数
BigDecimal totalDiscountMoney = itemGroupResults.stream()
.map(MoneyBillResult::getDiscountMoney)
.reduce(BigDecimal.ZERO, BigDecimal::add);
entity.setDiscountMoney(totalDiscountMoney);
// 3. 总质量
int totalWeight = itemGroupResults.stream()
.mapToInt(MoneyBillResult::getSettleWeight)
.sum();
entity.setTotalWeight(totalWeight);
// 5. 账单金额settleMoney求和可能是负数
BigDecimal totalSettleMoney = itemGroupResults.stream()
.map(MoneyBillResult::getSettleMoney)
.reduce(BigDecimal.ZERO, BigDecimal::add);
entity.setTotalMoney(totalSettleMoney);
// 4. 优惠金额discountMoney求和可能是负数
BigDecimal totalDiscountMoney = itemGroupResults.stream()
.map(MoneyBillResult::getDiscountMoney)
.reduce(BigDecimal.ZERO, BigDecimal::add);
entity.setDiscountMoney(totalDiscountMoney);
// 设置其他基本信息(取第一条记录的信息)
MoneyBillResult firstResult = itemGroupResults.get(0);
entity.setUserId(firstResult.getUserId());
entity.setGoodsName(firstResult.getExpenseItemName());
entityList.add(entity);
log.info("生成账单实体: transCompanyId={}, originExpenseItemId={}, 订单数={}, 总质量={}, 车数={}, 优惠金额={}, 账单金额={}",
transCompanyId, originExpenseItemId, orderCount, totalWeight, orderCount, totalDiscountMoney, totalSettleMoney);
// 5. 账单金额settleMoney求和可能是负数
BigDecimal totalSettleMoney = itemGroupResults.stream()
.map(MoneyBillResult::getSettleMoney)
.reduce(BigDecimal.ZERO, BigDecimal::add);
entity.setTotalMoney(totalSettleMoney);
// 设置其他基本信息(取第一条记录的信息)
MoneyBillResult firstResult = itemGroupResults.get(0);
entity.setUserId(firstResult.getUserId());
entity.setGoodsName(firstResult.getExpenseItemName());
// 如果存在订单类型字符串,则直接设置(实体中为 String 类型)
if (orderCategoryStr != null) {
entity.setOrderCategory(orderCategoryStr);
}
entityList.add(entity);
log.info("生成账单实体: transCompanyId={}, originExpenseItemId={}, orderCategory={}, 订单数={}, 总质量={}, 车数={}, 优惠金额={}, 账单金额={}",
transCompanyId, originExpenseItemId, orderCategoryStr, orderCount, totalWeight, orderCount, totalDiscountMoney, totalSettleMoney);
}
}
}
// 账单入库

View File

@ -7,7 +7,6 @@ import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.common.security.util.SecurityUtil;
import com.njzscloud.supervisory.biz.pojo.result.SearchCompanyResult;
import com.njzscloud.supervisory.goods.contant.MoneyStrategy;
import com.njzscloud.supervisory.money.mapper.MoneyBillMapper;
import com.njzscloud.supervisory.money.pojo.entity.MoneyBillEntity;
import com.njzscloud.supervisory.money.pojo.result.MoneyBillResult;
@ -31,6 +30,7 @@ import java.util.Map;
/**
*
* @author ljw
*/
@Slf4j
@Service
@ -107,9 +107,10 @@ public class MoneyBillService extends ServiceImpl<MoneyBillMapper, MoneyBillEnti
queryCondition.setEndTime(endTime);
queryCondition.setOriginExpenseItemId(entity.getGoodsId());
queryCondition.setTransCompanyId(entity.getStationId());
queryCondition.setOrderCategory(entity.getOrderCategory());
// 执行查询
List<MoneyBillResult> billResults = baseMapper.selectMoneyBillList(queryCondition);
if (null != billResults && billResults.size() > 0) {
if (null != billResults && !billResults.isEmpty()) {
// 1. 订单数量通过orderId分组条数就是订单数
long orderCount = billResults.stream().
map(MoneyBillResult::getOrderId)

View File

@ -27,7 +27,8 @@
u.nickname,
bc.company_name as companyName,
mb.goods_name as goodsName,
mb.car_count
mb.car_count,
mb.order_category
FROM money_bill mb
LEFT JOIN sys_user u ON mb.user_id = u.id
LEFT JOIN biz_company bc ON mb.station_id = bc.id
@ -59,6 +60,9 @@
<if test="entity.goodsName != null and entity.goodsName != ''">
AND mb.goods_name LIKE CONCAT('%', #{entity.goodsName}, '%')
</if>
<if test="entity.orderCategory != null">
AND mb.order_category = #{entity.orderCategory}
</if>
</where>
ORDER BY mb.modify_time DESC
</select>
@ -66,12 +70,16 @@
<select id="selectMoneyBillList" resultType="com.njzscloud.supervisory.money.pojo.result.MoneyBillResult">
SELECT
oi.trans_company_id,
oi.order_category,
bc.user_id,
bc.company_name,
su.nickname,
oei.expense_item_name,
oei.money_strategy,
oei.origin_expense_item_id,
CASE
WHEN oei.expense_item_category = 'ChanPin' THEN gi.id
ELSE oei.origin_expense_item_id
END AS origin_expense_item_id,
oei.order_id,
oei.discount_money,
oei.settle_money,
@ -79,6 +87,8 @@
FROM
order_expense_items oei
LEFT JOIN order_info oi ON oi.id = oei.order_id
LEFT JOIN order_goods og on og.id = oi.goods_id
LEFT JOIN goods_info gi on gi.id = og.origin_goods_id
LEFT JOIN biz_company bc ON oi.trans_company_id = bc.id
LEFT JOIN sys_user su ON bc.user_id = su.id
LEFT JOIN order_car_in_out ocio ON ocio.id = oi.car_in_out_id
@ -94,7 +104,10 @@
AND oi.trans_company_id = #{entity.transCompanyId}
</if>
<if test="entity.originExpenseItemId != null">
AND oei.origin_expense_item_id = #{entity.originExpenseItemId}
AND (CASE WHEN oei.expense_item_category = 'ChanPin' THEN gi.id ELSE oei.origin_expense_item_id END) = #{entity.originExpenseItemId}
</if>
<if test="entity.orderCategory != null">
AND oi.order_category = #{entity.orderCategory}
</if>
order by oei.create_time desc
</select>