package com.taoke.autopay.manager;
|
|
import com.taoke.autopay.entity.OrderChannelEnum;
|
import com.taoke.autopay.entity.OrderCountTypeEnum;
|
import com.taoke.autopay.exception.KeyOrderException;
|
import com.taoke.autopay.service.UserSettingService;
|
import com.taoke.autopay.service.WxUserOrderCountService;
|
import com.taoke.autopay.utils.Constant;
|
import com.taoke.autopay.utils.TimeUtil;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 支付次数管理器
|
*/
|
@Component
|
public class PayCountVerifyManager {
|
|
@Resource
|
private WxUserOrderCountService wxUserOrderCountService;
|
|
@Resource
|
private UserSettingService userSettingService;
|
|
/**
|
* 验证支付次数
|
*
|
* @param uid
|
* @param orderType
|
* @param orderChannel
|
* @throws Exception
|
*/
|
public void verifyPayCount(long uid, int orderType, OrderChannelEnum orderChannel) throws KeyOrderException {
|
OrderCountTypeEnum orderCountType = null;
|
if (orderType == Constant.ORDER_TYPE_DY) {
|
orderCountType = OrderCountTypeEnum.DY_ORDER_PAY;
|
} else if (orderType == Constant.ORDER_TYPE_KS) {
|
orderCountType = OrderCountTypeEnum.KS_ORDER_PAY;
|
}
|
long todayCount = wxUserOrderCountService.sum(uid, orderCountType, orderChannel, TimeUtil.getGernalTime(System.currentTimeMillis(), Constant.DB_DAY_FORMAT));
|
long totalCount = wxUserOrderCountService.sum(uid, orderCountType, null, null);
|
int maxPayCount = userSettingService.getLimitCountByTotalCount(orderType, totalCount, orderChannel);
|
if (todayCount >= maxPayCount) {
|
throw new KeyOrderException(String.format("老铁今日已达支付次数(%s)上限:%s", orderChannel.getName(), maxPayCount));
|
}
|
}
|
|
/**
|
* 计算用户剩余支付次数
|
*
|
* @param uid
|
*/
|
public Map<Integer,Long> computeUserLeftPayCount(Long uid) {
|
Map<Integer,Long> map=new HashMap<>();
|
OrderCountTypeEnum[] orderTypes = {OrderCountTypeEnum.DY_ORDER_PAY, OrderCountTypeEnum.KS_ORDER_PAY};
|
OrderChannelEnum[] orderChannels = {OrderChannelEnum.cyx, OrderChannelEnum.bps, OrderChannelEnum.own};
|
for (OrderCountTypeEnum type : orderTypes) {
|
List<Long> countList=new ArrayList<>();
|
for (OrderChannelEnum orderChannel : orderChannels) {
|
long todayCount = wxUserOrderCountService.sum(uid, type, orderChannel, TimeUtil.getGernalTime(System.currentTimeMillis(), Constant.DB_DAY_FORMAT));
|
long totalCount = wxUserOrderCountService.sum(uid, type, null, null);
|
int maxPayCount = userSettingService.getLimitCountByTotalCount(type.getType(), totalCount, orderChannel);
|
long leftCount = maxPayCount - todayCount;
|
countList.add(leftCount);
|
}
|
long maxCount=0;
|
for (Long count : countList) {
|
if(count>maxCount){
|
maxCount=count;
|
}
|
}
|
map.put(type.getType(),Math.min( maxCount, 50));
|
}
|
return map;
|
}
|
|
|
}
|