chenjiahe
2023-05-11 b3fdeac8c313971112a6245513f7215b39da537d
phi_platform_user/src/main/java/com/hx/phip/tool/refund/OrderRefundCancelTool.java
@@ -54,7 +54,7 @@
    /**处理总退款方式数据*/
    public static void refundRecordMotnedHandle(RefundRecord refundRecord,OrdersTotal ordersTotal,CommonService commonService){
    public static List<RefundRecordMethod> refundRecordMotnedHandle(RefundRecord refundRecord,OrdersTotal ordersTotal,StringBuilder orderNodeBuilder,CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
@@ -90,7 +90,7 @@
        }
        //处理回退到账
        //处理扣减账号资金
        for(RefundRecordMethod refundRecordMethod:refundRecordMethodList){
            if(PayMethodTypeConstants.PAY_STORED.equals(refundRecordMethod.getRefundNumberNo())){
                //储值金额 
@@ -99,6 +99,9 @@
                    UserMoneyUtil.setNewUserMoneyUnclaimed(ordersTotal.getPayUserId()==null?ordersTotal.getUserId():ordersTotal.getPayUserId(),refundRecord.getInvalidRemarks(),"作废退款:支付方式储值金额扣减",refundRecord.getInvalidId(),refundRecord.getOrderId(),refundRecord.getInvalidAppCode(),refundRecord.getId(),refundRecordMethod.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO);
                    OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord, refundRecord.getInvalidId(), refundRecord.getInvalidName(), refundRecordMethod.getName()+"作废退款金额:"+refundRecordMethod.getRealRefundTotal(), 1, OrderLogConstants.LOG_TYPE_REFUND_UNDO);
                    commonService.insert(OrderLogMapper.class,orderLog);
                    if(orderNodeBuilder != null){
                        orderNodeBuilder.append("-[作废退款]扣减退款金额(储值金):").append(refundRecordMethod.getRealRefundTotal());
                    }
                }
            }else if(PayMethodTypeConstants.PAY_ADD_FUND.equals(refundRecordMethod.getRefundNumberNo())){
                //增值金
@@ -107,6 +110,9 @@
                    UserMoneyUtil.setNewUserMoneyUnclaimed(ordersTotal.getPayUserId()==null?ordersTotal.getUserId():ordersTotal.getPayUserId(),refundRecord.getRemarks(),"作废退款:支付方式增值金退回",refundRecord.getInvalidId(),refundRecord.getOrderId(),refundRecord.getInvalidAppCode(),refundRecord.getId(),refundRecordMethod.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO);
                    OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,refundRecord.getInvalidId(),refundRecord.getInvalidName(),refundRecordMethod.getName(),1, OrderLogConstants.LOG_TYPE_REFUND_UNDO);
                    commonService.insert(OrderLogMapper.class,orderLog);
                    if(orderNodeBuilder != null){
                        orderNodeBuilder.append("-[作废退款]扣减退款金额(增值金):").append(refundRecordMethod.getRealRefundTotal());
                    }
                }
            }else if(PayMethodTypeConstants.PAY_INTEGRAL.equals(refundRecordMethod.getRefundNumberNo())){
                //积分
@@ -115,6 +121,9 @@
                    UserMoneyUtil.setNewUserMoneyUnclaimed(ordersTotal.getPayUserId()==null?ordersTotal.getUserId():ordersTotal.getPayUserId(),refundRecord.getRemarks(),"作废退款:支付方式积分退回",refundRecord.getInvalidId(),refundRecord.getOrderId(),refundRecord.getInvalidAppCode(),refundRecord.getId(),refundRecordMethod.getRealRefundTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO);
                    OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord,refundRecord.getInvalidId(),refundRecord.getInvalidName(),refundRecordMethod.getName()+"作废退款金额:"+refundRecordMethod.getRealRefundTotal(),1, OrderLogConstants.LOG_TYPE_REFUND_UNDO);
                    commonService.insert(OrderLogMapper.class,orderLog);
                    if(orderNodeBuilder != null){
                        orderNodeBuilder.append("-[作废退款]扣减退款金额(积分):").append(refundRecordMethod.getRealRefundTotal());
                    }
                }
            }else {
                //现金支付
@@ -126,6 +135,7 @@
                commonService.insert(OrderLogMapper.class,orderLog);
            }
        }
        return refundRecordMethodList;
    }
    /**充值单特需处理
@@ -135,7 +145,7 @@
     * @param optName 操作名称,如:申请退款、作废退款、审核拒绝
     * @param commonService 映射
     */
    public static void rechargeHandle(OrdersTotal ordersTotal, RefundRecord refundRecord,int optType,String optName, CommonService commonService){
    public static void rechargeHandle(OrdersTotal ordersTotal, RefundRecord refundRecord,int optType,String optName,StringBuilder orderNodeBuilder, CommonService commonService){
        if(!OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作失败,不是充值单!");
@@ -148,12 +158,16 @@
            //充值金额扣减
            UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getInvalidRemarks(),"退款单:"+refundRecord.getCode()+";"+optName+",扣减充值资金:"+ordersTotal.getTotal(),refundRecord.getOperatorId(),refundRecord.getOrderId(),refundRecord.getInvalidAppCode(),refundRecord.getId(),ordersTotal.getTotal().negate(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_REFUND_CANCEL,commonService,UserMoneyUnclaimed.NO);
            orderLog = RefundToolUtil.setOrderLog(refundRecord, refundRecord.getOperatorId(), refundRecord.getOperatorName(), "充值单:"+ordersTotal.getOrderNo()+";"+optName+",预扣除金额:"+ordersTotal.getTotal(), 0, OrderLogConstants.LOG_TYPE_REFUND);
        }else if(optType == OPT_TYPE_CANCEL){
            //充值金额回退
            UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getInvalidRemarks(),"退款单:"+refundRecord.getCode()+";"+optName+",退回充值资金:"+ordersTotal.getTotal(),refundRecord.getInvalidId(),refundRecord.getOrderId(),refundRecord.getInvalidAppCode(),refundRecord.getId(),ordersTotal.getTotal(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_REFUND_CANCEL,commonService,UserMoneyUnclaimed.NO);
            orderLog = RefundToolUtil.setOrderLog(refundRecord, refundRecord.getOperatorId(), refundRecord.getOperatorName(), "充值单:"+ordersTotal.getOrderNo()+";"+optName+",退回预扣除金额:"+ordersTotal.getTotal(), 0, OrderLogConstants.LOG_TYPE_REFUND);
        }else{
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作类型错误[1]["+optType+"]!");
        }
        if(orderNodeBuilder !=null){
            orderNodeBuilder.append("-[").append(optName).append("]充值单充值金操作(储值金):").append(ordersTotal.getTotal());
        }
        orderLog.setOrderId(ordersTotal.getId());
        commonService.insert(OrderLogMapper.class,orderLog);
@@ -166,7 +180,7 @@
     * @param optName 操作名称,如:申请退款、作废退款、审核拒绝
     * @param commonService 映射
     */
    public static void activityRuleHandle(OrdersTotal ordersTotal, RefundRecord refundRecord,int optType,String optName,CommonService commonService){
    public static void activityRuleHandle(OrdersTotal ordersTotal, RefundRecord refundRecord,int optType,String optName,StringBuilder orderNodeBuilder,CommonService commonService){
        //先获取活动规则
        SqlSentence sqlSentence = new SqlSentence();
@@ -177,7 +191,7 @@
        List<OrdersActivityInfo> ordersActivityInfoList = commonService.selectList(OrdersActivityInfoMapper.class,sqlSentence);
        if(ordersActivityInfoList.size() > 0){
            //订单活动信息处理
            activityRulePollingHandle2(ordersActivityInfoList,optType,optName,ordersTotal,refundRecord,commonService);
            activityRulePollingHandle2(ordersActivityInfoList,optType,optName,ordersTotal,refundRecord,orderNodeBuilder,commonService);
        }else{
            //没有订单活动信息处理,就获取保存在订单里面的活动标识
            values.clear();
@@ -185,7 +199,7 @@
            sqlSentence.sqlSentence("SELECT * FROM orders_info WHERE isDel = 0 AND orderId = #{m.orderId}",values);
            OrderInfo orderInfo = commonService.selectOne(OrderInfoMapper.class,sqlSentence);
            if(orderInfo!=null && StringUtils.noNull(orderInfo.getActivityId())){
                activityRulePollingHandle1(orderInfo.getActivityId(),optType,optName,ordersTotal,refundRecord,commonService);
                activityRulePollingHandle1(orderInfo.getActivityId(),optType,optName,ordersTotal,refundRecord,orderNodeBuilder,commonService);
            }
        }
    }
@@ -198,7 +212,8 @@
     * @param refundRecord 退款单
     * @param commonService 映射
     */
    public static void activityRulePollingHandle1(String activityId,int optType,String optName,OrdersTotal ordersTotal,RefundRecord refundRecord,CommonService commonService){
    public static void activityRulePollingHandle1(String activityId,int optType,String optName,OrdersTotal ordersTotal,RefundRecord refundRecord
            ,StringBuilder orderNodeBuilder,CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
@@ -228,6 +243,9 @@
                    }else{
                        throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作类型错误[2]["+optType+"]!");
                    }
                    if(orderNodeBuilder !=null){
                        orderNodeBuilder.append("-[").append(optName).append("]处理活动赠送(积分):").append(activityAction.getWorth());
                    }
                }else if(ActivityAction.TYPE_VALUEADDEDFUND.equals(activityAction.getType())){
                    //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
                    if(new BigDecimal(activityAction.getWorth()).compareTo(BigDecimal.ZERO) == 0 ){
@@ -240,13 +258,20 @@
                    }else{
                        throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作类型错误[3]["+optType+"]!");
                    }
                    if(orderNodeBuilder !=null){
                        orderNodeBuilder.append("-[").append(optName).append("]处理活动赠送(增值金):").append(activityAction.getWorth());
                    }
                }else if(ActivityAction.TYPE_COUPON.equals(activityAction.getType())){
                    values.put("oldValidState",optType==OPT_TYPE_EXECUTE? BaseEntity.YES:BaseEntity.NO);
                    values.put("newValidState",optType==OPT_TYPE_EXECUTE?BaseEntity.NO:BaseEntity.YES);
                    values.put("couponId",activityAction.getCrmCouponId());
                    values.put("commonId",ordersTotal.getId());
                    sqlSentence.sqlSentence(" validState=#{m.newValidState} where couponId=#{m.couponId} and commonId=#{m.commonId} and validState=#{m.oldValidState} ",values);
                    commonService.updateWhere(CouponNumberMapper.class,sqlSentence);
                    if(commonService.updateWhere(CouponNumberMapper.class,sqlSentence) != 0){
                        if(orderNodeBuilder !=null){
                            orderNodeBuilder.append("-[").append(optName).append("]处理活动赠送(优惠券):").append(activityAction.getWorthName());
                        }
                    }
                }
            }
        }
@@ -261,13 +286,15 @@
     * @param commonService 映射
     */
    public static void activityRulePollingHandle2(List<OrdersActivityInfo> ordersActivityInfoList,int optType,String optName,OrdersTotal ordersTotal
            ,RefundRecord refundRecord,CommonService commonService){
            ,RefundRecord refundRecord,StringBuilder orderNodeBuilder,CommonService commonService){
        if(ordersActivityInfoList == null || ordersActivityInfoList.size()==0){
            return;
        }
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        //是否变更订单活动状态
        boolean changeStatus;
        //获取订单活动条目
        List<OrdersActivityInfoItem> ordersActivityInfoItemList;
        for(OrdersActivityInfo ordersActivityInfo:ordersActivityInfoList){
@@ -278,6 +305,7 @@
            sqlSentence.sqlSentence("SELECT * FROM orders_activity_info_item WHERE isDel = 0 AND activityInfoId = #{m.activityInfoId} AND performStatus = #{m.performStatus} AND handleStatus = #{m.handleStatus}",values);
            ordersActivityInfoItemList = commonService.selectList(OrdersActivityInfoItemMapper.class,sqlSentence);
            for(OrdersActivityInfoItem ordersActivityInfoItem:ordersActivityInfoItemList){
                changeStatus = false;
                if(ActivityAction.TYPE_INTEGRAL.equals(ordersActivityInfoItem.getType())){
                    //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
                    if(new BigDecimal(ordersActivityInfoItem.getWorth()).compareTo(BigDecimal.ZERO) == 0){
@@ -290,6 +318,10 @@
                    }else{
                        throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作类型错误[2]["+optType+"]!");
                    }
                    if(orderNodeBuilder !=null){
                        orderNodeBuilder.append("-[").append(optName).append("]处理活动赠送(积分):").append(ordersActivityInfoItem.getWorth());
                    }
                    changeStatus = true;
                }else if(ActivityAction.TYPE_VALUEADDEDFUND.equals(ordersActivityInfoItem.getType())){
                    //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
                    if(new BigDecimal(ordersActivityInfoItem.getWorth()).compareTo(BigDecimal.ZERO) == 0 ){
@@ -302,6 +334,10 @@
                    }else{
                        throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作类型错误[3]["+optType+"]!");
                    }
                    if(orderNodeBuilder !=null){
                        orderNodeBuilder.append("-[").append(optName).append("]处理活动赠送(增值金):").append(ordersActivityInfoItem.getWorth());
                    }
                    changeStatus = true;
                }else if(ActivityAction.TYPE_COUPON.equals(ordersActivityInfoItem.getType())){
                    values.clear();
                    values.put("oldValidState",optType==OPT_TYPE_EXECUTE? BaseEntity.YES:BaseEntity.NO);
@@ -309,18 +345,24 @@
                    values.put("id",ordersActivityInfoItem.getCouponNumberId());
                    sqlSentence.sqlSentence(" validState=#{m.newValidState} where id=#{m.id} AND validState = #{m.oldValidState} ",values);
                    //优惠券不判断回退
                    commonService.updateWhere(CouponNumberMapper.class,sqlSentence);
                    if(commonService.updateWhere(CouponNumberMapper.class,sqlSentence) != 0){
                        if(orderNodeBuilder !=null){
                            orderNodeBuilder.append("-[").append(optName).append("]处理活动赠送(优惠券):").append(ordersActivityInfoItem.getWorthName());
                        }
                        changeStatus = true;
                    }
                }
                //更新订单活动信息状态
                values.clear();
                values.put("handleStatus",optType==OPT_TYPE_EXECUTE?OrdersActivityInfoItem.HANDLE_STATUS_CANCEL:OrdersActivityInfoItem.HANDLE_STATUS_SUCCESS);
                values.put("handleStatusOld",optType==OPT_TYPE_EXECUTE?OrdersActivityInfoItem.HANDLE_STATUS_SUCCESS:OrdersActivityInfoItem.HANDLE_STATUS_CANCEL);
                values.put("performStatusOld",OrdersActivityInfoItem.STATUS_PERFORM_SEND_OUT);
                values.put("id",ordersActivityInfoItem.getId());
                sqlSentence.sqlSentence("handleStatus = #{m.handleStatus} WHERE id = #{m.id} AND handleStatus = #{m.handleStatusOld} AND performStatus = #{m.performStatusOld}",values);
                if(commonService.updateWhere(OrdersActivityInfoItemMapper.class,sqlSentence) != 1){
                    throw new PlatTipsException(PlatformCode.ERROR_TIPS,"活动条目状态变化错误:");
                if(changeStatus){
                    //更新订单活动信息状态
                    values.clear();
                    values.put("handleStatus",optType==OPT_TYPE_EXECUTE?OrdersActivityInfoItem.HANDLE_STATUS_CANCEL:OrdersActivityInfoItem.HANDLE_STATUS_SUCCESS);
                    values.put("handleStatusOld",optType==OPT_TYPE_EXECUTE?OrdersActivityInfoItem.HANDLE_STATUS_SUCCESS:OrdersActivityInfoItem.HANDLE_STATUS_CANCEL);
                    values.put("performStatusOld",OrdersActivityInfoItem.STATUS_PERFORM_SEND_OUT);
                    values.put("id",ordersActivityInfoItem.getId());
                    sqlSentence.sqlSentence("handleStatus = #{m.handleStatus} WHERE id = #{m.id} AND handleStatus = #{m.handleStatusOld} AND performStatus = #{m.performStatusOld}",values);
                    if(commonService.updateWhere(OrdersActivityInfoItemMapper.class,sqlSentence) != 1){
                        throw new PlatTipsException(PlatformCode.ERROR_TIPS,"活动条目状态变化错误:");
                    }
                }
            }
        }
@@ -332,7 +374,7 @@
     * @param refundRecord 退款总数据
     * @param commonService 映射
     */
    public static void handCoupon(OrdersTotal ordersTotal,RefundRecord refundRecord,CommonService commonService) {
    public static void handCoupon(OrdersTotal ordersTotal,RefundRecord refundRecord,StringBuilder orderNodeBuilder,CommonService commonService) {
        //回退的优惠券变成使用状态
        //如果该优惠券在别的订单使用了,那么就不再理会
@@ -372,7 +414,10 @@
                values.put("useType", CouponNumber.USE_TYPE_USER);
                values.put("id", couponOrderDiscountLog.getCouponNumberId());
                sqlSentence.sqlSentence("  isUse=#{m.isUse},useTime=#{m.useTime},useType=#{m.useType},isUse=#{m.isUse} WHERE id = #{m.id} AND isUse = #{m.isUseOld}",values);
                if(commonService.updateWhere(CouponNumberMapper.class,sqlSentence) == 1){
                if(commonService.updateWhere(CouponNumberMapper.class,sqlSentence) != 0){
                    if(orderNodeBuilder != null){
                        orderNodeBuilder.append("-[作废退款]使用优惠券处理:"+couponOrderDiscountLog.getTitle());
                    }
                    //变更订单优惠券记录状态
                    values.clear();
                    values.put("status", BaseEntity.NO);
@@ -740,7 +785,7 @@
        StringBuilder sql;
        for(UserCardUsed userCardUsed:userCardUsedList){
            //判断是否够了,跳出循环
            if(opNum == returnNum){
            if(opNum <= 0){
                break;
            }
            if(userCardUsed.getCancelNum() <= 0){