admin
2024-07-29 734dfe9eb0a2176103dce8245c69b1194574c68e
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package com.taoke.autopay.service.impl.agent;
 
import com.taoke.autopay.dao.KeyOrderMapper;
import com.taoke.autopay.dao.agent.ChannelAgentSettleDetailMapper;
import com.taoke.autopay.dao.agent.ChannelAgentSettleRecordMapper;
import com.taoke.autopay.dto.ChannelOrderStatistic;
import com.taoke.autopay.entity.KeyOrder;
import com.taoke.autopay.entity.OrderChannelEnum;
import com.taoke.autopay.entity.agent.ChannelAgent;
import com.taoke.autopay.entity.agent.ChannelAgentSettleDetail;
import com.taoke.autopay.entity.agent.ChannelAgentSettleRecord;
import com.taoke.autopay.exception.ChannelAgentSettleException;
import com.taoke.autopay.service.KeyOrderService;
import com.taoke.autopay.service.agent.ChannelAgentService;
import com.taoke.autopay.service.agent.ChannelAgentSettleService;
import com.taoke.autopay.service.agent.ChannelAgentSharingRatioService;
import com.taoke.autopay.utils.TimeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
@Service
public class ChannelAgentSettleServiceImpl implements ChannelAgentSettleService {
 
    @Resource
    private ChannelAgentSettleRecordMapper channelAgentSettleRecordMapper;
 
 
    @Resource
    private ChannelAgentSettleDetailMapper channelAgentSettleDetailMapper;
 
    @Resource
    private ChannelAgentSharingRatioService channelAgentSharingRatioService;
 
    @Resource
    private KeyOrderService keyOrderService;
 
    @Resource
    private ChannelAgentService channelAgentService;
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void startSettle(String day) throws ChannelAgentSettleException {
        // 查询可结算的代理用户
        Date startTime = new Date(TimeUtil.convertToTimeTemp(day, "yyyy-MM-dd"));
        Date endTime = new Date(startTime.getTime() + 1000 * 60 * 60 * 24L);
        KeyOrderMapper.DaoQuery orderQuery = new KeyOrderMapper.DaoQuery();
        orderQuery.minCreateTime = startTime;
        orderQuery.maxCreateTime = endTime;
        orderQuery.hasAgentId = true;
        orderQuery.hasPayTime = true;
        orderQuery.state = KeyOrder.STATE_PAY;
        orderQuery.count = (int) keyOrderService.countAgentId(orderQuery);
        if (orderQuery.count <= 0) {
            throw new ChannelAgentSettleException("无可结算的代理");
        }
        List<Long> agentList = keyOrderService.listAgentId(orderQuery);
        for (Long agentId : agentList) {
            // 统计订单
            orderQuery = new KeyOrderMapper.DaoQuery();
            orderQuery.oMinCreateTime = startTime;
            orderQuery.oMaxCreateTime = endTime;
            orderQuery.agentId = agentId;
            orderQuery.hasPayTime = true;
            orderQuery.state = KeyOrder.STATE_PAY;
            List<ChannelOrderStatistic> list = keyOrderService.statisticChannelOrders(agentId, startTime, endTime);
            List<ChannelAgentSettleDetail> detailList = new ArrayList<>();
            Map<OrderChannelEnum, BigDecimal> shareMoneyMap = channelAgentSharingRatioService.getShareMoneyMap(agentId);
            for (ChannelOrderStatistic statistic : list) {
                OrderChannelEnum orderChannel = null;
                for (OrderChannelEnum channel : OrderChannelEnum.values()) {
                    if (channel.getKey().equalsIgnoreCase(statistic.getOrderChannel())) {
                        orderChannel = channel;
                        break;
                    }
                }
                if (orderChannel != null) {
                    if (shareMoneyMap.containsKey(orderChannel)) {
                        BigDecimal money = shareMoneyMap.get(orderChannel).multiply(new BigDecimal(statistic.getCount()));
                        // 查询是否已经结算
                        detailList.add(ChannelAgentSettleDetail.builder()
                                .orderChannel(orderChannel)
                                .payOrderCount(statistic.getCount())
                                .payMoney(statistic.getMoney())
                                .money(money)
                                .build());
                    }
                }
            }
            if (detailList.isEmpty()) {
                continue;
            }
 
            BigDecimal totalMoney = new BigDecimal(0);
            for (ChannelAgentSettleDetail detail : detailList) {
                totalMoney = totalMoney.add(detail.getMoney());
            }
            ChannelAgent agent = channelAgentService.selectByPrimaryKey(agentId);
 
            settle(ChannelAgentSettleRecord.builder()
                    .agentId(agentId)
                    .settleDay(day)
                    .settleMoney(totalMoney)
                    .alipayAccount(agent.getAlipayAccount())
                    .alipayName(agent.getAlipayName())
                    .detailList(detailList)
                    .build());
        }
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void settle(ChannelAgentSettleRecord record) {
        // 查询是否结算
        ChannelAgentSettleRecordMapper.DaoQuery daoQuery = new ChannelAgentSettleRecordMapper.DaoQuery();
        daoQuery.agentId = record.getAgentId();
        daoQuery.settleDay = record.getSettleDay();
        if (channelAgentSettleRecordMapper.count(daoQuery) > 0) {
            // 已经结算
            return;
        }
        if (record.getCreateTime() == null) {
            record.setCreateTime(new Date());
        }
        if (record.getStatus() == null) {
            record.setStatus(ChannelAgentSettleRecord.STATUS_NOT_SETTLE);
            record.setStatusDesc("已结算,待确认");
        }
        channelAgentSettleRecordMapper.insertSelective(record);
 
        for (ChannelAgentSettleDetail detail : record.getDetailList()) {
            detail.setSettleId(record.getId());
            if (detail.getCreateTime() == null) {
                detail.setCreateTime(new Date());
            }
            channelAgentSettleDetailMapper.insertSelective(detail);
        }
    }
 
    @Override
    public List<ChannelAgentSettleRecord> list(ChannelAgentSettleRecordMapper.DaoQuery query) {
        return channelAgentSettleRecordMapper.list(query);
    }
 
    @Override
    public long count(ChannelAgentSettleRecordMapper.DaoQuery query) {
        return channelAgentSettleRecordMapper.count(query);
    }
 
    @Override
    public void delete(Long id) {
        ChannelAgentSettleRecord record = channelAgentSettleRecordMapper.selectByPrimaryKey(id);
        if (record != null) {
            channelAgentSettleRecordMapper.deleteByPrimaryKey(id);
            for (ChannelAgentSettleDetail d : record.getDetailList()) {
                channelAgentSettleDetailMapper.deleteByPrimaryKey(d.getId());
            }
        }
 
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void processWithdraw(List<Long> ids, boolean pass) throws ChannelAgentSettleException {
        for (Long id : ids) {
            ChannelAgentSettleRecord old = channelAgentSettleRecordMapper.selectByPrimaryKeyForUpdate(id);
            if (old.getStatus() != ChannelAgentSettleRecord.STATUS_WITHDRAWING) {
                throw new ChannelAgentSettleException("提现已处理");
            }
 
            channelAgentSettleRecordMapper.updateByPrimaryKeySelective(ChannelAgentSettleRecord.builder().id(id)
                    .status(pass ? ChannelAgentSettleRecord.STATUS_WITHDRAW_SUCCESS : ChannelAgentSettleRecord.STATUS_WITHDRAW_REJECTED)
                    .withDrawProcessTime(new Date())
                    .updateTime(new Date())
                    .payTime(pass ? new Date() : null).build());
        }
 
 
    }
 
    @Override
    public void actualSettle(Long id, BigDecimal money) throws ChannelAgentSettleException {
        ChannelAgentSettleRecord old = channelAgentSettleRecordMapper.selectByPrimaryKeyForUpdate(id);
        if (old == null) {
            throw new ChannelAgentSettleException("结算ID为空");
        }
        if (old.getStatus() != ChannelAgentSettleRecord.STATUS_NOT_SETTLE) {
            throw new ChannelAgentSettleException("已经结算");
        }
 
        channelAgentSettleRecordMapper.updateByPrimaryKeySelective(ChannelAgentSettleRecord.builder()
                .id(id)
                .actualSettleMoney(money)
                .settleTime(new Date())
                .status(ChannelAgentSettleRecord.STATUS_SETTLED)
                .updateTime(new Date())
                .build());
 
 
    }
 
    @Override
    public void applyWithdraw(Long id) throws ChannelAgentSettleException {
 
        ChannelAgentSettleRecord old = channelAgentSettleRecordMapper.selectByPrimaryKeyForUpdate(id);
        if (old == null) {
            throw new ChannelAgentSettleException("结算ID为空");
        }
        if (old.getStatus() != ChannelAgentSettleRecord.STATUS_SETTLED) {
            throw new ChannelAgentSettleException("处于不可提现状态");
        }
        channelAgentSettleRecordMapper.updateByPrimaryKeySelective(ChannelAgentSettleRecord.builder()
                .id(id)
                .withDrawApplyTime(new Date())
                .settleTime(new Date())
                .status(ChannelAgentSettleRecord.STATUS_WITHDRAWING)
                .updateTime(new Date())
                .build());
    }
}