Quellcode durchsuchen

支付通道路由

yubin vor 1 Tag
Ursprung
Commit
68b4f98194

+ 3 - 0
cif-service/src/main/java/com/txz/cif/service/PaymentChannelService.java

@@ -10,4 +10,7 @@ public interface PaymentChannelService extends Service<PaymentChannel> {
     int add(PaymentChannel model);
 
     int modify(PaymentChannel model);
+
+    PaymentChannel paymentChannelReturnsByWeight(Long methodId);
+
 }

+ 49 - 1
cif-service/src/main/java/com/txz/cif/service/impl/PaymentChannelServiceImpl.java

@@ -2,8 +2,10 @@ package com.txz.cif.service.impl;
 
 import cn.hutool.core.date.DateTime;
 import cn.hutool.core.date.DateUtil;
+import com.txz.cif.core.ServiceException;
 import com.txz.cif.dao.MidChannelMethodMapper;
 import com.txz.cif.dao.PaymentChannelMapper;
+import com.txz.cif.model.DayCut;
 import com.txz.cif.model.MidChannelMethod;
 import com.txz.cif.model.PaymentChannel;
 
@@ -13,7 +15,9 @@ import com.txz.cif.core.AbstractService;
 import com.txz.cif.service.PaymentMethodService;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.CollectionUtils;
 import org.springframework.util.ObjectUtils;
+import tk.mybatis.mapper.entity.Condition;
 import tk.mybatis.mapper.entity.Example;
 
 import javax.annotation.Resource;
@@ -80,9 +84,53 @@ public class PaymentChannelServiceImpl extends AbstractService<PaymentChannel> i
         return cPaymentChannelMapper.updateByPrimaryKeySelective(model);
     }
 
+    @Override
+    public PaymentChannel paymentChannelReturnsByWeight(Long methodId) {
+
+
+        Condition condition = new Condition(MidChannelMethod.class);
+        Example.Criteria criteria = condition.createCriteria();
+        criteria.andEqualTo("method_id",methodId);
+        criteria.andEqualTo("agency_service",1);
+        criteria.andEqualTo("status",1);
+        criteria.andEqualTo("is_valid",1);
+        List<MidChannelMethod> midChannelMethodList = midChannelMethodService.findByCondition(condition);
+        if(CollectionUtils.isEmpty(midChannelMethodList)){
+            throw new ServiceException("该支付方式没有合适的通道请换一种支付方式");
+        }
+
+
+            double randomNum = Math.random();
+            double sum = midChannelMethodList.stream().mapToDouble(MidChannelMethod::getWeight).sum();
+
+            MidChannelMethod midChannelMethodResult = midChannelMethodList.get(0);
+            double cumulativeProbability = 0.0;
+            for (MidChannelMethod midChannelMethod : midChannelMethodList) {
+                // 计算当前订单金额占总金额的比例(概率)
+                double probability = midChannelMethod.getWeight() / sum;
+                cumulativeProbability += probability;
+
+                // 如果随机数落在当前订单的概率区间内,则返回该订单
+                if (randomNum <= cumulativeProbability) {
+                    midChannelMethodResult = midChannelMethod;
+                }
+            }
+
+        Long channelId = midChannelMethodResult.getChannelId();
+
+        Condition conditionPaymentChannel = new Condition(PaymentChannel.class);
+        Example.Criteria criteriaPaymentChannel = conditionPaymentChannel.createCriteria();
+        criteriaPaymentChannel.andEqualTo("id",channelId);
+        criteriaPaymentChannel.andEqualTo("is_valid",1);
+        List<PaymentChannel> byCondition = findByCondition(conditionPaymentChannel);
+        if(CollectionUtils.isEmpty(byCondition)){
+            throw new ServiceException("通道不可用请换一种支付方式");
+        }
+        return byCondition.get(0);
+    }
 
 
-  private  List<MidChannelMethod> createMidChannelMethodList(Long paymentChannelId,DateTime date){
+    private  List<MidChannelMethod> createMidChannelMethodList(Long paymentChannelId,DateTime date){
       List<MidChannelMethod>  midChannelMethodList = new ArrayList<>();
       MidChannelMethod midChannelMethod = new MidChannelMethod();
 

+ 163 - 0
cif-service/src/main/java/com/txz/cif/web/mng/appcontroller/PaymentMethodAppController.java

@@ -0,0 +1,163 @@
+package com.txz.cif.web.mng.appcontroller;
+
+import cn.hutool.core.bean.BeanUtil;
+import com.txz.cif.core.Result;
+import com.txz.cif.core.ResultCode;
+import com.txz.cif.core.ResultGenerator;
+import com.txz.cif.model.MidChannelMethod;
+import com.txz.cif.model.PaymentChannel;
+import com.txz.cif.model.PaymentMethod;
+import com.txz.cif.service.MidChannelMethodService;
+import com.txz.cif.service.PaymentChannelService;
+import com.txz.cif.service.PaymentMethodService;
+import com.txz.cif.web.bo.MidChannelMethodBO;
+import com.txz.cif.web.bo.PaymentMethodBO;
+import com.txz.cif.web.para.PaymentMethodParam;
+import com.txz.cif.web.para.updateparam.PaymentMethodUpdateParam;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.ObjectUtils;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+
+/**
+ * Created by CodeGenerator on 2025/08/04.
+ */
+@Api(tags = "[App]paymentMethod管理")
+@RestController
+@RequestMapping("/app/payment/method")
+public class PaymentMethodAppController {
+
+    private static Logger log = LoggerFactory.getLogger(PaymentMethodAppController.class);
+
+    @Resource
+    private PaymentMethodService paymentMethodService;
+
+    @Resource
+    private PaymentChannelService paymentChannelService;
+
+//    @PostMapping("/add")
+//    @ApiOperation(value = "paymentMethod新增", httpMethod = "POST")
+//    public Result add(@RequestBody PaymentMethod paymentMethod) {
+//        if (paymentMethod == null) {
+//            return ResultGenerator.genFailResult(ResultCode.OBJECT_IS_NULL);
+//        }
+//        try {
+//            //		paymentMethod.setCreateTime(new Date());
+//            //		paymentMethod.setCreateUserId(userId);
+//            paymentMethodService.save(paymentMethod);
+//        } catch (Exception e) {
+//            log.error("新增对象操作异常e:{}", e);
+//            return ResultGenerator.genFailResult(ResultCode.INTERNAL_SERVER_ERROR);
+//        }
+//
+//        return ResultGenerator.genSuccessResult(paymentMethod);
+//    }
+
+//    @GetMapping("/delete")
+//    @ApiOperation(value = "paymentMethod删除", httpMethod = "GET")
+//    public Result delete(@RequestParam Integer id) {
+//        if (id == null) {
+//            return ResultGenerator.genFailResult(ResultCode.ID_IS_NULL);
+//        }
+//        try {
+//            paymentMethodService.deleteById(id);
+//        } catch (Exception e) {
+//            log.error("删除对象操作异常e:{}", e);
+//            return ResultGenerator.genFailResult(ResultCode.INTERNAL_SERVER_ERROR);
+//        }
+//        return ResultGenerator.genSuccessResult();
+//    }
+
+//    @PostMapping("/update")
+//    @ApiOperation(value = "支付方式启用/关停", httpMethod = "POST")
+//    public Result update(@RequestBody PaymentMethodUpdateParam paymentMethodUpdateParam) {
+//        if (paymentMethodUpdateParam == null) {
+//            return ResultGenerator.genFailResult(ResultCode.OBJECT_IS_NULL);
+//        }
+//        if (paymentMethodUpdateParam.getId() == null) {
+//            return ResultGenerator.genFailResult(ResultCode.ID_IS_NULL);
+//        }
+//
+//        PaymentMethod bean = BeanUtil.toBean(paymentMethodUpdateParam, PaymentMethod.class);
+//        try {
+//
+//            bean.setUpdateUser("lala");
+//
+//            paymentMethodService.update(bean);
+//        } catch (Exception e) {
+//            log.error("更新对象操作异常e:{}", e);
+//            return ResultGenerator.genFailResult(ResultCode.INTERNAL_SERVER_ERROR);
+//        }
+//        return ResultGenerator.genSuccessResult();
+//    }
+
+//    @GetMapping("/detail")
+//    @ApiOperation(value = "paymentMethod获取详情", httpMethod = "GET")
+//    public Result<PaymentMethod> detail(@RequestParam Integer id) {
+//        if (id == null) {
+//            return ResultGenerator.genFailResult(ResultCode.ID_IS_NULL);
+//        }
+//
+//        PaymentMethod paymentMethod = null;
+//        try {
+//            paymentMethod = paymentMethodService.findById(id);
+//        } catch (Exception e) {
+//            log.error("查询对象操作异常e:{}", e);
+//            return ResultGenerator.genFailResult(ResultCode.INTERNAL_SERVER_ERROR);
+//        }
+//
+//        return ResultGenerator.genSuccessResult(paymentMethod);
+//    }
+
+
+
+
+
+    @GetMapping("/getpaymentchannel")
+    @ApiOperation(value = "app端getpaymentchannel", httpMethod = "GET")
+    public Result getpaymentchannel(@RequestParam("id") Long id) {
+
+        if(ObjectUtils.isEmpty(id)){
+            return ResultGenerator.genFailResult(ResultCode.OBJECT_IS_NULL);
+        }
+        PaymentChannel paymentChannel;
+        try {
+             paymentChannel = paymentChannelService.paymentChannelReturnsByWeight(id);
+        } catch (Exception e) {
+            log.error("查询对象操作异常e:{}", e);
+            return ResultGenerator.genFailResult(ResultCode.INTERNAL_SERVER_ERROR);
+        }
+        return ResultGenerator.genSuccessResult(paymentChannel);
+    }
+
+    @PostMapping("/list")
+    @ApiOperation(value = "app端paymentMethod获取列表", httpMethod = "POST")
+    public Result<List<PaymentMethodBO>> list(@RequestBody PaymentMethodParam paymentMethodParam) {
+
+        List<PaymentMethodBO> paymentMethodListBO = new ArrayList<>();
+        try {
+            List<PaymentMethod> paymentMethodList = paymentMethodService.findAll();
+            if (!CollectionUtils.isEmpty(paymentMethodList)) {
+                Byte status = 1;
+                paymentMethodListBO = paymentMethodList.stream().filter(a -> status.equals(a.getIsValid())).map(a -> {
+                            return BeanUtil.toBean(a, PaymentMethodBO.class);
+                }).collect(Collectors.toList());
+            }
+        } catch (Exception e) {
+            log.error("查询对象操作异常e:{}", e);
+            return ResultGenerator.genFailResult(ResultCode.INTERNAL_SERVER_ERROR);
+        }
+        return ResultGenerator.genSuccessResult(paymentMethodListBO);
+    }
+}