admin
2024-08-04 bc56870059cca013649077af0e53891cba8dbfd1
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
package com.taoke.autopay.service.impl.agent;
 
import com.taoke.autopay.dao.agent.ChannelAgentSharingRatioMapper;
import com.taoke.autopay.entity.OrderChannelEnum;
import com.taoke.autopay.entity.SystemConfigKeyEnum;
import com.taoke.autopay.entity.agent.ChannelAgentSharingRatio;
import com.taoke.autopay.service.SystemConfigService;
import com.taoke.autopay.service.agent.ChannelAgentSharingRatioService;
import com.taoke.autopay.utils.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
 
/**
 * @author hxh
 * @title: ChannelAgentSharingRatioServiceImpl
 * @description: TODO
 * @date 2024/7/26 0:38
 */
@Service
public class ChannelAgentSharingRatioServiceImpl implements ChannelAgentSharingRatioService {
 
    @Resource
    private SystemConfigService systemConfigService;
 
    @Resource
    private ChannelAgentSharingRatioMapper channelAgentSharingRatioMapper;
 
    @Override
    public void setShareRatio(ChannelAgentSharingRatio shareRatio) {
        ChannelAgentSharingRatio old = getShareRatio(shareRatio.getAgengId(), shareRatio.getOrderChannel());
        if (old != null) {
            if (shareRatio.getUpdateTime() != null) {
                shareRatio.setUpdateTime(new Date());
            }
            shareRatio.setId(old.getId());
            channelAgentSharingRatioMapper.updateByPrimaryKeySelective(shareRatio);
        } else {
            if (shareRatio.getCreateTime() == null) {
                shareRatio.setCreateTime(new Date());
            }
            channelAgentSharingRatioMapper.insertSelective(shareRatio);
        }
 
 
    }
 
    @Override
    public ChannelAgentSharingRatio getShareRatio(Long agengId, OrderChannelEnum orderChannel) {
        ChannelAgentSharingRatioMapper.DaoQuery daoQuery = new ChannelAgentSharingRatioMapper.DaoQuery();
        daoQuery.agengId = agengId;
        daoQuery.orderChannel = orderChannel;
        daoQuery.count = 1;
        List<ChannelAgentSharingRatio> list = channelAgentSharingRatioMapper.list(daoQuery);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }
 
    @Override
    public List<ChannelAgentSharingRatio> getShareRatios(Long agengId) {
        ChannelAgentSharingRatioMapper.DaoQuery daoQuery = new ChannelAgentSharingRatioMapper.DaoQuery();
        daoQuery.agengId = agengId;
        daoQuery.count = 1000;
        return channelAgentSharingRatioMapper.list(daoQuery);
    }
 
    @Override
    public Map<OrderChannelEnum, BigDecimal> getDefaultShareRatio() {
        String value = systemConfigService.getValueCache(SystemConfigKeyEnum.AGENT_ORDER_CHANNEL_SHARE_RATIO);
        if (StringUtil.isNullOrEmpty(value)) {
            return new HashMap<>();
        }
        Map<OrderChannelEnum, BigDecimal> map = new HashMap<>();
        JSONObject data = JSONObject.fromObject(value);
        for (OrderChannelEnum channel : OrderChannelEnum.values()) {
            if (data.optString(channel.name()) != null) {
                map.put(channel, new BigDecimal(data.optString(channel.name())));
            }
        }
        return map;
    }
 
    @Override
    public Map<OrderChannelEnum, BigDecimal> getShareMoneyMap(Long agengId) {
        List<ChannelAgentSharingRatio> list = getShareRatios(agengId);
        Map<OrderChannelEnum, ChannelAgentSharingRatio> listMap = new HashMap<>();
        for (ChannelAgentSharingRatio c : list) {
            listMap.put(c.getOrderChannel(), c);
        }
        Map<OrderChannelEnum, BigDecimal> defaultMap = getDefaultShareRatio();
        Map<OrderChannelEnum, BigDecimal> map = new HashMap<>();
        for (OrderChannelEnum channel : OrderChannelEnum.values()) {
            if (listMap.containsKey(channel)) {
                map.put(channel, listMap.get(channel).getShareValue());
            } else {
                map.put(channel, defaultMap.getOrDefault(channel, null));
            }
        }
        return map;
    }
 
    @Override
    public void setDefaultShareRatio(Map<OrderChannelEnum, BigDecimal> defaultShareRatio) {
        JSONObject data = new JSONObject();
        for (OrderChannelEnum key : defaultShareRatio.keySet()) {
            if (defaultShareRatio.get(key) != null) {
                data.put(key.name(), defaultShareRatio.get(key));
            }
        }
        systemConfigService.setValue(SystemConfigKeyEnum.AGENT_ORDER_CHANNEL_SHARE_RATIO,data.toString() );
    }
}