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.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);
|
}
|
|
|
@ResponseBody
|
@RequestMapping("add")
|
public String addAgent(AdminChannelAgentVO vo) {
|
// 创建代理
|
ChannelAgent agent = ChannelAgent.builder()
|
.name(vo.getName())
|
.account(vo.getAccount())
|
.pwd(StringUtil.Md5(vo.getPwd()))
|
.alipayAccount(StringUtil.isNullOrEmpty(vo.getAlipayAccount()) ? null : vo.getAlipayAccount())
|
.alipayName(StringUtil.isNullOrEmpty(vo.getAlipayName()) ? null : vo.getAlipayName())
|
.status(ChannelAgent.STATUS_NOMAL)
|
.build();
|
try {
|
agent = channelAgentService.addChannelAgent(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);
|
} 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);
|
}
|
}
|