puzhibing
2023-05-31 0a6f905b50a88993da05b9b3114394fb91dbc7d0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.stylefeng.guns.modular.system.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.controller.resp.PerformanceTableResp;
import com.stylefeng.guns.modular.system.controller.resp.RevenueExpenditureResp;
import com.stylefeng.guns.modular.system.dao.*;
import com.stylefeng.guns.modular.system.enums.CashWithdrawalTypeEnum;
import com.stylefeng.guns.modular.system.enums.RevenueTypeEnum;
import com.stylefeng.guns.modular.system.enums.SystemConfigTypeEnum;
import com.stylefeng.guns.modular.system.enums.UserTypeEnum;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.ITEvaluateService;
import com.stylefeng.guns.modular.system.service.ITOrderService;
import com.stylefeng.guns.modular.system.service.ITRevenueService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.system.service.ITSystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * <p>
 * 收入记录 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2023-03-13
 */
@Service
public class TRevenueServiceImpl extends ServiceImpl<TRevenueMapper, TRevenue> implements ITRevenueService {
 
    @Autowired
    private TRevenueMapper tRevenueMapper;
    @Autowired
    private TAppUserMapper tAppUserMapper;
    @Autowired
    private ITSystemConfigService tSystemConfigService;
    @Autowired
    private ITOrderService tOrderService;
    @Autowired
    private ITEvaluateService tEvaluateService;
 
    @Override
    public List<RevenueExpenditureResp> getPageList(String code, Integer businessType, Integer payType, String driverName, String businessTime) {
        String startTime = null;
        String endTime = null;
        // 开始,结束时间
        if(StringUtils.hasLength(businessTime)){
            String[] split = businessTime.split(" - ");
            startTime = split[0] + " 00:00:00";
            endTime = split[1] + " 23:59:59";
        }
        Integer roleType = Objects.requireNonNull(ShiroKit.getUser()).getRoleType();
        Integer objectId = Objects.requireNonNull(ShiroKit.getUser()).getObjectId();
        List<RevenueExpenditureResp> pageList = tRevenueMapper.getPageList(startTime, endTime, code, businessType, payType, driverName, roleType, objectId);
        // 查询抽佣规则
        TSystemConfig tSystemConfig = tSystemConfigService.selectOne(new EntityWrapper<TSystemConfig>()
                .eq("type", SystemConfigTypeEnum.EXTRACTION_RULE.getCode()));
        for (RevenueExpenditureResp revenueExpenditureResp : pageList) {
            BigDecimal commissionAmount = BigDecimal.ZERO;
            if(1 == revenueExpenditureResp.getBusinessType()){
                // 计算佣金提成
                // 1.该订单的代驾用户是否为该司机邀请
                Integer count = tAppUserMapper.selectCount(new EntityWrapper<TAppUser>()
                        .eq("inviterType", UserTypeEnum.DRIVER.getCode())
                        .eq("inviterId", revenueExpenditureResp.getDriverId()));
                if(count>0){
                    commissionAmount = commissionAmount.add(JSONObject.parseObject(tSystemConfig.getContent()).getBigDecimal("num1"));
                }
                // 2.查询跟该订单相关的分佣收入
                List<TRevenue> tRevenues = tRevenueMapper.selectList(new EntityWrapper<TRevenue>()
                        .eq("type", RevenueTypeEnum.COMMISSION_INCOME.getCode())
                        .eq("orderId", revenueExpenditureResp.getOrderId()));
                Optional<BigDecimal> reduce = tRevenues.stream().map(TRevenue::getAmount).reduce(BigDecimal::add);
                if(reduce.isPresent()){
                    commissionAmount = commissionAmount.add(reduce.get());
                }
                revenueExpenditureResp.setCommissionAmount(commissionAmount);
            }
        }
        return pageList;
    }
 
    @Override
    public void commissionDetail(String code, Model model) {
        RevenueExpenditureResp revenueExpenditureResp = tRevenueMapper.commissionOrBalanceDetail(code, CashWithdrawalTypeEnum.COMMISSION_WITHDRAWAL.getCode());
        this.packageModel(revenueExpenditureResp,model);
    }
 
    @Override
    public void balanceDetail(String code, Model model) {
        RevenueExpenditureResp revenueExpenditureResp = tRevenueMapper.commissionOrBalanceDetail(code, CashWithdrawalTypeEnum.BALANCE_WITHDRAWAL.getCode());
        this.packageModel(revenueExpenditureResp,model);
    }
 
    @Override
    public void packageModel(RevenueExpenditureResp revenueExpenditureResp,Model model) {
        model.addAttribute("id",revenueExpenditureResp.getId());
        model.addAttribute("code",revenueExpenditureResp.getCode());
        model.addAttribute("businessTime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(revenueExpenditureResp.getBusinessTime()));
        model.addAttribute("businessType",revenueExpenditureResp.getBusinessType());
        model.addAttribute("driverName",revenueExpenditureResp.getDriverName());
        model.addAttribute("driverPhone",revenueExpenditureResp.getDriverPhone());
        model.addAttribute("amount",revenueExpenditureResp.getAmount());
        model.addAttribute("accountBalance",revenueExpenditureResp.getAccountBalance());
        model.addAttribute("state",revenueExpenditureResp.getState());
    }
 
    @Override
    public void orderDetail(String code, Model model) {
        // 查询订单
        TOrder tOrder = tOrderService.selectOne(new EntityWrapper<TOrder>().eq("code", code)
                .last("LIMIT 1"));
        tOrderService.orderDetail(tOrder.getId(),model);
        // 查询评价
        TEvaluate tEvaluate = tEvaluateService.selectOne(new EntityWrapper<TEvaluate>().eq("orderId", tOrder.getId()));
        if(Objects.nonNull(tEvaluate)){
            model.addAttribute("evaluateScore",tEvaluate.getScore());
            model.addAttribute("evaluateContent",tEvaluate.getEvaluate());
        }else {
            model.addAttribute("evaluateScore","");
            model.addAttribute("evaluateContent","");
        }
    }
 
    @Override
    public void getDataStatisticsIncomeOrCommission(Integer agentId, String monthDate, Integer type, Model model, Map<String, Object> map) {
        List<PerformanceTableResp> list = new ArrayList<>();
        if(2 == type){
            list = tRevenueMapper.getDataStatisticsIncomeOrCommission(agentId,1,monthDate);
        }
        if (3 == type){
            list = tRevenueMapper.getDataStatisticsIncomeOrCommission(agentId,2,monthDate);
        }
        model.addAttribute("performanceResp",list);
        map.put("performanceResp",list);
    }
 
    @Override
    public void getDataStatisticsIncomeOrCommissionByIds(List<Integer> ids, String monthDate, Integer type, Model model, Map<String, Object> map) {
        List<PerformanceTableResp> list = new ArrayList<>();
        if(2 == type){
            list = tRevenueMapper.getDataStatisticsIncomeOrCommissionByIds(ids,1,monthDate);
        }
        if (3 == type){
            list = tRevenueMapper.getDataStatisticsIncomeOrCommissionByIds(ids,2,monthDate);
        }
        model.addAttribute("performanceResp",list);
        map.put("performanceResp",list);
    }
}