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.ChannelAgentSettleRecordMapper;
|
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.ChannelAgentSettleDetail;
|
import com.taoke.autopay.entity.agent.ChannelAgentSettleRecord;
|
import com.taoke.autopay.exception.ChannelAgentException;
|
import com.taoke.autopay.exception.ChannelAgentSettleException;
|
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.ChannelAgentSettleService;
|
import com.taoke.autopay.service.agent.ChannelAgentSharingRatioService;
|
import com.taoke.autopay.utils.AgentUtil;
|
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.AgentWithdrawVO;
|
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;
|
|
@Resource
|
private ChannelAgentSettleService channelAgentSettleService;
|
|
@Resource
|
private SystemConfigService systemConfigService;
|
|
|
@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.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);
|
}
|
|
|
@ResponseBody
|
@RequestMapping("getConfig")
|
public String getConfig(HttpSession session) {
|
ChannelAgent agent = (ChannelAgent) session.getAttribute(Constant.SESSION_KEY_AGENT);
|
if (agent == null) {
|
return JsonUtil.loadFalseResult("尚未登录");
|
}
|
String baseSubmitKeyLink = systemConfigService.getValueCache(SystemConfigKeyEnum.AGENT_SUBMIT_KEY_LINK);
|
String submitKeyUrl = AgentUtil.getSubmitKeyUrl(baseSubmitKeyLink, agent.getAlias());
|
JSONObject data = new JSONObject();
|
data.put("submitKeyUrl", submitKeyUrl);
|
return JsonUtil.loadTrueResult(data);
|
}
|
|
@ResponseBody
|
@RequestMapping("withdrawList")
|
public String withdrawList(int page, HttpSession session) {
|
ChannelAgent agent = (ChannelAgent) session.getAttribute(Constant.SESSION_KEY_AGENT);
|
if (agent == null) {
|
return JsonUtil.loadFalseResult("尚未登录");
|
}
|
|
ChannelAgentSettleRecordMapper.DaoQuery daoQuery = new ChannelAgentSettleRecordMapper.DaoQuery();
|
daoQuery.statusList = Arrays.asList(new Integer[]{ChannelAgentSettleRecord.STATUS_SETTLED, ChannelAgentSettleRecord.STATUS_WITHDRAWING, ChannelAgentSettleRecord.STATUS_WITHDRAW_SUCCESS, ChannelAgentSettleRecord.STATUS_WITHDRAW_REJECTED});
|
daoQuery.sortList = Arrays.asList(new String[]{"_update_time desc"});
|
daoQuery.count = 20;
|
daoQuery.start = (page - 1) * 20;
|
List<ChannelAgentSettleRecord> recordList = channelAgentSettleService.list(daoQuery);
|
long count = channelAgentSettleService.count(daoQuery);
|
List<AgentWithdrawVO> voList = new ArrayList<>();
|
for (ChannelAgentSettleRecord record : recordList) {
|
long totalOrderCount = 0;
|
for (ChannelAgentSettleDetail d : record.getDetailList()) {
|
totalOrderCount += d.getPayOrderCount();
|
}
|
String statusDesc = "";
|
switch (record.getStatus()) {
|
case ChannelAgentSettleRecord.STATUS_SETTLED:
|
statusDesc = "未提现";
|
break;
|
case ChannelAgentSettleRecord.STATUS_WITHDRAWING:
|
statusDesc = "正在审核";
|
break;
|
case ChannelAgentSettleRecord.STATUS_WITHDRAW_SUCCESS:
|
statusDesc = "提现成功";
|
break;
|
case ChannelAgentSettleRecord.STATUS_WITHDRAW_REJECTED:
|
statusDesc = "提现失败";
|
break;
|
}
|
|
voList.add(AgentWithdrawVO.builder()
|
.id(record.getId())
|
.money(record.getActualSettleMoney().toString())
|
.month(TimeUtil.getGernalTime(record.getSettleTime().getTime(), "yyyy年MM月"))
|
.status(record.getStatus())
|
.statusDesc(statusDesc)
|
.orderCount(totalOrderCount)
|
.settleTime(TimeUtil.getGernalTime(record.getSettleTime().getTime(), "yyyy.MM.dd HH:mm:ss"))
|
.build());
|
}
|
|
|
JSONObject data = new JSONObject();
|
data.put("list", voList);
|
data.put("count", count);
|
return JsonUtil.loadTrueResult(data);
|
}
|
|
@ResponseBody
|
@RequestMapping("withdraw")
|
public String withdraw(Long id, HttpSession session) {
|
ChannelAgent agent = (ChannelAgent) session.getAttribute(Constant.SESSION_KEY_AGENT);
|
if (agent == null) {
|
return JsonUtil.loadFalseResult("尚未登录");
|
}
|
|
if(StringUtil.isNullOrEmpty(agent.getAlipayName())||StringUtil.isNullOrEmpty(agent.getAlipayAccount()))
|
{
|
return JsonUtil.loadFalseResult("尚未设置支付宝信息");
|
}
|
|
try {
|
channelAgentSettleService.applyWithdraw(id);
|
return JsonUtil.loadTrueResult("");
|
} catch (ChannelAgentSettleException e) {
|
e.printStackTrace();
|
return JsonUtil.loadFalseResult("尚未登录");
|
}
|
}
|
|
|
}
|