chenjiahe
2023-08-14 f3ff4ed544dab12f9471a738e106379a466bb5e2
phi_platform_user/src/main/java/com/hx/phip/service/order/impl/OrderRefundServiceImpl.java
@@ -1636,34 +1636,40 @@
        orderRefundNoteListDto.setOperationId(operationId);
        orderRefundNoteListDto.setOrderId(ordersTotal.getId());
        //获取退款信息
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> map=new HashMap<>();
        sqlSentence.setM(map);
        map.put("orderId",ordersTotal.getId());
        //获取总退款方式
        map.put("refundRecordId",refundRecord.getId());
        map.put("refundStatus",RefundNote.STATUS_SUCCESS);
        sqlSentence.sqlSentence("SELECT * FROM refund_note WHERE orderId=#{m.orderId} and refundRecordId=#{m.refundRecordId} and isDel=0 AND refundStatus != #{m.refundStatus}",map);
        List<RefundNote> refundNotes=refundNoteMapper.selectList(sqlSentence);
        if(refundNotes==null || refundNotes.size()==0){
        sqlSentence.sqlSentence("SELECT * FROM refund_record_method WHERE isDel = 0 AND refundRecordId = #{m.refundRecordId}",map);
        List<RefundRecordMethod> refundRecordMethodList = commonService.selectList(RefundRecordMethodMapper.class,sqlSentence);
        if(refundRecordMethodList.size() == 0){
            return;
        }
        //组装数据,只有现金才生成
        List<OrderRefundNoteDto> orderRefundNoteDtos=new ArrayList<>();
        OrderRefundNoteDto orderRefundNoteDto;
        for (RefundNote refundNote : refundNotes) {
            if(StringUtils.isEmpty(refundNote.getRefundNumberNo())){
                throw new TipsException("退款方式接收失败[624]!");
        for (RefundRecordMethod refundRecordMethod : refundRecordMethodList) {
            //是现金
            if(refundRecordMethod.getIsMoneyPay().equals(BaseEntity.YES)){
                continue;
            }
            orderRefundNoteDto=new OrderRefundNoteDto();
            BeanUtils.copyProperties(refundNote,orderRefundNoteDto);
            //退款
            if(refundNote.getPaymentNumberNo().equals(refundNote.getRefundNumberNo())){
            orderRefundNoteDto = new OrderRefundNoteDto();
            //退款,是否原路退回
            if(refundRecordMethod.getNumberNo().equals(refundRecordMethod.getRefundNumberNo())){
                orderRefundNoteDto.setRefundroute(1);
            }else{
                orderRefundNoteDto.setRefundroute(0);
            }
            orderRefundNoteDto.setRefundNumberNo(refundNote.getRefundNumberNo());
            orderRefundNoteDto.setRefundName(refundNote.getRefundName());
            orderRefundNoteDto.setRefundRecordMethodId(refundRecordMethod.getId());
            orderRefundNoteDto.setPaymentTotal(refundRecordMethod.getActualTotal());
            orderRefundNoteDto.setPaymentNumberNo(refundRecordMethod.getNumberNo());
            orderRefundNoteDto.setPaymentName(refundRecordMethod.getName());
            orderRefundNoteDto.setRefundNumberNo(refundRecordMethod.getRefundNumberNo());
            orderRefundNoteDto.setRefundName(refundRecordMethod.getRefundName());
            orderRefundNoteDtos.add(orderRefundNoteDto);
        }
        orderRefundNoteListDto.setOrderRefundNoteDtos(orderRefundNoteDtos);
@@ -1671,7 +1677,7 @@
        noteConfirm(orderRefundNoteListDto, ordersTotal,refundRecord,orderNodeBuilder);
    }
    public PlatformResult noteConfirm(OrderRefundNoteListDto orderRefundNoteListDto,OrdersTotal ordersTotal,RefundRecord refundRecord
    public void noteConfirm(OrderRefundNoteListDto orderRefundNoteListDto,OrdersTotal ordersTotal,RefundRecord refundRecord
            ,StringBuilder orderNodeBuilder) {
        logger.info("退款单确认打印参数:{}",JSON.toJSONString(orderRefundNoteListDto));
        Map<String, String> operator = ApiOrderUtil.getOperator(commonService, orderRefundNoteListDto.getOperationId());//获取操作人信息
@@ -1683,29 +1689,32 @@
        Map<String,Object> map=new HashMap<>();
        sqlSentence.setM(map);
        List<OrderRefundNoteDto> orderRefundNoteDtos=orderRefundNoteListDto.getOrderRefundNoteDtos();
        if(orderRefundNoteDtos == null || orderRefundNoteDtos.size() == 0){
            return;
        }
        if("crm".equals(ordersTotal.getAppIdCode())){
            //CRM退款
            handleCrmRefundOrder(orderRefundNoteDtos,ordersTotal,refundRecord,operatorId,operatorName,orderNodeBuilder);
        }else {
            sqlSentence.sqlSentence(" refundroute=#{m.refundroute}, refundStoredTotal=#{m.refundStoredTotal},refundStatus=#{m.refundStatus},remarks=#{m.remarks},shopId=#{m.shopId}, " +
                    " shopName=#{m.shopName},operationName=#{m.operationName},operationId=#{m.operationId} where id=#{m.id} ",map);
            map.put("shopId",ordersTotal.getShopId());
            map.put("shopName",ordersTotal.getShopName());
            map.put("operationName",operatorName);
            map.put("operationId",operatorId);
            map.put("refundStatus",RefundNote.STATUS_SUCCESS);
            RefundCashVo refundCashVo = new RefundCashVo();
            refundCashVo.setOrderId(ordersTotal.getId());
            refundCashVo.setRemark(refundRecord.getRemarks());
            List<RefundCashItemVo> refundCashItemVoList = new ArrayList<>();
            RefundCashItemVo refundCashItemVo;
            for (OrderRefundNoteDto orderRefundNoteDto : orderRefundNoteDtos) {
                map.put("refundroute",orderRefundNoteDto.getRefundroute());
                map.put("refundStoredTotal",orderRefundNoteDto.getRefundStoredTotal());
                map.put("remarks",orderRefundNoteDto.getRemarks());
                map.put("id",orderRefundNoteDto.getId());
                refundNoteMapper.updateWhere(sqlSentence);
                if(BaseEntity.YES.equals(orderRefundNoteDto.getRefundroute())){
                    refundCashItemVo = new RefundCashItemVo();
                    refundCashItemVo.setPaymentNo(orderRefundNoteDto.getRefundNumberNo());
                    refundCashItemVo.setRefundTotal(orderRefundNoteDto.getPaymentTotal());
                    refundCashItemVo.setRefundRecordId(refundRecord.getId());
                    refundCashItemVo.setRefundRecordMethodId(orderRefundNoteDto.getRefundRecordMethodId());
                    refundCashItemVoList.add(refundCashItemVo);
                }
            }
            refundCashVo.setRefundCashItemVoList(refundCashItemVoList);
            refundCash(refundRecord.getId(),refundCashVo);
        }
        //记录操作日志
        StringBuffer stringBuffer=new StringBuffer("操作人:"+operator.get("operatorName")+",已支付订单现金支付退款:");
@@ -1713,7 +1722,6 @@
                ,operator.get("operatorName"),operator.get("operatorId"),OrderTotalConstants.STATUS_CANCEL);
        commonService.insert(OrderLogMapper.class,orderLog);
        return PlatformResult.success();
    }
    /**处理crm退款*/
    public void handleCrmRefundOrder(List<OrderRefundNoteDto> orderRefundNoteDtos,OrdersTotal ordersTotal,RefundRecord refundRecord
@@ -1779,43 +1787,19 @@
            }
        }
        logger.info("订单状态为:{}",ordersTotal.getRefundStatus());
        sqlSentence.sqlSentence(" refundroute=#{m.refundroute}, refundStoredTotal=#{m.refundStoredTotal},refundStatus=#{m.refundStatus},remarks=#{m.remarks},shopId=#{m.shopId}, " +
                " shopName=#{m.shopName},operationName=#{m.operationName},operationId=#{m.operationId} where id=#{m.id} ",map);
        map.put("shopId",ordersTotal.getShopId());
        map.put("shopName",ordersTotal.getShopName());
        map.put("operationName",operatorName);
        map.put("operationId",operatorId);
        map.put("refundStatus",RefundNote.STATUS_SUCCESS);
        for (OrderRefundNoteDto orderRefundNoteDto : orderRefundNoteDtos) {
            map.put("refundroute",orderRefundNoteDto.getRefundroute());
            map.put("refundStoredTotal",orderRefundNoteDto.getRefundStoredTotal());
            map.put("remarks",orderRefundNoteDto.getRemarks());
            map.put("id",orderRefundNoteDto.getId());
            refundNoteMapper.updateWhere(sqlSentence);
            if (!StringUtils.isEmpty(orderRefundNoteDto.getImageList())){
                String [] imageList= orderRefundNoteDto.getImageList().split(",");
                for (String s : imageList) {
                    RefundNoteItems refundNoteItems=new RefundNoteItems();
                    refundNoteItems.setRefundNoteId(orderRefundNoteDto.getId());
                    refundNoteItems.setImgUrl(s);
                    refundNoteItemsMapper.insert(refundNoteItems);
                }
            }
        try{
            //退款成功发送企业微信给订单开单人
            JSONObject object = new JSONObject();
            object.put("abnormal","订单退款成功,现金支付资产已退回!");
            object.put("orderNo",ordersTotal.getOrderNo());
            User user=commonService.selectOneByKeyBlob(UserMapper.class,ordersTotal.getUserId());
            object.put("userName",user==null?"":user.getName());
            object.put("notificationSubject", NotificationLog.NOTIFICATION_SUBJECT_REFUND_NOTE_PHISKIN_SUC_NOTICE);
            SendNoticeUtil.successOrderSendNotice(commonService, customParameter, ordersTotal,object);
        }catch (Exception e){
            logger.error("退款成功发送企业微信给订单开单人错误:",e);
        }
        //退款成功发送企业微信给订单开单人
        JSONObject object = new JSONObject();
        object.put("abnormal","订单退款成功,现金支付资产已退回!");
        object.put("orderNo",ordersTotal.getOrderNo());
        User user=commonService.selectOneByKeyBlob(UserMapper.class,ordersTotal.getUserId());
        object.put("userName",user==null?"":user.getName());
        object.put("notificationSubject", NotificationLog.NOTIFICATION_SUBJECT_REFUND_NOTE_PHISKIN_SUC_NOTICE);
        SendNoticeUtil.successOrderSendNotice(commonService, customParameter, ordersTotal,object);
    }
    /**
@@ -2560,6 +2544,7 @@
        List<ConsumeNotify> consumeNotifyList;
        String refundNo;
        BigDecimal consumeTotal;
        BigDecimal consumeRefundTotal;
        int refundStatus;
        for (Map.Entry<String, BigDecimal> entry : refundMap.entrySet()) {
            refundTotal = entry.getValue();
@@ -2569,11 +2554,19 @@
            //初始化可退金额
            consumeTotal = BigDecimal.ZERO;
            consumeRefundTotal = BigDecimal.ZERO;
            //获取支付信息(回调)
            consumeNotifyList = ConsumeTool.selectList(refundCashVo.getOrderId(),entry.getKey(),null,commonService);
            for(ConsumeNotify consumeNotify:consumeNotifyList){
                //计算总可退款金额
                consumeTotal = consumeTotal.add(consumeNotify.getPayAmount()).subtract(consumeNotify.getRefundTotal());
                consumeRefundTotal = consumeRefundTotal.add(consumeNotify.getRefundTotal());
            }
            //避免多退,要减去已退款
            refundTotal = refundTotal.subtract(consumeRefundTotal);
            if(refundTotal.compareTo(BigDecimal.ZERO) < 1){
                continue;
            }
            if(refundTotal.compareTo(consumeTotal) > 0){
@@ -2607,19 +2600,24 @@
                    //生成退款记录
                    RefundCash refundCash = new RefundCash();
                    refundCash.setRefundTotal(consumeTotal);
                    refundCash.setOutOrderNo(consumeNotify.getOutOrderNo());
                    refundCash.setPlatTrxNo(consumeNotify.getPlatTrxNo());
                    refundCash.setRefunNo(refundNo);
                    refundCash.setOrderId(refundCashVo.getOrderId());
                    refundCash.setConsumeNotifyId(consumeNotify.getConsumePayId());
                    refundCash.setRefundRecordId(refundRecordId);
                    commonService.insert(RefundCashMapper.class,refundCash);
                    //调起中心平台的退款
                    CenterPayUtil.unifyRefund(consumeNotify.getAppId(),consumeNotify.getPlatTrxNo(),refundNo,refundCashVo.getRemark(),consumeNotify.getPayAmount()
                    CenterPayUtil.unifyRefund(consumeNotify.getAppId(),consumeNotify.getPlatTrxNo(),refundNo,"员工退款",consumeNotify.getPayAmount()
                            ,consumeTotal,customParameter.getAppId(),customParameter.getPrivatekey(),fRefundService);
                    //已经退的跳过
                    refundTotal = refundTotal.subtract(consumeTotal);
                    commonService.insert(RefundCashMapper.class,refundCash);
                    //支付回调信息变化
                    ConsumeTool.updateRefundTotal(consumeNotify,consumeTotal,refundStatus,commonService);
                }catch (Exception e){
                    logger.error("第三方退款失败",e);
                    logger.error("第三方退款失败,错误信息:",e);
                }
            }
        }