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() { @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() { @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 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 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 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>(){}.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>(){}.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 list = channelAgentSettleService.list(daoQuery); List 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() { @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(""); } }