订单按天计算详情

This commit is contained in:
xiezhijun
2021-03-26 19:04:44 +08:00
parent 275f8c2437
commit 74cfe232a8
13 changed files with 472 additions and 105 deletions

View File

@ -3,6 +3,7 @@ package com.stdiet.custom.domain;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.stdiet.common.annotation.Excel;
import com.stdiet.common.core.domain.BaseEntity;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDate;
@ -12,22 +13,24 @@ import java.util.Map;
/**
* 每个订单的提成详情
* */
@Data
public class SysOrderCommisionDayDetail extends BaseEntity {
private static final long serialVersionUID = 1L;
/**订单成交时间*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime orderTime;
/**客户姓名*/
private String name;
/**服务结束时间*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Excel(name = "成交时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate serverStartDate;
/**服务结束时间*/
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate serverEndDate;
/**服务月数*/
@ -39,12 +42,18 @@ public class SysOrderCommisionDayDetail extends BaseEntity {
/**订单金额*/
private BigDecimal orderAmount;
/**订单总提成**/
private BigDecimal orderCommission;
/**服务天数*/
private Integer serverDay;
/**每天金额*/
private BigDecimal dayMoney;
/** 暂停总天数 **/
private int pauseTotalDay;
/**每年每月暂停天数*/
private Map<String, Integer> everyYearMonthPauseDay;
@ -54,99 +63,12 @@ public class SysOrderCommisionDayDetail extends BaseEntity {
/**每年每月对应金额*/
private Map<String, BigDecimal> everyYearMonthServerMoney;
public LocalDateTime getOrderTime() {
return orderTime;
}
/**每年每月对应提成*/
private Map<String, BigDecimal> everyYearMonthServerCommission;
public void setOrderTime(LocalDateTime orderTime) {
this.orderTime = orderTime;
}
//该笔订单成交的当月的总成交额,用于确定提成比例
private BigDecimal monthOrderTotalAmount;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDate getServerStartDate() {
return serverStartDate;
}
public void setServerStartDate(LocalDate serverStartDate) {
this.serverStartDate = serverStartDate;
}
public LocalDate getServerEndDate() {
return serverEndDate;
}
public void setServerEndDate(LocalDate serverEndDate) {
this.serverEndDate = serverEndDate;
}
public Integer getServerMonth() {
return serverMonth;
}
public void setServerMonth(Integer serverMonth) {
this.serverMonth = serverMonth;
}
public Integer getGiveDay() {
return giveDay;
}
public void setGiveDay(Integer giveDay) {
this.giveDay = giveDay;
}
public BigDecimal getOrderAmount() {
return orderAmount;
}
public void setOrderAmount(BigDecimal orderAmount) {
this.orderAmount = orderAmount;
}
public Integer getServerDay() {
return serverDay;
}
public void setServerDay(Integer serverDay) {
this.serverDay = serverDay;
}
public BigDecimal getDayMoney() {
return dayMoney;
}
public void setDayMoney(BigDecimal dayMoney) {
this.dayMoney = dayMoney;
}
public Map<String, Integer> getEveryYearMonthPauseDay() {
return everyYearMonthPauseDay;
}
public void setEveryYearMonthPauseDay(Map<String, Integer> everyYearMonthPauseDay) {
this.everyYearMonthPauseDay = everyYearMonthPauseDay;
}
public Map<String, Integer> getEveryYearMonthServerDay() {
return everyYearMonthServerDay;
}
public void setEveryYearMonthServerDay(Map<String, Integer> everyYearMonthServerDay) {
this.everyYearMonthServerDay = everyYearMonthServerDay;
}
public Map<String, BigDecimal> getEveryYearMonthServerMoney() {
return everyYearMonthServerMoney;
}
public void setEveryYearMonthServerMoney(Map<String, BigDecimal> everyYearMonthServerMoney) {
this.everyYearMonthServerMoney = everyYearMonthServerMoney;
}
//该笔订单对应提成比例
private Float commissionRate;
}

View File

@ -0,0 +1,13 @@
package com.stdiet.custom.dto.response;
import lombok.Data;
import java.math.BigDecimal;
@Data
public class EveryMonthTotalAmount {
private String yearMonth;
private BigDecimal totalAmount;
}

View File

@ -2,12 +2,14 @@ package com.stdiet.custom.mapper;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.stdiet.custom.domain.SysCommision;
import com.stdiet.custom.domain.SysCommissionDayDetail;
import com.stdiet.custom.domain.SysCustomer;
import com.stdiet.custom.domain.SysOrder;
import com.stdiet.custom.dto.request.SysOrderCommision;
import com.stdiet.custom.dto.response.EveryMonthTotalAmount;
import org.apache.ibatis.annotations.Param;
/**
@ -85,4 +87,17 @@ public interface SysOrderMapper
* @return
*/
int getOrderCountByCustomer(SysCustomer sysCustomer);
/**
* 查询每年每月的总金额
* @return
*/
List<EveryMonthTotalAmount> getTotalAmountByUserId(SysCommision sysCommision);
/**
* 获取订单数量(按天提成计算)
* @param sysCommision
* @return
*/
int selectSimpleOrderMessageCount(SysCommision sysCommision);
}

View File

@ -1,5 +1,6 @@
package com.stdiet.custom.service;
import com.stdiet.common.core.domain.AjaxResult;
import com.stdiet.custom.domain.SysCommision;
import com.stdiet.custom.domain.SysCommissionDayDetail;
import com.stdiet.custom.domain.SysOrder;
@ -33,4 +34,9 @@ public interface ISysCommissionDayService {
*/
LocalDate getServerEndDate(SysOrder sysOrder);
/**
* 计算订单提成详情
*/
AjaxResult calculateOrderCommissionDetail(SysCommision sysCommision);
}

View File

@ -3,9 +3,12 @@ package com.stdiet.custom.service;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.stdiet.custom.domain.SysCommision;
import com.stdiet.custom.domain.SysCustomer;
import com.stdiet.custom.domain.SysOrder;
import com.stdiet.custom.dto.response.EveryMonthTotalAmount;
/**
* 销售订单Service接口
@ -84,4 +87,17 @@ public interface ISysOrderService
* @return
*/
int getOrderCountByCustomer(SysCustomer sysCustomer);
/**
* 查询每年每月的总金额
* @return
*/
List<EveryMonthTotalAmount> getTotalAmountByUserId(SysCommision sysCommision);
/**
* 获取订单数量(按天提成计算)
* @param sysCommision
* @return
*/
int selectSimpleOrderMessageCount(SysCommision sysCommision);
}

View File

@ -1,9 +1,11 @@
package com.stdiet.custom.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.stdiet.common.core.domain.AjaxResult;
import com.stdiet.common.utils.DateUtils;
import com.stdiet.custom.domain.*;
import com.stdiet.custom.dto.request.SysOrderCommision;
import com.stdiet.custom.dto.response.EveryMonthTotalAmount;
import com.stdiet.custom.mapper.SysCommisionMapper;
import com.stdiet.custom.mapper.SysOrderMapper;
import com.stdiet.custom.mapper.SysOrderPauseMapper;
@ -103,6 +105,63 @@ public class SysCommissionDayServiceImpl implements ISysCommissionDayService {
return serverEndDate;
}
/**
* 计算订单提成详情
* @param sysCommision
*/
@Override
public AjaxResult calculateOrderCommissionDetail(SysCommision sysCommision){
AjaxResult result = AjaxResult.error("参数错误");
if(sysCommision.getUserId() == null){
return result;
}
//分页查询2021年1月份之后所有订单
Map<Long, List<SysOrderCommisionDayDetail>> orderUserMap = getOrderByList(sysCommision);
if(orderUserMap == null || !orderUserMap.containsKey(sysCommision.getUserId())){
return result;
}
//查询用户
List<SysCommision> list = sysCommisionMapper.getAfterSaleAndNutri(sysCommision);
if(list == null || list.size() == 0){
return result;
}
Long userId = list.get(0).getUserId();
Long postId = list.get(0).getPostId();
//获取每个月的成交总额度
List<EveryMonthTotalAmount> everyMonthTotalAmountList = sysOrderMapper.getTotalAmountByUserId(sysCommision);
if(everyMonthTotalAmountList == null || everyMonthTotalAmountList.size() == 0){
return result;
}
Map<String, BigDecimal> everyMonthTotalAmountMap = new TreeMap<>(new MyComparator());
for (EveryMonthTotalAmount everyMonthTotalAmount : everyMonthTotalAmountList) {
everyMonthTotalAmountMap.put(everyMonthTotalAmount.getYearMonth(), everyMonthTotalAmount.getTotalAmount());
}
//获取每个月的提成比例以及计算提成
Map<String, Float> rateMap = getRateByAmount(userId, postId, everyMonthTotalAmountMap);
BigDecimal totalCommission = BigDecimal.valueOf(0);
//根据用户ID获取对应订单列表
List<SysOrderCommisionDayDetail> orderDetailList = orderUserMap.get(userId);
for(SysOrderCommisionDayDetail sysOrderCommisionDayDetail : orderDetailList){
String yearMonth = sysOrderCommisionDayDetail.getOrderTime().getYear() + "" + sysOrderCommisionDayDetail.getOrderTime().getMonth().getValue();
sysOrderCommisionDayDetail.setMonthOrderTotalAmount(everyMonthTotalAmountMap.get(yearMonth));
sysOrderCommisionDayDetail.setCommissionRate(rateMap.get(yearMonth));
//计算该笔订单总提成
sysOrderCommisionDayDetail.setOrderCommission(getMoney(sysOrderCommisionDayDetail.getOrderAmount().doubleValue() * sysOrderCommisionDayDetail.getCommissionRate() / 100D));
Map<String, BigDecimal> everyYearMonthServerCommission = new TreeMap<>(new MyComparator());
for (String everyMonth : sysOrderCommisionDayDetail.getEveryYearMonthServerMoney().keySet()) {
everyYearMonthServerCommission.put(everyMonth, getMoney(sysOrderCommisionDayDetail.getEveryYearMonthServerMoney().get(everyMonth).doubleValue() * sysOrderCommisionDayDetail.getCommissionRate() / 100D));
}
sysOrderCommisionDayDetail.setEveryYearMonthServerCommission(everyYearMonthServerCommission);
totalCommission = totalCommission.add(sysOrderCommisionDayDetail.getOrderCommission());
}
result = AjaxResult.success();
int total = sysOrderMapper.selectSimpleOrderMessageCount(sysCommision);
result.put("total", total);
result.put("list", orderDetailList);
result.put("totalCommission", totalCommission);
return result;
}
/**
* 根据用户ID统计出该用户在该月所有订单的服务数量、服务总天数、服务订单总额、暂停总天数
@ -135,14 +194,14 @@ public class SysCommissionDayServiceImpl implements ISysCommissionDayService {
for (String ym : commissionMonthSet) {
BigDecimal ym_mession = new BigDecimal(0);
if(orderAmount.containsKey(ym)){
totalCommissionAmount = totalCommissionAmount.add(getMoney(orderAmount.get(ym).doubleValue() * rateMap.get(ym) / 100D, 1));
totalCommissionAmount = totalCommissionAmount.add(getMoney(orderAmount.get(ym).doubleValue() * rateMap.get(ym) / 100D));
}
for (SysOrderCommisionDayDetail sysOrderCommisionDayDetail : orderDetailList) {
Map<String, BigDecimal> everyYearMonthServerMoney = sysOrderCommisionDayDetail.getEveryYearMonthServerMoney();
if(everyYearMonthServerMoney.containsKey(ym)){
String orderYearMonth = sysOrderCommisionDayDetail.getOrderTime().getYear()+""+sysOrderCommisionDayDetail.getOrderTime().getMonth().getValue();
BigDecimal m = (everyYearMonthServerMoney.get(ym) == null) ? new BigDecimal(0) : everyYearMonthServerMoney.get(ym);
ym_mession = ym_mession.add(getMoney(m.doubleValue() * rateMap.get(orderYearMonth) / 100D, 1));
ym_mession = ym_mession.add(getMoney(m.doubleValue() * rateMap.get(orderYearMonth) / 100D));
}
}
if(isSendCommissionByYearMonth(ym)){
@ -288,7 +347,7 @@ public class SysCommissionDayServiceImpl implements ISysCommissionDayService {
//服务总天数
int serverDay = getTotalByMap(everyYearMonthServerDay);
//每天对应金额
BigDecimal dayMoney = getMoney(orderAmount.doubleValue()/serverDay, 1);
BigDecimal dayMoney = getMoney(orderAmount.doubleValue()/serverDay);
//每年每月对于金额
Map<String, BigDecimal> everyYearMonthServerMoney = getEveryMonthServerMoney(everyYearMonthServerDay, orderAmount, dayMoney);
@ -299,12 +358,14 @@ public class SysCommissionDayServiceImpl implements ISysCommissionDayService {
sysOrderCommisionDayDetail.setServerEndDate(serverEndDate);
sysOrderCommisionDayDetail.setServerMonth(serverMonth);
sysOrderCommisionDayDetail.setGiveDay(giveDay);
sysOrderCommisionDayDetail.setPauseTotalDay(pauseTotalDay);
sysOrderCommisionDayDetail.setOrderAmount(orderAmount);
sysOrderCommisionDayDetail.setServerDay(serverDay);
sysOrderCommisionDayDetail.setDayMoney(dayMoney);
sysOrderCommisionDayDetail.setEveryYearMonthPauseDay(everyYearMonthPauseDay);
sysOrderCommisionDayDetail.setEveryYearMonthServerDay(everyYearMonthServerDay);
sysOrderCommisionDayDetail.setEveryYearMonthServerMoney(everyYearMonthServerMoney);
return sysOrderCommisionDayDetail;
}
@ -385,11 +446,11 @@ public class SysCommissionDayServiceImpl implements ISysCommissionDayService {
double totalMoney = 0.0;
for(String key : keySet){
if(i++ != keySet.size()){
everyMonthServerMoney.put(key, getMoney(everyMonthServerDay.get(key) * dayMoney.doubleValue(), 1));
everyMonthServerMoney.put(key, getMoney(everyMonthServerDay.get(key) * dayMoney.doubleValue()));
totalMoney += everyMonthServerMoney.get(key).doubleValue();
}else{
//由于小数点只保留一位,最后一个月的金额等于总额减去前几个月金额,避免总数不一致
everyMonthServerMoney.put(key, getMoney(orderMoney.doubleValue() - totalMoney, 1));
everyMonthServerMoney.put(key, getMoney(orderMoney.doubleValue() - totalMoney));
}
}
return everyMonthServerMoney;
@ -436,9 +497,9 @@ public class SysCommissionDayServiceImpl implements ISysCommissionDayService {
}
/**
* double转为BigDecimal保留位小数,向下舍去
* double转为BigDecimal保留2位小数,四舍五入
* */
public BigDecimal getMoney(Double money, int n){
public BigDecimal getMoney(Double money){
return new BigDecimal(money.toString()).setScale(2, BigDecimal.ROUND_HALF_UP);
}

View File

@ -4,8 +4,10 @@ import com.stdiet.common.annotation.Excel;
import com.stdiet.common.utils.DateUtils;
import com.stdiet.common.utils.SecurityUtils;
import com.stdiet.common.utils.StringUtils;
import com.stdiet.custom.domain.SysCommision;
import com.stdiet.custom.domain.SysCustomer;
import com.stdiet.custom.domain.SysOrder;
import com.stdiet.custom.dto.response.EveryMonthTotalAmount;
import com.stdiet.custom.mapper.SysOrderMapper;
import com.stdiet.custom.service.ISysCommissionDayService;
import com.stdiet.custom.service.ISysOrderPauseService;
@ -22,6 +24,7 @@ import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 销售订单Service业务层处理
@ -349,7 +352,26 @@ public class SysOrderServiceImpl implements ISysOrderService {
* @param sysCustomer
* @return
*/
@Override
public int getOrderCountByCustomer(SysCustomer sysCustomer){
return sysOrderMapper.getOrderCountByCustomer(sysCustomer);
}
/**
* 查询每年每月的总金额
* @return
*/
@Override
public List<EveryMonthTotalAmount> getTotalAmountByUserId(SysCommision sysCommision){
return sysOrderMapper.getTotalAmountByUserId(sysCommision);
}
/**
* 获取订单数量(按天提成计算)
* @param sysCommision
* @return
*/
public int selectSimpleOrderMessageCount(SysCommision sysCommision){
return sysOrderMapper.selectSimpleOrderMessageCount(sysCommision);
}
}

View File

@ -404,7 +404,7 @@
<if test="reviewStatus != null and reviewStatus != ''">
and review_status = #{reviewStatus}
</if>
<if test="userId != null">
<if test="userId != null"><!-- 限于营养师、售后不可能同一个的情况下 -->
and (su_sale.user_id = #{userId} or su_nutritionist.user_id = #{userId})
</if>
<if test="endTime != null and endTime != ''">
@ -413,6 +413,48 @@
order by o.order_time desc
</select>
<!-- 查询2021年开始的已审核的订单信息的总条数用于计算提成,请勿随意改动) -->
<select id="selectSimpleOrderMessageCount" resultType="int" parameterType="SysCommision">
select count(o.order_id) as orderCount
from sys_order o
left join sys_user su_sale on su_sale.user_id = o.after_sale_id and su_sale.del_flag = 0
left join sys_user su_nutritionist on su_nutritionist.user_id = o.nutritionist_id and su_nutritionist.del_flag = 0
where o.order_time >= '2021-01-01' and o.del_flag = 0
<if test="reviewStatus != null and reviewStatus != ''">
and review_status = #{reviewStatus}
</if>
<if test="userId != null"><!-- 限于营养师、售后不可能同一个的情况下 -->
and (su_sale.user_id = #{userId} or su_nutritionist.user_id = #{userId})
</if>
<if test="endTime != null and endTime != ''">
AND DATE_FORMAT(o.order_time,'%Y-%m-%d') &lt;= #{endTime}
</if>
order by o.order_time desc
</select>
<!-- 查询售后、营养师的每个月服务总金额 -->
<select id="getTotalAmountByUserId" parameterType="SysCommision" resultType="com.stdiet.custom.dto.response.EveryMonthTotalAmount">
SELECT s.yearMonth,SUM(s.amount) AS totalAmount FROM
(
SELECT CONCAT(YEAR(o.order_time),'',MONTH(o.order_time)) AS yearMonth,o.amount
FROM sys_order o
LEFT JOIN sys_user su_sale ON su_sale.user_id = o.after_sale_id AND su_sale.del_flag = 0
LEFT JOIN sys_user su_nutritionist ON su_nutritionist.user_id = o.nutritionist_id AND su_nutritionist.del_flag = 0
where o.order_time >= '2021-01-01' and o.del_flag = 0
<if test="reviewStatus != null and reviewStatus != ''">
and o.review_status = #{reviewStatus}
</if>
<if test="userId != null"><!-- 限于营养师、售后不可能同一个的情况下 -->
and (su_sale.user_id = #{userId} or su_nutritionist.user_id = #{userId})
</if>
<if test="endTime != null and endTime != ''">
AND DATE_FORMAT(o.order_time,'%Y-%m-%d') &lt;= #{endTime}
</if>
ORDER BY o.order_time ASC
) s
GROUP BY s.yearMonth
</select>
<!-- 根据客户信息查询客户的订单列表数量 -->
<select id="getOrderCountByCustomer" parameterType="SysCustomer" resultType="int">
SELECT count(so.order_id) FROM sys_order so LEFT JOIN sys_customer sc ON sc.id = so.cus_id AND sc.del_flag = 0