对账单
parent
3bdb6185b6
commit
0a26032558
|
|
@ -2,7 +2,12 @@ package com.njzscloud.dispose.finance.mapper;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
import com.njzscloud.dispose.finance.pojo.entity.BillEntity;
|
import com.njzscloud.dispose.finance.pojo.entity.BillEntity;
|
||||||
|
import com.njzscloud.dispose.finance.pojo.result.BillStatisticsResult;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对账单
|
* 对账单
|
||||||
|
|
@ -10,5 +15,13 @@ import org.apache.ibatis.annotations.Mapper;
|
||||||
*/
|
*/
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface BillMapper extends BaseMapper<BillEntity> {
|
public interface BillMapper extends BaseMapper<BillEntity> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 统计对账单数据
|
||||||
|
* @param startTime 统计开始时间
|
||||||
|
* @param endTime 统计结束时间
|
||||||
|
* @return 对账单统计数据列表
|
||||||
|
*/
|
||||||
|
List<BillStatisticsResult> statisticsBillData(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,66 @@
|
||||||
|
package com.njzscloud.dispose.finance.pojo.result;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对账单统计结果
|
||||||
|
* @author ljw
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@ToString
|
||||||
|
@Accessors(chain = true)
|
||||||
|
public class BillStatisticsResult {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户 Id
|
||||||
|
*/
|
||||||
|
private Long userId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 客户 Id
|
||||||
|
*/
|
||||||
|
private Long customerId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 组织 Id
|
||||||
|
*/
|
||||||
|
private Long orgId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 账户类型;GeRen-->个人、QiYe-->企业
|
||||||
|
*/
|
||||||
|
private String accountType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订单数
|
||||||
|
*/
|
||||||
|
private Integer orderCount;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 总车数
|
||||||
|
*/
|
||||||
|
private Integer carCount;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 总质量(单位:吨)
|
||||||
|
*/
|
||||||
|
private Integer totalWeight;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 总金额
|
||||||
|
*/
|
||||||
|
private BigDecimal totalMoney;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 优惠金额
|
||||||
|
*/
|
||||||
|
private BigDecimal discountMoney;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,50 @@
|
||||||
|
package com.njzscloud.dispose.finance.quartz;
|
||||||
|
|
||||||
|
import com.njzscloud.dispose.finance.service.BillService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.LocalTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成对账单
|
||||||
|
* @author ljw
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class GenerateBillQuartz {
|
||||||
|
|
||||||
|
private final BillService billService;
|
||||||
|
|
||||||
|
// @Scheduled(cron = "0 0/1 * * * ?")//每1分钟一次
|
||||||
|
@Scheduled(cron = "0 0 1 1 * ?")// 每月1号凌晨1点执行
|
||||||
|
public void syncAssetsDiscover() {
|
||||||
|
generateBill();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generateBill() {
|
||||||
|
log.info("开始执行生成对账单任务...");
|
||||||
|
try {
|
||||||
|
// 计算上个月的时间范围
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
// 上个月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);
|
||||||
|
|
||||||
|
log.info("统计时间范围:{} ~ {}", lastMonthStart, lastMonthEnd);
|
||||||
|
|
||||||
|
// 生成对账单
|
||||||
|
int billCount = billService.generateBill(lastMonthStart, lastMonthEnd);
|
||||||
|
log.info("生成对账单任务执行完成,共生成 {} 条对账单", billCount);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("生成对账单任务执行失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,16 +1,20 @@
|
||||||
package com.njzscloud.dispose.finance.service;
|
package com.njzscloud.dispose.finance.service;
|
||||||
|
|
||||||
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.njzscloud.common.mp.support.PageParam;
|
import com.njzscloud.common.mp.support.PageParam;
|
||||||
import com.njzscloud.common.mp.support.PageResult;
|
import com.njzscloud.common.mp.support.PageResult;
|
||||||
import com.njzscloud.dispose.finance.mapper.BillMapper;
|
import com.njzscloud.dispose.finance.mapper.BillMapper;
|
||||||
import com.njzscloud.dispose.finance.pojo.entity.BillEntity;
|
import com.njzscloud.dispose.finance.pojo.entity.BillEntity;
|
||||||
|
import com.njzscloud.dispose.finance.pojo.result.BillStatisticsResult;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -58,4 +62,39 @@ public class BillService extends ServiceImpl<BillMapper, BillEntity> {
|
||||||
public PageResult<BillEntity> paging(PageParam pageParam, BillEntity billEntity) {
|
public PageResult<BillEntity> paging(PageParam pageParam, BillEntity billEntity) {
|
||||||
return PageResult.of(this.page(pageParam.toPage(), Wrappers.query(billEntity)));
|
return PageResult.of(this.page(pageParam.toPage(), Wrappers.query(billEntity)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成对账单
|
||||||
|
*
|
||||||
|
* @param startTime 统计开始时间
|
||||||
|
* @param endTime 统计结束时间
|
||||||
|
* @return 生成的账单数量
|
||||||
|
*/
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public int generateBill(LocalDateTime startTime, LocalDateTime endTime) {
|
||||||
|
log.info("开始生成对账单,时间范围:{} ~ {}", startTime, endTime);
|
||||||
|
|
||||||
|
// 统计对账单数据
|
||||||
|
List<BillStatisticsResult> statisticsList = baseMapper.statisticsBillData(startTime, endTime);
|
||||||
|
log.info("统计到 {} 条对账单数据", statisticsList.size());
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
for (BillStatisticsResult statistics : statisticsList) {
|
||||||
|
BillEntity billEntity = BeanUtil.copyProperties(statistics, BillEntity.class);
|
||||||
|
// 转换统计数据为对账单实体
|
||||||
|
billEntity.setBillPeriod(LocalDate.now());
|
||||||
|
billEntity.setStartTime(startTime.toLocalDate());
|
||||||
|
billEntity.setEndTime(endTime.toLocalDate());
|
||||||
|
// 设置备注
|
||||||
|
billEntity.setMemo("系统自动生成");
|
||||||
|
this.save(billEntity);
|
||||||
|
count++;
|
||||||
|
log.info("已生成对账单,客户ID:{},订单数:{},总金额:{}",
|
||||||
|
statistics.getCustomerId(), statistics.getOrderCount(), statistics.getTotalMoney());
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("对账单生成完成,共生成 {} 条对账单", count);
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.njzscloud.dispose.finance.mapper.BillMapper">
|
||||||
|
|
||||||
|
<resultMap id="BillStatisticsResultMap" type="com.njzscloud.dispose.finance.pojo.result.BillStatisticsResult" autoMapping="true">
|
||||||
|
<result column="user_id" property="userId"/>
|
||||||
|
<result column="customer_id" property="customerId"/>
|
||||||
|
<result column="org_id" property="orgId"/>
|
||||||
|
<result column="account_type" property="accountType"/>
|
||||||
|
<result column="order_count" property="orderCount"/>
|
||||||
|
<result column="car_count" property="carCount"/>
|
||||||
|
<result column="total_weight" property="totalWeight"/>
|
||||||
|
<result column="total_money" property="totalMoney"/>
|
||||||
|
<result column="discount_money" property="discountMoney"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<!-- 统计对账单数据 -->
|
||||||
|
<select id="statisticsBillData" resultMap="BillStatisticsResultMap">
|
||||||
|
SELECT
|
||||||
|
co.user_id AS user_id,
|
||||||
|
co.customer_id AS customer_id,
|
||||||
|
co.trans_org_id AS org_id,
|
||||||
|
fma.account_type AS account_type,
|
||||||
|
-- 订单数:统计已完成订单的数量
|
||||||
|
COUNT(DISTINCT copr.order_id) AS order_count,
|
||||||
|
-- 总车数:统计运输状态为已完成的运输信息条数
|
||||||
|
COUNT(DISTINCT cot.id) AS car_count,
|
||||||
|
-- 总质量:统计运输信息的净重,单位吨(千克除以1000转换为吨)
|
||||||
|
COALESCE(SUM(cot.settle_weight) / 1000, 0) AS total_weight,
|
||||||
|
-- 总金额:付费记录的总金额
|
||||||
|
COALESCE(SUM(copr.total_money), 0) AS total_money,
|
||||||
|
-- 优惠金额:付费记录的优惠金额
|
||||||
|
COALESCE(SUM(copr.discount_money), 0) AS discount_money
|
||||||
|
FROM cst_order_payment_record copr
|
||||||
|
-- 关联付费项表
|
||||||
|
INNER JOIN cst_order_expense_items coei ON coei.id = copr.expense_item_id
|
||||||
|
-- 关联订单表
|
||||||
|
INNER JOIN cst_order co ON co.id = copr.order_id
|
||||||
|
-- 关联运输信息表
|
||||||
|
LEFT JOIN cst_order_trans cot ON cot.id = copr.trans_id
|
||||||
|
-- 关联资金账户表获取账户类型
|
||||||
|
LEFT JOIN fin_money_account fma ON fma.customer_id = co.customer_id AND fma.deleted = 0
|
||||||
|
-- 筛选条件
|
||||||
|
WHERE co.deleted = 0
|
||||||
|
AND co.order_status = 'YiWanCheng'
|
||||||
|
AND co.finish_time BETWEEN #{startTime} AND #{endTime}
|
||||||
|
-- 运输状态为已完成才计入车数和净重
|
||||||
|
AND (cot.trans_status = 'YiWanCheng')
|
||||||
|
-- 按源付费项ID分组
|
||||||
|
GROUP BY coei.origin_expense_items_id
|
||||||
|
</select>
|
||||||
|
|
||||||
|
</mapper>
|
||||||
Loading…
Reference in New Issue