package com.hx.phip.service.order.impl; import com.alibaba.fastjson.JSON; import com.hx.common.service.CommonService; import com.hx.mybatisTool.SqlSentence; import com.hx.phiappt.common.ConsumePayConstants; import com.hx.phiappt.common.OrderLogConstants; import com.hx.phiappt.common.OrderTotalConstants; import com.hx.phiappt.common.RefundSoruceConstants; import com.hx.phiappt.model.BaseEntity; import com.hx.phiappt.model.PaymentMethod; import com.hx.phiappt.model.coupon.CouponNumber; import com.hx.phiappt.model.coupon.CouponOrderDiscountLog; import com.hx.phiappt.model.order.*; import com.hx.phiappt.model.refund.*; import com.hx.phip.config.CustomParameter; import com.hx.phip.dao.mapper.*; import com.hx.phip.service.PaymentMethodService; import com.hx.phip.service.order.OrderRefundService; import com.hx.phip.service.order.OrderService; import com.hx.phip.service.refund.RefundRecordItemService; import com.hx.phip.service.refund.RefundRecordMethodService; import com.hx.phip.service.refund.RefundRecordService; import com.hx.phip.tool.CreateNo; import com.hx.phip.util.api.ApiOrderUtil; import com.hx.phip.util.api.PartialRefundUtil; import com.hx.util.StringUtils; import com.hz.crm.feign.FOderService; import com.hz.his.dto.order.*; import com.hz.his.feign.service.marketing.MOrderService; import com.hz.his.feign.service.sync.SyncExecutionRecordService; import com.hz.his.feign.service.sync.SyncOrderService; import com.hz.his.feign.service.sync.SyncUserCardService; import com.platform.exception.PlatTipsException; import com.platform.resultTool.PlatformCode; import com.platform.resultTool.PlatformResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @Author */ @Transactional @Service public class OrderRefundServiceImpl implements OrderRefundService { //log4j日志 private static Logger logger = LoggerFactory.getLogger(OrderRefundServiceImpl.class.getName()); @Resource private OrdersTotalMapper ordersTotalMapper; @Resource private OrderItemMapper orderItemMapper; @Resource private ConsumePayMapper consumePayMapper; @Resource private OrderService orderService; @Resource private UserProjectItemMapper userProjectItemMapper; @Resource private UserCardMapper userCardMapper; @Resource private UserCardUsedMapper userCardUsedMapper; @Resource private OrderItemSonMapper orderItemSonMapper; @Resource private RefundNoteMapper refundNoteMapper; @Resource private RefundNoteItemsMapper refundNoteItemsMapper; @Resource private CommonService commonService; @Resource private FOderService fOderService; @Resource private SyncOrderService syncOrderService; @Resource private SyncExecutionRecordService syncExecutionRecordService; @Resource private SyncUserCardService syncUserCardService; @Resource private SystemParameterMapper systemParameterMapper; @Resource private RefundRecordService refundRecordService; @Resource private PaymentMethodService paymentMethodService; @Resource private RefundRecordMethodService refundRecordMethodService; @Resource private RefundRecordItemService refundRecordItemService; @Resource private RefundRecordItemSourceMapper refundRecordItemSourceMapper; @Resource private CustomParameter customParameter; @Resource private MOrderService mOrderService; @Resource private CouponNumberMapper couponNumberMapper; @Resource private CouponOrderDiscountLogMapper couponOrderDiscountLogMapper; @Resource private RefundRecordCouponMapper refundRecordCouponMapper; @Override public PlatformResult partRefund(OrderRefundDto orderRefundDto) { logger.info("部分退款打印参数:{}", JSON.toJSONString(orderRefundDto)); //先判断项目的状态是否存在 OrdersTotal ordersTotal=null; try { ordersTotal=ordersTotalMapper.selectOneByKey(orderRefundDto.getOrderId()); if(ordersTotal==null){ logger.info("取消订单流程:未找到总订单信息!订单id:{}",orderRefundDto.getOrderId()); return PlatformResult.failure(PlatformCode.ERROR_TIPS,"取消订单流程:未找到总订单信息!"); } }catch (Exception e){ logger.info("取消订单流程:根据订单id查询多条总订单信息!订单id:{}",orderRefundDto.getOrderId()); return PlatformResult.failure(PlatformCode.ERROR_TIPS,"取消订单流程:根据订单id查询多条总订单信息!"); } Maprefund =handlePartRefund(ordersTotal,orderRefundDto); return PlatformResult.success(refund); } public Map handlePartRefund(OrdersTotal ordersTotal,OrderRefundDto orderRefundDto){ //订单节点日志 OrdersNodeLog ordersNodeLog = new OrdersNodeLog(); StringBuilder orderNodeBuilder = new StringBuilder(); orderNodeBuilder.append("开始部分退款"); List refundPayMethod=orderRefundDto.getRefundPayMethod(); List refundList=orderRefundDto.getRefundList(); List couponList=orderRefundDto.getCouponList(); BigDecimal sumMoney=refundPayMethod.stream().map(OrderPayMethodDto::getMoney).reduce(BigDecimal.ZERO,BigDecimal::add); BigDecimal refundSumMoney=refundList.stream().map(OrderItemRefundDto::getRefundMoney).reduce(BigDecimal.ZERO,BigDecimal::add); BigDecimal percentage=sumMoney.divide(refundSumMoney,2,BigDecimal.ROUND_HALF_UP); Map operator = ApiOrderUtil.getOperator(commonService, orderRefundDto.getOperatorId());//获取操作人信息 SqlSentence sqlSentence = new SqlSentence(); Map map=new HashMap<>(); sqlSentence.setM(map); //退款总记录 orderNodeBuilder.append("-处理退款总记录"); String totalCode = CreateNo.createTimeNo(systemParameterMapper, "R", 16, "yyyyMMddHHmmss");//总订单编号 RefundRecord refundRecord=new RefundRecord(totalCode,ordersTotal.getShopId(),ordersTotal.getShopName(),sumMoney, OrderTotalConstants.STATUS_REFUND_APPLY,0, RefundSoruceConstants.TYPE_SOURCE_ORDER,"phis部分退款",ordersTotal.getId(),ordersTotal.getUserId()); refundRecordService.insert(refundRecord); //记录退回客户的支付方式 RefundRecordItem refundRecordItem=null; RefundRecordMethod refundRecordMethod=null; RefundRecordItemSource refundRecordItemSource=null; orderNodeBuilder.append("-处理退款方式"); for (OrderPayMethodDto orderPayMethodDto : refundPayMethod) { String payMethodNo = orderPayMethodDto.getPayMethodNo();//支付方式编码 BigDecimal money = orderPayMethodDto.getMoney();//实退金额 PaymentMethod paymentMethod = paymentMethodService.selectNumberNo(payMethodNo); if(paymentMethod==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该支付方式编码"+payMethodNo); } if(money ==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"实退金额为空"); } refundRecordMethod=new RefundRecordMethod(payMethodNo,paymentMethod.getName(),money, ConsumePayConstants.STATUS_ORDER,ordersTotal.getId(),refundRecord.getId(),paymentMethod.getId()); refundRecordMethod.setActualTotal(money); refundRecordMethod.setRealRefundTotal(money);//实退款总金额 后期需要修改 refundRecordMethod.setRemarks(orderPayMethodDto.getRemarks()); refundRecordMethodService.insert(refundRecordMethod); } List orderItemSourceRefundDtos=null; String orderItemId =null; Integer refundNum =null; BigDecimal refundMoney =null; String refundReason =null; String type =null; OrderItem orderItem =null; OrderItemSon orderItemSon =null; orderNodeBuilder.append("-处理退款详情"); for (OrderItemRefundDto orderItemRefundDto : refundList) { orderItemId = orderItemRefundDto.getOrderItemId();//orderItemId refundNum = orderItemRefundDto.getRefundNum();//实退数 refundMoney = orderItemRefundDto.getRefundMoney().multiply(percentage).setScale(2,BigDecimal.ROUND_HALF_UP);//实退金额 refundReason = orderItemRefundDto.getRefundReason();//实退原因 type =orderItemRefundDto.getType(); orderItemSourceRefundDtos = orderItemRefundDto.getOrderItemSourceRefundDtos(); orderItem = orderItemMapper.selectOneByKey(orderItemId); if(StringUtils.isEmpty(orderItemId)){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单的子订单id为空"); } if(refundNum==null ){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请正确填写退款实退数"); } if(refundMoney==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请正确填写退款实退金额"); } if(orderItem==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该子项订单信息"); }else{ //保存退款详情订单 refundRecordItem=new RefundRecordItem(orderItem.getGoodsNo(),orderItem.getGoodsName(),orderItem.getCommonId(),refundNum,refundMoney,refundReason,refundRecord.getId(),orderItem.getSpecs(),orderItem.getId()); refundRecordItem.setType(type); refundRecordItem.setRealRefundNum(refundNum);//实退次数 后期需要修改 refundRecordItem.setRealRefundTotal(refundMoney);//实退款总金额 后期需要修改 refundRecordItemService.insert(refundRecordItem); } if(orderItemSourceRefundDtos == null || orderItemSourceRefundDtos.size()<1){ continue; } for (OrderItemSourceRefundDto orderItemSourceRefundDto : orderItemSourceRefundDtos) { orderItemId = orderItemSourceRefundDto.getOrderItemSonId();//orderItemId refundNum = orderItemSourceRefundDto.getRefundNum();//实退数 refundMoney = orderItemSourceRefundDto.getRefundMoney().multiply(percentage).setScale(2,BigDecimal.ROUND_HALF_UP);//实退金额 refundReason = orderItemSourceRefundDto.getRefundReason();//实退原因 type =orderItemSourceRefundDto.getType(); orderItemSon = orderItemSonMapper.selectOneByKey(orderItemId); if(StringUtils.isEmpty(orderItemId)){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单的子订单id为空"); } if(refundNum==null ){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请正确填写退款实退数"); } if(refundMoney==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请正确填写退款实退金额"); } if(orderItem==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该子项订单信息"); }else{ //保存退款详情订单 refundRecordItemSource=new RefundRecordItemSource(type,orderItemSon.getGoodsNo(),orderItemSon.getGoodsName(),orderItemSon.getSpecs(),orderItemSon.getGoodsId(),refundNum,refundMoney,refundReason,refundRecord.getId(),refundRecordItem.getId(),orderItemSon.getId()); refundRecordItemSource.setRealRefundNum(refundNum);//实退次数 后期需要修改 refundRecordItemSource.setRealRefundTotal(refundMoney);//实退款总金额 后期需要修改 refundRecordItemSourceMapper.insert(refundRecordItemSource); } } } //处理优惠卷 orderNodeBuilder.append("-处理退款优惠卷"); CouponOrderDiscountLog couponOrderDiscountLog=null; RefundRecordCoupon refundRecordCoupon=null; for (OrderCouponRefunDto orderCouponRefunDto : couponList) { couponOrderDiscountLog=couponOrderDiscountLogMapper.selectOneByKey(orderCouponRefunDto.getOrderCouponId()); if(couponOrderDiscountLog==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单优惠卷id不正确"); } refundRecordCoupon=new RefundRecordCoupon(ordersTotal.getId(),refundRecordItem.getId(),ordersTotal.getUserId(),1,1,couponOrderDiscountLog.getId(), couponOrderDiscountLog.getCouponId(),couponOrderDiscountLog.getTitle(),couponOrderDiscountLog.getCouponNumberId()); refundRecordCouponMapper.insert(refundRecordCoupon); } String operationId=operator.get("operatorId"); String operatorName=operator.get("operatorName"); orderNodeBuilder.append("-处理退款支付方式,用户待执行项目"); PartialRefundUtil.refundProcess(commonService, operationId, operatorName, refundRecord.getId(), customParameter,ordersTotal.getAppIdCode()); //返回数据 Map data=new HashMap<>(); data.put("refundRecordId",refundRecord.getId()); data.put("refundRecordCode",refundRecord.getCode()); //记录操作日志 orderNodeBuilder.append("-记录操作日志"); StringBuffer stringBuffer=new StringBuffer("操作人:"+operator.get("operatorName")+",进行部分退款"); OrderLog orderLog=new OrderLog(ordersTotal.getId(), OrderLogConstants.LOG_TYPE_REFUND,stringBuffer.toString() ,operator.get("operatorName"),operator.get("operatorId"),OrderTotalConstants.STATUS_CANCEL); commonService.insert(OrderLogMapper.class,orderLog); orderNodeBuilder.append("-结束处理退款"); ordersNodeLog.setCommonType(OrdersNodeLog.TYPE_COMMON_ORDER_REFUND); ordersNodeLog.setContent(orderNodeBuilder.toString()); ordersNodeLog.setOrderId(ordersTotal.getId()); commonService.insert(OrdersNodeLogMapper.class,ordersNodeLog); return data; } }