xuhy
2026-02-05 337d48dbcb60bacb9bad2d4a0c3f2c34b132ef73
ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TOrderController.java
@@ -7,9 +7,12 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.ImmutableMap;
import com.lkl.laop.sdk.exception.SDKException;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.framework.web.service.TokenService;
@@ -19,7 +22,11 @@
import com.ruoyi.system.service.TCrmSupplierService;
import com.ruoyi.system.service.TErpProcurementGoodsService;
import com.ruoyi.system.service.TErpProcurementService;
import com.ruoyi.system.task.base.QuartzManager;
import com.ruoyi.system.task.base.TimeJobType;
import com.ruoyi.system.task.jobs.RevenueSharingJob;
import com.ruoyi.web.core.config.LakalaConfig;
import com.ruoyi.web.listener.event.TradeOrderEvent;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
@@ -34,6 +41,7 @@
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@@ -76,14 +84,15 @@
    private final TCrmSupplierService crmSupplierService;
    private final TokenService tokenService;
    private final ApplicationContext applicationContext;
    @Autowired
    public TOrderController(TErpProcurementService erpProcurementService, TokenService tokenService, TErpProcurementGoodsService erpProcurementGoodsService, TCrmSupplierService crmSupplierService) {
    public TOrderController(TErpProcurementService erpProcurementService, TokenService tokenService, TErpProcurementGoodsService erpProcurementGoodsService, TCrmSupplierService crmSupplierService, ApplicationContext applicationContext) {
        this.erpProcurementService = erpProcurementService;
        this.tokenService = tokenService;
        this.erpProcurementGoodsService = erpProcurementGoodsService;
        this.crmSupplierService = crmSupplierService;
        this.applicationContext = applicationContext;
    }
    private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
@@ -105,6 +114,7 @@
    @ApiOperation(value = "支付")
    @PostMapping(value = "/payOrder")
    @SneakyThrows
    @Log(title = "采购管理-支付", businessType = BusinessType.UPDATE)
    public R<?> payOrder(@RequestParam String id, String type, HttpServletRequest request) {
        try {
@@ -131,7 +141,7 @@
            }
            String apiPath = "sit/api/v3/labs/trans/preorder";
            String apiPath = "api/v3/labs/trans/preorder";
            String body = "{\n" +
                    "\t\"req_time\": \"" + time + "\",\n" +
                    "\t\"version\": \"3.0\",\n" +
@@ -139,7 +149,7 @@
                    "\t\t\"out_trade_no\": \"" + outTradeNo + "\",\n" +
                    "\t\t\"merchant_no\": \""+supplier.getRecvMerchantNo()+"\",\n" +
                    "\t\t\"term_no\": \""+supplier.getTermNo()+"\",\n" +
                    "\t\t\"notify_url\": \"http://221.182.45.100:8089/t-sys-order/messageHandle\",\n" +
                    "\t\t\"notify_url\": \"https://haizhentong.com/manage/t-sys-order/messageHandle\",\n" +
                    "\t\t\"location_info\": {\n" +
                    "\t\t\t\"request_ip\": \"" + ipAddr + "\"\n" +
                    "\t\t},\n" +
@@ -152,12 +162,14 @@
                    "}";
            String authorization = getAuthorization(body);
            System.err.println("支付请求"+body);
            org.apache.http.HttpResponse response = post(LakalaConfig.getServerUrl() + apiPath, body, authorization);
            if (response.getStatusLine().getStatusCode() != 200) {
                return R.fail(500, "请求失败,statusCode  " + response.getStatusLine()
                        + IOUtils.toString(response.getEntity().getContent(), ENCODING));
            }
            String responseStr = IOUtils.toString(response.getEntity().getContent(), ENCODING);
            System.err.println("支付结果"+responseStr);
            erpProcurement.setPayNumber(outTradeNo);
@@ -217,7 +229,7 @@
            // 操作分账
            HttpRequest post = HttpUtil.createPost(LakalaConfig.getServerUrl() + "sit/api/v3/sacs/separate");
            HttpRequest post = HttpUtil.createPost(LakalaConfig.getServerUrl() + "api/v3/sacs/separate");
            HashMap<String, Object> reqMap = new HashMap<>();
            reqMap.put("version", "3.0");
@@ -228,7 +240,8 @@
            map.put("merchant_no", erpProcurement.getMerchantNo());
            map.put("log_no", erpProcurement.getPayTransactionId());
            map.put("log_date", DateUtils.dateTime());
            map.put("notify_url", "http://221.182.45.100:8089/t-sys-order/messageSeparateHandle");
            map.put("cal_type", "0");
            map.put("notify_url", "https://haizhentong.com/manage/t-sys-order/messageSeparateHandle");
            String time = DateUtils.dateTimeNow();
            // SYMBOLS 随机取4位数
@@ -246,22 +259,10 @@
            // 平台应得分账
            BigDecimal payMoney = erpProcurement.getMoney();
            ArrayList<HashMap<String, Object>> objects = new ArrayList<>();
            // 根据供应商id分组
            Map<String, List<TErpProcurementGoods>> collect = list.stream().collect(Collectors.groupingBy(TErpProcurementGoods::getSupplierId));
//            for (Map.Entry<String, List<TErpProcurementGoods>> entry : collect.entrySet()) {
//                HashMap<String, Object> map1 = new HashMap<>();
//                String supplierId = entry.getKey();
//                TCrmSupplier supplier = crmSupplierService.getById(supplierId);
//                List<TErpProcurementGoods> value = entry.getValue();
//                BigDecimal reduce = value.stream().map(TErpProcurementGoods::getSupplierMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
//                payMoney = payMoney.subtract(reduce);
//                map1.put("recv_no", supplier.getRecvMerchantNo());
//                map1.put("separate_value", reduce.multiply(BigDecimal.valueOf(100)).intValue() + "");
////                objects.add(map1);
//            }
            HashMap<String, Object> map1 = new HashMap<>();
            //TODO hzt
            map1.put("recv_no", "SR2024000006413");
            map1.put("recv_no", "SR2024000144253");
            // 手续费
            BigDecimal bigDecimal = allMoney.multiply(new BigDecimal("0.038")).setScale(2, RoundingMode.HALF_UP);
            payMoney = payMoney.subtract(bigDecimal);
@@ -270,6 +271,25 @@
            if(payMoney.doubleValue()>0){
                objects.add(map1);
            }
            // 根据供应商id分组
            Map<String, List<TErpProcurementGoods>> collect = list.stream().collect(Collectors.groupingBy(TErpProcurementGoods::getSupplierId));
            for (Map.Entry<String, List<TErpProcurementGoods>> entry : collect.entrySet()) {
                HashMap<String, Object> map2 = new HashMap<>();
                String supplierId = entry.getKey();
                TCrmSupplier supplier = crmSupplierService.getById(supplierId);
//                List<TErpProcurementGoods> value = entry.getValue();
//                BigDecimal reduce = value.stream().map(TErpProcurementGoods::getSupplierMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
//                payMoney = payMoney.subtract(reduce);
                map2.put("recv_merchant_no", supplier.getRecvMerchantNo());
                int count = allMoney.subtract(payMoney).multiply(BigDecimal.valueOf(100)).intValue();
                if(count>0){
                    map2.put("separate_value", count+ "");
                    objects.add(map2);
                }
            }
            erpProcurement.setMoney(payMoney);
            erpProcurement.setOutSeparateNo(outTradeNo);
            erpProcurementService.updateById(erpProcurement);
@@ -279,6 +299,7 @@
            reqMap.put("req_data", map);
            String jsonString = JSON.toJSONString(reqMap);
//            String authorization1 = getAuthorization(jsonString);
//            post.header("Authorization", SCHEMA + " " + authorization);
@@ -286,20 +307,34 @@
//            post.body(jsonString);
//            HttpResponse response = post.execute();
//            String responseStr = response.body();
            String authorization1 = getAuthorization(jsonString);
            org.apache.http.HttpResponse response1 = post(LakalaConfig.getServerUrl() + "sit/api/v3/sacs/separate", jsonString, authorization1);
            String responseStr = IOUtils.toString(response1.getEntity().getContent(), ENCODING);
            Map<String, ? extends Object> maps =
                    new ImmutableMap.Builder<String, String>().
                            put("id", erpProcurement.getId()).put("authorization", authorization1).put("body", jsonString)
                            .build();
            QuartzManager.addJob(
                    RevenueSharingJob.class,
                    (RevenueSharingJob.name+erpProcurement.getId()).toUpperCase(),
                    TimeJobType.REVENUE_SHARING,
                    DateUtils.localDateTimeToDate(LocalDateTime.now().plusSeconds(30)),
                    maps
            );
            JSONObject jsonObject = JSONObject.parseObject(responseStr);
            if ("成功".equals(jsonObject.getString("msg"))) {
                Object o1 = jsonObject.get("resp_data");
                JSONObject jsonObject2 = JSONObject.parseObject(o1.toString());
                String separate_no = jsonObject2.getString("separate_no");
                erpProcurement.setSeparateNo(separate_no);
                erpProcurementService.updateById(erpProcurement);
            }
//            org.apache.http.HttpResponse response1 = post(LakalaConfig.getServerUrl() + "api/v3/sacs/separate", jsonString, authorization1);
//            String responseStr = IOUtils.toString(response1.getEntity().getContent(), ENCODING);
//            System.err.println("分佣!!!!"+responseStr);
//            JSONObject jsonObject = JSONObject.parseObject(responseStr);
//            if ("成功".equals(jsonObject.getString("msg"))) {
//                Object o1 = jsonObject.get("resp_data");
//                JSONObject jsonObject2 = JSONObject.parseObject(o1.toString());
//                String separate_no = jsonObject2.getString("separate_no");
//                erpProcurement.setSeparateNo(separate_no);
//                erpProcurementService.updateById(erpProcurement);
//            }
            // 执行的推送创建云仓订单
            applicationContext.publishEvent(new TradeOrderEvent(erpProcurement.getId()+"_1"));
            // 响应success
            JSONObject responseJsonObject = new JSONObject();
@@ -313,6 +348,8 @@
    @RequestMapping("/messageSeparateHandle")
    @ApiOperation(value = "拉卡拉分账信息回调接口")
    public Object messageSeparateHandle(HttpServletRequest request) throws Exception {
        System.err.println("拉卡拉分账信息回调接口---------进入");
        String body = this.getBody(request);
        // 先处理支付完成  修改状态 后处理分账  后走分账回调
        JSONObject jsonObject1 = JSONObject.parseObject(body);
@@ -323,8 +360,9 @@
        JSONArray jsonArray = JSONArray.parseArray(detail_datas.toString());
        if("SEPARATE".equals(cmd_type.toString())){
            TErpProcurement erpProcurement = erpProcurementService.getOne(new LambdaQueryWrapper<TErpProcurement>().eq(TErpProcurement::getOutSeparateNo, o.toString()));
            if ( erpProcurement!=null && "SUCCESS".equals(status)) {
            if ( erpProcurement!=null && "SUCCESS".equals(status) && erpProcurement.getRefundStatus()==1) {
                erpProcurement.setRefundStatus(1);
                erpProcurement.setIsPlatformCommission(1);
                erpProcurementService.updateById(erpProcurement);
                // 修改分账状态
                List<TErpProcurementGoods> list1 = erpProcurementGoodsService.list(new LambdaQueryWrapper<TErpProcurementGoods>().eq(TErpProcurementGoods::getProcurementId, erpProcurement.getId()));
@@ -336,11 +374,12 @@
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("code", "SUCCESS");
                jsonObject.put("message", "执行成功");
                System.err.println("拉卡拉分账信息回调接口---------完成");
                return jsonObject;
            }
        }else if("FALLBACK".equals(cmd_type.toString())){
            TErpProcurement erpProcurement = erpProcurementService.getOne(new LambdaQueryWrapper<TErpProcurement>().eq(TErpProcurement::getRefundOutSeparateNo, o.toString()));
            if ( erpProcurement!=null && "SUCCESS".equals(status) && erpProcurement.getRefundStatus()==1) {
            if ( erpProcurement!=null && "SUCCESS".equals(status) && erpProcurement.getRefundStatus()!=2) {
                erpProcurement.setRefundStatus(2);
                erpProcurementService.updateById(erpProcurement);
                List<TErpProcurementGoods> list1 = erpProcurementGoodsService.list(new LambdaQueryWrapper<TErpProcurementGoods>().eq(TErpProcurementGoods::getProcurementId, erpProcurement.getId()));
@@ -359,23 +398,29 @@
                String outTradeNo = time + str;
                String refund_amount = erpProcurement.getPayMoney().multiply(BigDecimal.valueOf(100)).intValue() + "";
                String apiPath = "sit/api/v3/rfd/refund_front/refund";
                String apiPath = "api/v3/rfd/refund_front/refund";
                String body1 = "{\n" +
                        "\t\"req_time\": \"" + time + "\",\n" +
                        "\t\"version\": \"3.0\",\n" +
                        "\t\"req_data\": {\n" +
                        "\t\t\"merchant_no\": \""+erpProcurement.getMerchantNo()+"\",\n" +
                        "\t\t\"term_no\": \""+erpProcurement.getTermNo()+"\",\n" +
                        "\t\t\"refund_amount\": " + refund_amount + ",\n" +
                        "\t\t\"out_trade_no\": \"" + outTradeNo + "\",\n" +
                        "\t\t\"notify_url\": \"" + "http://221.182.45.100:8089/t-sys-order/messageRefundHandle" + "\",\n" +
                        "\t\t\"origin_log_no\": \"" + erpProcurement.getPayTransactionId() + "\",\n" +
                        "\t\t\"refund_amount\": \"" + refund_amount + "\",\n" +
                        "\t\t\"refund_reason\": \"退款\",\n" +
                        "\t\t\"notify_url\": \"" + "https://haizhentong.com/manage/t-sys-order/messageRefundHandle" + "\",\n" +
                        "\t\t\"origin_out_trade_no\": \"" + erpProcurement.getPayNumber() + "\",\n" +
                        "\t\t\"refund_acc_mode\": \"00\",\n" +
                        "\t\t\"refund_amt_sts\": \"00\",\n" +
                        "\t\t\"location_info\": {\n" +
                        "\t\t\t\"request_ip\": \"" + ipAddr + "\"\n" +
                        "\t\t\t\"request_ip\": \"" + ipAddr + "\",\n" +
                        "\t\t\t\"location\": \"\"\n" +
                        "\t\t}\n" +
                        "\t\t},\n" +
                        "\t\"version\": \"3.0\",\n" +
                        "\t\"req_time\": \"" + time + "\"\n" +
                        "\t}\n" +
                        "}";
                String authorization = getAuthorization(body1);
                org.apache.http.HttpResponse response = post(LakalaConfig.getServerUrl() + apiPath, body1, authorization);
                if (response.getStatusLine().getStatusCode() != 200) {
                    return R.fail(500, "请求失败,statusCode  " + response.getStatusLine()
@@ -389,7 +434,7 @@
                erpProcurement.setRefundNo(outTradeNo);
                erpProcurementService.updateById(erpProcurement);
                System.err.println("拉卡拉分账信息回调接口---------完成"+responseStr);
                // 响应success
@@ -405,6 +450,7 @@
    @RequestMapping("/messageRefundHandle")
    @ApiOperation(value = "拉卡拉退款信息回调接口")
    public Object messageRefundHandle(HttpServletRequest request) throws Exception {
        System.err.println("拉卡拉退款信息回调接口---------进入");
        String body = this.getBody(request);
        // 先处理支付完成  修改状态 后处理分账  后走分账回调
        JSONObject jsonObject1 = JSONObject.parseObject(body);
@@ -415,6 +461,8 @@
            if ( erpProcurement!=null && "SUCCESS".equals(status)) {
                erpProcurement.setStatus(6);
                erpProcurementService.updateById(erpProcurement);
                // 执行的推送取消云仓订单
                applicationContext.publishEvent(new TradeOrderEvent(erpProcurement.getId()+"_2"));
                // 响应success
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("code", "SUCCESS");
@@ -431,17 +479,78 @@
    @ApiOperation(value = "退款")
    @PostMapping(value = "/refundOrder")
    @SneakyThrows
    public R<?> refundOrder(@RequestParam String id, HttpServletRequest request) {
    @Log(title = "采购管理-退款", businessType = BusinessType.UPDATE)
    public synchronized R<?> refundOrder(@RequestParam String id, HttpServletRequest request) {
        SysUser user = tokenService.getLoginUser().getUser();
        try {
            // 先退分账  在退订单
            // 查出采购单 算出价格  下单  算出应该分佣金额 应该分给谁
            TErpProcurement erpProcurement = erpProcurementService.getById(id);
            // 还未分账成功不能退款
            if(erpProcurement.getStatus()==2){
                erpProcurement.setStatus(6);
                erpProcurementService.updateById(erpProcurement);
                return R.ok("操作成功");
            }
        if(erpProcurement.getStatus()==null || erpProcurement.getStatus()!=1){
            return R.fail("订单处理中,请稍后操作");
        }
            // 判断回退没有回退了直接退款
            long count = erpProcurementGoodsService.count(new LambdaQueryWrapper<TErpProcurementGoods>().eq(TErpProcurementGoods::getProcurementId, id).eq(TErpProcurementGoods::getStatus, 1));
            if(count==0){
                // 发起退款
                String time = DateUtils.dateTimeNow();
                String ipAddr = IpUtils.getIpAddr(request);
                // SYMBOLS 随机取4位数
                String str = "";
                for (int i = 0; i < 6; i++) {
                    str += SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
                }
                String outTradeNo = time + str;
                String refund_amount = erpProcurement.getPayMoney().multiply(BigDecimal.valueOf(100)).intValue() + "";
                String apiPath = "api/v3/rfd/refund_front/refund";
                String body1 = "{\n" +
                        "\t\"req_data\": {\n" +
                        "\t\t\"merchant_no\": \""+erpProcurement.getMerchantNo()+"\",\n" +
                        "\t\t\"term_no\": \""+erpProcurement.getTermNo()+"\",\n" +
                        "\t\t\"out_trade_no\": \"" + outTradeNo + "\",\n" +
                        "\t\t\"refund_amount\": \"" + refund_amount + "\",\n" +
                        "\t\t\"refund_reason\": \"退款\",\n" +
                        "\t\t\"notify_url\": \"" + "https://haizhentong.com/manage/t-sys-order/messageRefundHandle" + "\",\n" +
                        "\t\t\"origin_out_trade_no\": \"" + erpProcurement.getPayNumber() + "\",\n" +
                        "\t\t\"refund_acc_mode\": \"00\",\n" +
                        "\t\t\"refund_amt_sts\": \"00\",\n" +
                        "\t\t\"location_info\": {\n" +
                        "\t\t\t\"request_ip\": \"" + ipAddr + "\",\n" +
                        "\t\t\t\"location\": \"\"\n" +
                        "\t\t}\n" +
                        "\t\t},\n" +
                        "\t\"version\": \"3.0\",\n" +
                        "\t\"req_time\": \"" + time + "\"\n" +
                        "\t}\n" +
                        "}";
                String authorization = getAuthorization(body1);
                org.apache.http.HttpResponse response = post(LakalaConfig.getServerUrl() + apiPath, body1, authorization);
                if (response.getStatusLine().getStatusCode() != 200) {
                    return R.fail(500, "请求失败,statusCode  " + response.getStatusLine()
                            + IOUtils.toString(response.getEntity().getContent(), ENCODING));
                }
                String responseStr = IOUtils.toString(response.getEntity().getContent(), ENCODING);
                System.out.println("退款!!"+responseStr);
                JSONObject jsonObject = JSONObject.parseObject(responseStr);
                JSONObject jsonObject2 = jsonObject.getJSONObject("resp_data");
                String code = jsonObject2.getString("log_no");
                erpProcurement.setRefundLogNo(code);
                erpProcurement.setRefundNo(outTradeNo);
                erpProcurementService.updateById(erpProcurement);
                return R.ok("操作成功");
            }
                // 还未分账成功不能退款
            if(erpProcurement.getIsPlatformCommission()==2){
                return R.fail("订单处理中,请稍后操作");
            }
            String time = DateUtils.dateTimeNow();
@@ -480,7 +589,7 @@
//            }
            HashMap<String, Object> map1 = new HashMap<>();
            // TODO hzt
            map1.put("recv_no", "SR2024000006413");
            map1.put("recv_no", "SR2024000144253");
            map1.put("amt", erpProcurement.getMoney().multiply(BigDecimal.valueOf(100)).intValue() + "");
            objects.add(map1);
            map.put("total_amt",erpProcurement.getMoney().multiply(BigDecimal.valueOf(100)).intValue() + "");
@@ -491,10 +600,11 @@
            String jsonString = JSON.toJSONString(reqMap);
            String authorization1 = getAuthorization(jsonString);
            org.apache.http.HttpResponse response1 = post(LakalaConfig.getServerUrl() + "sit/api/v3/sacs/fallback", jsonString, authorization1);
            org.apache.http.HttpResponse response1 = post(LakalaConfig.getServerUrl() + "api/v3/sacs/fallback", jsonString, authorization1);
            String responseStr = IOUtils.toString(response1.getEntity().getContent(), ENCODING);
//            post.body(jsonString);
//            HttpResponse response = post.execute();
//            String responseStr = response.body();