admin
2024-09-27 17caebabf7a6a529b7039c71e21e5a324e31ea20
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.controller.admin;
 
import com.alibaba.excel.EasyExcel;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.taoke.autopay.dao.KeyOrderMapper;
import com.taoke.autopay.dao.agent.ChannelAgentMapper;
import com.taoke.autopay.dto.admin.OrderExcelDataDto;
import com.taoke.autopay.entity.ClientInfo;
import com.taoke.autopay.entity.KeyOrder;
import com.taoke.autopay.entity.OrderChannelEnum;
import com.taoke.autopay.entity.SystemConfigKeyEnum;
import com.taoke.autopay.entity.agent.ChannelAgent;
import com.taoke.autopay.entity.agent.ChannelAgentSettings;
import com.taoke.autopay.entity.agent.ChannelAgentSharingRatio;
import com.taoke.autopay.exception.ChannelAgentException;
import com.taoke.autopay.factory.AgentFactory;
import com.taoke.autopay.factory.OrderFactory;
import com.taoke.autopay.manager.ChannelAgentManager;
import com.taoke.autopay.service.ClientInfoService;
import com.taoke.autopay.service.KeyOrderService;
import com.taoke.autopay.service.SystemConfigService;
import com.taoke.autopay.service.agent.ChannelAgentService;
import com.taoke.autopay.service.agent.ChannelAgentSettingService;
import com.taoke.autopay.service.agent.ChannelAgentSharingRatioService;
import com.taoke.autopay.utils.Constant;
import com.taoke.autopay.utils.TimeUtil;
import com.taoke.autopay.vo.admin.AdminChannelAgentVO;
import com.taoke.autopay.vo.admin.AdminOrderVO;
import com.taoke.autopay.vo.admin.AgentSearchVO;
import com.taoke.autopay.vo.admin.OrderSearchVO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.NumberUtil;
import org.yeshi.utils.StringUtil;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;
 
@Controller
@RequestMapping("/admin/api/agent")
public class AdminAgentController {
 
    private Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new TypeAdapter<Date>() {
        @Override
        public void write(JsonWriter out, Date value) throws IOException {
            String desc = "";
            if (value != null) {
                // 判断是否是同一天
                desc = TimeUtil.getGernalTime(value.getTime(), "yyyy-MM-dd HH:mm:ss");
                out.value(desc);
            } else {
                out.value("");
            }
        }
 
        @Override
        public Date read(JsonReader in) throws IOException {
            return new Date();
        }
    }).create();
 
    @Resource
    private ChannelAgentService channelAgentService;
 
    @Resource
    private ChannelAgentSettingService channelAgentSettingService;
 
    @Resource
    private ChannelAgentSharingRatioService channelAgentSharingRatioService;
 
    @Resource
    private SystemConfigService systemConfigService;
 
 
    @ResponseBody
    @RequestMapping("list")
    public String listAgent(AgentSearchVO search, int page, int limit) {
        //先查询所有的数据
        ChannelAgentMapper.DaoQuery query = new ChannelAgentMapper.DaoQuery();
        query.searchKey = search.getKey();
        query.sortList = Arrays.asList(new String[]{"_create_time desc"});
        query.start = (long) (page - 1) * limit;
        query.count = limit;
 
        if (!StringUtil.isNullOrEmpty(search.getStartDate())) {
            query.minCreateTime = new Date(TimeUtil.convertToTimeTemp(search.getStartDate(), "yyyy-MM-dd"));
        }
        if (!StringUtil.isNullOrEmpty(search.getEndDate())) {
            query.maxCreateTime = TimeUtil.getNextDay(1, new Date(TimeUtil.convertToTimeTemp(search.getEndDate(), "yyyy-MM-dd")).getTime());
        }
 
        List<ChannelAgent> agentList = channelAgentService.list(query);
        List<Long> agentIds = new ArrayList<>();
        for (ChannelAgent agent : agentList) {
            agentIds.add(agent.getId());
        }
        List<ChannelAgentSettings> settings = channelAgentSettingService.listByIds(agentIds);
        Map<Long, ChannelAgentSettings> settingsMap = new HashMap<>();
        for (ChannelAgentSettings setting : settings) {
            settingsMap.put(setting.getId(), setting);
        }
 
        long count = channelAgentService.count(query);
        // 转vo
        List<AdminChannelAgentVO> voList = new ArrayList<>();
        String baseSubmitKeyLink = systemConfigService.getValueCache(SystemConfigKeyEnum.AGENT_SUBMIT_KEY_LINK);
        for (ChannelAgent agent : agentList) {
            voList.add(AgentFactory.create(agent, settingsMap.get(agent.getId()), null, baseSubmitKeyLink));
        }
        JSONObject data = new JSONObject();
        data.put("count", count);
        data.put("list", gson.toJson(voList));
        return JsonUtil.loadTrueResult(data);
    }
 
 
    @Resource
    private ChannelAgentManager channelAgentManager;
 
    @ResponseBody
    @RequestMapping("add")
    public String addAgent(AdminChannelAgentVO vo) {
 
        try {
          ChannelAgent agent=  channelAgentManager.addAgent(vo);
            return JsonUtil.loadTrueResult(agent);
        } catch (ChannelAgentException e) {
            return JsonUtil.loadFalseResult(e.getMessage());
        }
    }
 
    @ResponseBody
    @RequestMapping("update")
    public String update(AdminChannelAgentVO vo) {
        // 创建代理
        ChannelAgent agent = ChannelAgent.builder()
                .id(vo.getId())
                .name(vo.getName())
                .account(vo.getAccount())
                .pwd(StringUtil.isNullOrEmpty(vo.getPwd()) ? null :StringUtil.Md5(vo.getPwd()))
                .alipayAccount(StringUtil.isNullOrEmpty(vo.getAlipayAccount()) ? null : vo.getAlipayAccount())
                .alipayName(StringUtil.isNullOrEmpty(vo.getAlipayName()) ? null : vo.getAlipayName())
                .build();
            channelAgentService.updateSelective(agent);
            // 添加设置
            ChannelAgentSettings settings = ChannelAgentSettings.builder()
                    .id(agent.getId())
                    .startSubmitTime(StringUtil.isNullOrEmpty(vo.getStartSubmitTime()) ? null : vo.getStartSubmitTime())
                    .endSubmitTime(StringUtil.isNullOrEmpty(vo.getEndSubmitTime()) ? null : vo.getEndSubmitTime())
                    .maxKeyCountPerDay(StringUtil.isNullOrEmpty(vo.getMaxKeyCountPerDay()) ? null : Long.parseLong(vo.getMaxKeyCountPerDay()))
                    .maxPayMoneyPerDay(StringUtil.isNullOrEmpty(vo.getMaxPayMoneyPerDay()) ? null : new BigDecimal(vo.getMaxPayMoneyPerDay()))
                    .build();
            channelAgentSettingService.add(settings);
            // 添加分成比例设置
            if (!StringUtil.isNullOrEmpty(vo.getShareRatioInfos())) {
                JSONObject shareRatioData = JSONObject.fromObject(vo.getShareRatioInfos());
                for (Object key : shareRatioData.keySet()) {
                    String value = shareRatioData.optString(key.toString());
                    OrderChannelEnum channel = OrderChannelEnum.valueOf(key.toString());
                    if (!StringUtil.isNullOrEmpty(value)) {
                        channelAgentSharingRatioService.setShareRatio(ChannelAgentSharingRatio.builder().agengId(agent.getId()).orderChannel(channel).shareType(ChannelAgentSharingRatio.SHARE_TYPE_MONEY).shareValue(new BigDecimal(value)).build());
                    }
                }
            }
            return JsonUtil.loadTrueResult(agent);
    }
 
 
    @ResponseBody
    @RequestMapping("delete")
    public String delete(Long id){
        // 删除agent
        channelAgentService.delete(id);
        return JsonUtil.loadTrueResult("");
    }
 
    @ResponseBody
    @RequestMapping("getDetail")
    public String getDetail(Long id) {
 
        ChannelAgent agent = channelAgentService.selectByPrimaryKey(id);
        if (agent == null) {
            return JsonUtil.loadFalseResult("代理ID不存在");
        }
        Map<OrderChannelEnum, BigDecimal> shareRatio = channelAgentSharingRatioService.getShareMoneyMap(id);
        ChannelAgentSettings settings = channelAgentSettingService.selectByAgentId(id);
        if(settings.getStartSubmitTime()==null||settings.getEndSubmitTime()==null){
            String timeStr =   systemConfigService.getValueCache(SystemConfigKeyEnum.KEY_SUBMIT_TIME_RANGE);
            settings.setStartSubmitTime(timeStr.split(",")[0]);
            settings.setEndSubmitTime(timeStr.split(",")[1]);
        }
        String baseSubmitKeyLink = systemConfigService.getValueCache(SystemConfigKeyEnum.AGENT_SUBMIT_KEY_LINK);
        AdminChannelAgentVO vo = AgentFactory.create(agent, settings, shareRatio, baseSubmitKeyLink);
        return JsonUtil.loadTrueResult(gson.toJson(vo));
    }
 
    /**
     * 获取订单所有的渠道
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("getAgentOrderChannels")
    public String getAgentOrderChannels() {
        JSONArray array = new JSONArray();
        for (OrderChannelEnum channel : OrderChannelEnum.values()) {
            JSONObject item = new JSONObject();
            item.put("label", channel.getName());
            item.put("name", channel.name());
            array.add(item);
        }
        return JsonUtil.loadTrueResult(array);
    }
}