admin
2024-07-29 734dfe9eb0a2176103dce8245c69b1194574c68e
src/main/java/com/taoke/autopay/service/impl/KeyOrderServiceImpl.java
@@ -2,25 +2,33 @@
import com.taoke.autopay.dao.KeyOrderMapper;
import com.taoke.autopay.dao.WxUserSettingsMapper;
import com.taoke.autopay.entity.KeyOrder;
import com.taoke.autopay.entity.OrderCountTypeEnum;
import com.taoke.autopay.entity.OrderDistributeCountInfo;
import com.taoke.autopay.entity.WxUserSettings;
import com.taoke.autopay.dto.ChannelOrderStatistic;
import com.taoke.autopay.dto.DYOrderDto;
import com.taoke.autopay.entity.*;
import com.taoke.autopay.entity.agent.ChannelAgent;
import com.taoke.autopay.exception.KeyOrderException;
import com.taoke.autopay.exception.KeyVerifyException;
import com.taoke.autopay.exception.WxOrderCountException;
import com.taoke.autopay.factory.OrderFactory;
import com.taoke.autopay.service.KeyOrderService;
import com.taoke.autopay.service.SystemConfigService;
import com.taoke.autopay.service.WxUserOrderCountService;
import com.taoke.autopay.service.WxUserSettingService;
import com.taoke.autopay.utils.StringUtil;
import com.taoke.autopay.utils.TimeUtil;
import com.taoke.autopay.service.agent.ChannelAgentService;
import com.taoke.autopay.service.agent.ChannelAgentSharingRatioService;
import com.taoke.autopay.utils.*;
import com.taoke.autopay.utils.order.DYOrderApi;
import com.taoke.autopay.vo.SubmitKeyInfo;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.net.URI;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * @author hxh
@@ -39,6 +47,15 @@
    @Resource
    private WxUserOrderCountService wxUserOrderCountService;
    @Resource
    private SystemConfigService systemConfigService;
    @Resource
    private ChannelAgentService channelAgentService;
    @Resource
    private ChannelAgentSharingRatioService channelAgentSharingRatioService;
    @Override
@@ -60,9 +77,27 @@
            throw new KeyOrderException("请勿重复提交口令");
        }
        order = new KeyOrder();
        // 解析referer中的链接
        if (!StringUtil.isNullOrEmpty(keyInfo.getReferer())) {
            Map<String, String> paramsMap = HttpUtil.getPramsFromUrl(keyInfo.getReferer());
            if (paramsMap.containsKey("a")) {
                String alias = paramsMap.get("a");
                ChannelAgent agent = channelAgentService.selectByAlias(alias);
                if (agent == null) {
                    throw new KeyOrderException("渠道不存在");
                }
                if (agent.getStatus() != ChannelAgent.STATUS_NOMAL) {
                    throw new KeyOrderException("渠道受限");
                }
                order.setAgentId(agent.getId());
            }
        }
        order.setId(id);
        order.setKey(keyInfo.getKey());
        if(!StringUtil.isNullOrEmpty(keyInfo.getMoney())){
        if (!StringUtil.isNullOrEmpty(keyInfo.getMoney())) {
            order.setOrderMoney(new BigDecimal(keyInfo.getMoney()));
        }
        order.setUid(uid);
@@ -93,22 +128,22 @@
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void paySuccess(String id, String stateDesc,String day) throws WxOrderCountException {
    public void paySuccess(String id, String stateDesc, String day) throws WxOrderCountException {
        KeyOrder old = keyOrderMapper.selectByPrimaryKeyForUpdate(id);
        if(old==null){
           return;
        }
        if(old.getState() == KeyOrder.STATE_PAY){
        if (old == null) {
            return;
        }
        if(old.getUid()!=null) {
            wxUserOrderCountService.addOrderCount(old.getUid(),OrderCountTypeEnum.DY_ORDER_PAY,day,1,null);
        if (old.getState() == KeyOrder.STATE_PAY) {
            return;
        }
        if (old.getUid() != null) {
            wxUserOrderCountService.addOrderCount(old.getUid(), OrderCountTypeEnum.DY_ORDER_PAY, day, 1, null);
        }
        KeyOrder orderUpdate = new KeyOrder();
        orderUpdate.setId(id);
        orderUpdate.setState(KeyOrder.STATE_PAY);
        orderUpdate.setStateDesc(stateDesc);
        if(old.getPayTime()==null){
        if (old.getPayTime() == null) {
            orderUpdate.setPayTime(new Date());
        }
        update(orderUpdate);
@@ -144,9 +179,56 @@
    }
    @Override
    public List<KeyOrder> listWithUser(KeyOrderMapper.DaoQuery query) {
        return keyOrderMapper.listWithUser(query);
    }
    @Override
    public long countWithUser(KeyOrderMapper.DaoQuery query) {
        return keyOrderMapper.countWithUser(query);
    }
    @Override
    public ChannelOrderStatistic statisticWithUser(KeyOrderMapper.DaoQuery query) {
        Map<OrderChannelEnum, BigDecimal> shareMoneyMap = channelAgentSharingRatioService.getShareMoneyMap(query.agentId);
        List<ChannelOrderStatistic> channelOrderStatisticList = keyOrderMapper.statisticChannelOrders(query);
        BigDecimal totalMoney = new BigDecimal(0);
        for (ChannelOrderStatistic s : channelOrderStatisticList) {
            for (OrderChannelEnum channel : OrderChannelEnum.values()) {
                if (channel.getKey().equalsIgnoreCase(s.getOrderChannel())) {
                    if (shareMoneyMap.containsKey(channel)) {
                        totalMoney = totalMoney.add(shareMoneyMap.get(channel).multiply(new BigDecimal(s.getCount())));
                    }
                    break;
                }
            }
        }
        long userCount = keyOrderMapper.countUser(query);
        ChannelOrderStatistic statistic = new ChannelOrderStatistic();
        statistic.setCount(userCount);
        statistic.setMoney(totalMoney);
        return statistic;
    }
    @Override
    public List<ChannelOrderStatistic> statisticChannelOrders(Long agentId, Date startTime, Date endTime) {
        KeyOrderMapper.DaoQuery daoQuery=new KeyOrderMapper.DaoQuery();
        daoQuery.oMinCreateTime=startTime;
        daoQuery.oMaxCreateTime=endTime;
        daoQuery.agentId = agentId;
        daoQuery.state =KeyOrder.STATE_PAY;
        daoQuery.hasPayTime=true;
        return keyOrderMapper.statisticChannelOrders(daoQuery);
    }
    @Override
    public Long getCanDistributeUid() {
        // 最近1小时有活跃,且不算12以上未执行的数据
        List<OrderDistributeCountInfo> list = keyOrderMapper.listDistributeUids(new Date(System.currentTimeMillis() - 1000 * 60 * 60L),new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 12L));
        List<OrderDistributeCountInfo> list = keyOrderMapper.listDistributeUids(new Date(System.currentTimeMillis() - 1000 * 60 * 60L), new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 12L));
        if (list == null || list.size() == 0) {
            return null;
        }
@@ -194,4 +276,83 @@
    public void deleteAll(Date maxCreateTime) {
        keyOrderMapper.deleteAll(maxCreateTime);
    }
    @Override
    public DYOrderDto verifyKey(String orderNoDesc, String orderStatus, String money) throws KeyVerifyException {
        int orderType = Constant.ORDER_TYPE_UNKNOWN;
        if (orderNoDesc.contains("抖音")||orderNoDesc.contains("上海格物致品")) {
            orderType = Constant.ORDER_TYPE_DY;
        } else if (orderNoDesc.contains("快手")) {
            orderType = Constant.ORDER_TYPE_KS;
        }
        String orderNo = "";
        // 匹配连续的数字
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(orderNoDesc);
        while (matcher.find()) {
            // 获取匹配到的数字字符串
            String number = matcher.group();
            if (number.length() > 10) {
                orderNo = number;
                break;
            }
        }
        if (orderType == Constant.ORDER_TYPE_UNKNOWN) {
            throw new KeyVerifyException(KeyVerifyException.CODE_ORDER_TYPE_ERROR, "未定义的订单类型");
        }
        if (!StringUtil.isNullOrEmpty(orderStatus) && !orderStatus.contains("未支付")) {
            throw new KeyVerifyException(KeyVerifyException.CODE_ORDER_STATUS_ERROR, "订单状态:" + orderStatus);
        }
        DYOrderDto dyOrderDto = null;
        // 先匹配订单
        try {
            if (orderType == Constant.ORDER_TYPE_DY && !StringUtil.isNullOrEmpty(orderNo)) {
                dyOrderDto = DYOrderApi.getOrderDetail(orderNo);
                if (dyOrderDto != null) {
                    // 验证金额
                    if (dyOrderDto.getPay_amount().intValue() != new BigDecimal(money).multiply(new BigDecimal(100)).setScale(0).intValue()) {
                        throw new KeyVerifyException(KeyVerifyException.CODE_ORDER_MONEY_NOT_MATCH, String.format("支付金额与订单金额不一致:%s-%d", money, dyOrderDto.getPay_amount()));
                    }
                }
            } else {
                throw new KeyOrderException("抖音订单获取失败");
            }
        } catch (KeyOrderException e) {
            // 抖音订单未验证通过,匹配金额
            // 验证提交的金额是否正确
            String moneyStr = systemConfigService.getValueCache(SystemConfigKeyEnum.PAY_MONEY_LIST);
            if (StringUtil.isNullOrEmpty(moneyStr)) {
                throw new KeyVerifyException(KeyVerifyException.CODE_KEY_MONEY_NOT_MATCH, "尚未配置金额");
            }
            JSONArray array = JSONArray.fromObject(moneyStr);
            Set<String> moneySet = new HashSet<>();
            for (int i = 0; i < array.size(); i++) {
                moneySet.add(MoneyUtil.getMoneyStr(new BigDecimal(array.optString(i))));
            }
            // 匹配金额
            if (!moneySet.contains(money)) {
                throw new KeyVerifyException(KeyVerifyException.CODE_KEY_MONEY_NOT_MATCH, String.format("金额未在系统设置中:%s", money));
            }
        }
        return dyOrderDto;
    }
    @Override
    public void removeDistributedClient(String id) {
        keyOrderMapper.removeDistributedClient(id);
    }
    @Override
    public List<Long> listAgentId(KeyOrderMapper.DaoQuery query) {
        return keyOrderMapper.listAgentId(query);
    }
    @Override
    public long countAgentId(KeyOrderMapper.DaoQuery query) {
        return keyOrderMapper.countAgentId(query);
    }
}