Administrator
2025-04-23 595b7935a30e84fba1bc3561d05f9d19d3e32e1f
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package com.taoke.autopay.controller.admin;
 
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.taoke.autopay.dao.agent.ChannelAgentMapper;
import com.taoke.autopay.dao.agent.ChannelAgentSettleRecordMapper;
import com.taoke.autopay.dto.admin.AgentSettleExcelDataDto;
import com.taoke.autopay.dto.admin.OrderExcelDataDto;
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.ChannelAgentSettleRecord;
import com.taoke.autopay.entity.agent.ChannelAgentSharingRatio;
import com.taoke.autopay.exception.ChannelAgentException;
import com.taoke.autopay.exception.ChannelAgentSettleException;
import com.taoke.autopay.factory.AgentFactory;
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.ChannelAgentSettleService;
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.AgentSearchVO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;
 
/**
 * 渠道结算
 */
@Controller
@RequestMapping("/admin/api/agentsettle")
public class AdminAgentSettleController {
 
    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();
        }
    }).registerTypeAdapter(BigDecimal.class, new TypeAdapter<BigDecimal>() {
        @Override
        public void write(JsonWriter out, BigDecimal value) throws IOException {
            if (value != null) {
                out.value(value.toString());
            } else {
                out.value("");
            }
        }
 
        @Override
        public BigDecimal read(JsonReader in) throws IOException {
            return new BigDecimal("0.00");
        }
    }).create();
 
    @Resource
    private ChannelAgentService channelAgentService;
 
    @Resource
    private ChannelAgentSettleService channelAgentSettleService;
 
 
    @ResponseBody
    @RequestMapping("list")
    public String listAgent(String key, String startDay,String endDay, int page, int limit) {
        //先查询所有的数据
        ChannelAgentSettleRecordMapper.DaoQuery query = new ChannelAgentSettleRecordMapper.DaoQuery();
        query.minSettleDay = com.taoke.autopay.utils.StringUtil.isNullOrEmpty(startDay)?null:startDay;
        query.maxSettleDay = com.taoke.autopay.utils.StringUtil.isNullOrEmpty(endDay)?null:endDay;
        query.sortList = Arrays.asList(new String[]{"_create_time desc"});
        query.start = (long) (page - 1) * limit;
        query.count = limit;
        List<ChannelAgentSettleRecord> recordList = channelAgentSettleService.list(query);
        long count = channelAgentSettleService.count(query);
        JSONObject data = new JSONObject();
        data.put("count", count);
        data.put("list", gson.toJson(recordList));
        return JsonUtil.loadTrueResult(data);
    }
 
 
    @ResponseBody
    @RequestMapping("delete")
    public String delete(Long id){
        // 删除agent
        channelAgentSettleService.delete(id);
        return JsonUtil.loadTrueResult("");
    }
 
    /**
     * @author hxh 
     * @description 开始结算
     * @date 0:28 2024/10/14
     * @param: startDay 开始日期
     * @param: endDay 结束日期
     * @return java.lang.String
     **/
    @ResponseBody
    @RequestMapping("startSettle")
    public String startSettle(String  startDay, String endDay) {
        if(StringUtil.isNullOrEmpty(startDay)||StringUtil.isNullOrEmpty(endDay)){
            return JsonUtil.loadFalseResult("结算日期输入不完整");
        }
        if(Integer.parseInt(startDay.replace("-",""))>Integer.parseInt(endDay.replace("-",""))){
            return JsonUtil.loadFalseResult("开始日期不能大于结束日期");
        }
 
        long startTimeStamp =  TimeUtil.convertToTimeTemp(startDay,"yyyy-MM-dd");
        long endTimeStamp =  TimeUtil.convertToTimeTemp(endDay,"yyyy-MM-dd");
 
        List<String> days=new ArrayList<>();
        for(int i=0;i<100;i++){
          if(endTimeStamp< startTimeStamp + i*24*60*60*1000L){
              break;
          }
          days.add(TimeUtil.getGernalTime(startTimeStamp + i*24*60*60*1000L,"yyyy-MM-dd"));
       }
 
        try {
            for(String day:days) {
                channelAgentSettleService.startSettle(day);
            }
            return JsonUtil.loadTrueResult("");
        } catch (ChannelAgentSettleException e) {
            return JsonUtil.loadFalseResult(e.getMessage());
        }
    }
 
    /**
     * @author hxh 
     * @description 提现列表
     * @date 13:14 2024/7/29
     * @param: key
     * @param: startTime
     * @param: endTime
     * @param: status
     * @param: page
     * @param: limit
     * @return java.lang.String
     **/
    @ResponseBody
    @RequestMapping("listWithdraw")
    public String listWithdraw(String key, String startTime,String endTime, String status, int page, int limit) {
        //先查询所有的数据
        ChannelAgentSettleRecordMapper.DaoQuery query = new ChannelAgentSettleRecordMapper.DaoQuery();
        query.agentId = !NumberUtil.isNumeric(key)?null:Long.parseLong(key);
        query.minWithDrawApplyTime = StringUtil.isNullOrEmpty(startTime)?null:new Date(TimeUtil.convertToTimeTemp(startTime,"yyyy-MM-dd HH:mm:ss"));
        query.maxWithDrawApplyTime = StringUtil.isNullOrEmpty(endTime)?null:new Date(TimeUtil.convertToTimeTemp(endTime,"yyyy-MM-dd HH:mm:ss"));
        if(status==null||Integer.parseInt(status)<0){
            query.statusList=Arrays.asList(new Integer[]{ ChannelAgentSettleRecord.STATUS_WITHDRAWING,ChannelAgentSettleRecord.STATUS_WITHDRAW_SUCCESS,ChannelAgentSettleRecord.STATUS_WITHDRAW_REJECTED});
        }else{
            query.status=Integer.parseInt(status);
        }
        query.sortList = Arrays.asList(new String[]{"_withdraw_apply_time desc"});
        query.start = (long) (page - 1) * limit;
        query.count = limit;
        List<ChannelAgentSettleRecord> recordList = channelAgentSettleService.list(query);
        long count = channelAgentSettleService.count(query);
        JSONObject data = new JSONObject();
        data.put("count", count);
        data.put("list", gson.toJson(recordList));
        return JsonUtil.loadTrueResult(data);
    }
 
    /**
     * @author hxh 
     * @description
     * @date 13:23 2024/7/29
     * @param: ids JSONARRAY
     * @return java.lang.String
     **/
    @ResponseBody
    @RequestMapping("pass")
    public String passWithdraw(String ids) {
       if(StringUtil.isNullOrEmpty(ids)){
           return JsonUtil.loadFalseResult("请上传ids");
       }
        Type type=new TypeToken<List<Long>>(){}.getType();
        try {
            channelAgentSettleService.processWithdraw(gson.fromJson(ids,type), true);
            return JsonUtil.loadTrueResult("");
        } catch (ChannelAgentSettleException e) {
            return JsonUtil.loadFalseResult(e.getMessage());
        }
    }
 
    @ResponseBody
    @RequestMapping("reject")
    public String rejectWithdraw(String ids) {
        if(StringUtil.isNullOrEmpty(ids)){
            return JsonUtil.loadFalseResult("请上传ids");
        }
        Type type=new TypeToken<List<Long>>(){}.getType();
        try {
            channelAgentSettleService.processWithdraw(gson.fromJson(ids,type), false);
            return JsonUtil.loadTrueResult("");
        } catch (ChannelAgentSettleException e) {
            return JsonUtil.loadFalseResult(e.getMessage());
        }
    }
 
 
 
    @RequestMapping("downloadSettleTable")
    public void downloadSettleTable(String startDay,String endDay, HttpServletResponse response) throws IOException {
        ChannelAgentSettleRecordMapper.DaoQuery daoQuery=new ChannelAgentSettleRecordMapper.DaoQuery();
        daoQuery.minSettleDay = com.taoke.autopay.utils.StringUtil.isNullOrEmpty(startDay)?null:startDay;
        daoQuery.maxSettleDay = com.taoke.autopay.utils.StringUtil.isNullOrEmpty(endDay)?null:endDay;
        daoQuery.count = 10000;
        daoQuery.status =  ChannelAgentSettleRecord.STATUS_NOT_SETTLE;
        List<ChannelAgentSettleRecord> list =   channelAgentSettleService.list(daoQuery);
 
        List<AgentSettleExcelDataDto> dataList=new ArrayList<>();
        for(ChannelAgentSettleRecord record:list){
            AgentSettleExcelDataDto dto=AgentSettleExcelDataDto.builder()
                    .id(record.getId())
                    .agentId(record.getAgentId())
                    .actualSettleMoney(record.getSettleMoney().toString())
                    .settleMoney(record.getSettleMoney().toString())
                    .day(record.getSettleDay())
                    .build();
            dataList.add(dto);
        }
 
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName="settle_"+URLEncoder.encode(TimeUtil.getGernalTime(System.currentTimeMillis(),"yyyyMMdd_HHmmss"),"UTF-8");
        response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
        EasyExcel.write(response.getOutputStream(),AgentSettleExcelDataDto.class).sheet("结算确认单").doWrite(dataList);
    }
 
 
    @ResponseBody
    @RequestMapping("uploadSettleExcel")
    public String uploadImg(@RequestParam("file") MultipartFile file, HttpSession session) throws IOException {
        InputStream inputStream = file.getInputStream();
        EasyExcel.read(inputStream, AgentSettleExcelDataDto.class, new ReadListener<AgentSettleExcelDataDto>() {
            @Override
            public void invoke(AgentSettleExcelDataDto o, AnalysisContext analysisContext) {
                System.out.println("读取到数据: " + o.getDay());
                try {
                    channelAgentSettleService.actualSettle(o.getId(),new BigDecimal(o.getActualSettleMoney()));
                } catch (ChannelAgentSettleException e) {
                    e.printStackTrace();
                }
            }
 
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
 
            }
        }).sheet().doRead();
        return JsonUtil.loadTrueResult("");
    }
 
 
 
 
 
}