Administrator
2024-07-29 a053811c774ac07340e46561f5d2ab4d892282a0
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
package com.taoke.autopay.controller.agent;
 
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.ChannelOrderStatistic;
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.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.StringUtil;
import com.taoke.autopay.utils.TimeUtil;
import com.taoke.autopay.vo.AgentOrderFilter;
import com.taoke.autopay.vo.AgentOrderVO;
import com.taoke.autopay.vo.OrderFilter;
import com.taoke.autopay.vo.admin.AdminChannelAgentVO;
import com.taoke.autopay.vo.admin.AgentSearchVO;
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 javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
 
@Controller
@RequestMapping("/agentapi/admin")
public class AgentController {
 
    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 KeyOrderService keyOrderService;
 
    @Resource
    private ChannelAgentSharingRatioService channelAgentSharingRatioService;
 
 
    @ResponseBody
    @RequestMapping("login")
    public String login(String account, String pwd, HttpSession session) {
        try {
            ChannelAgent agent = channelAgentService.login(account, pwd);
            agent.setPwd(null);
            session.setAttribute(Constant.SESSION_KEY_AGENT, agent);
            return JsonUtil.loadTrueResult(agent);
        } catch (ChannelAgentException e) {
            return JsonUtil.loadFalseResult(e.getMessage());
        }
    }
 
    @ResponseBody
    @RequestMapping("setAlipayAccount")
    public String setAlipayAccount(String alipayAccount, String alipayName, HttpSession session) {
        ChannelAgent agent = (ChannelAgent) session.getAttribute(Constant.SESSION_KEY_AGENT);
        if (agent == null) {
            return JsonUtil.loadFalseResult("尚未登录");
        }
 
        try {
            channelAgentService.setAlipayAccount(agent.getId(), alipayName, alipayAccount);
            return JsonUtil.loadTrueResult("");
        } catch (ChannelAgentException e) {
            return JsonUtil.loadFalseResult(e.getMessage());
        }
    }
 
 
    @ResponseBody
    @RequestMapping("orderList")
    public String orderList(AgentOrderFilter filter, HttpSession session) {
        ChannelAgent agent = (ChannelAgent) session.getAttribute(Constant.SESSION_KEY_AGENT);
        KeyOrderMapper.DaoQuery query = new KeyOrderMapper.DaoQuery();
        query.agentId = agent.getId();
        if (!StringUtil.isNullOrEmpty(filter.getKey())) {
            query.nickName = filter.getKey().trim();
        }
        query.start = (filter.getPage() - 1) * 20L;
        query.count = 20;
 
        long now = System.currentTimeMillis();
        String nowDay = TimeUtil.getGernalTime(now, "yyyy-MM-dd");
        switch (filter.getTimeIndex()) {
            case 0:
                query.oMinCreateTime = new Date(TimeUtil.convertToTimeTemp(nowDay, "yyyy-MM-dd"));
                break;
            case 1:
                query.oMaxCreateTime = new Date(TimeUtil.convertToTimeTemp(nowDay, "yyyy-MM-dd"));
                query.oMinCreateTime = new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(now - 24 * 60 * 60 * 1000L, "yyyy-MM-dd"), "yyyy-MM-dd"));
                break;
            case 2:
                query.oMaxCreateTime = new Date(TimeUtil.convertToTimeTemp(nowDay, "yyyy-MM-dd"));
                query.oMinCreateTime = new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(now - 24 * 60 * 60 * 1000L * 3, "yyyy-MM-dd"), "yyyy-MM-dd"));
            case 3:
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date(now));
                int day = calendar.get(Calendar.DAY_OF_WEEK);
                day -= 1;
                if (day == 0) {
                    // 星期天
                    day = 7;
                }
                query.oMinCreateTime = new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(now - 24 * 60 * 60 * 1000L * (day - 1), "yyyy-MM-dd"), "yyyy-MM-dd"));
                break;
        }
 
        query.sortList = Arrays.asList(new String[]{"o.create_time desc"});
 
        List<KeyOrder> list = keyOrderService.listWithUser(query);
        long count = keyOrderService.countWithUser(query);
        query.hasPayTime = true;
        ChannelOrderStatistic statistic = keyOrderService.statisticWithUser(query);
 
        List<AgentOrderVO> voList = new ArrayList<>();
        Map<OrderChannelEnum, BigDecimal> shareMoneyMap = channelAgentSharingRatioService.getShareMoneyMap(agent.getId());
        for (KeyOrder order : list) {
            BigDecimal money = null;
            for (OrderChannelEnum channel : OrderChannelEnum.values()) {
                if (channel.getKey().equalsIgnoreCase(order.getOrderChannel())) {
                    money = shareMoneyMap.get(channel);
                    break;
                }
            }
            if (money == null) {
                money = new BigDecimal(0);
            }
            AgentOrderVO vo = OrderFactory.createAgentOrder(order, money);
            voList.add(vo);
        }
        JSONObject data = new JSONObject();
        data.put("list", voList);
        data.put("count", count);
        data.put("statistic", statistic);
        return JsonUtil.loadTrueResult(data);
    }
 
 
}