package com.taoke.autopay.service.impl.agent;
|
|
import com.taoke.autopay.dao.KeyOrderMapper;
|
import com.taoke.autopay.dao.agent.ChannelAgentSettleDetailMapper;
|
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.agent.ChannelAgent;
|
import com.taoke.autopay.entity.agent.ChannelAgentSettleDetail;
|
import com.taoke.autopay.entity.agent.ChannelAgentSettleRecord;
|
import com.taoke.autopay.exception.ChannelAgentSettleException;
|
import com.taoke.autopay.service.KeyOrderService;
|
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.TimeUtil;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Map;
|
|
@Service
|
public class ChannelAgentSettleServiceImpl implements ChannelAgentSettleService {
|
|
@Resource
|
private ChannelAgentSettleRecordMapper channelAgentSettleRecordMapper;
|
|
|
@Resource
|
private ChannelAgentSettleDetailMapper channelAgentSettleDetailMapper;
|
|
@Resource
|
private ChannelAgentSharingRatioService channelAgentSharingRatioService;
|
|
@Resource
|
private KeyOrderService keyOrderService;
|
|
@Resource
|
private ChannelAgentService channelAgentService;
|
|
@Transactional(rollbackFor = Exception.class)
|
@Override
|
public void startSettle(String day) throws ChannelAgentSettleException {
|
// 查询可结算的代理用户
|
Date startTime = new Date(TimeUtil.convertToTimeTemp(day, "yyyy-MM-dd"));
|
Date endTime = new Date(startTime.getTime() + 1000 * 60 * 60 * 24L);
|
KeyOrderMapper.DaoQuery orderQuery = new KeyOrderMapper.DaoQuery();
|
orderQuery.minCreateTime = startTime;
|
orderQuery.maxCreateTime = endTime;
|
orderQuery.hasAgentId = true;
|
orderQuery.hasPayTime = true;
|
orderQuery.state = KeyOrder.STATE_PAY;
|
orderQuery.count = (int) keyOrderService.countAgentId(orderQuery);
|
if (orderQuery.count <= 0) {
|
throw new ChannelAgentSettleException("无可结算的代理");
|
}
|
List<Long> agentList = keyOrderService.listAgentId(orderQuery);
|
for (Long agentId : agentList) {
|
// 统计订单
|
orderQuery = new KeyOrderMapper.DaoQuery();
|
orderQuery.oMinCreateTime = startTime;
|
orderQuery.oMaxCreateTime = endTime;
|
orderQuery.agentId = agentId;
|
orderQuery.hasPayTime = true;
|
orderQuery.state = KeyOrder.STATE_PAY;
|
List<ChannelOrderStatistic> list = keyOrderService.statisticChannelOrders(agentId, startTime, endTime);
|
List<ChannelAgentSettleDetail> detailList = new ArrayList<>();
|
Map<OrderChannelEnum, BigDecimal> shareMoneyMap = channelAgentSharingRatioService.getShareMoneyMap(agentId);
|
for (ChannelOrderStatistic statistic : list) {
|
OrderChannelEnum orderChannel = null;
|
for (OrderChannelEnum channel : OrderChannelEnum.values()) {
|
if (channel.getKey().equalsIgnoreCase(statistic.getOrderChannel())) {
|
orderChannel = channel;
|
break;
|
}
|
}
|
if (orderChannel != null) {
|
if (shareMoneyMap.containsKey(orderChannel)) {
|
BigDecimal money = shareMoneyMap.get(orderChannel).multiply(new BigDecimal(statistic.getCount()));
|
// 查询是否已经结算
|
detailList.add(ChannelAgentSettleDetail.builder()
|
.orderChannel(orderChannel)
|
.payOrderCount(statistic.getCount())
|
.payMoney(statistic.getMoney())
|
.money(money)
|
.build());
|
}
|
}
|
}
|
if (detailList.isEmpty()) {
|
continue;
|
}
|
|
BigDecimal totalMoney = new BigDecimal(0);
|
for (ChannelAgentSettleDetail detail : detailList) {
|
totalMoney = totalMoney.add(detail.getMoney());
|
}
|
ChannelAgent agent = channelAgentService.selectByPrimaryKey(agentId);
|
|
settle(ChannelAgentSettleRecord.builder()
|
.agentId(agentId)
|
.settleDay(day)
|
.settleMoney(totalMoney)
|
.alipayAccount(agent.getAlipayAccount())
|
.alipayName(agent.getAlipayName())
|
.detailList(detailList)
|
.build());
|
}
|
}
|
|
@Transactional(rollbackFor = Exception.class)
|
@Override
|
public void settle(ChannelAgentSettleRecord record) {
|
// 查询是否结算
|
ChannelAgentSettleRecordMapper.DaoQuery daoQuery = new ChannelAgentSettleRecordMapper.DaoQuery();
|
daoQuery.agentId = record.getAgentId();
|
daoQuery.settleDay = record.getSettleDay();
|
if (channelAgentSettleRecordMapper.count(daoQuery) > 0) {
|
// 已经结算
|
return;
|
}
|
if (record.getCreateTime() == null) {
|
record.setCreateTime(new Date());
|
}
|
if (record.getStatus() == null) {
|
record.setStatus(ChannelAgentSettleRecord.STATUS_NOT_SETTLE);
|
record.setStatusDesc("已结算,待确认");
|
}
|
channelAgentSettleRecordMapper.insertSelective(record);
|
|
for (ChannelAgentSettleDetail detail : record.getDetailList()) {
|
detail.setSettleId(record.getId());
|
if (detail.getCreateTime() == null) {
|
detail.setCreateTime(new Date());
|
}
|
channelAgentSettleDetailMapper.insertSelective(detail);
|
}
|
}
|
|
@Override
|
public List<ChannelAgentSettleRecord> list(ChannelAgentSettleRecordMapper.DaoQuery query) {
|
return channelAgentSettleRecordMapper.list(query);
|
}
|
|
@Override
|
public long count(ChannelAgentSettleRecordMapper.DaoQuery query) {
|
return channelAgentSettleRecordMapper.count(query);
|
}
|
|
@Override
|
public void delete(Long id) {
|
ChannelAgentSettleRecord record = channelAgentSettleRecordMapper.selectByPrimaryKey(id);
|
if (record != null) {
|
channelAgentSettleRecordMapper.deleteByPrimaryKey(id);
|
for (ChannelAgentSettleDetail d : record.getDetailList()) {
|
channelAgentSettleDetailMapper.deleteByPrimaryKey(d.getId());
|
}
|
}
|
|
}
|
|
@Transactional(rollbackFor = Exception.class)
|
@Override
|
public void processWithdraw(List<Long> ids, boolean pass) throws ChannelAgentSettleException {
|
for (Long id : ids) {
|
ChannelAgentSettleRecord old = channelAgentSettleRecordMapper.selectByPrimaryKeyForUpdate(id);
|
if (old.getStatus() != ChannelAgentSettleRecord.STATUS_WITHDRAWING) {
|
throw new ChannelAgentSettleException("提现已处理");
|
}
|
|
channelAgentSettleRecordMapper.updateByPrimaryKeySelective(ChannelAgentSettleRecord.builder().id(id)
|
.status(pass ? ChannelAgentSettleRecord.STATUS_WITHDRAW_SUCCESS : ChannelAgentSettleRecord.STATUS_WITHDRAW_REJECTED)
|
.withDrawProcessTime(new Date())
|
.updateTime(new Date())
|
.payTime(pass ? new Date() : null).build());
|
}
|
|
|
}
|
|
@Override
|
public void actualSettle(Long id, BigDecimal money) throws ChannelAgentSettleException {
|
ChannelAgentSettleRecord old = channelAgentSettleRecordMapper.selectByPrimaryKeyForUpdate(id);
|
if (old == null) {
|
throw new ChannelAgentSettleException("结算ID为空");
|
}
|
if (old.getStatus() != ChannelAgentSettleRecord.STATUS_NOT_SETTLE) {
|
throw new ChannelAgentSettleException("已经结算");
|
}
|
|
channelAgentSettleRecordMapper.updateByPrimaryKeySelective(ChannelAgentSettleRecord.builder()
|
.id(id)
|
.actualSettleMoney(money)
|
.settleTime(new Date())
|
.status(ChannelAgentSettleRecord.STATUS_SETTLED)
|
.updateTime(new Date())
|
.build());
|
|
|
}
|
|
@Override
|
public void applyWithdraw(Long id) throws ChannelAgentSettleException {
|
|
ChannelAgentSettleRecord old = channelAgentSettleRecordMapper.selectByPrimaryKeyForUpdate(id);
|
if (old == null) {
|
throw new ChannelAgentSettleException("结算ID为空");
|
}
|
if (old.getStatus() != ChannelAgentSettleRecord.STATUS_SETTLED) {
|
throw new ChannelAgentSettleException("处于不可提现状态");
|
}
|
channelAgentSettleRecordMapper.updateByPrimaryKeySelective(ChannelAgentSettleRecord.builder()
|
.id(id)
|
.withDrawApplyTime(new Date())
|
.settleTime(new Date())
|
.status(ChannelAgentSettleRecord.STATUS_WITHDRAWING)
|
.updateTime(new Date())
|
.build());
|
}
|
}
|