chenjiahe
2023-05-10 01fbcb62fbd9ac4d72d1bed3d87da5a6c73af698
phi_platform_user/src/main/java/com/hx/phip/service/order/impl/OrderRefundServiceImpl.java
@@ -12,7 +12,6 @@
import com.hx.phiappt.model.*;
import com.hx.phiappt.model.activity.ActivityAction;
import com.hx.phiappt.model.activity.ActivityRule;
import com.hx.phiappt.model.consume.ConsumePay;
import com.hx.phiappt.model.coupon.CouponOrderDiscountLog;
import com.hx.phiappt.model.order.*;
import com.hx.phiappt.model.performance.PerformanceInfo;
@@ -22,20 +21,25 @@
import com.hx.phiappt.model.userMoney.UserMoneyUnclaimed;
import com.hx.phip.config.CustomParameter;
import com.hx.phip.dao.mapper.*;
import com.hx.phip.service.CreateNoService;
import com.hx.phip.service.PaymentMethodService;
import com.hx.phip.service.order.OrderRefundNewService;
import com.hx.phip.service.order.OrderRefundService;
import com.hx.phip.service.order.OrderService;
import com.hx.phip.service.order.OrderV3Service;
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.service.userMoney.UserMoneyUnclaimedService;
import com.hx.phip.tool.CreateNo;
import com.hx.phip.util.api.*;
import com.hx.phip.tool.refund.OrderRefundCancelTool;
import com.hx.phip.tool.refund.PartialRefundUtil;
import com.hx.phip.tool.refund.PaymentCountTool;
import com.hx.phip.util.api.ApiOrderUtil;
import com.hx.phip.util.api.SendNoticeUtil;
import com.hx.phip.util.api.UserLevelUtil;
import com.hx.phip.util.api.UserMoneyUtil;
import com.hx.phip.vo.order.payment.PayMethodVo;
import com.hx.phip.vo.order.refund.DistributionRedundMethodVo;
import com.hx.phip.vo.order.refund.DistributionRedundVo;
import com.hx.resultTool.Result;
import com.hx.util.StringUtils;
import com.hz.crm.dto.OrderStatusDto;
import com.hz.crm.dto.order.refund.RefundAmountModelDto;
import com.hz.crm.dto.order.refund.RefundDto;
import com.hz.crm.dto.order.refund.RefundReturnDto;
@@ -46,9 +50,8 @@
import com.hz.his.dto.marketing.RefundProjectDto;
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.hz.his.vo.order.refund.RefundCancelVo;
import com.platform.entity.ThirtApplication;
import com.platform.exception.PlatTipsException;
import com.platform.resultTool.PlatformCode;
import com.platform.resultTool.PlatformResult;
@@ -60,32 +63,26 @@
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
/**
 * @Author
 * @Author CJH
 * 重构与2023-03-20
 */
@Transactional
@Service
public class OrderRefundServiceImpl implements OrderRefundService {
    //log4j日志
    private static Logger logger = LoggerFactory.getLogger(OrderRefundServiceImpl.class.getName());
    /**log4j日志*/
    private static final 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;
    private RefundRecordMapper refundRecordMapper;
    @Resource
    private OrderItemSonMapper orderItemSonMapper;
    @Resource
@@ -98,13 +95,6 @@
    @Resource
    private FOderService fOderService;
    @Resource
    private SyncOrderService syncOrderService;
    @Resource
    private SyncExecutionRecordService syncExecutionRecordService;
    @Resource
    private SyncUserCardService syncUserCardService;
    @Resource
    private SystemParameterMapper systemParameterMapper;
@@ -124,8 +114,6 @@
    private MOrderService mOrderService;
    @Resource
    private CouponNumberMapper couponNumberMapper;
    @Resource
    private CouponOrderDiscountLogMapper couponOrderDiscountLogMapper;
    @Resource
    private RefundRecordCouponMapper refundRecordCouponMapper;
@@ -136,466 +124,365 @@
    @Resource
    private RefundRecordItemMethodMapper refundRecordItemMethodMapper;
    @Resource
    private OrderRefundNewService orderRefundNewService;
    private RefundMapper refundMapper;
    @Resource
    private OrderV3Service orderV3Service;
    @Resource
    private UserMoneyUnclaimedService userMoneyUnclaimedService;
    private CreateNoService createNoService;
    /**
     * 获取退款页面详情
     *
     * @param orderId
     * @return
     * @param ordersTotal 订单
     * @return 可退款数量详情
     */
    @Override
    public List<Map<String, Object>> refundDetails(String orderId) {
    public List<Map<String, Object>> refundDetails(OrdersTotal ordersTotal) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> sqlMap = new HashMap<>();
        sqlSentence.setM(sqlMap);
        sqlMap.put("isDel", BaseEntity.NO);
        sqlMap.put("orderId", orderId);
        sqlSentence.setSqlSentence("SELECT * FROM orders_total  WHERE id = #{m.orderId}  and  isDel = #{m.isDel} ");
        OrdersTotal ordersTotal = ordersTotalMapper.selectOne(sqlSentence);
        sqlMap.put("orderId", ordersTotal.getId());
        //一级子订单
        sqlSentence.setSqlSentence("SELECT * FROM order_item WHERE  orderId = #{m.orderId} and  isDel = #{m.isDel}");
        sqlSentence.sqlSentence("SELECT * FROM order_item WHERE  orderId = #{m.orderId} AND isDel = #{m.isDel}",sqlMap);
        List<OrderItem> orderItemList = orderItemMapper.selectList(sqlSentence);
        List<Map<String, Object>> orderItems = new ArrayList<>();
        Map<String, Object> userProjectItem = null;
        //
        boolean isCard = false;
        for (OrderItem orderItem : orderItemList) {
            //判断这个项目能不能显示处理
            switch (orderItem.getType()) {
                case OrderItemConstants.TYPE_PROJECT:
                    handleOrderItemIsProject(orderItems, userProjectItem, orderItem, sqlSentence, sqlMap,ordersTotal.getUserId());
                    handleOrderItemIsProject(orderItems,orderItem);
                    break;
                case OrderItemConstants.TYPE_RETAIL:
                    handleOrderItemIsRetail(orderItems, userProjectItem, orderItem, sqlSentence, sqlMap);
                    handleOrderItemIsRetail(orderItems,orderItem);
                    break;
                case OrderItemConstants.TYPE_DRUG:
                    handleOrderItemIsRetail(orderItems, userProjectItem, orderItem, sqlSentence, sqlMap);
                    handleOrderItemIsRetail(orderItems, orderItem);
                    break;
                case OrderItemConstants.TYPE_CARD:
                    //handleOrderItemIsCard(orderItems,userProjectItem,orderItem,sqlSentence,sqlMap);
                    isCard = true;
                    handleOrderItemIsCard(orderItems, orderItem);
                    break;
                case OrderItemConstants.TYPE_PROMOTION:
                    handleOrderItemIsPromotion(orderItems, userProjectItem, orderItem, sqlSentence, sqlMap,ordersTotal.getUserId());
                    handleOrderItemIsPromotion(orderItems, orderItem);
                    break;
                case OrderItemConstants.CARD_BAG:
                    handleOrderItemIsCardBag(orderItems, userProjectItem, orderItem, sqlSentence, sqlMap,ordersTotal.getUserId());
                    handleOrderItemIsCardBag(orderItems, orderItem);
                    break;
                default:
                    break;
            }
        }
        if (isCard) {
            handleOrderItemIsCard(orderItems, userProjectItem, ordersTotal, sqlSentence, sqlMap);
        }
        return orderItems;
    }
    /**
     * 处理一级是项目的
     */
    public void handleOrderItemIsProject(List<Map<String, Object>> orderItems, Map<String, Object> userProjectItem, OrderItem orderItem, SqlSentence sqlSentence, Map<String, Object> sqlMap,String userId) {
        sqlSentence.setSqlSentence("SELECT oi.id,oi.goodsName,oi.curPrice,oi.specs,oi.type,oi.buyNum,upi.notUsedNum,oi.hasReNum  FROM order_item oi " +
                " LEFT JOIN user_project_item upi ON upi.commonId=oi.id WHERE oi.id = #{m.itemsId}  AND oi.isDel=0 and upi.isDel=0 and oi.refundStatus IN ('-1','2') " +
                " AND upi.userId=#{m.userId} ");
        sqlMap.put("itemsId", orderItem.getId());
        sqlMap.put("userId", userId);
    public void handleOrderItemIsProject(List<Map<String, Object>> orderItems, OrderItem orderItem) {
        //获取用户项目
        UserProjectItem userProjectItem = PartialRefundUtil.getUserProject(orderItem.getId(),commonService);
        if(userProjectItem == null){
            throw new TipsException("用户项目获取失败!");
        }
        userProjectItem = orderItemMapper.selectOneMap(sqlSentence);
        if (userProjectItem == null) {
            return;
        orderItems.add(itemRefandDataPackage(orderItem,null,userProjectItem,null));
    }
    /**订单子项退款信息封装*/
    public Map<String,Object> itemRefandDataPackage(OrderItem orderItem,OrderItemSon orderItemSon,UserProjectItem userProjectItem,List<UserCard> userCardList){
        Map<String,Object> itemData = new HashMap<>();
        if(orderItemSon != null){
            itemData.put("id",orderItemSon.getId());
            itemData.put("type",orderItemSon.getType());
            itemData.put("goodsName",orderItemSon.getGoodsName());
            itemData.put("curPrice",orderItemSon.getCurPrice());
            itemData.put("specs",orderItemSon.getSpecs());
            itemData.put("buyNum",orderItemSon.getBuyNum());
            itemData.put("hasReNum",orderItemSon.getHasReNum());
            itemData.put("notUsedNum",orderItemSon.getBuyNum()-orderItemSon.getHasReNum());
        }else{
            itemData.put("id",orderItem.getId());
            itemData.put("type",orderItem.getType());
            itemData.put("goodsName",orderItem.getGoodsName());
            itemData.put("curPrice",orderItem.getCurPrice());
            itemData.put("specs",orderItem.getSpecs());
            itemData.put("buyNum",orderItem.getBuyNum());
            itemData.put("hasReNum",orderItem.getHasReNum());
            itemData.put("notUsedNum",orderItem.getBuyNum()-orderItem.getHasReNum());
        }
        if (userProjectItem.get("buyNum").toString().equals(userProjectItem.get("hasReNum").toString())) {
            return;//已经退款了
        if(userProjectItem != null){
            itemData.put("notUsedNum",userProjectItem.getNotUsedNum());
        }
        if (StringUtils.isEmpty(String.valueOf(userProjectItem.get("buyNum")))) {
            return;//
        if(userCardList != null){
            itemData.put("notUsedNum",userCardList.size());
        }
        if (Integer.parseInt(String.valueOf(userProjectItem.get("notUsedNum"))) < 1) {
            return;//用户未执行数量不够了
        }
        orderItems.add(userProjectItem);
        return itemData;
    }
    /**
     * 处理一级是商品的
     */
    public void handleOrderItemIsRetail(List<Map<String, Object>> orderItems, Map<String, Object> userProjectItem, OrderItem orderItem, SqlSentence sqlSentence, Map<String, Object> sqlMap) {
        sqlSentence.setSqlSentence("SELECT oi.id,oi.goodsName,oi.curPrice,oi.specs,oi.type,oi.buyNum,oi.hasReNum FROM order_item oi " +
                " WHERE oi.id = #{m.itemsId}  AND oi.isDel=0 and oi.refundStatus IN ('-1','2')");
        sqlMap.put("itemsId", orderItem.getId());
        userProjectItem = orderItemMapper.selectOneMap(sqlSentence);
        if (userProjectItem == null) {
            return;
        }
        if (userProjectItem.get("buyNum").toString().equals(userProjectItem.get("hasReNum").toString())) {
            return;//已经退款了
        }
        userProjectItem.put("notUsedNum", Integer.parseInt(userProjectItem.get("buyNum").toString()) - Integer.parseInt(userProjectItem.get("hasReNum").toString()));
        orderItems.add(userProjectItem);
    public void handleOrderItemIsRetail(List<Map<String, Object>> orderItems, OrderItem orderItem) {
        orderItems.add(itemRefandDataPackage(orderItem,null,null,null));
    }
    /**
     * 处理一级是卡项的
     */
    public void handleOrderItemIsCard(List<Map<String, Object>> orderItems, Map<String, Object> userProjectItem, OrdersTotal ordersTotal, SqlSentence sqlSentence, Map<String, Object> sqlMap) {
        sqlSentence.setSqlSentence("SELECT oi.id,oi.goodsName,oi.curPrice,oi.specs,oi.type,oi.buyNum," +
                " oi.hasReNum,ci.id AS cardItemId,count(uc.cardItemId) as notUsedNum " +
                " FROM orders_total ot " +
                " LEFT JOIN user_card uc ON uc.orderId = ot.id OR uc.hisOrderId = ot.hisOrderId " +
                " LEFT JOIN order_item oi ON oi.orderId=ot.id AND oi.commonId=uc.cardItemId " +
                " LEFT JOIN card_item ci ON ci.id = uc.cardItemId  " +
                " WHERE ot.id = #{m.orderId} AND  uc.effectiveStatus= #{m.effectiveStatus} AND uc.status= #{m.status} AND uc.doneeUserId IS NULL GROUP BY uc.cardItemId");
        sqlMap.put("orderId", ordersTotal.getId());
        sqlMap.put("effectiveStatus", UserProjectConstants.EFF_STATUS_YES);
        sqlMap.put("status", UserCard.TYPE_NO_USED);
    public void handleOrderItemIsCard(List<Map<String, Object>> orderItems,OrderItem orderItem) {
        List<Map<String, Object>> userCardList = userCardMapper.selectListMap(sqlSentence);
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> sqlMap = new HashMap<>();
        if (userCardList == null) {
            return;
        }
        //获取用户卡项
        List<UserCard> userCardList = PartialRefundUtil.getRefundCard(orderItem.getId(),UserProjectConstants.EFF_STATUS_YES,commonService);
        //信息封装
        Map<String,Object> itemData = itemRefandDataPackage(orderItem,null,null,userCardList);
        sqlSentence.setSqlSentence(" SELECT groupName,shareMoney FROM card_equity WHERE cardItemId = #{m.equityId} ");
        List<Map<String, Object>> cardEquitys = null;
        for (Map<String, Object> map : userCardList) {
            userProjectItem = map;
            sqlMap.put("equityId", map.get("cardItemId"));
            cardEquitys = cardEquityMapper.selectListMap(sqlSentence);
            if (cardEquitys == null || cardEquitys.size() == 0) {
                break;//卡项没权益不展示
            }
            userProjectItem.put("list", cardEquitys);
            orderItems.add(userProjectItem);
        }
        //获取卡项的详细条目
        sqlMap.put("commonId",orderItem.getCommonId());
        sqlSentence.sqlSentence(" SELECT groupName,shareMoney FROM card_equity WHERE cardItemId = #{m.commonId} ",sqlMap);
        List<Map<String, Object>> cardEquitys = cardEquityMapper.selectListMap(sqlSentence);
        itemData.put("list", cardEquitys);
        orderItems.add(itemData);
    }
    /**
     * 处理一级是促销的
     */
    public void handleOrderItemIsPromotion(List<Map<String, Object>> orderItems, Map<String, Object> userProjectItem, OrderItem orderItem, SqlSentence sqlSentence, Map<String, Object> sqlMap,String userId) {
    public void handleOrderItemIsPromotion(List<Map<String, Object>> orderItems, OrderItem orderItem) {
        sqlSentence.setSqlSentence("SELECT oi.id,oi.goodsName,oi.curPrice,oi.specs,oi.type,oi.buyNum,oi.hasReNum FROM order_item oi " +
                " WHERE oi.id = #{m.itemsId}  AND oi.isDel=0 and oi.refundStatus IN ('-1','2')");
        sqlMap.put("itemsId", orderItem.getId());
        userProjectItem = orderItemMapper.selectOneMap(sqlSentence);
        if (userProjectItem == null) {
            return;
        }
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> sqlMap = new HashMap<>();
        userProjectItem.put("notUsedNum", Integer.parseInt(userProjectItem.get("buyNum").toString()) - Integer.parseInt(userProjectItem.get("hasReNum").toString()));
        //封装参数
        Map<String,Object> itemData = itemRefandDataPackage(orderItem,null,null,null);
        //二级子订单
        //获取二级子订单
        sqlMap.put("orderItemId", orderItem.getId());
        sqlSentence.setSqlSentence("SELECT * FROM order_item_source WHERE orderItemId= #{m.orderItemId} and  isDel = 0 ");
        sqlSentence.sqlSentence("SELECT * FROM order_item_source WHERE orderItemId = #{m.orderItemId} AND isDel = 0 ",sqlMap);
        List<OrderItemSon> orderItemSonList = orderItemSonMapper.selectList(sqlSentence);
        System.out.println("orderItemSonList:"+JSON.toJSONString(orderItemSonList));
        if (orderItemSonList == null) {
            return;
        }
        List<Map<String, Object>> promotionList = new ArrayList<>();
        Map<String, Object> promotion = null;
        for (OrderItemSon orderItemSon : orderItemSonList) {
            //判断这个项目能不能显示处理
            switch (orderItemSon.getType()) {
                case OrderGoodsConstants.TYPE_PROJECT:
                    handleOrderItemSonIsProject(promotionList, promotion, orderItemSon, sqlSentence, sqlMap,userId);
                    handleOrderItemSonIsProject(promotionList, orderItemSon);
                    break;
                case OrderItemConstants.TYPE_RETAIL:
                    handleOrderItemSonIsRetail(promotionList, promotion, orderItemSon, sqlSentence, sqlMap);
                    handleOrderItemSonIsRetail(promotionList, orderItemSon);
                    break;
                default:
                    break;
            }
        }
        if (promotionList == null || promotionList.size() == 0) {
            return;
        }
        userProjectItem.put("list", promotionList);
        orderItems.add(userProjectItem);
        itemData.put("list", promotionList);
        orderItems.add(itemData);
    }
    /**
     * 处理一级是卡包
     */
    public void handleOrderItemIsCardBag(List<Map<String, Object>> orderItems, Map<String, Object> userProjectItem, OrderItem orderItem, SqlSentence sqlSentence, Map<String, Object> sqlMap,String userId) {
    public void handleOrderItemIsCardBag(List<Map<String, Object>> orderItems, OrderItem orderItem) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> sqlMap = new HashMap<>();
        sqlSentence.setSqlSentence("SELECT oi.id,oi.goodsName,oi.curPrice,oi.specs,oi.type,oi.buyNum,oi.hasReNum FROM order_item oi " +
                " WHERE oi.id = #{m.itemsId}  AND oi.isDel=0 and oi.refundStatus IN ('-1','2')");
        sqlMap.put("itemsId", orderItem.getId());
        userProjectItem = orderItemMapper.selectOneMap(sqlSentence);
        if (userProjectItem == null) {
            return;
        }
        userProjectItem.put("notUsedNum", Integer.parseInt(userProjectItem.get("buyNum").toString()) - Integer.parseInt(userProjectItem.get("hasReNum").toString()));
        Map<String,Object> itemData = itemRefandDataPackage(orderItem,null,null,null);
        //二级子订单
        sqlMap.put("orderItemId", orderItem.getId());
        sqlSentence.setSqlSentence("SELECT * FROM order_item_source WHERE orderItemId= #{m.orderItemId} and  isDel = 0 ");
        sqlSentence.sqlSentence("SELECT * FROM order_item_source WHERE orderItemId= #{m.orderItemId} and  isDel = 0 ",sqlMap);
        List<OrderItemSon> orderItemSonList = orderItemSonMapper.selectList(sqlSentence);
        if (orderItemSonList == null) {
            return;
        }
        List<Map<String, Object>> promotionList = new ArrayList<>();
        Map<String, Object> promotion = null;
        for (OrderItemSon orderItemSon : orderItemSonList) {
            //判断这个项目能不能显示处理
            switch (orderItemSon.getType()) {
                case OrderGoodsConstants.TYPE_PROJECT:
                    handleOrderItemSonIsProject(promotionList, promotion, orderItemSon, sqlSentence, sqlMap,userId);
                    handleOrderItemSonIsProject(promotionList, orderItemSon);
                    break;
                case OrderItemConstants.TYPE_RETAIL:
                    handleOrderItemSonIsRetail(promotionList, promotion, orderItemSon, sqlSentence, sqlMap);
                    handleOrderItemSonIsRetail(promotionList, orderItemSon);
                    break;
                default:
                    break;
            }
        }
        if (promotionList == null || promotionList.size() == 0) {
            return;
        }
        userProjectItem.put("list", promotionList);
        orderItems.add(userProjectItem);
        itemData.put("list", promotionList);
        orderItems.add(itemData);
    }
    /**
     * 处理二级是项目的
     */
    public void handleOrderItemSonIsProject(List<Map<String, Object>> promotionList, Map<String, Object> promotion, OrderItemSon orderItemSon, SqlSentence sqlSentence, Map<String, Object> sqlMap,String userId) {
        sqlSentence.setSqlSentence("SELECT ois.id,ois.goodsName,ois.curPrice,ois.specs,ois.type,ois.buyNum,upi.notUsedNum,ois.hasReNum  FROM order_item_source ois " +
                " LEFT JOIN user_project_item upi ON upi.commonId=ois.id WHERE ois.id = #{m.itemsId}  AND ois.isDel=0 AND upi.isDel=0 AND upi.userId=#{m.userId} ");
        sqlMap.put("itemsId", orderItemSon.getId());
        sqlMap.put("userId", userId);
    public void handleOrderItemSonIsProject(List<Map<String, Object>> promotionList, OrderItemSon orderItemSon) {
        promotion = orderItemMapper.selectOneMap(sqlSentence);
        if (promotion == null) {
            return;
        //获取用户项目
        UserProjectItem userProjectItem = PartialRefundUtil.getUserProject(orderItemSon.getId(),commonService);
        if(userProjectItem == null){
            throw new TipsException("用户项目获取失败[24]!");
        }
        if (promotion.get("buyNum").toString().equals(promotion.get("hasReNum").toString())) {
            return;//已经退款了
        }
        if (Integer.parseInt(String.valueOf(promotion.get("notUsedNum"))) < 1) {
            return;//用户未执行数量不够了
        }
        Map<String,Object> itemData = itemRefandDataPackage(null,orderItemSon,userProjectItem,null);
        promotionList.add(promotion);
        promotionList.add(itemData);
    }
    /**
     * 处理二级是商品的
     */
    public void handleOrderItemSonIsRetail(List<Map<String, Object>> promotionList, Map<String, Object> promotion, OrderItemSon orderItemSon, SqlSentence sqlSentence, Map<String, Object> sqlMap) {
        sqlSentence.setSqlSentence("SELECT ois.id,ois.goodsName,ois.curPrice,ois.specs,ois.type,ois.buyNum,ois.hasReNum FROM order_item_source ois " +
                " WHERE ois.id = #{m.itemsId}  AND ois.isDel=0 and ois.refundStatus IN ('-1','2')");
        sqlMap.put("itemsId", orderItemSon.getId());
        promotion = orderItemMapper.selectOneMap(sqlSentence);
        if (promotion == null) {
            return;
        }
        if (promotion.get("buyNum").toString().equals(promotion.get("hasReNum").toString())) {
            return;//已经退款了
        }
        promotion.put("notUsedNum", Integer.parseInt(promotion.get("buyNum").toString()) - Integer.parseInt(promotion.get("hasReNum").toString()));
        promotionList.add(promotion);
    public void handleOrderItemSonIsRetail(List<Map<String, Object>> promotionList, OrderItemSon orderItemSon) {
        promotionList.add(itemRefandDataPackage(null,orderItemSon,null,null));
    }
    /**
     * 退款选择退款数量组装金额信息
     *
     * @param orderRefundDto
     * @return
     * 退款选择退款数量组装金额信息,退款里面这个非常重要
     * @param orderRefundDto 退款数量信息
     * @return 退款金额信息
     */
    @Override
    public OrderRefundDto nextStep(OrderRefundDto orderRefundDto) {
    public OrderRefundDto nextStep(OrdersTotal ordersTotal,OrderRefundDto orderRefundDto) {
        logger.info("退款选择退款数量组装金额信息入参:{}", JSON.toJSONString(orderRefundDto));
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> sqlMap = new HashMap<>();
        sqlSentence.setM(sqlMap);
        sqlMap.put("isDel", BaseEntity.NO);
        sqlMap.put("orderId", orderRefundDto.getOrderId());
        sqlSentence.setSqlSentence("SELECT * FROM  orders_total   WHERE  id = #{m.orderId}  and  isDel = #{m.isDel} ");
        OrdersTotal ordersTotal = ordersTotalMapper.selectOne(sqlSentence);
        if (ordersTotal == null) {
            throw new TipsException("订单标识错误,找不到订单!");
        }
        //返回集合
        OrderRefundDto returnRefund=new OrderRefundDto();
        returnRefund.setOrderId(ordersTotal.getId());
        //子单退款
        List<OrderItemRefundDto> refundList = orderRefundDto.getRefundList();
        //支付方式退款数据整合
        List<OrderPayMethodDto> refundPayMethods = new ArrayList<>();
        //退款总金额
        BigDecimal totalAmount = BigDecimal.ZERO;
        List<OrderItemRefundDto> refundList = orderRefundDto.getRefundList();
        if(!OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){
            if (refundList == null || refundList.size() == 0) {
                throw new TipsException("请选择要退款的内容!");
        if (OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())) {
            ///////充值订单退款
            refundPayMethods = rechargeRefundWay(ordersTotal);
            for (OrderPayMethodDto orderPayMethodDto : refundPayMethods) {
                totalAmount = totalAmount.add(orderPayMethodDto.getPayTotal()).setScale(2,RoundingMode.HALF_UP);
            }
        }else{
            //////其他订单类型
            //整合支付方式退款金额
            Map<String,OrderPayMethodDto> orderPayMethodDtoMap = new HashMap<>();
            OrderPayMethodDto orderPayMethodDto;
            DistributionRedundVo distributionRedundVo;
            OrderItem orderItem;
            //计算每个条目需要退多少钱
            for (OrderItemRefundDto orderItemRefundDto : refundList) {
                if(StringUtils.isEmpty(orderItemRefundDto.getOrderItemId())){
                    throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款项标识必填");
                }
                orderItem = orderItemMapper.selectOneByKey(orderItemRefundDto.getOrderItemId());
                if (orderItem == null) {
                    throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款项标识错误");
                }
                if(!orderItem.getOrderId().equals(ordersTotal.getId())){
                    throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到退款项[012]");
                }
                //判断这个项目能不能显示处理
                switch (orderItem.getType()) {
                    case OrderItemConstants.TYPE_PROJECT:
                        distributionRedundVo = itemOneRefund(orderItem,orderItemRefundDto);
                        break;
                    case OrderItemConstants.TYPE_RETAIL:
                        distributionRedundVo = itemOneRefund(orderItem,orderItemRefundDto);
                        break;
                    case OrderItemConstants.TYPE_DRUG:
                        distributionRedundVo = itemOneRefund(orderItem,orderItemRefundDto);
                        break;
                    case OrderItemConstants.TYPE_CARD:
                        distributionRedundVo = itemOneRefund(orderItem,orderItemRefundDto);
                        break;
                    case OrderItemConstants.TYPE_PROMOTION:
                        distributionRedundVo = nextStepIsPromotion(orderItem,orderItemRefundDto);
                        break;
                    case OrderItemConstants.CARD_BAG:
                        distributionRedundVo = nextStepIsPromotion(orderItem,orderItemRefundDto);
                        break;
                    default:
                        throw new TipsException("未知商品类型[008]!");
                }
                //数据填充
                orderItemRefundDto.setApproveRefundTotal(distributionRedundVo.getRefundTotal());
                orderItemRefundDto.setType(distributionRedundVo.getGoodsType());
                orderItemRefundDto.setGoodsName(distributionRedundVo.getGoodsName());
                //可总退款金额计算
                totalAmount = totalAmount.add(distributionRedundVo.getRefundTotal()).setScale(2,RoundingMode.HALF_UP);
                //整合可退款方式金额
                for(DistributionRedundMethodVo refundPaymentMethodVo:distributionRedundVo.getDistributionRedundMethodVoList()){
                    orderPayMethodDto = orderPayMethodDtoMap.computeIfAbsent(refundPaymentMethodVo.getNumberNo(),
                            k->new OrderPayMethodDto(refundPaymentMethodVo.getNumberNo(),refundPaymentMethodVo.getName(),BigDecimal.ZERO,refundPaymentMethodVo.getIsMoneyPay(),refundPaymentMethodVo.getIsExecute()));
                    orderPayMethodDto.setPayTotal(orderPayMethodDto.getPayTotal().add(refundPaymentMethodVo.getRefundTotal()).setScale(2,RoundingMode.HALF_UP));
                }
            }
            for(Map.Entry<String, OrderPayMethodDto> entry : orderPayMethodDtoMap.entrySet()) {
                refundPayMethods.add(entry.getValue());
            }
        }
        for (OrderItemRefundDto orderItemRefundDto : refundList) {
            //判断这个项目能不能显示处理
            switch (orderItemRefundDto.getType()) {
                case OrderItemConstants.TYPE_PROJECT:
                    totalAmount = nextStepIsProject(orderItemRefundDto, totalAmount, sqlSentence, sqlMap,ordersTotal.getUserId());
                    break;
                case OrderItemConstants.TYPE_RETAIL:
                    totalAmount = nextStepIsRetail(orderItemRefundDto, totalAmount, sqlSentence, sqlMap);
                    break;
                case OrderItemConstants.TYPE_DRUG:
                    totalAmount = nextStepIsRetail(orderItemRefundDto, totalAmount, sqlSentence, sqlMap);
                    break;
                case OrderItemConstants.TYPE_CARD:
                    totalAmount = nextStepIsCard(orderItemRefundDto, totalAmount, sqlSentence, sqlMap);
                    break;
                case OrderItemConstants.TYPE_PROMOTION:
                    totalAmount = nextStepIsPromotion(orderItemRefundDto, totalAmount, sqlSentence, sqlMap,ordersTotal.getUserId());
                    break;
                case OrderItemConstants.CARD_BAG:
                    nextStepIsPromotion(orderItemRefundDto, totalAmount, sqlSentence, sqlMap,ordersTotal.getUserId());
                    break;
                default:
                    break;
            }
        }
        returnRefund.setRefundList(refundList);
        returnRefund.setTotalAmount(totalAmount);
        if (!OrderTotalConstants.TYPE_CARD_BAG.equals(ordersTotal.getType())) {
            //支付方式
            List<OrderPayMethodDto> refundPayMethods = nextStepIsSummary(ordersTotal.getId(),ordersTotal,refundList);
            returnRefund.setRefundPayMethod(refundPayMethods);
            //优惠卷
            List<OrderCouponRefunDto> couponList = nextStepIsCoupon(ordersTotal.getId());
            returnRefund.setCouponList(couponList);
        }else {
            returnRefund.setRefundPayMethod(new ArrayList<>());
            returnRefund.setCouponList(new ArrayList<>());
        }
        //支付方式
        orderRefundDto.setPayMethodList(refundPayMethods);
        //可退总金额
        orderRefundDto.setTotalAmount(totalAmount);
        //优惠券
        List<OrderCouponRefunDto> couponList = nextStepIsCoupon(ordersTotal.getId());
        orderRefundDto.setPayCouponList(couponList);
        return returnRefund;
        return orderRefundDto;
    }
    /**
     * 判断每个支付方式最大可分摊退金额
     */
    public List<OrderPayMethodDto> nextStepIsSummary(String orderId,OrdersTotal ordersTotal,List<OrderItemRefundDto> refundList) {
    /**获取充值单的退款方式金额*/
    public List<OrderPayMethodDto> rechargeRefundWay(OrdersTotal ordersTotal){
        //获取总支付方式
        List<PayMethodVo> payMethodVoList = refundMapper.getConsumePayGroupByNumberNo(ordersTotal.getId());
//        BigDecimal actualTotal=ordersTotal.getActualTotal();
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> sqlMap = new HashMap<>();
        sqlSentence.setM(sqlMap);
        sqlMap.put("isDel", BaseEntity.NO);
        sqlMap.put("orderId", orderId);
        //后续改成用代码处理分组
        sqlSentence.setSqlSentence("SELECT numberNo ,name,CAST(COALESCE(sum(actualTotal),0) as  DECIMAL(11,2))   AS actualTotal, " +
                " CAST(COALESCE(sum(refundTotal),0) as  DECIMAL(11,2))    AS refundTotal  FROM  consume_pay WHERE orderId= #{m.orderId}  and actualTotal>refundTotal and  isDel = #{m.isDel} GROUP BY numberNo ");
        List<ConsumePay> consumePays = consumePayMapper.selectList(sqlSentence);
        //拼接返回参数
        List<OrderPayMethodDto> consumeList=new ArrayList<>();
        List<OrderPayMethodDto> consumeList = new ArrayList<>();
        OrderPayMethodDto orderPayMethodDto;
        //判断这个支付方式可退金额是否够退,用于计算最后一个支付方式的可退金额
        BigDecimal total = BigDecimal.ZERO;
        BigDecimal surplusTotal = BigDecimal.ZERO;
        ConsumePay consumePay;
        if (OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())) {
            //充值订单只能全退
            for (int i = consumePays.size()-1; i >=0; i--) {
                consumePay=consumePays.get(i);
                //支付方式可退金额
                surplusTotal=consumePay.getActualTotal().subtract(consumePay.getRefundTotal()).setScale(2,BigDecimal.ROUND_HALF_UP);
                orderPayMethodDto=new OrderPayMethodDto();
                orderPayMethodDto.setPayMethodNo(consumePay.getNumberNo());
                orderPayMethodDto.setRefundNumberNo(consumePay.getNumberNo());
                orderPayMethodDto.setPayMethodName(consumePay.getName());
                orderPayMethodDto.setRefundNumberName(consumePay.getName());
                orderPayMethodDto.setPayTotal(surplusTotal);
                orderPayMethodDto.setMoney(orderPayMethodDto.getPayTotal());
                consumeList.add(orderPayMethodDto);
            }
            return consumeList;
        }
        //本次退款总金额
        BigDecimal refundMoney=refundList.stream().map(OrderItemRefundDto::getRefundMoney).reduce(BigDecimal.ZERO,BigDecimal::add);
        BigDecimal actualTotal=consumePays.stream().map(ConsumePay::getActualTotal).reduce(BigDecimal.ZERO,BigDecimal::add);
        //计算分摊百分比
        BigDecimal percentage;
        if (refundMoney.compareTo(BigDecimal.ZERO) == 0 || actualTotal.compareTo(BigDecimal.ZERO)==0){
            percentage=BigDecimal.ZERO;
        }else {
            percentage=refundMoney.divide(actualTotal,10,BigDecimal.ROUND_HALF_UP);
        }
        for (int i = consumePays.size()-1; i >=0; i--) {
            consumePay=consumePays.get(i);
        //充值订单只能全退
        for (PayMethodVo payMethodVo:payMethodVoList) {
            //支付方式可退金额
            surplusTotal=consumePay.getActualTotal().subtract(consumePay.getRefundTotal()).setScale(2,BigDecimal.ROUND_HALF_UP);
            orderPayMethodDto=new OrderPayMethodDto();
            orderPayMethodDto.setPayMethodNo(consumePay.getNumberNo());
            orderPayMethodDto.setRefundNumberNo(consumePay.getNumberNo());
            orderPayMethodDto.setPayMethodName(consumePay.getName());
            orderPayMethodDto.setRefundNumberName(consumePay.getName());
            //计算出支付方式本次分摊下来的资金
            if(i==0){
                total=refundMoney;
            }else {
                total=consumePay.getActualTotal().multiply(percentage).setScale(2,BigDecimal.ROUND_HALF_UP);
            }
            if(surplusTotal.compareTo(total)==1){
                orderPayMethodDto.setPayTotal(total);
                refundMoney=refundMoney.subtract(total).setScale(2,BigDecimal.ROUND_HALF_UP);
            }else {
                orderPayMethodDto.setPayTotal(surplusTotal);
                refundMoney=refundMoney.subtract(surplusTotal).setScale(2,BigDecimal.ROUND_HALF_UP);
            }
            orderPayMethodDto.setPayMethodNo(payMethodVo.getNumberNo());
            orderPayMethodDto.setPayMethodName(payMethodVo.getName());
            orderPayMethodDto.setRefundNumberNo(payMethodVo.getNumberNo());
            orderPayMethodDto.setRefundNumberName(payMethodVo.getName());
            orderPayMethodDto.setPayTotal(payMethodVo.getSurplusTotal());
            orderPayMethodDto.setMoney(orderPayMethodDto.getPayTotal());
            consumeList.add(orderPayMethodDto);
        }
        if(refundMoney.compareTo(BigDecimal.ZERO)!=0){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款金额错误:"+refundMoney);
        }
        return consumeList;
    }
    /**
     * 判断是否有使用优惠卷
     */
    public List<OrderCouponRefunDto> nextStepIsCoupon(String orderId) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> sqlMap = new HashMap<>();
        sqlSentence.setM(sqlMap);
        sqlMap.put("isDel", BaseEntity.NO);
        sqlMap.put("orderId", orderId);
        sqlSentence.setSqlSentence("SELECT id,title FROM coupon_order_discount_log WHERE orderId=#{m.orderId} AND isDel=  #{m.isDel} ");
        sqlMap.put("status", BaseEntity.NO);
        sqlSentence.sqlSentence("SELECT id,title FROM coupon_order_discount_log WHERE orderId=#{m.orderId} AND status = #{m.status} AND isDel=  #{m.isDel} ",sqlMap);
        List<CouponOrderDiscountLog> couponOrderDiscountLogs = couponOrderDiscountLogMapper.selectList(sqlSentence);
        //拼接返回参数
        List<OrderCouponRefunDto> consumeList=new ArrayList<>();
@@ -611,337 +498,341 @@
    }
    /**
     * 一级是项目
     * 一级子单计算可退款金额
     * @param orderItemRefundDto 封装的方法
     * @return 退款金额
     */
    public BigDecimal nextStepIsProject(OrderItemRefundDto orderItemRefundDto, BigDecimal totalAmount,
                                        SqlSentence sqlSentence, Map<String, Object> sqlMap,String userId) {
        Integer refundNum = orderItemRefundDto.getRefundNum();
        if (refundNum < 1) {
//            return totalAmount;
    public DistributionRedundVo itemOneRefund(OrderItem orderItem,OrderItemRefundDto orderItemRefundDto) {
        if(orderItemRefundDto.getRefundNum() == null) {
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请填写退款数量!");
        }
        if(orderItemRefundDto.getRefundNum() < 1) {
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款数量不能小于1");
        }
       /* sqlSentence.setSqlSentence("SELECT id,goodsName,actualTotal,usedTotal,type " +
                " FROM order_item   WHERE id = #{m.itemsId} ");*/
        sqlSentence.setSqlSentence("SELECT oi.id,oi.goodsName,oi.actualTotal,oi.usedTotal,oi.type ,upi.notUsedNum as hasReNum FROM order_item oi LEFT JOIN user_project_item upi ON upi.commonId=oi.id " +
                " WHERE oi.id = #{m.itemsId} AND oi.isDel=0 AND upi.isDel=0 AND upi.userId=#{m.userId} ");
        sqlMap.put("itemsId", orderItemRefundDto.getOrderItemId());
        sqlMap.put("userId", userId);
        OrderItem orderItem = orderItemMapper.selectOne(sqlSentence);
        if (orderItem == null) {
//            return totalAmount;
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到正确的子订单标识");
        }
        //2022-12-29 荣爷说因为优惠卷超过了金额,数据库是负数,所以退款取0
        if(orderItem.getActualTotal().compareTo(BigDecimal.ZERO)<1){
            orderItem.setActualTotal(BigDecimal.ZERO);
        }
        BigDecimal total=BigDecimal.ZERO;
        if(orderItem.getActualTotal().compareTo(BigDecimal.ZERO)==1){
            BigDecimal price = orderItem.getActualTotal().divide(new BigDecimal(String.valueOf(orderItem.getUsedTotal())), 2, BigDecimal.ROUND_HALF_UP);
            total=BigDecimal.ZERO;
            if(refundNum==orderItem.getHasReNum()){
                for (Integer i = 1; i <= refundNum; i++) {
                    if(i==refundNum){
                        total=total.add(orderItem.getActualTotal().subtract(price.multiply(new BigDecimal(orderItem.getUsedTotal()-1)).setScale(2,BigDecimal.ROUND_HALF_UP)).setScale(2,BigDecimal.ROUND_HALF_UP));
                    }else {
                        total=total.add(price.multiply(new BigDecimal(1)).setScale(2, BigDecimal.ROUND_HALF_UP));
                    }
                }
            }else {
                total = price.multiply(new BigDecimal(refundNum)).setScale(2, BigDecimal.ROUND_HALF_UP);
        ////算数量,退款用
        //总数量
        Integer buyNum;
        //剩余数量
        Integer surplusNum;
        //获取用户项目
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> sqlMap = new HashMap<>();
        if(OrderItemConstants.TYPE_PROJECT.equals(orderItem.getType())){
            //////项目
            UserProjectItem userProjectItem = PartialRefundUtil.getUserProject(orderItem.getId(),commonService);
            if(userProjectItem == null){
                //buyNum = orderItem.getUsedTotal();
                //surplusNum = orderItem.getUsedTotal() - orderItem.getHasReNum();
                throw new TipsException("用户项目获取失败!");
            }
            buyNum = userProjectItem.getUsedTotal();
            surplusNum = userProjectItem.getNotUsedNum();
            if(orderItemRefundDto.getRefundNum() > surplusNum){
                throw new TipsException("退款数量与可退数量不符!");
            }
            //替换成子单的剩余数量
            surplusNum = orderItem.getUsedTotal() - orderItem.getHasReNum();
        }else if(OrderItemConstants.TYPE_CARD.equals(orderItem.getType())){
            ///////卡项
            //获取可退款的卡项次数
            List<UserCard> userCardList = PartialRefundUtil.getRefundCard(orderItem.getId(),UserProjectConstants.EFF_STATUS_YES,commonService);
            buyNum = orderItem.getBuyNum();
            surplusNum = userCardList.size();
            if(orderItemRefundDto.getRefundNum() > surplusNum){
                throw new TipsException("退款数量与可退数量不符!");
            }
            //替换成子单的剩余数量
            surplusNum = orderItem.getBuyNum() - orderItem.getHasReNum();
        }else{
            buyNum = orderItem.getBuyNum();
            //替换成子单的剩余数量
            surplusNum = orderItem.getBuyNum() - orderItem.getHasReNum();
        }
        if(orderItemRefundDto.getRefundNum() > surplusNum){
            throw new TipsException("退款数量与可退数量不符!");
        }
        //获取该子单的支付方式,相同的支付方式求和返回
        List<PayMethodVo> payMethodVoList = refundMapper.getConsumePayOneGroupByNumberNo(orderItem.getId());
        orderItemRefundDto.setRefundMoney(total);
        orderItemRefundDto.setGoodsName(orderItem.getGoodsName());
        orderItemRefundDto.setType(orderItem.getType());
        totalAmount = totalAmount.add(total);
        return totalAmount;
        //获取已退款的金额
        sqlMap.clear();
        sqlMap.put("refundStatus",RefundStatus.STATUS_SUCC_REFUND);
        sqlMap.put("orderItemId",orderItem.getId());
        sqlSentence.sqlSentence("SELECT ROUND(SUM(rri.occupyRefundTotal),2) AS occupyRefundTotal FROM refund_record_item rri" +
                " JOIN refund_record rr ON rr.id = rri.refundRecordId AND rr.isDel = 0 AND rr.refundStatus = #{m.refundStatus}" +
                " WHERE rri.isDel = 0 AND rri.orderItemId = #{m.orderItemId}",sqlMap);
        RefundRecordItem refundRecordItem = commonService.selectOne(RefundRecordItemMapper.class,sqlSentence);
        //计算退款方式金额
        DistributionRedundVo distributionRedundVo = PaymentCountTool.countMakeWay(orderItem.getId(),orderItem.getActualTotal(),refundRecordItem==null?BigDecimal.ZERO:refundRecordItem.getOccupyRefundTotal()
                ,buyNum,surplusNum,null,orderItemRefundDto.getRefundNum(),payMethodVoList,commonService);
        distributionRedundVo.setGoodsType(orderItem.getType());
        distributionRedundVo.setGoodsName(orderItem.getGoodsName());
        return distributionRedundVo;
    }
    /**
     * 一级是商品
     * 二级子单计算可退款金额
     * @param orderItemSourceRefundDto 封装的方法
     * @param goodsType 子单的商品类型
     * @return 退款金额
     */
    public BigDecimal nextStepIsRetail(OrderItemRefundDto orderItemRefundDto, BigDecimal totalAmount,
                                       SqlSentence sqlSentence, Map<String, Object> sqlMap) {
        Integer refundNum = orderItemRefundDto.getRefundNum();
        if (orderItemRefundDto.getRefundNum() < 1) {
//            return totalAmount;
    public DistributionRedundVo itemTwoRefund(OrderItemSon orderItemSon,OrderItemSourceRefundDto orderItemSourceRefundDto,String goodsType) {
        if(orderItemSourceRefundDto.getRefundNum() == null) {
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请填写退款数量!");
        }
        if(orderItemSourceRefundDto.getRefundNum() < 1) {
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款数量不能小于1");
        }
        sqlSentence.setSqlSentence("SELECT id,goodsName,actualTotal,buyNum,type " +
                " FROM order_item   WHERE id = #{m.itemsId} ");
        sqlMap.put("itemsId", orderItemRefundDto.getOrderItemId());
        OrderItem orderItem = orderItemMapper.selectOne(sqlSentence);
        if (orderItem == null) {
//            return totalAmount;
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到正确的子订单标识");
        }
        //2022-12-29 荣爷说因为优惠卷超过了金额,数据库是负数,所以退款取0
        if(orderItem.getActualTotal().compareTo(BigDecimal.ZERO)<1){
            orderItem.setActualTotal(BigDecimal.ZERO);
        }
        BigDecimal total=BigDecimal.ZERO;
        if(orderItem.getActualTotal().compareTo(BigDecimal.ZERO)==1){
            BigDecimal price = orderItem.getActualTotal().divide(new BigDecimal(String.valueOf(orderItem.getBuyNum())), 2, BigDecimal.ROUND_HALF_UP);
            total = price.multiply(new BigDecimal(refundNum)).setScale(2, BigDecimal.ROUND_HALF_UP);
        ////算数量
        //总数量
        Integer buyNum;
        //剩余数量
        Integer surplusNum;
        //获取用户项目
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> sqlMap = new HashMap<>();
        UserProjectItem userProjectItem = null;
        if(OrderItemConstants.TYPE_PROJECT.equals(goodsType)){
            //获取用户项目
            userProjectItem = PartialRefundUtil.getUserProject(orderItemSon.getId(),commonService);
            if(userProjectItem == null){
                throw new TipsException("用户项目获取失败!");
            }
            buyNum = userProjectItem.getUsedTotal();
            surplusNum = userProjectItem.getNotUsedNum();
            if(orderItemSourceRefundDto.getRefundNum() > surplusNum){
                throw new TipsException("退款数量与可退数量不符!");
            }
            //替换成子单
            surplusNum = orderItemSon.getUsedTotal() - orderItemSon.getHasReNum();
        }else{
            buyNum = orderItemSon.getBuyNum();
            surplusNum = orderItemSon.getBuyNum() - orderItemSon.getHasReNum();
        }
        orderItemRefundDto.setRefundMoney(total);
        orderItemRefundDto.setGoodsName(orderItem.getGoodsName());
        orderItemRefundDto.setType(orderItem.getType());
        //叠加金额
        totalAmount = totalAmount.add(total);
        return totalAmount;
    }
    /**
     * 一级是卡项
     */
    public BigDecimal nextStepIsCard(OrderItemRefundDto orderItemRefundDto, BigDecimal totalAmount,
                                     SqlSentence sqlSentence, Map<String, Object> sqlMap) {
        Integer refundNum = orderItemRefundDto.getRefundNum();
        if (orderItemRefundDto.getRefundNum() < 1) {
//            return totalAmount;
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款数量不能小于1");
        }
        sqlSentence.setSqlSentence("SELECT id,goodsName,actualTotal,buyNum,type " +
                " FROM order_item   WHERE id = #{m.itemsId} ");
        sqlMap.put("itemsId", orderItemRefundDto.getOrderItemId());
        OrderItem orderItem = orderItemMapper.selectOne(sqlSentence);
        if (orderItem == null) {
//            return totalAmount;
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到正确的子订单标识");
        }
        //2022-12-29 荣爷说因为优惠卷超过了金额,数据库是负数,所以退款取0
        if(orderItem.getActualTotal().compareTo(BigDecimal.ZERO)<1){
            orderItem.setActualTotal(BigDecimal.ZERO);
        }
        BigDecimal total=BigDecimal.ZERO;
        if(orderItem.getActualTotal().compareTo(BigDecimal.ZERO)==1){
            BigDecimal price = orderItem.getActualTotal().divide(new BigDecimal(String.valueOf(orderItem.getBuyNum())), 2, BigDecimal.ROUND_HALF_UP);
            total = price.multiply(new BigDecimal(refundNum)).setScale(2, BigDecimal.ROUND_HALF_UP);
        if(orderItemSourceRefundDto.getRefundNum() > surplusNum){
            throw new TipsException("退款数量与可退数量不符!");
        }
        orderItemRefundDto.setRefundMoney(total);
        orderItemRefundDto.setGoodsName(orderItem.getGoodsName());
        orderItemRefundDto.setType(orderItem.getType());
        //获取该子单的支付方式,相同的支付方式算和返回
        List<PayMethodVo> payMethodVoList = refundMapper.getConsumePayTwoGroupByNumberNo(orderItemSon.getId());
        //叠加金额
        totalAmount = totalAmount.add(total);
        return totalAmount;
        //获取已退款的金额
        sqlMap.clear();
        sqlMap.put("refundStatus",RefundStatus.STATUS_SUCC_REFUND);
        sqlMap.put("orderItemSonId",orderItemSon.getId());
        sqlSentence.sqlSentence("SELECT ROUND(SUM(rris.occupyRefundTotal),2) AS occupyRefundTotal FROM refund_record_item_source rris" +
                " JOIN refund_record rr ON rr.id = rris.refundRecordId AND rr.isDel = 0 AND rr.refundStatus = #{m.refundStatus}" +
                " WHERE rris.isDel = 0 AND rris.orderItemSonId = #{m.orderItemSonId}",sqlMap);
        RefundRecordItemSource refundRecordItemSource = commonService.selectOne(RefundRecordItemSourceMapper.class,sqlSentence);
        //计算退款方式金额
        DistributionRedundVo distributionRedundVo = PaymentCountTool.countMakeWay(orderItemSon.getId(),orderItemSon.getActualTotal(),refundRecordItemSource==null?BigDecimal.ZERO:refundRecordItemSource.getOccupyRefundTotal()
                ,buyNum,surplusNum,null,orderItemSourceRefundDto.getRefundNum(),payMethodVoList,commonService);
        distributionRedundVo.setGoodsType(orderItemSon.getType());
        distributionRedundVo.setGoodsName(orderItemSon.getGoodsName());
        return distributionRedundVo;
    }
    /**
     * 一级是促销
     */
    public BigDecimal nextStepIsPromotion(OrderItemRefundDto orderItemRefundDto, BigDecimal totalAmount,
                                          SqlSentence sqlSentence, Map<String, Object> sqlMap,String userId) {
    public DistributionRedundVo nextStepIsPromotion(OrderItem orderItem,OrderItemRefundDto orderItemRefundDto) {
        List<OrderItemSourceRefundDto> orderItemSourceRefundDtos = orderItemRefundDto.getOrderItemSourceRefundDtos();
        if (orderItemSourceRefundDtos == null) {
            //            return totalAmount;
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款数量不能小于1");
        }
        sqlSentence.setSqlSentence("SELECT id,goodsName,actualTotal,buyNum,type " +
                " FROM order_item   WHERE id = #{m.itemsId} ");
        sqlMap.put("itemsId", orderItemRefundDto.getOrderItemId());
        OrderItem orderItem = orderItemMapper.selectOne(sqlSentence);
        if (orderItem == null) {
//            return totalAmount;
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到正确的子订单标识");
        }
        //2022-12-29 荣爷说因为优惠卷超过了金额,数据库是负数,所以退款取0
        if(orderItem.getActualTotal().compareTo(BigDecimal.ZERO)<1){
            orderItem.setActualTotal(BigDecimal.ZERO);
        }
        BigDecimal orderItemAmount = BigDecimal.ZERO;
        //返回结构
        DistributionRedundVo distributionRedundVo = new DistributionRedundVo();
        //整合当前一级子单的退款金额数据,key值:支付方式编号
        Map<String,DistributionRedundMethodVo> refundPaymentMethodVoMap = new HashMap<>();
        DistributionRedundMethodVo refundPaymentMethodVo;
        DistributionRedundVo distributionRedundVoSon;
        for (OrderItemSourceRefundDto orderItemSourceRefundDto : orderItemSourceRefundDtos) {
            switch (orderItemSourceRefundDto.getType()) {
            if(StringUtils.isEmpty(orderItemSourceRefundDto.getOrderItemSonId())){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款项标识必填[54]");
            }
            OrderItemSon orderItemSon = orderItemSonMapper.selectOneByKey(orderItemSourceRefundDto.getOrderItemSonId());
            if (orderItemSon == null) {
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款项标识错误[54]");
            }
            if (!orderItemSon.getOrderItemId().equals(orderItem.getId())) {
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到退款项[54]");
            }
            switch (orderItemSon.getType()) {
                case OrderGoodsConstants.TYPE_PROJECT:
                    orderItemAmount= nextStepSonIsProject(orderItemSourceRefundDto, orderItemAmount, sqlSentence, sqlMap,userId) ;
                    distributionRedundVoSon = itemTwoRefund(orderItemSon,orderItemSourceRefundDto,orderItemSourceRefundDto.getType());
                    break;
                case OrderItemConstants.TYPE_RETAIL:
                    orderItemAmount= nextStepSonIsRetail(orderItemSourceRefundDto, orderItemAmount, sqlSentence, sqlMap);
                    distributionRedundVoSon = itemTwoRefund(orderItemSon,orderItemSourceRefundDto,orderItemSourceRefundDto.getType());
                    break;
                case OrderItemConstants.TYPE_DRUG:
                    orderItemAmount= nextStepSonIsRetail(orderItemSourceRefundDto, orderItemAmount, sqlSentence, sqlMap);
                    distributionRedundVoSon = itemTwoRefund(orderItemSon,orderItemSourceRefundDto,orderItemSourceRefundDto.getType());
                    break;
                default:
                    break;
                    throw new TipsException("未知商品类型[098]!");
            }
        }
        orderItemRefundDto.setRefundMoney(orderItemAmount);
        orderItemRefundDto.setGoodsName(orderItem.getGoodsName());
        orderItemRefundDto.setType(orderItem.getType());
        totalAmount = totalAmount.add(orderItemAmount);
        return totalAmount;
    }
            //数据填充
            orderItemSourceRefundDto.setType(distributionRedundVoSon.getGoodsType());
            orderItemSourceRefundDto.setGoodsName(distributionRedundVoSon.getGoodsName());
            orderItemSourceRefundDto.setApproveRefundTotal(distributionRedundVoSon.getRefundTotal());
    /**
     * 二级是项目
     */
    public BigDecimal nextStepSonIsProject(OrderItemSourceRefundDto orderItemSourceRefundDto, BigDecimal totalAmount,
                                           SqlSentence sqlSentence, Map<String, Object> sqlMap,String userId) {
        Integer refundNum = orderItemSourceRefundDto.getRefundNum();
        if (refundNum < 1) {
            //            return totalAmount;
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款数量不能小于1");
        }
       /* sqlSentence.setSqlSentence("SELECT id,goodsName,actualTotal,usedTotal,type " +
                " FROM order_item_source   WHERE id = #{m.itemSonId} ");*/
        sqlSentence.setSqlSentence("SELECT oi.id,oi.goodsName,oi.actualTotal,oi.usedTotal,oi.type ,upi.notUsedNum as hasReNum FROM order_item_source oi LEFT JOIN user_project_item upi ON upi.commonId=oi.id " +
                " WHERE oi.id = #{m.itemSonId} AND oi.isDel=0 AND upi.isDel=0 AND upi.userId=#{m.userId} ");
        sqlMap.put("itemSonId", orderItemSourceRefundDto.getOrderItemSonId());
        sqlMap.put("userId", userId);
        OrderItemSon orderItemSon = orderItemSonMapper.selectOne(sqlSentence);
        if (orderItemSon == null) {
            //            return totalAmount;
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到正确的二级子订单标识");
        }
        //2022-12-29 荣爷说因为优惠卷超过了金额,数据库是负数,所以退款取0
        if(orderItemSon.getActualTotal().compareTo(BigDecimal.ZERO)<1){
            orderItemSon.setActualTotal(BigDecimal.ZERO);
        }
            //返回结构金额填充
            distributionRedundVo.setRefundTotal(distributionRedundVo.getRefundTotal().add(distributionRedundVoSon.getRefundTotal()).setScale(2,RoundingMode.HALF_UP));
            distributionRedundVo.setRefundCash(distributionRedundVo.getRefundCash().add(distributionRedundVoSon.getRefundCash()).setScale(2,RoundingMode.HALF_UP));
            distributionRedundVo.setRefundExecute(distributionRedundVo.getRefundExecute().add(distributionRedundVoSon.getRefundExecute()).setScale(2,RoundingMode.HALF_UP));
        BigDecimal total=BigDecimal.ZERO;
        if(orderItemSon.getActualTotal().compareTo(BigDecimal.ZERO)==1){
            BigDecimal price = orderItemSon.getActualTotal().divide(new BigDecimal(String.valueOf(orderItemSon.getUsedTotal())), 2, BigDecimal.ROUND_HALF_UP);
            if(refundNum==orderItemSon.getHasReNum()){
                for (Integer i = 1; i <= refundNum; i++) {
                    if(i==refundNum){
                        total= total.add(orderItemSon.getActualTotal().subtract(orderItemSon.getActualTotal().divide(new BigDecimal(orderItemSon.getUsedTotal()),2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(orderItemSon.getUsedTotal()-1)).setScale(2,BigDecimal.ROUND_HALF_UP)).setScale(2,BigDecimal.ROUND_HALF_UP));
                    }else {
                        total= total.add(price.multiply(new BigDecimal(1)).setScale(2, BigDecimal.ROUND_HALF_UP));
                    }
                }
            }else {
                total = price.multiply(new BigDecimal(refundNum)).setScale(2, BigDecimal.ROUND_HALF_UP);
            //整合退款金额
            for(DistributionRedundMethodVo paymentMethodVo:distributionRedundVoSon.getDistributionRedundMethodVoList()){
                //整合金额,上级用到
                refundPaymentMethodVo = refundPaymentMethodVoMap.computeIfAbsent(paymentMethodVo.getNumberNo()
                        ,k-> new DistributionRedundMethodVo(paymentMethodVo.getNumberNo(),paymentMethodVo.getName(),BigDecimal.ZERO,paymentMethodVo.getIsMoneyPay(),paymentMethodVo.getIsExecute()));
                refundPaymentMethodVo.setRefundTotal(refundPaymentMethodVo.getRefundTotal().add(paymentMethodVo.getRefundTotal()).setScale(2,RoundingMode.HALF_UP));
            }
        }
        //返回结构数据填充
        distributionRedundVo.setGoodsName(orderItem.getGoodsName());
        distributionRedundVo.setGoodsType(orderItem.getType());
        orderItemSourceRefundDto.setRefundMoney(total);
        orderItemSourceRefundDto.setGoodsName(orderItemSon.getGoodsName());
        orderItemSourceRefundDto.setType(orderItemSon.getType());
        totalAmount = totalAmount.add(total);
        return totalAmount;
        //整合退款金额的数据
        List<DistributionRedundMethodVo> distributionPayList = new ArrayList<>();
        for(Map.Entry<String,DistributionRedundMethodVo> entry : refundPaymentMethodVoMap.entrySet()){
            distributionPayList.add(entry.getValue());
        }
        distributionRedundVo.setDistributionRedundMethodVoList(distributionPayList);
        return distributionRedundVo;
    }
    /**
     * 二级是是商品
     */
    public BigDecimal nextStepSonIsRetail(OrderItemSourceRefundDto orderItemSourceRefundDto, BigDecimal totalAmount,
                                          SqlSentence sqlSentence, Map<String, Object> sqlMap) {
        Integer refundNum = orderItemSourceRefundDto.getRefundNum();
        if (refundNum < 1) {
            //            return totalAmount;
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款数量不能小于1");
        }
        sqlSentence.setSqlSentence("SELECT id,goodsName,actualTotal,buyNum,type " +
                " FROM order_item_source   WHERE id = #{m.itemSonId} ");
        sqlMap.put("itemSonId", orderItemSourceRefundDto.getOrderItemSonId());
        OrderItemSon orderItemSon = orderItemSonMapper.selectOne(sqlSentence);
        if (orderItemSon == null) {
            //            return totalAmount;
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到正确的二级子订单标识");
        }
        //2022-12-29 荣爷说因为优惠卷超过了金额,数据库是负数,所以退款取0
        if(orderItemSon.getActualTotal().compareTo(BigDecimal.ZERO)<1){
            orderItemSon.setActualTotal(BigDecimal.ZERO);
        }
        BigDecimal total=BigDecimal.ZERO;
        if(orderItemSon.getActualTotal().compareTo(BigDecimal.ZERO)==1){
            BigDecimal price = orderItemSon.getActualTotal().divide(new BigDecimal(String.valueOf(orderItemSon.getBuyNum())), 2, BigDecimal.ROUND_HALF_UP);
            total = price.multiply(new BigDecimal(refundNum)).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        orderItemSourceRefundDto.setRefundMoney(total);
        orderItemSourceRefundDto.setGoodsName(orderItemSon.getGoodsName());
        orderItemSourceRefundDto.setType(orderItemSon.getType());
        totalAmount = totalAmount.add(total);
        return totalAmount;
    }
    /**
     * 确认退款处理退款逻辑
     * @param orderRefundDto
     * @return
     */
    @Override
    public PlatformResult partRefund(OrderRefundDto orderRefundDto) {
    public PlatformResult partRefund(OrdersTotal ordersTotal, OrderRefundDto orderRefundDto, ThirtApplication thirtApplication) {
        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查询多条总订单信息!");
        if(OrderTotalConstants.PAY_STATUS_SUC != ordersTotal.getPayStatus()){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单未支付!");
        }
        if(OrderTotalConstants.STATUS_PAY != ordersTotal.getStatus()
                && OrderTotalConstants.STATUS_WAIT_RECEIVE != ordersTotal.getStatus()
                && OrderTotalConstants.STATUS_DONE != ordersTotal.getStatus()){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单不是已支付状态!");
        }
        if(!(OrderTotalConstants.STATUS_REFUND_NONE==ordersTotal.getRefundStatus()) &&
                !(OrderTotalConstants.STATUS_REFUND_PART==ordersTotal.getRefundStatus())){
            throw new TipsException("订单标识错误,订单退款状态不正确!");
        if(OrderTotalConstants.STATUS_REFUND_NONE != ordersTotal.getRefundStatus() &&
                OrderTotalConstants.STATUS_REFUND_PART != ordersTotal.getRefundStatus()){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单退款状态不正确!");
        }
        //先优先改成待退款
        //先优先改成申请退款中
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> map=new HashMap<>();
        sqlSentence.setM(map);
        map.put("refundStatus",OrderTotalConstants.STATUS_REFUND_APPLY);
        map.put("isSyncOrder", BaseEntity.NO);
        map.put("id",ordersTotal.getId());
        sqlSentence.setSqlSentence(" refundStatus=#{m.refundStatus},isSyncOrder=#{m.isSyncOrder}  WHERE id=#{m.id}   AND refundStatus IN('"+OrderTotalConstants.STATUS_REFUND_NONE+"','"+OrderTotalConstants.STATUS_REFUND_PART+"')  ");
        int count= ordersTotalMapper.updateWhere(sqlSentence);
        if(count!=1){
            throw new TipsException("订单标识错误,订单正在进行操作!");
        sqlSentence.sqlSentence(" refundStatus=#{m.refundStatus},isSyncOrder=#{m.isSyncOrder}  WHERE id=#{m.id}   AND refundStatus IN('"+OrderTotalConstants.STATUS_REFUND_NONE+"','"+OrderTotalConstants.STATUS_REFUND_PART+"')  ",map);
        if(ordersTotalMapper.updateWhere(sqlSentence) != 1){
            throw new TipsException("操作失败,当前订单退款状态不能再次发起退款!");
        }
        orderRefundDto=parameterVerification(orderRefundDto);
        //校验参数和获取系统参数,非常重要
        orderRefundDto = parameterVerification(ordersTotal,orderRefundDto);
        return handlePartRefund(ordersTotal,orderRefundDto);
        return handlePartRefund(ordersTotal,orderRefundDto,thirtApplication);
    }
    /**
     * 校验参数,拼接参数
     */
    public OrderRefundDto  parameterVerification(OrderRefundDto orderRefundDto){
        //怕被获取到项目信息收到被改了项目价格,重新去获取一下项目价格
        OrderRefundDto returnMap =  nextStep(orderRefundDto);
    public OrderRefundDto  parameterVerification(OrdersTotal ordersTotal,OrderRefundDto orderRefundDto){
        List<OrderPayMethodDto> payMethodDtoList= orderRefundDto.getRefundPayMethod();
        List<OrderPayMethodDto> refundPayMethodList= returnMap.getRefundPayMethod();
        for (OrderPayMethodDto orderPayMethodDto : refundPayMethodList) {
            boolean existence=false;
            for (OrderPayMethodDto payMethodDto : payMethodDtoList) {
                if(orderPayMethodDto.getPayMethodNo().equals(payMethodDto.getPayMethodNo())){
                    if (payMethodDto.getMoney().compareTo(orderPayMethodDto.getMoney())==1){
                        throw new TipsException("当前退款方式金额超出可退金额!");
                    }
                    orderPayMethodDto.setMoney(payMethodDto.getMoney());
                    orderPayMethodDto.setRefundNumberNo(payMethodDto.getRefundNumberNo());
                    orderPayMethodDto.setRefundNumberName(payMethodDto.getRefundNumberName());
                    existence=true;
                }
            }
            if (!existence){
                throw new TipsException("退款支付方式错误!");
            }
        if(orderRefundDto.getRefundPayMethod() == null){
            throw new TipsException("退款方式结构不能空");
        }
        orderRefundDto.setRefundPayMethod(returnMap.getRefundPayMethod());
        orderRefundDto.setRefundList(returnMap.getRefundList());
        //怕被获取到项目信息收到被改了项目价格,重新去获取一下项目价格
        orderRefundDto =  nextStep(ordersTotal,orderRefundDto);
        //可退款支付格式,key值:支付编号
        Map<String,OrderPayMethodDto> orderPayMethodDtoMap = new HashMap<>();
        for (OrderPayMethodDto orderPayMethodDto : orderRefundDto.getPayMethodList()) {
            orderPayMethodDtoMap.put(orderPayMethodDto.getPayMethodNo(),orderPayMethodDto);
        }
        //填写退款的总金额
        BigDecimal refundTotal = BigDecimal.ZERO;
        //校验金额
        OrderPayMethodDto payMethodDto;
        //遍历系统获取到的可退款方式
        OrderPayMethodDto orderPayMethodDto;
        for (int i = orderRefundDto.getRefundPayMethod().size()-1; i>=0 ;i--) {
            orderPayMethodDto = orderRefundDto.getRefundPayMethod().get(i);
            //获取是传值过来的金额数据
            payMethodDto = orderPayMethodDtoMap.get(orderPayMethodDto.getPayMethodNo());
            if(payMethodDto == null){
                throw new TipsException("未找到["+orderPayMethodDto.getPayMethodName()+"]支付方式!");
            }
            //判断退款方式
            PaymentMethod refundMethod = paymentMethodService.selectNumberNoUncheckUp(orderPayMethodDto.getRefundNumberNo());
            if(refundMethod==null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该转换方式:"+orderPayMethodDto.getRefundNumberName()+"["+orderPayMethodDto.getRefundNumberNo()+"]");
            }
            if(orderPayMethodDto.getMoney() == null){
                throw new TipsException("["+orderPayMethodDto.getPayMethodName()+"]退款金额必填!");
            }
            if(orderPayMethodDto.getMoney().compareTo(BigDecimal.ZERO) < 0){
                throw new TipsException("["+orderPayMethodDto.getPayMethodName()+"]退款金额错误!");
            }
            if (orderPayMethodDto.getMoney().compareTo(payMethodDto.getPayTotal()) > 0){
                throw new TipsException("当前退款方式["+orderPayMethodDto.getPayMethodName()+"]金额超出可退金额!");
            }
            //退款金额是0元,去掉退款方式
            if(orderPayMethodDto.getMoney().compareTo(BigDecimal.ZERO) == 0){
                orderRefundDto.getRefundPayMethod().remove(i);
            }
            orderPayMethodDto.setPayMethodName(payMethodDto.getPayMethodName());
            orderPayMethodDto.setPayMethodNo(payMethodDto.getPayMethodNo());
            orderPayMethodDto.setPayTotal(payMethodDto.getPayTotal());
            refundTotal = refundTotal.add(orderPayMethodDto.getMoney()).setScale(2,RoundingMode.HALF_UP);
        }
        orderRefundDto.setRefundTotal(refundTotal);
        if(orderRefundDto.getRefundTotal().compareTo(orderRefundDto.getTotalAmount()) > 0){
            throw new TipsException("退款金额不能大于可退金额[004]!");
        }
        return orderRefundDto;
    }
@@ -949,114 +840,265 @@
    /**
     * 处理退款详情
     */
    public PlatformResult  handlePartRefund(OrdersTotal ordersTotal,OrderRefundDto orderRefundDto){
    public PlatformResult handlePartRefund(OrdersTotal ordersTotal,OrderRefundDto orderRefundDto,ThirtApplication thirtApplication){
        //订单节点日志
        OrdersNodeLog ordersNodeLog = new OrdersNodeLog();
        StringBuilder orderNodeBuilder = new StringBuilder();
        orderNodeBuilder.append("开始退款");
        // 获取退款方式,退款项目,退款优惠卷
        List<OrderPayMethodDto> refundPayMethod=orderRefundDto.getRefundPayMethod();
        List<OrderItemRefundDto> refundList=orderRefundDto.getRefundList();
        List<OrderCouponRefunDto> 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);
        if(!OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){
            if(sumMoney.compareTo(refundSumMoney)==1){
                throw new TipsException("退款金额不能大于可退金额!");
            }
            orderNodeBuilder.append("-本次退款项目总金额:"+refundSumMoney+",客户选的退款方式总金额:"+sumMoney);
        }else {
            orderNodeBuilder.append("-本次退款项目总金额:"+ordersTotal.getActualTotal()+",客户选的退款方式总金额:"+sumMoney);
        }
        orderNodeBuilder.append("-本次退款项目总金额:"+orderRefundDto.getTotalAmount()+",客户选的退款方式总金额:"+orderRefundDto.getRefundTotal());
        Map<String, String> operator = ApiOrderUtil.getOperator(commonService, orderRefundDto.getOperatorId());//获取操作人信息
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> map=new HashMap<>();
        sqlSentence.setM(map);
        //退款总记录
        orderNodeBuilder.append("-处理退款总记录");
        String totalCode = CreateNo.createTimeNo(systemParameterMapper, "R", 8, "yyyyMMddHHmmss");//总订单编号
        RefundRecord refundRecord=new RefundRecord(totalCode,ordersTotal.getShopId(),ordersTotal.getShopName(),sumMoney, RefundStatus.STATUS_APPLY_REFUND,0, RefundSoruceConstants.TYPE_SOURCE_ORDER,"phis退款",ordersTotal.getId(),ordersTotal.getUserId());
        refundRecord.setOperatorType(RefundRecord.OPERATOR_TYPE_EMPLOYEE);
        Employee employee = commonService.selectOneByKey(EmployeeMapper.class,orderRefundDto.getOperatorId());
        refundRecord.setOperatorId(employee.getId());
        refundRecord.setRefundOperationType(orderRefundDto.getRefundOperationType());
        refundRecord.setOperatorName(employee.getCnName());
        refundRecordService.insert(refundRecord);
        if(refundPayMethod!=null && refundPayMethod.size()>0){
            //处理退款方式记录表
            handleRefundPayMethod(refundPayMethod,orderNodeBuilder,ordersTotal,refundRecord);
        }
        if(refundList!=null && refundList.size()>0){
            //记录本次退款的项目详情
            //分摊比例
            //因为优惠卷抵扣金额可能超过订单的金额,所以不会有支付记录
            BigDecimal percentage;
            if(sumMoney.compareTo(BigDecimal.ZERO)==0){
                percentage=new BigDecimal(1);
            }else {
                percentage=sumMoney.divide(refundSumMoney,10,BigDecimal.ROUND_HALF_UP);
            }
            handleRefundRecordItem(refundList,orderNodeBuilder,ordersTotal,refundRecord,percentage);
        }
        if(couponList!=null && couponList.size()>0){
            //处理优惠卷
            handleOrderCouponRefunDto(couponList,orderNodeBuilder,ordersTotal,refundRecord);
        }
        //保存退款信息,不是真正的退款,提前扣减用户资金
        RefundRecord refundRecord = insertRefundInfo(orderRefundDto,ordersTotal,orderNodeBuilder,thirtApplication);
        //判断已支付订单是否需要营销助手审批
        //记录取消日志
        CancelOrder cancelOrder= handleCancelOrder(ordersTotal,orderRefundDto);
        CancelOrder cancelOrder = handleCancelOrder(ordersTotal,orderRefundDto);
        if (orderRefundDto.getIsApproval()==BaseEntity.YES){
            //调用审批成功
            //判断是否是充值订单,是的话先冻结储值金,就是扣掉,审批通过就不处理储值金了,不通过还原资金
            if(OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){
                //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
                if(ordersTotal.getTotal().compareTo(BigDecimal.ZERO)!=0){
                    String operationId=operator.get("operatorId");
                    String operatorName=operator.get("operatorName");
                    UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"充值单:"+ordersTotal.getOrderNo()+"申请退款,预扣除金额:"+ordersTotal.getTotal(),operationId,refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(String.valueOf(ordersTotal.getTotal())).negate(), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO);
                    OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord, operationId, operatorName, "充值单:"+ordersTotal.getOrderNo()+"申请退款,预扣除金额:"+ordersTotal.getTotal(), 0, OrderLogConstants.LOG_TYPE_REFUND);
                    commonService.insert(OrderLogMapper.class,orderLog);
                    //活动规则退款处理
                    map.put("orderId", ordersTotal.getId());
                    sqlSentence.setSqlSentence("select * from orders_info where isDel=0 and orderId=#{m.orderId}");
                    OrderInfo orderInfo=commonService.selectOne(OrderInfoMapper.class,sqlSentence);
                    handActivityRule(commonService, operationId, operatorName, sqlSentence, map, refundRecord,ordersTotal,orderInfo,0);
                }
            }
            PlatformResult platformResult=handleApproval(ordersNodeLog,orderNodeBuilder,ordersTotal,cancelOrder,refundRecord);
            return platformResult;
        if (orderRefundDto.getIsApproval().equals(BaseEntity.YES)){
            //调用审批功能
            return handleApproval(ordersNodeLog,orderNodeBuilder,ordersTotal,cancelOrder,refundRecord);
        }
        Map<String, Object> refund =handleRefundOrder(operator,refundRecord,ordersTotal,ordersNodeLog,orderNodeBuilder,cancelOrder);
        //这里开始真正的退款到用户账户的
        Map<String, Object> refund = handleRefundOrder(operator,refundRecord,ordersTotal,ordersNodeLog,orderNodeBuilder,cancelOrder);
        return PlatformResult.success(refund);
    }
    /**
     * 退款-处理活动规则增值金和积分
     * @param commonService
     * @param operationId
     * @param operationNme
     * @param sqlSentence
     * @param map
     * @param refundRecord
     * @param ordersTotal
     * @param "0"减 1加
    /**保存退款信息
     * 保存退款信息,并没有真正退款
     */
    public RefundRecord insertRefundInfo(OrderRefundDto orderRefundDto,OrdersTotal ordersTotal
            ,StringBuilder orderNodeBuilder, ThirtApplication thirtApplication){
        //操作人
        Employee employee = commonService.selectOneByKey(EmployeeMapper.class,orderRefundDto.getOperatorId());
        if(employee == null){
            throw new TipsException("操作人标识错误!");
        }
        ///////退款总表数据填充
        //生成退款编号
        String totalCode = createNoService.createNo("R",System.currentTimeMillis()+"",8);
        RefundRecord refundRecord=new RefundRecord(totalCode,ordersTotal.getShopId(),ordersTotal.getShopName(),orderRefundDto.getRefundTotal()
                , RefundStatus.STATUS_APPLY_REFUND,0, RefundSoruceConstants.TYPE_SOURCE_ORDER,orderRefundDto.getRemarks(),ordersTotal.getId(),ordersTotal.getUserId());
        //操作人信息
        refundRecord.setOperatorType(RefundRecord.OPERATOR_TYPE_EMPLOYEE);
        refundRecord.setOperatorId(employee.getId());
        refundRecord.setOperatorNo(employee.getEmployeeNo());
        refundRecord.setOperatorName(employee.getCnName());
        refundRecord.setRefundOperationType(orderRefundDto.getRefundOperationType());
        /////退款的备注
        refundRecord.setRemarks(orderRefundDto.getRemarks());
        refundRecord.setRefundReason(orderRefundDto.getRefundReason());
        refundRecord.setOperatorAppId(thirtApplication.getAppId());
        refundRecord.setOperatorAppCode(thirtApplication.getAppIdCode());
        refundRecord.setOperatorAppName(thirtApplication.getName());
        refundRecordService.insert(refundRecord);
        //退款方式处理
        if(orderRefundDto.getRefundPayMethod() !=null && orderRefundDto.getRefundPayMethod().size() > 0){
            handleRefundPayMethod(orderRefundDto,orderNodeBuilder,ordersTotal,refundRecord);
        }
        //退款的子项处理
        if(orderRefundDto.getRefundList()!=null && orderRefundDto.getRefundList().size()>0){
            insertRefundRecordItem(orderRefundDto,orderNodeBuilder,refundRecord);
        }
        //回退的优惠卷处理
        if(orderRefundDto.getCouponList() != null && orderRefundDto.getCouponList().size()>0){
            handleOrderCouponRefunDto(orderRefundDto.getCouponList(),orderNodeBuilder,ordersTotal,refundRecord);
        }
        //*******扣减赠送的账户资金,如果订单有老带新的赠送规则,那么如果申请退款,就优先把老带新的退回来*****
        //获取老带新记录
        oldBlingNewRefund(refundRecord,orderNodeBuilder,commonService);
        //判断是否是充值订单,是的话先冻结储值金,就是扣掉,审批通过就不处理储值金了,不通过还原资金
        if(OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){
            OrderRefundCancelTool.rechargeHandle(ordersTotal,refundRecord,OrderRefundCancelTool.OPT_TYPE_EXECUTE,commonService);
        }
        //活动处理
        OrderRefundCancelTool.activityRuleHandle(ordersTotal,refundRecord,OrderRefundCancelTool.OPT_TYPE_EXECUTE,commonService);
        return refundRecord;
    }
    /**老带新的订单退款,佣金处理*/
    public void oldBlingNewRefund(RefundRecord refundRecord,StringBuilder orderNodeBuilder,CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        //查找老带新记录,未删除且有效的,没有退回的
        values.put("operationReason",OperationReasonConstants.OP_REASON_BRING_NEW_USER_GIFT);
        values.put("orderId",refundRecord.getOrderId());
        sqlSentence.sqlSentence("SELECT * FROM user_money_unclaimed mu WHERE mu.isDel = 0 AND mu.isValid = 1 AND mu.operationReason = #{m.operationReason}" +
                " AND mu.orderId = #{m.orderId} AND NOT EXISTS(SELECT * FROM refund_user_assets rua WHERE rua.isDel = 0 AND rua.status = 1" +
                " AND rua.amountStatus IN (1,3) AND rua.userMoneyUnclaimedId = mu.id)",values);
        List<UserMoneyUnclaimed> userMoneyUnclaimedList = commonService.selectList(UserMoneyUnclaimedMapper.class,sqlSentence);
        RefundUserAssets refundUserAssets;
        UserMoney userMoney;
        User user;
        BigDecimal amount;
        for (UserMoneyUnclaimed userMoneyUnclaimed:userMoneyUnclaimedList){
            amount = BigDecimal.ZERO;
            //生成记录
            refundUserAssets = new RefundUserAssets();
            refundUserAssets.setType(userMoneyUnclaimed.getOperationReason());
            refundUserAssets.setStatus(RefundUserAssets.STATUS_NORMAL);
            refundUserAssets.setAmountType(userMoneyUnclaimed.getFundType());
            refundUserAssets.setAmount(userMoneyUnclaimed.getOpNumber().negate());
            refundUserAssets.setOperatorId(refundRecord.getOperatorId());
            refundUserAssets.setOperatorNo(refundRecord.getOperatorNo());
            refundUserAssets.setOperatorName(refundRecord.getOperatorName());
            refundUserAssets.setOrderId(refundRecord.getOrderId());
            refundUserAssets.setRefundRecordId(refundRecord.getId());
            refundUserAssets.setUserMoneyUnclaimedId(userMoneyUnclaimed.getId());
            refundUserAssets.setUserId(userMoneyUnclaimed.getUserId());
            user = commonService.selectOneByKey(UserMapper.class,refundUserAssets.getUserId());
            orderNodeBuilder.append("-退款处理老带新,扣减用户:").append(user.getName()).append(",CIQ:").append(user.getCIQ()).append(",");
            orderNodeBuilder.append(RefundUserAssets.amountTypeName(refundUserAssets.getAmountType())).append(refundUserAssets.getAmount());
            if(userMoneyUnclaimed.getStatus() == UserMoneyUnclaimed.STATUS_WAI){
                refundUserAssets.setAmountStatus(RefundUserAssets.AMOUNT_STATUS_NOT_RECEIVE);
                //用户未领取,那么直接作废,作废是逻辑删除
                values.put("id",userMoneyUnclaimed.getId());
                values.put("status",UserMoneyUnclaimed.STATUS_WAI);
                sqlSentence.sqlUpdate("isDel = 1,isValid = 0 WHERE id = #{m.id} AND isDel = 0 AND isValid = 1 AND status = #{m.status}",values);
                if(commonService.updateWhere(UserMoneyUnclaimedMapper.class,sqlSentence) != 1){
                    throw new TipsException("老带新资产记录状态已变化!");
                }
                orderNodeBuilder.append(",用户未领取,作废领取记录");
            }else if(userMoneyUnclaimed.getStatus() == UserMoneyUnclaimed.STATUS_SUCCEED){
                //用户已经领取了,那么要扣掉,但是,要先判断用户是否够,如果不够,那么也生成记录,显示未操作资金
                values.clear();
                values.put("userId",userMoneyUnclaimed.getUserId());
                sqlSentence.sqlSentence("SELECT * FROM user_money WHERE isDel = 0 AND userId = #{m.userId}",values);
                userMoney = commonService.selectOne(UserMoneyMapper.class,sqlSentence);
                if(userMoneyUnclaimed.getFundType() == UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND){
                    //储值金
                    if(userMoney.getStoredValueFund() != null){
                        amount = new BigDecimal(userMoney.getStoredValueFund());
                    }
                }else if(userMoneyUnclaimed.getFundType() == UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND){
                    //增值金
                    if(userMoney.getValueAddedFund() != null){
                        amount = new BigDecimal(userMoney.getValueAddedFund());
                    }
                }else if(userMoneyUnclaimed.getFundType() == UserMoneyUnclaimed.FUND_TYPE_INTEGRAL){
                    //预定金
                    if(userMoney.getDeposit() != null){
                        amount = new BigDecimal(userMoney.getDeposit());
                    }
                }else if(userMoneyUnclaimed.getFundType() == UserMoneyUnclaimed.FUND_TYPE_DEPOSIT){
                    //积分
                    if(userMoney.getIntegral() != null){
                        amount = new BigDecimal(userMoney.getIntegral());
                    }
                }else{
                    throw new TipsException("老带新资产记录金额类型未知!");
                }
                if(amount.compareTo(refundUserAssets.getAmount()) >= 0){
                    //用户资产够减
                    orderNodeBuilder.append(",扣减成功");
                    refundUserAssets.setAmountStatus(RefundUserAssets.AMOUNT_STATUS_NORMAL);
                }else {
                    orderNodeBuilder.append(",余额不足,扣减失败");
                    //用户资产不够减
                    refundUserAssets.setAmountStatus(RefundUserAssets.AMOUNT_STATUS_NOT);
                }
            }else{
                throw new TipsException("老带新资产记录状态错误!");
            }
            if(refundUserAssets.getAmountStatus() == RefundUserAssets.AMOUNT_STATUS_NORMAL
                    || refundUserAssets.getAmountStatus() == RefundUserAssets.AMOUNT_STATUS_NOT_RECEIVE ){
                //保存记录,正常扣减和未领取才去保存这个记录
                commonService.insert(RefundUserAssetsMapper.class,refundUserAssets);
            }
            //用户资金操作
            if(refundUserAssets.getAmountStatus() == RefundUserAssets.AMOUNT_STATUS_NORMAL){
                UserMoneyUtil.setNewUserMoneyUnclaimed(refundUserAssets.getUserId(),refundRecord.getRemarks(),"退款:老带新资金回退",refundUserAssets.getOperatorId()
                        ,refundRecord.getOrderId(),refundRecord.getOperatorAppCode(),refundUserAssets.getId(),refundUserAssets.getAmount(),refundUserAssets.getAmountType()
                        ,OperationReasonConstants.OP_REASON_BRING_NEW_USER_GIFT_RETRUN,commonService,UserMoneyUnclaimed.YES);
            }
        }
    }
    /**老带新的订单退款不通过或者作废,佣金处理*/
    public void oldBlingNewRefundFail(RefundRecord refundRecord,StringBuilder orderNodeBuilder,CommonService commonService) {
        ////订单退款状态是没有退款才退回
        //获取回退记录
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        values.put("orderId",refundRecord.getOrderId());
        sqlSentence.sqlSentence("SELECT * FROM refund_user_assets rua WHERE rua.isDel = 0 AND rua.status = 1 AND rua.amountStatus IN(1,3) AND rua.orderId = #{m.orderId}",values);
        List<RefundUserAssets> refundUserAssetsList = commonService.selectList(RefundUserAssetsMapper.class,sqlSentence);
        User user;
        for(RefundUserAssets refundUserAssets:refundUserAssetsList){
            user = commonService.selectOneByKey(UserMapper.class,refundUserAssets.getUserId());
            orderNodeBuilder.append("-退款处理老带新,回退资金到用户:").append(user.getName()).append(",CIQ:").append(user.getCIQ()).append(",");
            orderNodeBuilder.append(RefundUserAssets.amountTypeName(refundUserAssets.getAmountType())).append(refundUserAssets.getAmount());
            //是否有扣减用户资产
            if(refundUserAssets.getAmountStatus() == RefundUserAssets.AMOUNT_STATUS_NORMAL){
                orderNodeBuilder.append(",增加成功");
                UserMoneyUtil.setNewUserMoneyUnclaimed(refundUserAssets.getUserId(),refundRecord.getRemarks(),"退款作废:老带新资金回退作废",refundUserAssets.getOperatorId()
                        ,refundRecord.getOrderId(),refundRecord.getOperatorAppCode(),refundUserAssets.getId(),refundUserAssets.getAmount().negate(),refundUserAssets.getAmountType()
                        ,OperationReasonConstants.OP_REASON_BRING_NEW_USER_GIFT_RETRU_CANCEL,commonService,UserMoneyUnclaimed.YES);
            }else if(refundUserAssets.getAmountStatus() == RefundUserAssets.AMOUNT_STATUS_NOT_RECEIVE){
                //用户未领取记录,还原
                orderNodeBuilder.append(",用户未领取,作废的领取记录还原");
                values.clear();
                values.put("id",refundUserAssets.getUserMoneyUnclaimedId());
                values.put("status",UserMoneyUnclaimed.STATUS_WAI);
                sqlSentence.sqlUpdate("isDel = 0,isValid = 1 WHERE id = #{m.id} AND isDel = 1 AND isValid = 0 AND status = #{m.status}",values);
                if(commonService.updateWhere(UserMoneyUnclaimedMapper.class,sqlSentence) != 1){
                    throw new TipsException("老带新资产记录状态已变化[作废]!");
                }
            }
            //记录作废
            values.clear();
            values.put("id",refundUserAssets.getId());
            values.put("status",RefundUserAssets.STATUS_CANCEL);
            values.put("oldStatus",RefundUserAssets.STATUS_NORMAL);
            sqlSentence.sqlSentence("status = #{m.status} WHERE id = #{m.id} AND status = #{m.oldStatus}",values);
            if(commonService.updateWhere(RefundUserAssetsMapper.class,sqlSentence) != 1){
                throw new TipsException("老带新资产退款记录状态已变化[作废]!");
            }
        }
    }
        /**
         * 退款-处理活动规则增值金和积分
         * @param commonService 映射对象
         * @param operationId 操作人标识
         * @param operationNme 操作人名称
         * @param sqlSentence 映射对象
         * @param map 映射对象
         * @param refundRecord 退款记录
         * @param ordersTotal 订单
         * @param optType 操作:"0"减 1加
         */
    private static void handActivityRule(CommonService commonService, String operationId, String operationNme, SqlSentence sqlSentence,
                                         Map<String, Object> map, RefundRecord refundRecord, OrdersTotal ordersTotal, OrderInfo orderInfo,int optType) {
        if(orderInfo!=null && StringUtils.noNull(orderInfo.getActivityId())){
@@ -1066,7 +1108,7 @@
                map.put("type", ActivityAction.TYPE_INTEGRAL);
                map.put("type1",ActivityAction.TYPE_VALUEADDEDFUND);
                map.put("type2",ActivityAction.TYPE_COUPON);
                sqlSentence.setSqlSentence("select * from activity_action where activityRuleId=#{m.activityRuleId} and (type=#{m.type} or type=#{m.type1} or type=#{m.type2}) and isDel=0");
                sqlSentence.sqlSentence("select * from activity_action where activityRuleId=#{m.activityRuleId} and (type=#{m.type} or type=#{m.type1} or type=#{m.type2}) and isDel=0",map);
                List<ActivityAction> activityActions = commonService.selectList(ActivityActionMapper.class, sqlSentence);
                if(activityActions!=null && activityActions.size()>0){
                    for (ActivityAction activityAction : activityActions) {
@@ -1085,15 +1127,15 @@
                            map.put("newValidState",optType==0?BaseEntity.NO:BaseEntity.YES);
                            map.put("couponId",activityAction.getCrmCouponId());
                            map.put("commonId",ordersTotal.getId());
                            sqlSentence.setSqlSentence(" validState=#{m.newValidState} where couponId=#{m.couponId} and commonId=#{m.commonId} and validState=#{m.oldValidState} ");
                            sqlSentence.sqlSentence(" validState=#{m.newValidState} where couponId=#{m.couponId} and commonId=#{m.commonId} and validState=#{m.oldValidState} ",map);
                            commonService.updateWhere(CouponNumberMapper.class,sqlSentence);
                        }
                    }
                }
            }
        }
    }
    /**
     * 对接审批
     */
@@ -1116,7 +1158,12 @@
        orderPartRefundDto.setShopName(cancelOrder.getShopName());
        orderPartRefundDto.setRoleId(cancelOrder.getRoleId());
        orderPartRefundDto.setRoleUniqueStr(cancelOrder.getRoleStr());
        orderPartRefundDto.setRemark(cancelOrder.getOperatorName()+"退款,订单号:"+ordersTotal.getOrderNo());
        if(StringUtils.noNull(refundRecord.getRemarks())){
            orderPartRefundDto.setRemark(refundRecord.getRemarks());
        }else{
            orderPartRefundDto.setRemark(cancelOrder.getOperatorName()+"退款,订单号:"+ordersTotal.getOrderNo());
        }
        orderPartRefundDto.setUserId(ordersTotal.getUserId());
        //本次退款金额
@@ -1125,10 +1172,10 @@
        /**本次退款支付方式*/
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> map=new HashMap<>();
        sqlSentence.setM(map);
        map.put("refundRecordId", refundRecord.getId());
        sqlSentence.setSqlSentence("SELECT refundNumberNo AS payMethodNo,refundName AS payMethodName,actualTotal as refundMoney,refundRecordId as payMethodId FROM refund_record_method " +
                " WHERE refundRecordId=#{m.refundRecordId} ");
        sqlSentence.sqlSentence("SELECT refundNumberNo AS payMethodNo,refundName AS payMethodName,actualTotal as refundMoney,refundRecordId as payMethodId FROM refund_record_method " +
                " WHERE refundRecordId=#{m.refundRecordId} ",map);
        List<Map<String, Object>> refundMethodList = commonService.selectListMap(RefundRecordMethodMapper.class,sqlSentence);
        if(refundMethodList!=null && refundMethodList.size()!=0){
            JSONArray refundMethodArray=new JSONArray();
@@ -1138,16 +1185,16 @@
        }
        /**本次退款项目*/
        //退款一级项目
        sqlSentence.setSqlSentence("SELECT type AS projectType, commonId AS projectId, goodsNo AS projectNo, goodsName AS projectName, refundNum AS refundNum  " +
                " FROM refund_record_item WHERE refundRecordId=#{m.refundRecordId} AND refundNum>0");
        sqlSentence.sqlSentence("SELECT type AS projectType, commonId AS projectId, goodsNo AS projectNo, goodsName AS projectName, refundNum AS refundNum  " +
                " FROM refund_record_item WHERE refundRecordId=#{m.refundRecordId} AND refundNum>0",map);
        List<Map<String, Object>> refundRecordItemList = commonService.selectListMap(RefundRecordItemMapper.class,sqlSentence);
        JSONArray refundProjectDtoArray=new JSONArray();
        if(refundRecordItemList!=null && refundRecordItemList.size()!=0){
            refundProjectDtoArray.addAll(refundRecordItemList);
        }
        //退款二级项目
        sqlSentence.setSqlSentence("SELECT type AS projectType, commonId AS projectId, goodsNo AS projectNo, goodsName AS projectName, refundNum AS refundNum " +
                " FROM refund_record_item_source   WHERE refundRecordId=#{m.refundRecordId} AND refundNum>0");
        sqlSentence.sqlSentence("SELECT type AS projectType, commonId AS projectId, goodsNo AS projectNo, goodsName AS projectName, refundNum AS refundNum " +
                " FROM refund_record_item_source   WHERE refundRecordId=#{m.refundRecordId} AND refundNum>0",map);
        List<Map<String, Object>> refundRecordItemSourceList = commonService.selectListMap(RefundRecordItemSourceMapper.class,sqlSentence);
        if(refundRecordItemSourceList!=null && refundRecordItemSourceList.size()!=0){
            refundProjectDtoArray.addAll(refundRecordItemSourceList);
@@ -1199,23 +1246,20 @@
        //记录取消日志
        CancelOrder cancelOrder=new CancelOrder();
        cancelOrder.setOrderId(ordersTotal.getId());
        Employee employee = commonService.selectOneByKey(EmployeeMapper.class,orderRefundDto.getOperatorId());
        if(employee==null){
            cancelOrder.setOperatorId(orderRefundDto.getOperatorId());
            cancelOrder.setOperatorName("找不到对应员工");
        }else {
        if(StringUtils.noNull(orderRefundDto.getOperatorId())){
            Employee employee = commonService.selectOneByKey(EmployeeMapper.class,orderRefundDto.getOperatorId());
            if(employee==null){
                throw new TipsException("操作人标识错误!");
            }
            cancelOrder.setOperatorId(employee.getId());
            cancelOrder.setOperatorName(employee.getCnName());
        }
        EmployeeRole employeeRole=commonService.selectOneByKey(EmployeeRoleMapper.class,orderRefundDto.getRoleId());
        if(employeeRole==null){
            cancelOrder.setRoleId(orderRefundDto.getRoleId());
            cancelOrder.setRoleStr("找不到对应角色");
            cancelOrder.setShopId(employeeRole.getShopId());
            cancelOrder.setShopName("找不到对应的门店");
        }else {
        if(StringUtils.noNull(orderRefundDto.getRoleId())){
            EmployeeRole employeeRole=commonService.selectOneByKey(EmployeeRoleMapper.class,orderRefundDto.getRoleId());
            if(employeeRole==null){
                throw new TipsException("操作角色标识错误!");
            }
            cancelOrder.setRoleId(employeeRole.getRoleTypeId());
            cancelOrder.setRoleStr(employeeRole.getRoleUniqueStr());
            Shop shop=commonService.selectOneByKey(ShopMapper.class,employeeRole.getShopId());
@@ -1226,8 +1270,8 @@
                cancelOrder.setShopId(shop.getId());
                cancelOrder.setShopName(shop.getName());
            }
        }
        cancelOrder.setIsApproval(orderRefundDto.getIsApproval());
        cancelOrder.setIsRefund(orderRefundDto.getIsRefund());
        cancelOrder.setPlatformSource(orderRefundDto.getPlatformSource());
@@ -1239,374 +1283,255 @@
    /**
     * 处理退款方式记录表
     */
    public void handleRefundPayMethod(List<OrderPayMethodDto> refundPayMethod,StringBuilder orderNodeBuilder,OrdersTotal ordersTotal,RefundRecord refundRecord){
        RefundRecordMethod refundRecordMethod=null;
        RefundMethodTransformation refundMethodTransformation=null;
    public List<RefundRecordMethod> handleRefundPayMethod(OrderRefundDto orderRefundDto,StringBuilder orderNodeBuilder,OrdersTotal ordersTotal,RefundRecord refundRecord){
        orderNodeBuilder.append("-记录退款方式:");
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> map=new HashMap<>();
        sqlSentence.setM(map);
        map.put("orderId",ordersTotal.getId());
        sqlSentence.setSqlSentence("SELECT id,numberNo,actualTotal,refundTotal,createTime,isMoneyPay,isExecute,isPay,paymentMethodId FROM consume_pay " +
                " WHERE orderId=#{m.orderId} AND isDel = 0  AND actualTotal>refundTotal ORDER BY createTime ");
        List<ConsumePay> consumePayList = consumePayMapper.selectList(sqlSentence);
        //获取订单可退款方式,就是支付方式总表的信息,已经支付编号求和
        List<PayMethodVo> payMethodVoList = refundMapper.getConsumePayGroupByNumberNo(ordersTotal.getId());
        if(consumePayList==null || consumePayList.size()==0){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该订单的支付记录");
        }
        Map<String, List<ConsumePay>> consumePayMap = consumePayList.stream().collect(Collectors.groupingBy(ConsumePay::getNumberNo));
        //用map装载,后面根据支付编号直接获取便可
        Map<String, PayMethodVo> payMethodVoMap = payMethodVoList.stream().collect(Collectors.toMap(PayMethodVo::getNumberNo,(a) -> a));
        for (OrderPayMethodDto orderPayMethodDto : refundPayMethod) {
            String payMethodNo = orderPayMethodDto.getPayMethodNo();//支付方式编码
            String payMethodName = orderPayMethodDto.getPayMethodName();//支付方式名称
            String refundNumberNo = orderPayMethodDto.getRefundNumberNo();//转换方式编码
        List<RefundRecordMethod> refundRecordMethodList = new ArrayList<>();
            BigDecimal money = orderPayMethodDto.getMoney();//实退金额
            String remarks = orderPayMethodDto.getRemarks();//备注
        PayMethodVo payMethodVo;
        RefundRecordMethod refundRecordMethod;
        for (OrderPayMethodDto orderPayMethodDto : orderRefundDto.getRefundPayMethod()) {
            if(money ==null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"实退金额为空");
            payMethodVo = payMethodVoMap.get(orderPayMethodDto.getPayMethodNo());
            //判断是不是在支付方式里面的
            if (payMethodVo == null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"可退支付方式未找到:"+orderPayMethodDto.getPayMethodName()+"["+orderPayMethodDto.getPayMethodNo()+"]");
            }
            if (!consumePayMap.containsKey(payMethodNo)){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"可退支付方式未找到对应的支付编码"+payMethodNo);
            //判断支付方式
            PaymentMethod payMethod = paymentMethodService.selectNumberNoUncheckUp(orderPayMethodDto.getPayMethodNo());
            if(payMethod == null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到对应的支付:"+orderPayMethodDto.getPayMethodName()+"["+orderPayMethodDto.getPayMethodNo()+"]");
            }
            PaymentMethod refundMethod = paymentMethodService.selectNumberNo(refundNumberNo);
            //判断退款方式
            PaymentMethod refundMethod = paymentMethodService.selectNumberNoUncheckUp(orderPayMethodDto.getRefundNumberNo());
            if(refundMethod==null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该转换方式编码"+payMethodNo);
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到退款方式:"+orderPayMethodDto.getRefundNumberName()+"["+orderPayMethodDto.getRefundNumberNo()+"]");
            }
            if(orderPayMethodDto.getMoney() == null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请填写退款方式["+orderPayMethodDto.getPayMethodName()+"["+orderPayMethodDto.getPayMethodNo()+"]"+"]金额");
            }
            //判断金额
            if(payMethodVo.getSurplusTotal().compareTo(orderPayMethodDto.getMoney()) < 0){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款金额错误:"+orderPayMethodDto.getPayMethodName()+"["+orderPayMethodDto.getPayMethodNo()+"]");
            }
            //新增转换表记录
            refundMethodTransformation=new RefundMethodTransformation();
            BeanUtils.copyProperties(orderPayMethodDto, refundMethodTransformation);
            refundMethodTransformation.setRefundTotal(money);
            refundMethodTransformation.setRefundRecordId(refundRecord.getId());
            refundMethodTransformationMapper.insert(refundMethodTransformation);
            //因为支付方式可能是多次付款,先优先处理先付款的
            BigDecimal surplus;//可退金额
            for (ConsumePay consumePay : consumePayList) {
                if(payMethodNo.equals(consumePay.getNumberNo())){
                    surplus=consumePay.getActualTotal().subtract(consumePay.getRefundTotal()).setScale(2,BigDecimal.ROUND_HALF_UP);
                    //退回客户的支付方式
                    refundRecordMethod=new RefundRecordMethod(payMethodNo,payMethodName,null, ConsumePayConstants.STATUS_ORDER,ordersTotal.getId(),refundRecord.getId(),consumePay.getPaymentMethodId());
                    refundRecordMethod.setConsumePayId(consumePay.getId());
                    refundRecordMethod.setRefundMethodTransId(refundMethodTransformation.getId());
                    refundRecordMethod.setRefundNumberNo(refundNumberNo);
                    refundRecordMethod.setIsMoneyPay(consumePay.getIsMoneyPay());
                    refundRecordMethod.setIsExecute(consumePay.getIsExecute());
                    refundRecordMethod.setIsPay(consumePay.getIsPay());
                    //refundRecordMethod.setRealRefundTotal(money);//实退款总金额 后期需要修改
                    refundRecordMethod.setRemarks(remarks);
                    refundRecordMethod.setRefundName(refundMethod.getName());
                    refundRecordMethod.setRefundMethodId(refundMethod.getId());
                    refundRecordMethod.setIsMoneyPayRefund(refundMethod.getIsMoneyPay());
                    refundRecordMethod.setIsExecuteRefund(refundMethod.getIsExecute());
                    refundRecordMethod.setIsPayRefund(BaseEntity.YES);
                    if(money.compareTo(surplus)==1){
                        refundRecordMethod.setActualTotal(surplus);
                    }else {
                        refundRecordMethod.setActualTotal(money);
                    }
                    money=money.subtract(refundRecordMethod.getActualTotal()).setScale(2,BigDecimal.ROUND_HALF_UP);
                    refundRecordMethodService.insert(refundRecordMethod);
                    if(money.compareTo(BigDecimal.ZERO)==0){
                        break;
                    }
                }
            }
            ////退款方式数据填充
            refundRecordMethod = new RefundRecordMethod();
            refundRecordMethod.setType(refundRecord.getSourceType());
            refundRecordMethod.setActualTotal(orderPayMethodDto.getMoney());
            //支付方式信息
            refundRecordMethod.setNumberNo(payMethod.getNumberNo());
            refundRecordMethod.setPaymentMethodId(payMethod.getId());
            refundRecordMethod.setName(payMethod.getName());
            refundRecordMethod.setIsMoneyPay(payMethodVo.getIsMoneyPay());
            refundRecordMethod.setIsExecute(payMethodVo.getIsExecute());
            //退款方式信息
            refundRecordMethod.setRefundNumberNo(refundMethod.getNumberNo());
            refundRecordMethod.setRefundName(refundMethod.getName());
            refundRecordMethod.setRefundMethodId(refundMethod.getId());
            if(money.compareTo(BigDecimal.ZERO)!=0){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款方式金额大于可退金额");
            }
            refundRecordMethod.setIsMoneyPayRefund(refundMethod.getIsMoneyPay());
            refundRecordMethod.setIsExecuteRefund(refundMethod.getIsExecute());
            refundRecordMethod.setRefundRecordId(refundRecord.getId());
            refundRecordMethod.setOrderId(ordersTotal.getId());
            refundRecordMethod.setRemarks(orderPayMethodDto.getRemarks());
            refundRecordMethodService.insert(refundRecordMethod);
            refundRecordMethodList.add(refundRecordMethod);
        }
        return refundRecordMethodList;
    }
    /**
     * 退款记录子项
     */
    public void handleRefundRecordItem(List<OrderItemRefundDto> refundList,StringBuilder orderNodeBuilder,OrdersTotal ordersTotal,RefundRecord refundRecord,BigDecimal percentage){
    public void insertRefundRecordItem(OrderRefundDto orderRefundDto,StringBuilder orderNodeBuilder,RefundRecord refundRecord){
        //本次退款所有项目详情,用于解散分摊金额
        List<RefundRecordItem> refundRecordItems=new ArrayList<>();
        RefundRecordItem refundRecordItem=null;
        RefundRecordItemSource refundRecordItemSource=null;
        List<OrderItemSourceRefundDto> orderItemSourceRefundDtos=null;
        String orderItemId =null;
        Integer refundNum =null;
        Integer surplusNum =null;
        Integer totalNum =null;
        BigDecimal refundMoney =null;
        String refundReason =null;
        String type =null;
        OrderItem orderItem =null;
        OrderItemSon orderItemSon =null;
        UserProjectItem userProjectItem =null;
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> map=new HashMap<>();
        sqlSentence.setM(map);
        sqlSentence.setSqlSentence(" select * from user_project_item where commonId=#{m.commonId}  and isDel=0 AND userId=#{m.userId} ");
        map.put("userId", ordersTotal.getUserId());
        orderNodeBuilder.append("-记录退款详情");
        //计算金额占比,客户选择退款总金额和可退总金额占比
        BigDecimal percentage;
        if(orderRefundDto.getTotalAmount().compareTo(BigDecimal.ZERO) < 1){
            percentage = BigDecimal.ZERO;
        }else {
            percentage = orderRefundDto.getRefundTotal().divide(orderRefundDto.getTotalAmount(),15,RoundingMode.HALF_UP);
        }
        //计算本次实际退款的总金额
        BigDecimal refundTotal=refundRecord.getRefundTotal();
        //总退款金额,用于后面计算分摊金额
        BigDecimal refundTotal = refundRecord.getRefundTotal();
        //后面设计分配金额问题,要进行升序排序,避免到最后不够
        List<OrderItemRefundDto> refundList = orderRefundDto.getRefundList();
        refundList = refundList.stream().sorted(Comparator.comparing(OrderItemRefundDto::getApproveRefundTotal)).collect(Collectors.toList());
        /////引用参数
        RefundRecordItem refundRecordItem;
        OrderItemRefundDto orderItemRefundDto;
        for (int j = refundList.size()-1; j >=0; j--) {
            orderItemRefundDto=refundList.get(j);
            orderItemId = orderItemRefundDto.getOrderItemId();//orderItemId
            refundNum = orderItemRefundDto.getRefundNum();//实退数
        OrderItem orderItem;
        for(int i = 0;i < refundList.size();i++){
            orderItemRefundDto = refundList.get(i);
            if(j==0){
                refundMoney=refundTotal;
            }else {
                refundMoney = orderItemRefundDto.getRefundMoney().multiply(percentage).setScale(2,BigDecimal.ROUND_HALF_UP);//实退金额
            orderItem = orderItemMapper.selectOneByKey(orderItemRefundDto.getOrderItemId());
            if(orderItem == null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"子订单标识错误!");
            }
            refundTotal=refundTotal.subtract(refundMoney).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为空");
            //////数据填充
            refundRecordItem = new RefundRecordItem();
            refundRecordItem.setType(orderItem.getType());
            refundRecordItem.setGoodsNo(orderItem.getGoodsNo());
            refundRecordItem.setGoodsName(orderItem.getGoodsName());
            refundRecordItem.setSpecs(orderItem.getSpecs());
            refundRecordItem.setCommonId(orderItem.getCommonId());
            if(PartialRefundUtil.CONTAIN_SON_TYPE_SET.contains(orderItem.getType())){
                refundRecordItem.setRefundNum(0);
            }else{
                refundRecordItem.setRefundNum(orderItemRefundDto.getRefundNum());
            }
            if(refundNum==null ){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请正确填写退款实退数");
            refundRecordItem.setRefundRecordId(refundRecord.getId());
            refundRecordItem.setOrderItemId(orderItem.getId());
            refundRecordItem.setOccupyRefundTotal(orderItemRefundDto.getApproveRefundTotal());
            ////退款备注
            if(StringUtils.isEmpty(orderItemRefundDto.getRemarks())){
                orderItemRefundDto.setRemarks(refundRecord.getRemarks());
            }
            if(refundMoney==null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请正确填写退款实退金额");
            if(StringUtils.isEmpty(orderItemRefundDto.getRefundReason())){
                orderItemRefundDto.setRefundReason(orderItemRefundDto.getRefundReason());
            }
            if(orderItem==null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该子项订单信息");
            }
            //保存退款详情订单
            refundRecordItem=new RefundRecordItem(orderItem.getGoodsNo(),orderItem.getGoodsName(),orderItem.getCommonId(),refundNum,refundMoney,refundReason,refundRecord.getId(),orderItem.getSpecs(),orderItem.getId());
            refundRecordItem.setType(type);
            refundRecordItem.setRealRefundNum(0);//实退次数 后期需要修改
//                refundRecordItem.setRealRefundTotal(refundMoney);//实退款总金额 后期需要修改
            refundRecordItem.setRemarks(orderItemRefundDto.getRemarks());
            refundRecordItem.setRefundReason(orderItemRefundDto.getRefundReason());
            if(refundNum>=1){
                if(OrderItemConstants.TYPE_PROJECT.equals(type)){
                    //项目类型处理方式
                    map.put("commonId",orderItemId);
                    userProjectItem= userProjectItemMapper.selectOne(sqlSentence);
                    if(userProjectItem==null){
                        throw new PlatTipsException(PlatformCode.ERROR_TIPS,"项目"+orderItemRefundDto.getGoodsName()+"未找到可执行项目记录");
                    }
                    surplusNum=userProjectItem.getNotUsedNum();
                    totalNum=orderItem.getUsedTotal();
                }else {
                    //非项目类型处理方式
                    surplusNum=orderItem.getBuyNum()-orderItem.getHasReNum();
                    totalNum=orderItem.getBuyNum();
                }
                //校验可退数量
                if(surplusNum<refundNum){
                    throw new PlatTipsException(PlatformCode.ERROR_TIPS,"项目"+orderItemRefundDto.getGoodsName()+"可退数量不足");
                }
                refundRecordItem.setHeadPrice(orderItem.getActualTotal().divide(new BigDecimal(totalNum),2,BigDecimal.ROUND_HALF_UP));
                if (surplusNum==refundNum){
                    refundRecordItem.setTailPrice(orderItem.getActualTotal().subtract(orderItem.getActualTotal().divide(new BigDecimal(totalNum),2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(totalNum-1)).setScale(2,BigDecimal.ROUND_HALF_UP)).setScale(2,BigDecimal.ROUND_HALF_UP));
                }
            //////////分配退款金额
            if(i == refundList.size()-1){
                refundRecordItem.setRefundMoney(refundTotal);
            }else{
                refundRecordItem.setRefundMoney(orderItemRefundDto.getApproveRefundTotal().multiply(percentage).setScale(2,RoundingMode.UP));
            }
            if(refundRecordItem.getRefundMoney().compareTo(orderItemRefundDto.getApproveRefundTotal()) > 0){
                refundRecordItem.setRefundMoney(orderItemRefundDto.getApproveRefundTotal());
            }
            //判断与未分配剩余金额
            if(refundRecordItem.getRefundMoney().compareTo(refundTotal) > 0){
                refundRecordItem.setRefundMoney(refundTotal);
            }
            refundRecordItem.setRealRefundNum(refundRecordItem.getRefundNum());
            refundRecordItem.setRealRefundTotal(refundRecordItem.getRefundMoney());
            refundRecordItemService.insert(refundRecordItem);
            //处理每个项目每个支付方式的分摊金额
            if(refundNum>=1) {
//                    handleRefundRecordItemMethod(refundRecord, refundRecordItem, OrderSourceConstans.TYPE_PROJECT);
                refundRecordItem.setType(OrderSourceConstans.TYPE_PROJECT);
                refundRecordItems.add(refundRecordItem);
            //减去已经分配的退款金额
            refundTotal = refundTotal.subtract(refundRecordItem.getRefundMoney()).setScale(2,RoundingMode.HALF_UP);
            if(PartialRefundUtil.CONTAIN_SON_TYPE_SET.contains(orderItem.getType())){
                //子单子项处理
                insertRefundRecordItemTwo(refundRecordItem,refundRecord,orderItemRefundDto.getOrderItemSourceRefundDtos());
            }
            if(orderItemSourceRefundDtos == null || orderItemSourceRefundDtos.size()<1){
                continue;
            }
            OrderItemSourceRefundDto orderItemSourceRefundDto;
            //一级退款项目总分摊金额
            BigDecimal orderItemRefundTotal=refundRecordItem.getRefundMoney();
            for (int i = orderItemSourceRefundDtos.size()-1; i >=0; i--) {
                orderItemSourceRefundDto=orderItemSourceRefundDtos.get(i);
                orderItemId = orderItemSourceRefundDto.getOrderItemSonId();//orderItemId
                refundNum = orderItemSourceRefundDto.getRefundNum();//实退数
                if(i==0){
                    refundMoney=orderItemRefundTotal;
                }else {
                    refundMoney = orderItemSourceRefundDto.getRefundMoney().multiply(percentage).setScale(2,BigDecimal.ROUND_HALF_UP);//实退金额
                }
                orderItemRefundTotal=orderItemRefundTotal.subtract(refundMoney).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,"未找到该子项订单信息");
                }
                //保存退款详情订单
                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);//实退款总金额 后期需要修改
                if(refundNum>=1){
                    if(OrderItemConstants.TYPE_PROJECT.equals(type)){
                        //项目类型处理方式
                        map.put("commonId",orderItemId);
                        userProjectItem= userProjectItemMapper.selectOne(sqlSentence);
                        if(userProjectItem==null){
                            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"项目"+orderItemSourceRefundDto.getGoodsName()+"未找到可执行项目记录");
                        }
                        surplusNum=userProjectItem.getNotUsedNum();
                        totalNum=orderItemSon.getUsedTotal();
                    }else {
                        //非项目类型处理方式
                        surplusNum=orderItemSon.getBuyNum()-orderItemSon.getHasReNum();
                        totalNum=orderItemSon.getBuyNum();
                    }
                    //校验可退数量
                    if(surplusNum<refundNum){
                        throw new PlatTipsException(PlatformCode.ERROR_TIPS,"项目"+orderItemSourceRefundDto.getGoodsName()+"可退数量不足");
                    }
                    refundRecordItemSource.setHeadPrice(orderItem.getActualTotal().divide(new BigDecimal(totalNum),2,BigDecimal.ROUND_HALF_UP));
                    if (surplusNum==refundNum){
                        refundRecordItemSource.setTailPrice(orderItem.getActualTotal().subtract(orderItem.getActualTotal().divide(new BigDecimal(totalNum),2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(totalNum-1)).setScale(2,BigDecimal.ROUND_HALF_UP)).setScale(2,BigDecimal.ROUND_HALF_UP));
                    }
                }
                refundRecordItemSourceMapper.insert(refundRecordItemSource);
                //处理每个项目每个支付方式的分摊金额
                if(refundNum>=1) {
                    RefundRecordItem refundRecordItemCopy=new RefundRecordItem();
                    BeanUtils.copyProperties(refundRecordItemSource, refundRecordItemCopy);
//                        handleRefundRecordItemMethod(refundRecord, refundRecordItemCopy, OrderSourceConstans.TYPE_RETAIL);
                    refundRecordItemCopy.setType(OrderSourceConstans.TYPE_RETAIL);
                    refundRecordItemCopy.setOrderItemId(refundRecordItemSource.getOrderItemSonId());
                    refundRecordItems.add(refundRecordItemCopy);
                }
            }
        }
        //处理每个项目每个支付方式的分摊金额
        handleRefundRecordItemMethod(refundRecord, refundRecordItems);
        if(refundTotal.compareTo(BigDecimal.ZERO) > 0){
            throw new TipsException("退款金额分配错误[01]!");
        }
    }
    /**
     * 处理每个项目每个支付方式的分摊金额
     */
    public void handleRefundRecordItemMethod(RefundRecord refundRecord,List<RefundRecordItem> refundRecordItems){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> map=new HashMap<>();
        sqlSentence.setM(map);
        //计算本次实际退款的总金额
        BigDecimal refundTotal=refundRecord.getRefundTotal();
        if(refundTotal.compareTo(BigDecimal.ZERO)<1){
    /**二级子单处理*/
    public void insertRefundRecordItemTwo(RefundRecordItem recordItem,RefundRecord refundRecord,List<OrderItemSourceRefundDto> orderItemSourceRefundDtos){
        if(orderItemSourceRefundDtos == null || orderItemSourceRefundDtos.size() == 0){
            return;
        }
        //查询退款金额详情,遍历关联到项目分摊金额
        map.put("refundRecordId",refundRecord.getId());
        sqlSentence.setSqlSentence(" SELECT id,numberNo,refundNumberNo,name,paymentMethodId,actualTotal,actualTotal as realRefundTotal,isMoneyPay,isExecute,isPay,type,orderId" +
                " FROM refund_record_method  where refundRecordId=#{m.refundRecordId} ");
        List<RefundRecordMethod> refundRecordMethods=refundRecordMethodService.selectList(sqlSentence);
        //本条退款记录详情的退款金额
        BigDecimal refundMoney;
        //换算率
        //计算占比
        BigDecimal percentage;
        //分摊金额
        BigDecimal total;
        RefundRecordItem refundRecordItem;
        RefundRecordItemMethod refundRecordItemMethod;
        RefundRecordMethod refundRecordMethod;
        for (int j = refundRecordItems.size()-1; j >=0; j--) {
            refundRecordItem=refundRecordItems.get(j);
            refundMoney=refundRecordItem.getRefundMoney();
            percentage=refundMoney.divide(refundTotal,10,BigDecimal.ROUND_HALF_UP);
            for (int i = refundRecordMethods.size()-1; i >=0; i--) {
                refundRecordMethod=refundRecordMethods.get(i);
                refundRecordItemMethod=new RefundRecordItemMethod();
                refundRecordItemMethod.setNumberNo(refundRecordMethod.getNumberNo());
                refundRecordItemMethod.setRefundNumberNo(refundRecordMethod.getRefundNumberNo());
                refundRecordItemMethod.setName(refundRecordMethod.getName());
                refundRecordItemMethod.setPaymentMethodId(refundRecordMethod.getPaymentMethodId());
                refundRecordItemMethod.setHeadPrice(refundRecordItem.getHeadPrice());
                refundRecordItemMethod.setTailPrice(refundRecordItem.getTailPrice());
                //计算出支付方式本次分摊下来的资金
                if(j==0){
                    if(i==0){
                        refundRecordItemMethod.setActualTotal(refundRecordMethod.getRealRefundTotal());
                    }else {
                        refundRecordItemMethod.setActualTotal(refundRecordMethod.getActualTotal().multiply(percentage).setScale(2,BigDecimal.ROUND_HALF_UP));
                    }
                }else {
                    if(i==0){
                        refundRecordItemMethod.setActualTotal(refundMoney);
                    }else {
                        refundRecordItemMethod.setActualTotal(refundRecordMethod.getActualTotal().multiply(percentage).setScale(2,BigDecimal.ROUND_HALF_UP));
                    }
                }
                refundRecordMethod.setRealRefundTotal(refundRecordMethod.getRealRefundTotal().subtract(refundRecordItemMethod.getActualTotal()).setScale(2,BigDecimal.ROUND_HALF_UP));
                refundMoney=refundMoney.subtract(refundRecordItemMethod.getActualTotal()).setScale(2,BigDecimal.ROUND_HALF_UP);
                refundRecordItemMethod.setRealRefundTotal(refundRecordItemMethod.getActualTotal());
                refundRecordItemMethod.setIsMoneyPay(refundRecordMethod.getIsMoneyPay());
                refundRecordItemMethod.setIsExecute(refundRecordMethod.getIsExecute());
                refundRecordItemMethod.setIsPay(refundRecordMethod.getIsPay());
                refundRecordItemMethod.setCommonType(refundRecordItem.getType());
                refundRecordItemMethod.setCommonId(refundRecordItem.getOrderItemId());
                refundRecordItemMethod.setOrderId(refundRecord.getOrderId());
                refundRecordItemMethod.setRefundRecordId(refundRecord.getId());
                refundRecordItemMethod.setRefundRecordItemId(refundRecordItem.getId());
                refundRecordItemMethod.setRefundMethodId(refundRecordMethod.getId());
                refundRecordItemMethodMapper.insert(refundRecordItemMethod);
            }
        if(recordItem.getOccupyRefundTotal().compareTo(BigDecimal.ZERO) > 0){
            percentage = recordItem.getRefundMoney().divide(recordItem.getOccupyRefundTotal(),15,RoundingMode.HALF_UP);
        }else{
            percentage = BigDecimal.ZERO;
        }
        //进行升序排序,避免后面不够
        orderItemSourceRefundDtos = orderItemSourceRefundDtos.stream().sorted(Comparator.comparing(OrderItemSourceRefundDto::getApproveRefundTotal)).collect(Collectors.toList());
        //退款总金额
        BigDecimal refundTotal = recordItem.getRefundMoney();
        OrderItemSon orderItemSon;
        OrderItemSourceRefundDto orderItemSourceRefundDto;
        RefundRecordItemSource refundRecordItemSource;
        for(int i = 0;i<orderItemSourceRefundDtos.size();i++){
            orderItemSourceRefundDto = orderItemSourceRefundDtos.get(i);
            orderItemSon = orderItemSonMapper.selectOneByKey(orderItemSourceRefundDto.getOrderItemSonId());
            if(orderItemSon == null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"子订单标识错误[024]!");
            }
            //////数据填充
            refundRecordItemSource = new RefundRecordItemSource();
            refundRecordItemSource.setType(orderItemSon.getType());
            refundRecordItemSource.setGoodsNo(orderItemSon.getGoodsNo());
            refundRecordItemSource.setGoodsName(orderItemSon.getGoodsName());
            refundRecordItemSource.setSpecs(orderItemSon.getSpecs());
            refundRecordItemSource.setCommonId(orderItemSon.getGoodsId());
            refundRecordItemSource.setRefundNum(orderItemSourceRefundDto.getRefundNum());
            refundRecordItemSource.setRefundRecordId(refundRecord.getId());
            refundRecordItemSource.setOrderItemSonId(orderItemSon.getId());
            refundRecordItemSource.setOccupyRefundTotal(orderItemSourceRefundDto.getApproveRefundTotal());
            refundRecordItemSource.setRefundRecordItemId(recordItem.getId());
            ////退款备注
            if(StringUtils.isEmpty(orderItemSourceRefundDto.getRemarks())){
                orderItemSourceRefundDto.setRemarks(refundRecord.getRemarks());
            }
            if(StringUtils.isEmpty(orderItemSourceRefundDto.getRefundReason())){
                orderItemSourceRefundDto.setRefundReason(refundRecord.getRefundReason());
            }
            refundRecordItemSource.setRemarks(orderItemSourceRefundDto.getRemarks());
            refundRecordItemSource.setRefundReason(orderItemSourceRefundDto.getRefundReason());
            //计算退款金额
            if(i == orderItemSourceRefundDtos.size()-1){
                refundRecordItemSource.setRefundMoney(refundTotal);
            }else{
                refundRecordItemSource.setRefundMoney(orderItemSourceRefundDto.getApproveRefundTotal().multiply(percentage).setScale(2,RoundingMode.UP));
            }
            if(refundRecordItemSource.getRefundMoney().compareTo(orderItemSourceRefundDto.getApproveRefundTotal()) > 0){
                refundRecordItemSource.setRefundMoney(orderItemSourceRefundDto.getApproveRefundTotal());
            }
            if(refundRecordItemSource.getRefundMoney().compareTo(refundTotal) > 0){
                refundRecordItemSource.setRefundMoney(refundTotal);
            }
            refundRecordItemSource.setRealRefundNum(refundRecordItemSource.getRefundNum());
            refundRecordItemSource.setRealRefundTotal(refundRecordItemSource.getRefundMoney());
            refundRecordItemSourceMapper.insert(refundRecordItemSource);
            //减去已经分配退款金额
            refundTotal = refundTotal.subtract(refundRecordItemSource.getRefundMoney()).setScale(2,RoundingMode.HALF_UP);
            System.out.println("refundRecordItemSource.getRefundMoney():"+refundRecordItemSource.getRefundMoney());
        }
        System.out.println("refundTotal:"+refundTotal);
        if(refundTotal.compareTo(BigDecimal.ZERO) > 0){
            throw new TipsException("退款金额分配错误[02]!");
        }
    }
    /**
     * 记录退款记录优惠卷
     */
    public void handleOrderCouponRefunDto(List<OrderCouponRefunDto> couponList,StringBuilder orderNodeBuilder,OrdersTotal ordersTotal,RefundRecord refundRecord){
        CouponOrderDiscountLog couponOrderDiscountLog=null;
        RefundRecordCoupon refundRecordCoupon=null;
        CouponOrderDiscountLog couponOrderDiscountLog;
        RefundRecordCoupon refundRecordCoupon;
        orderNodeBuilder.append("-处理退款优惠卷");
        for (OrderCouponRefunDto orderCouponRefunDto : couponList) {
            couponOrderDiscountLog=couponOrderDiscountLogMapper.selectOneByKey(orderCouponRefunDto.getOrderCouponId());
            if(couponOrderDiscountLog==null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单优惠卷id不正确");
            if( couponOrderDiscountLog == null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单优惠卷标识不正确");
            }
            refundRecordCoupon=new  RefundRecordCoupon(ordersTotal.getId(),refundRecord.getId(),ordersTotal.getUserId(),1,1,couponOrderDiscountLog.getId(),
                    couponOrderDiscountLog.getCouponId(),couponOrderDiscountLog.getTitle(),couponOrderDiscountLog.getCouponNumberId());
@@ -1616,23 +1541,27 @@
    /**
    /**真正退款,是对钱和状态的回退到账开始操作
     * 进行退款操作,退款用户项目,商品,促销,卡项,卡包
     */
    public Map<String,Object> handleRefundOrder(Map<String, String> operator,RefundRecord refundRecord,OrdersTotal ordersTotal,OrdersNodeLog ordersNodeLog,StringBuilder orderNodeBuilder,CancelOrder cancelOrder ){
    public Map<String,Object> handleRefundOrder(Map<String, String> operator,RefundRecord refundRecord,OrdersTotal ordersTotal
            ,OrdersNodeLog ordersNodeLog,StringBuilder orderNodeBuilder,CancelOrder cancelOrder ){
        String operationId=operator.get("operatorId");
        String operatorName=operator.get("operatorName");
        //处理主业务
        orderNodeBuilder.append("-处理退款支付方式,用户待执行项目");
        ordersTotal=  PartialRefundUtil.refundProcess(commonService, operationId, operatorName, refundRecord.getId(), customParameter,ordersTotal.getAppIdCode());
        //处理主业务
        ordersTotal =  PartialRefundUtil.refundProcess(commonService, operationId, operatorName, refundRecord.getId());
        //返回数据
        Map<String,Object> data=new HashMap<>();
        data.put("refundRecordId",refundRecord.getId());
        data.put("refundRecordCode",refundRecord.getCode());
        //处理次业务,报错不影响主业务
        refundRecord=refundRecordService.selectOneByKey(refundRecord.getId());
        refundRecord = refundRecordService.selectOneByKey(refundRecord.getId());
        /**
         * 用户升降级(调用航爷) 金额=用户支付的实付现金金额(不包含储值金)
         */
@@ -1641,61 +1570,23 @@
        sqlSentence.setM(map);
        //退款成功
        map.put("refundRecordId", refundRecord.getId());
        sqlSentence.setSqlSentence("SELECT CAST(COALESCE(sum(rrm.realRefundTotal),0) as  DECIMAL(11,2))  as total FROM refund_record_method rrm LEFT JOIN payment_method pm ON pm.numberNo=rrm.refundNumberNo " +
                "WHERE rrm.refundRecordId=#{m.refundRecordId} and pm.isMoneyPay=1");
        sqlSentence.sqlSentence("SELECT CAST(COALESCE(sum(rrm.realRefundTotal),0) as  DECIMAL(11,2))  as total FROM refund_record_method rrm LEFT JOIN payment_method pm ON pm.numberNo=rrm.refundNumberNo " +
                "WHERE rrm.refundRecordId=#{m.refundRecordId} and pm.isMoneyPay=1",map);
        Map<String,Object> totalMap =commonService.selectOneMap(RefundRecordMethodMapper.class,sqlSentence);
        BigDecimal total=new BigDecimal(String.valueOf(totalMap.get("total"))) ;
        if(BigDecimal.ZERO.compareTo(total) < 0){
            try {
                orderNodeBuilder.append("-开始处理用户升降级,金额:"+total.negate());
                orderNodeBuilder.append("-开始处理用户升降级,金额:").append(total.negate());
                UserLevelUtil.refund(ordersTotal.getUserId(),ordersTotal.getId(),total.negate(),ordersTotal.getAppIdCode());
                orderNodeBuilder.append("-处理用户升降级成功");
            }catch (Exception e){
                String snapshot="处理用户升降级失败";
                orderNodeBuilder.append("-处理用户升降级失败,异常原因:"+e.getMessage());
                orderNodeBuilder.append("-处理用户升降级失败,异常原因:").append(e.getMessage());
                logger.error("处理用户升降级失败:" + e.getMessage());
                //发送企业微信通知给工作人员
                SendNoticeUtil.failOrderSendNotice(ordersTotal,e.getMessage(),snapshot,commonService,customParameter);
            }
        }
        //判断本次是否需要退回储值金,有的话需要开单同步到领建
       /* map.put("refundNumberNo", PayMethodTypeConstants.PAY_STORED);
        sqlSentence.setSqlSentence(" SELECT CAST(COALESCE(sum(realRefundTotal),0) as  DECIMAL(11,2)) as total FROM refund_record_method   " +
                "WHERE refundRecordId=#{m.refundRecordId} and refundNumberNo=#{m.refundNumberNo}");
        Map<String,Object> moneyMap =commonService.selectOneMap(RefundRecordMethodMapper.class,sqlSentence);
        BigDecimal money=new BigDecimal(String.valueOf(moneyMap.get("total"))) ;
        if(BigDecimal.ZERO.compareTo(money) < 0  && !StringUtils.isEmpty(ordersTotal.getHisOrderId())){
            try {
                orderNodeBuilder.append("-开始处理增加储值金开充值单到领建,金额:"+money);
                orderRefundNewService.handStroedValue(ordersTotal,operationId,money);
                orderNodeBuilder.append("-处理增加储值金成功");
            }catch (Exception e){
                orderNodeBuilder.append("-处理增加储值金失败:"+e.getMessage());
            }
        }*/
        //判断订单是否是充值订单,如果是的话,需要同步储值金到领建去
       /* if(OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())  && !StringUtils.isEmpty(ordersTotal.getHisOrderId())){
            try {
                orderNodeBuilder.append("-开始处理扣减储值金开充值单到领建,金额:"+ordersTotal.getTotal());
                orderRefundNewService.deductionsStroedValue(ordersTotal,operationId,ordersTotal.getTotal());
                orderNodeBuilder.append("-处理扣减储值金成功");
            }catch (Exception e){
                orderNodeBuilder.append("-处理扣减储值金失败:"+e.getMessage());
            }
        }*/
        //同步领建,处理订单状态,未执行项目,卡包
       /* if(!StringUtils.isEmpty(ordersTotal.getHisOrderId())){
            try {
                orderNodeBuilder.append("-开始处理his数据同步");
                OrderCancelUtil.hisPartOrder(commonService,syncOrderService,syncExecutionRecordService,ordersTotal,syncUserCardService,customParameter,orderNodeBuilder, refundRecord);
                orderNodeBuilder.append("-结束处理his数据同步");
            }catch (Exception e){
                orderNodeBuilder.append("-处理his数据同步失败,异常原因:"+e.getMessage());
                logger.error("处理his数据同步失败:" + e.getMessage());
            }
        }*/
        try{
            orderNodeBuilder.append("-开始记录收入确认表");
            PerformanceInfoTool.handRefundPerformanceInfo(commonService,refundRecord);
@@ -1707,9 +1598,10 @@
            PerformanceInfoTool.addErrorLog(commonService, refundRecord.getId(), PerformanceInfo.ASSOCIATION_TYPE_GOODS_REFUND, e.getMessage());
        }
        if(CancelOrderDto.HIS_ASSISTANT==cancelOrder.getPlatformSource()){
        if(CancelOrderDto.HIS_ASSISTANT.equals(cancelOrder.getPlatformSource())){
            //如果是his自动处理退款单原路退回
            hisAssistantNoteList(ordersTotal,refundRecord.getId(),operationId,orderNodeBuilder);
            //CRM的操作也在这里
            hisAssistantNoteList(ordersTotal,refundRecord,operationId,orderNodeBuilder);
        }
        //记录操作日志
@@ -1730,21 +1622,50 @@
    /**
     * his助手原路退回退款单
     */
    public void hisAssistantNoteList(OrdersTotal ordersTotal,String refundRecordId,String operationId,StringBuilder orderNodeBuilder){
    public void hisAssistantNoteList(OrdersTotal ordersTotal,RefundRecord refundRecord,String operationId,StringBuilder orderNodeBuilder){
        OrderRefundNoteListDto orderRefundNoteListDto=new OrderRefundNoteListDto();
        orderRefundNoteListDto.setOperationId(operationId);
        orderRefundNoteListDto.setOrderId(ordersTotal.getId());
        //统一在crm取消订单
        // 没有现金支付, 如果是crm的订单则调用crm取消订单的方法
    /*    if("crm".equals(ordersTotal.getAppIdCode()) && OrderTotalConstants.STATUS_CANCEL == ordersTotal.getStatus()){
            //通知crm
            if(OrderTotalConstants.STATUS_REFUND_FINSH==ordersTotal.getRefundStatus()){
                //全部退款完成
                //通知crm订单取消成功了
                Map<String, String> operator = ApiOrderUtil.getOperator(commonService, orderRefundNoteListDto.getOperationId());//获取操作人信息
                String operatorId= operator.get("operatorId");
                String operatorName=  operator.get("operatorName");
                OrderStatusDto orderStatusDto=new OrderStatusDto();
                orderStatusDto.setId(ordersTotal.getId());
                orderStatusDto.setOpName(operatorName);
                orderStatusDto.setOpId(operatorId);
                orderStatusDto.setCancelInfo("phis取消退款");
                orderStatusDto.setOpDetail("phis取消退款");
                try {
                    logger.info("通知CRM订单号:{}退款成功请求参数:{}",ordersTotal.getOrderNo(),JSONObject.toJSONString(orderStatusDto));
                    Result result=  fOderService.cancelOrder(orderStatusDto);
                    logger.info("通知CRM订单号:{}退款成功返回参数:{}",ordersTotal.getOrderNo(),JSONObject.toJSONString(result));
                    orderNodeBuilder.append("通知crm退款状态返回成功");
                }catch (Exception e){
                    logger.info("通知crm订单号:{}退款成功错误:{}",ordersTotal.getOrderNo(),e.getMessage());
                    orderNodeBuilder.append("通知crm退款状态返回失败");
                }
            }
        }*/
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> map=new HashMap<>();
        sqlSentence.setM(map);
        map.put("orderId",ordersTotal.getId());
        map.put("refundRecordId",refundRecordId);
        sqlSentence.setSqlSentence("SELECT * FROM refund_note WHERE orderId=#{m.orderId} and  refundRecordId=#{m.refundRecordId} and isDel=0 ");
        map.put("refundRecordId",refundRecord.getId());
        sqlSentence.sqlSentence("SELECT * FROM refund_note WHERE orderId=#{m.orderId} and  refundRecordId=#{m.refundRecordId} and isDel=0 ",map);
        List<RefundNote> refundNotes=refundNoteMapper.selectList(sqlSentence);
        if(refundNotes==null || refundNotes.size()==0){
            return;
        }
        OrderRefundNoteListDto orderRefundNoteListDto=new OrderRefundNoteListDto();
        orderRefundNoteListDto.setOperationId(operationId);
        orderRefundNoteListDto.setOrderId(ordersTotal.getId());
        List<OrderRefundNoteDto> orderRefundNoteDtos=new ArrayList<>();
        OrderRefundNoteDto orderRefundNoteDto;
@@ -1758,10 +1679,12 @@
        }
        orderRefundNoteListDto.setOrderRefundNoteDtos(orderRefundNoteDtos);
        noteConfirm(orderRefundNoteListDto, ordersTotal,orderNodeBuilder);
        noteConfirm(orderRefundNoteListDto, ordersTotal,refundRecord,orderNodeBuilder);
    }
    public PlatformResult noteConfirm(OrderRefundNoteListDto orderRefundNoteListDto,OrdersTotal ordersTotal,StringBuilder orderNodeBuilder) {
        logger.info("退款单确认打印参数:{}",JSON.toJSONString(orderRefundNoteListDto));
    public PlatformResult noteConfirm(OrderRefundNoteListDto orderRefundNoteListDto,OrdersTotal ordersTotal,RefundRecord refundRecord
            ,StringBuilder orderNodeBuilder) {
        logger.info("退款单确认打印参数CRM:{}",JSON.toJSONString(orderRefundNoteListDto));
        Map<String, String> operator = ApiOrderUtil.getOperator(commonService, orderRefundNoteListDto.getOperationId());//获取操作人信息
        String operatorId= operator.get("operatorId");
        String operatorName=  operator.get("operatorName");
@@ -1774,11 +1697,12 @@
        List<OrderRefundNoteDto> orderRefundNoteDtos=orderRefundNoteListDto.getOrderRefundNoteDtos();
        if("crm".equals(ordersTotal.getAppIdCode())){
            handleCrmRefundOrder(orderRefundNoteDtos,ordersTotal,operatorId,operatorName,orderNodeBuilder);
            //CRM退款
            handleCrmRefundOrder(orderRefundNoteDtos,ordersTotal,refundRecord,operatorId,operatorName,orderNodeBuilder);
        }else {
            sqlSentence.setSqlSentence(" refundroute=#{m.refundroute},refundNumberNo=#{m.refundNumberNo},refundName=#{m.refundName}, " +
            sqlSentence.sqlSentence(" refundroute=#{m.refundroute},refundNumberNo=#{m.refundNumberNo},refundName=#{m.refundName}, " +
                    " 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} ");
                    " shopName=#{m.shopName},operationName=#{m.operationName},operationId=#{m.operationId} where id=#{m.id} ",map);
            map.put("shopId",ordersTotal.getShopId());
            map.put("shopName",ordersTotal.getShopName());
@@ -1815,8 +1739,9 @@
        return PlatformResult.success();
    }
    //处理crm退款
    public void handleCrmRefundOrder(List<OrderRefundNoteDto> orderRefundNoteDtos,OrdersTotal ordersTotal,String operatorId, String operatorName,StringBuilder orderNodeBuilder){
    /**处理crm退款*/
    public void handleCrmRefundOrder(List<OrderRefundNoteDto> orderRefundNoteDtos,OrdersTotal ordersTotal,RefundRecord refundRecord
            ,String operatorId, String operatorName,StringBuilder orderNodeBuilder){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> map=new HashMap<>();
        sqlSentence.setM(map);
@@ -1848,6 +1773,14 @@
            refundDto.setIsWholeOrder(0);
            refundDto.setRemarks("phis退款单退款,订单号:"+ordersTotal.getOrderNo());
            refundDto.setAmountModelList(amountModelList);
            refundDto.setOperatorId(operatorId);
            refundDto.setOperatorName(operatorName);
            if(OrderTotalConstants.STATUS_REFUND_FINSH==ordersTotal.getRefundStatus() &&
                    OrderTotalConstants.STATUS_CANCEL == ordersTotal.getStatus()){
                //全部退款完成则取消订单
                refundDto.setIsCancelOrder(BaseEntity.YES);
            }
            logger.info("调用crm退款入参:{}",JSONObject.toJSONString(refundDto));
            Result result= fOderService.refundOrder(refundDto);;
            logger.info("调用crm退款返回:{}",JSONObject.toJSONString(result));
@@ -1855,13 +1788,14 @@
                result.checkTips();
                if("100".equals(result.getCode())){
                    RefundReturnDto refundReturnDto=JSON.parseObject(JSONObject.toJSONString(result.getData()),RefundReturnDto.class);
                    if (RefundReturnDto.REFUND_STATUS_SUECCESS==refundReturnDto.getRefundStatus()){
                    if (RefundReturnDto.REFUND_STATUS_SUECCESS == refundReturnDto.getRefundStatus()){
                        orderNodeBuilder.append("-调用CRM退款金额:"+crmAmount+",返回成功返回流水号:"+refundReturnDto.getRefundNo()+",json:"+result);
                        logger.info("crm退款成功进行下面操作");
                    }else {
                        throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请求crm退款接口报错,退款失败!退款状态不是成功状态");
                    }
                    //生成CRM回执记录
                    createCrmRefundReceipt(refundReturnDto,refundRecord,commonService);
                }else{
                    throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请求crm退款接口报错,接口返回错误!:code不是100");
                }
@@ -1871,32 +1805,10 @@
        }
        logger.info("订单状态为:{}",ordersTotal.getRefundStatus());
        //通知crm
        if(OrderTotalConstants.STATUS_REFUND_FINSH==ordersTotal.getRefundStatus()){
            //全部退款完成
            //通知crm订单取消成功了
            OrderStatusDto orderStatusDto=new OrderStatusDto();
            orderStatusDto.setId(ordersTotal.getId());
            orderStatusDto.setOpName(operatorName);
            orderStatusDto.setOpId(operatorId);
            orderStatusDto.setCancelInfo("phis取消退款");
            orderStatusDto.setOpDetail("phis取消退款");
            try {
                logger.info("通知CRM订单号:{}退款成功请求参数:{}",ordersTotal.getOrderNo(),JSONObject.toJSONString(orderStatusDto));
                Result result=  fOderService.cancelOrder(orderStatusDto);
                logger.info("通知CRM订单号:{}退款成功返回参数:{}",ordersTotal.getOrderNo(),JSONObject.toJSONString(result));
                orderNodeBuilder.append("通知crm退款状态返回成功");
            }catch (Exception e){
                logger.info("通知crm订单号:{}退款成功错误:{}",ordersTotal.getOrderNo(),e.getMessage());
                orderNodeBuilder.append("通知crm退款状态返回失败");
            }
        }
        sqlSentence.setSqlSentence(" refundroute=#{m.refundroute},refundNumberNo=#{m.refundNumberNo},refundName=#{m.refundName}, " +
        sqlSentence.sqlSentence(" refundroute=#{m.refundroute},refundNumberNo=#{m.refundNumberNo},refundName=#{m.refundName}, " +
                " 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} ");
                " shopName=#{m.shopName},operationName=#{m.operationName},operationId=#{m.operationId} where id=#{m.id} ",map);
        map.put("shopId",ordersTotal.getShopId());
        map.put("shopName",ordersTotal.getShopName());
@@ -1935,6 +1847,69 @@
    }
    /**
     * 生成CRM退款回执
     * @param refundReturnDto crm退款返回总结构
     * @param refundRecord 退款总单
     * @param commonService 映射
     */
    public static void createCrmRefundReceipt(RefundReturnDto refundReturnDto,RefundRecord refundRecord,CommonService commonService){
        try{
            if (RefundReturnDto.REFUND_STATUS_SUECCESS==refundReturnDto.getRefundStatus()
                    ||RefundReturnDto.REFUND_STATUS_PART==refundReturnDto.getRefundStatus()){
                StringBuilder crmReturnData = new StringBuilder();
                crmReturnData.append("<p>【CRM退款】</p>");
                crmReturnData.append("<p>CRM退款流水号:").append(refundReturnDto.getRefundNo()).append("</p>");
                crmReturnData.append("<p>已退款总金额:");
                crmReturnData.append(refundReturnDto.getRefundTotal());
                crmReturnData.append(";");
                crmReturnData.append("已退款其他金额:");
                crmReturnData.append(refundReturnDto.getRefundAmountTotal());
                crmReturnData.append(";");
                crmReturnData.append("已退款储值金:");
                crmReturnData.append(refundReturnDto.getRefundRecharge());
                crmReturnData.append(";");
                crmReturnData.append("已退款增值金:");
                crmReturnData.append(refundReturnDto.getRefundIncrement());
                crmReturnData.append(";");
                crmReturnData.append("已退款积分:");
                crmReturnData.append(refundReturnDto.getRefundIntegral());
                crmReturnData.append("</p>");
                crmReturnData.append("<p>退款方式:</p>");
                if(refundReturnDto.getRefundAmountModelDtoList() != null && refundReturnDto.getRefundAmountModelDtoList().size() > 0){
                    for(RefundAmountModelDto refundAmountModelDto1:refundReturnDto.getRefundAmountModelDtoList()){
                        crmReturnData.append("<p>");
                        switch (refundAmountModelDto1.getRefundType()){
                            case RefundAmountModelDto.REFUND_TYPE_WX:
                                crmReturnData.append("微信[CRM]:").append(refundAmountModelDto1.getAmountTotal());
                                break;
                            case RefundAmountModelDto.REFUND_TYPE_ZFB:
                                crmReturnData.append("支付宝[CRM]:").append(refundAmountModelDto1.getAmountTotal());
                                break;
                            default:
                                crmReturnData.append("[").append(refundAmountModelDto1.getRefundType()).append("]");
                                crmReturnData.append("未知方式[CRM]:").append(refundAmountModelDto1.getAmountTotal());
                        }
                        crmReturnData.append("</p>");
                    }
                }
                RefundRecordReceipt refundRecordReceipt = new RefundRecordReceipt();
                refundRecordReceipt.setReceiptSource(RefundRecordReceipt.RECEIPT_SOURCE_CRM);
                refundRecordReceipt.setReceiptSourceNo(refundReturnDto.getRefundNo());
                refundRecordReceipt.setReceiptSourceData(JSON.toJSONString(refundReturnDto));
                refundRecordReceipt.setReceiptType(RefundRecordReceipt.RECEIPT_TYPE_REFUND);
                refundRecordReceipt.setRemarks(crmReturnData.toString());
                refundRecordReceipt.setOperatorId(refundRecord.getOperatorId());
                refundRecordReceipt.setOperatorNo(refundRecord.getOperatorNo());
                refundRecordReceipt.setOperatorName(refundRecord.getOperatorName());
                refundRecordReceipt.setRefundRecordId(refundRecord.getId());
                commonService.insert(RefundRecordReceiptMapper.class,refundRecordReceipt);
            }
        }catch (Exception e){
            logger.error("退款生成CRM回执报错:{}",e);
        }
    }
    /**
     * 营销中心回调审批处理接口
     * @param aduitDto
     * @return
@@ -1946,12 +1921,12 @@
        Map<String,Object> map=new HashMap<>();
        sqlSentence.setM(map);
        map.put("applyId",aduitDto.getApplyId());
        sqlSentence.setSqlSentence("SELECT * FROM cancel_order WHERE applyId=#{m.applyId} and isDel=0 ");
        sqlSentence.sqlSentence("SELECT * FROM cancel_order WHERE applyId=#{m.applyId} and isDel=0 ",map);
        CancelOrder cancelOrder=commonService.selectOne(CancelOrderMapper.class,sqlSentence);
        if(cancelOrder==null){
        if( cancelOrder == null ){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该审核标识");
        }
        if(cancelOrder.PENDING!=cancelOrder.getApprovalstatus()){
        if(!CancelOrder.PENDING.equals(cancelOrder.getApprovalstatus())){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该条审核标识已经处理过了,不要重复提交");
        }
@@ -1960,39 +1935,45 @@
        operator.put("operatorName",cancelOrder.getOperatorName());
        //先判断项目的状态是否存在
        OrdersTotal ordersTotal=null;
        try {
            ordersTotal=ordersTotalMapper.selectOneByKey(cancelOrder.getOrderId());
            if(ordersTotal==null){
                logger.info("取消订单流程:未找到总订单信息!订单id:{}",cancelOrder.getOrderId());
                return PlatformResult.failure(PlatformCode.ERROR_TIPS,"取消订单流程:未找到总订单信息!");
            }
        }catch (Exception e){
            logger.info("取消订单流程:根据订单id查询多条总订单信息!订单id:{}",cancelOrder.getOrderId());
            return PlatformResult.failure(PlatformCode.ERROR_TIPS,"取消订单流程:根据订单id查询多条总订单信息!");
        OrdersTotal ordersTotal= ordersTotalMapper.selectOneByKey(cancelOrder.getOrderId());
        if(ordersTotal==null){
            logger.info("取消订单流程:未找到总订单信息!订单id:{}",cancelOrder.getOrderId());
            return PlatformResult.failure(PlatformCode.ERROR_TIPS,"取消订单流程:未找到总订单信息!");
        }
        cancelOrder.setExamEmplId(aduitDto.getExamEmplId());
        cancelOrder.setExamEmplName(aduitDto.getExamEmplName());
        RefundRecord refundRecord=commonService.selectOneByKey(RefundRecordMapper.class,cancelOrder.getRefundRecordId());
        //获取退款记录
        RefundRecord refundRecord = commonService.selectOneByKey(RefundRecordMapper.class,cancelOrder.getRefundRecordId());
        if(refundRecord==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该审核的退款记录");
        }
        if(BaseEntity.NO== aduitDto.getType()){
        //订单节点日志
        OrdersNodeLog ordersNodeLog = new OrdersNodeLog();
        StringBuilder orderNodeBuilder = new StringBuilder();
        if(BaseEntity.NO.equals(aduitDto.getType())){
            orderNodeBuilder.append("审核不通过");
            orderNodeBuilder.append("-最后审核人:").append(aduitDto.getExamEmplName());
            //审核不通过
            cancelOrder.setApprovalstatus(CancelOrder.FAIL);
            cancelOrder.setEditTime(new Date());
            commonService.updateAll(CancelOrderMapper.class,cancelOrder);
            //更改总订单退款状态
            map.put("orderId",ordersTotal.getId());
            sqlSentence.setSqlSentence("select * from order_item WHERE  orderId=#{m.orderId} and isDel=0");
            sqlSentence.sqlSentence("select * from order_item WHERE  orderId=#{m.orderId} and isDel=0",map);
            List<OrderItem> orderItemList=commonService.selectList(OrderItemMapper.class,sqlSentence);
            List<Integer> collect = orderItemList.stream().map(o -> o.getRefundStatus()).collect(Collectors.toList());
            if(collect.contains(OrderTotalConstants.STATUS_REFUND_PART)){
                ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
            }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE) && collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
            }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE)
                    && collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
                ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
            }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE)){
                ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
@@ -2004,40 +1985,48 @@
            map.put("refundStatus",ordersTotal.getRefundStatus());
            map.put("id",ordersTotal.getId());
            sqlSentence.setSqlSentence(" refundStatus=#{m.refundStatus}  WHERE id=#{m.id}  ");
            sqlSentence.sqlSentence(" refundStatus=#{m.refundStatus}  WHERE id=#{m.id}  ",map);
            ordersTotalMapper.updateWhere(sqlSentence);
            refundRecord.setRefundStatus(RefundStatus.STATUS_INVALID_REFUND);
            refundRecord.setEditTime(new Date());
            commonService.updateAll(RefundRecordMapper.class,refundRecord);
            //处理老带新的佣金,没有退款才退
            if(ordersTotal.getRefundStatus() == OrderTotalConstants.STATUS_REFUND_NONE){
                oldBlingNewRefundFail(refundRecord,orderNodeBuilder,commonService);
            }
            //拒绝审批需要还原回去储值金
            //判断是否是充值订单,是的话需要把前面预扣的还原
            if(OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){
                //判断金额不等于0,才执行操作,不然操作余额的时候会爆操作数量或金额不能为0
                if(ordersTotal.getTotal().compareTo(BigDecimal.ZERO)!=0){
                if(ordersTotal.getActualTotal().compareTo(BigDecimal.ZERO)!=0){
                    UserMoneyUtil.setNewUserMoneyUnclaimed(refundRecord.getUserId(),refundRecord.getRemarks(),"充值单:"+ordersTotal.getOrderNo()+" 申请退款,拒绝审批,返还预扣除金额:"+ordersTotal.getTotal(),operator.get("operatorId"),refundRecord.getOrderId(),ordersTotal.getAppIdCode(),refundRecord.getId(),new BigDecimal(String.valueOf(ordersTotal.getTotal())), UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND, OperationReasonConstants.OP_REASON_RECHARGE_REFUND,commonService,UserMoneyUnclaimed.NO);
                    OrderLog orderLog = RefundToolUtil.setOrderLog(refundRecord, operator.get("operatorId"), operator.get("operatorName"), "充值单:"+ordersTotal.getOrderNo()+"申请退款,预扣除金额:"+ordersTotal.getTotal(), 0, OrderLogConstants.LOG_TYPE_REFUND);
                    commonService.insert(OrderLogMapper.class,orderLog);
                    //活动规则退款处理
                    map.put("orderId", ordersTotal.getId());
                    sqlSentence.setSqlSentence("select * from orders_info where isDel=0 and orderId=#{m.orderId}");
                    sqlSentence.sqlSentence("select * from orders_info where isDel=0 and orderId=#{m.orderId}",map);
                    OrderInfo orderInfo=commonService.selectOne(OrderInfoMapper.class,sqlSentence);
                    handActivityRule(commonService, operator.get("operatorId"), operator.get("operatorName"), sqlSentence, map, refundRecord,ordersTotal,orderInfo,1);
                }
            }
            ordersNodeLog.setCommonType(OrdersNodeLog.TYPE_COMMON_ORDER_REFUND);
            ordersNodeLog.setContent(orderNodeBuilder.toString());
            ordersNodeLog.setOrderId(ordersTotal.getId());
            commonService.insert(OrdersNodeLogMapper.class,ordersNodeLog);
            return PlatformResult.success("拒绝取消审核通过");
        }else {
            cancelOrder.setEditTime(new Date());
            cancelOrder.setApprovalstatus(CancelOrder.SUCCESS);
            commonService.updateAll(CancelOrderMapper.class,cancelOrder);
        }
        //订单节点日志
        OrdersNodeLog ordersNodeLog = new OrdersNodeLog();
        StringBuilder orderNodeBuilder = new StringBuilder();
        cancelOrder.setEditTime(new Date());
        cancelOrder.setApprovalstatus(CancelOrder.SUCCESS);
        commonService.updateAll(CancelOrderMapper.class,cancelOrder);
        orderNodeBuilder.append("审核通过,开始处理退款");
@@ -2052,10 +2041,13 @@
     * @return
     */
    @Override
    public PlatformResult wholeRefund(OrderRefundDto orderRefundDto) {
    public PlatformResult wholeRefund(OrderRefundDto orderRefundDto,ThirtApplication thirtApplication) {
        OrdersTotal ordersTotal = ordersTotalMapper.selectOneByKey(orderRefundDto.getOrderId());
        //获取该笔订单的项目详情
        List<Map<String, Object>> refundDetails=refundDetails(orderRefundDto.getOrderId());
        List<Map<String, Object>> refundDetails=refundDetails(ordersTotal);
        // 退款信息集合
        List<OrderItemRefundDto> refundList=new ArrayList<>();
        //退款二级子订单信息集合
@@ -2065,11 +2057,16 @@
        OrderItemRefundDto orderItemRefundDto;
        OrderItemSourceRefundDto orderItemSourceRefundDto;
        for (Map<String, Object> refundDetail : refundDetails) {
            orderItemRefundDto=new OrderItemRefundDto();
            orderItemRefundDto = new OrderItemRefundDto();
            orderItemRefundDto.setOrderItemId(String.valueOf(refundDetail.get("id")));
            orderItemRefundDto.setType(String.valueOf(refundDetail.get("type")));
            if(OrderItemConstants.TYPE_PROMOTION.equals(orderItemRefundDto.getType()) || OrderItemConstants.CARD_BAG.equals(orderItemRefundDto.getType())){
            ////退款备注
            orderItemRefundDto.setRemarks(orderRefundDto.getRemarks());
            orderItemRefundDto.setRefundReason(orderRefundDto.getRefundReason());
            if(OrderItemConstants.TYPE_PROMOTION.equals(orderItemRefundDto.getType())
                    || OrderItemConstants.CARD_BAG.equals(orderItemRefundDto.getType())){
                orderItemRefundDto.setRefundNum(0);
                refundSonDetails= (List<Map<String, Object>>) refundDetail.get("list");
                orderItemSourceRefundDtos=new ArrayList<>();
@@ -2078,6 +2075,11 @@
                    orderItemSourceRefundDto.setOrderItemSonId(String.valueOf(refundSonDetail.get("id")));
                    orderItemSourceRefundDto.setType(String.valueOf(refundSonDetail.get("type")));
                    orderItemSourceRefundDto.setRefundNum(Integer.parseInt(String.valueOf(refundSonDetail.get("notUsedNum"))));
                    //退款备注
                    orderItemSourceRefundDto.setRemarks(orderRefundDto.getRemarks());
                    orderItemSourceRefundDto.setRefundReason(orderRefundDto.getRefundReason());
                    orderItemSourceRefundDtos.add(orderItemSourceRefundDto);
                }
                orderItemRefundDto.setOrderItemSourceRefundDtos(orderItemSourceRefundDtos);
@@ -2089,7 +2091,7 @@
        orderRefundDto.setRefundList(refundList);
        //获取退款资金信息
        OrderRefundDto  parameter= nextStep(orderRefundDto);
        OrderRefundDto  parameter= nextStep(ordersTotal,orderRefundDto);
        parameter.setOperatorId(orderRefundDto.getOperatorId());
        parameter.setIsRefund(orderRefundDto.getIsRefund());
@@ -2097,8 +2099,22 @@
        parameter.setRoleId(orderRefundDto.getRoleId());
        parameter.setPlatformSource(orderRefundDto.getPlatformSource());
        parameter.setRefundOperationType(BaseEntity.NO);
        parameter.setRemarks(orderRefundDto.getRemarks());
        parameter.setRefundReason(orderRefundDto.getRefundReason());
        parameter.setCouponList(parameter.getPayCouponList());
        //退款金额
        List<OrderPayMethodDto> refundPayMethodList = parameter.getPayMethodList();
        for(OrderPayMethodDto orderPayMethodDto:refundPayMethodList){
            orderPayMethodDto.setRefundNumberNo(orderPayMethodDto.getPayMethodNo());
            orderPayMethodDto.setRefundNumberName(orderPayMethodDto.getPayMethodName());
            orderPayMethodDto.setMoney(orderPayMethodDto.getPayTotal());
        }
        parameter.setRefundPayMethod(refundPayMethodList);
        //调用退款处理退款逻辑
        return partRefund(parameter);
        return partRefund(ordersTotal,parameter,thirtApplication);
    }
    /**
@@ -2132,6 +2148,8 @@
        return handleforgePartRefund(ordersTotal,orderRefundDto);
    }
    /**
     * 伪造处理退款详情
@@ -2181,7 +2199,7 @@
        if(refundPayMethod!=null && refundPayMethod.size()>0){
            //处理退款方式记录表
            handleRefundPayMethod(refundPayMethod,orderNodeBuilder,ordersTotal,refundRecord);
            handleRefundPayMethod(orderRefundDto,orderNodeBuilder,ordersTotal,refundRecord);
        }
        if(refundList!=null && refundList.size()>0){
            //记录本次退款的项目详情
@@ -2194,7 +2212,7 @@
                percentage=sumMoney.divide(refundSumMoney,10,BigDecimal.ROUND_HALF_UP);
            }
            handleRefundRecordItem(refundList,orderNodeBuilder,ordersTotal,refundRecord,percentage);
            //handleRefundRecordItem(orderRefundDto,orderNodeBuilder,ordersTotal,refundRecord,percentage);
        }
        if(couponList!=null && couponList.size()>0){
            //处理优惠卷
@@ -2226,4 +2244,253 @@
        return PlatformResult.success("成功");
    }
    /**
     * 根据订单 重新绑定订单退款状态
     *
     */
    @Override
    public void verifyOrderRefundStatus(OrdersTotal ordersTotalDto) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> sqlMap = new HashMap<>();
        sqlSentence.setM(sqlMap);
        sqlMap.put("isDel", BaseEntity.NO);
        sqlMap.put("orderId", ordersTotalDto.getId());
        sqlMap.put("type", OrderItemConstants.TYPE_PROJECT);
        sqlSentence.sqlSentence("SELECT * FROM  orders_total   WHERE  id = #{m.orderId}  and  isDel = #{m.isDel} ",sqlMap);
        OrdersTotal ordersTotal = ordersTotalMapper.selectOne(sqlSentence);
        if(ordersTotal==null){
            throw new TipsException("订单标识错误,找不到订单!");
        }
        //查询二级订单
        sqlSentence.sqlSentence("SELECT * FROM  order_item_source   WHERE  orderId = #{m.orderId}  and  type = #{m.type}  and  isDel = #{m.isDel} ",sqlMap);
        List<OrderItemSon> orderItemSonList=orderItemSonMapper.selectList(sqlSentence);
        if(orderItemSonList!=null && orderItemSonList.size()>0){
            int oldRefundStatus;
            int newRefundStatus;
            UserProjectItem userProjectItem;
            for (OrderItemSon orderItemSon : orderItemSonList) {
                if(OrderTotalConstants.STATUS_REFUND_FINSH==orderItemSon.getRefundStatus()){
                    oldRefundStatus=OrderTotalConstants.STATUS_REFUND_FINSH;
                    userProjectItem = PartialRefundUtil.getUserProject(orderItemSon.getId(),commonService);
                    if(userProjectItem.getNotUsedNum()>0){
                        newRefundStatus=OrderTotalConstants.STATUS_REFUND_PART;
                    }else {
                        newRefundStatus=oldRefundStatus;
                    }
                    if(newRefundStatus!=oldRefundStatus){
                        sqlMap.put("refundStatus", newRefundStatus);
                        sqlMap.put("id", orderItemSon.getId());
                        sqlSentence.sqlSentence(" refundStatus = #{m.refundStatus} WHERE id = #{m.id}  ",sqlMap);
                        orderItemSonMapper.updateWhere(sqlSentence);
                        sqlMap.put("refundStatus", newRefundStatus);
                        sqlMap.put("id", orderItemSon.getOrderItemId());
                        sqlSentence.sqlSentence(" refundStatus = #{m.refundStatus} WHERE id = #{m.id}  ",sqlMap);
                        orderItemMapper.updateWhere(sqlSentence);
                    }
                }
            }
        }
        //查询二级订单
        sqlSentence.sqlSentence("SELECT * FROM  order_item   WHERE  orderId = #{m.orderId}  and  type = #{m.type}  and  isDel = #{m.isDel} ",sqlMap);
        List<OrderItem> orderItemList=orderItemMapper.selectList(sqlSentence);
        if(orderItemList!=null && orderItemList.size()>0){
            int oldRefundStatus;
            int newRefundStatus;
            UserProjectItem userProjectItem;
            for (OrderItem orderItem : orderItemList) {
                oldRefundStatus=OrderTotalConstants.STATUS_REFUND_FINSH;
                userProjectItem = PartialRefundUtil.getUserProject(orderItem.getId(),commonService);
                if(userProjectItem.getNotUsedNum()>0){
                    newRefundStatus=OrderTotalConstants.STATUS_REFUND_PART;
                }else {
                    newRefundStatus=oldRefundStatus;
                }
                if(newRefundStatus!=oldRefundStatus){
                    sqlMap.put("refundStatus", newRefundStatus);
                    sqlMap.put("id", orderItem.getId());
                    sqlSentence.sqlSentence(" refundStatus = #{m.refundStatus} WHERE id = #{m.id}  ",sqlMap);
                    orderItemMapper.updateWhere(sqlSentence);
                }
            }
        }
        sqlMap.put("orderId",ordersTotal.getId());
        sqlSentence.sqlSentence("select * from order_item WHERE  orderId=#{m.orderId} and isDel=0",sqlMap);
        List<OrderItem> orderItems=commonService.selectList(OrderItemMapper.class,sqlSentence);
        List<Integer> collect = orderItems.stream().map(o -> o.getRefundStatus()).collect(Collectors.toList());
        if(collect.contains(OrderTotalConstants.STATUS_REFUND_PART)){
            ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
            ordersTotal.setStatus(OrderTotalConstants.STATUS_PAY);
        }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE) && collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
            ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
            ordersTotal.setStatus(OrderTotalConstants.STATUS_PAY);
        }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE)){
            ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
            ordersTotal.setStatus(OrderTotalConstants.STATUS_PAY);
        }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
            ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_FINSH);
            ordersTotal.setStatus(OrderTotalConstants.STATUS_CANCEL);
        }else {
            ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
            ordersTotal.setStatus(OrderTotalConstants.STATUS_PAY);
        }
        ordersTotal.setEditTime(new Date());
        commonService.updateAll(OrdersTotalMapper.class,ordersTotal);
    }
    /**作废退款*/
    @Override
    public void refundCancel(RefundCancelVo refundCancelVo, RefundRecord refundRecord,Employee employee,EmployeeRole employeeRole
            , ThirtApplication thirtApplication) {
        //判断退款单状态
        if(refundRecord.getRefundStatus() != RefundStatus.STATUS_SUCC_REFUND){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作失败,退款单状态错误!");
        }
        //获取订单信息
        OrdersTotal ordersTotal = ordersTotalMapper.selectOneByKey(refundRecord.getOrderId());
        if(ordersTotal == null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作失败,获取订单信息失败!");
        }
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        StringBuilder sql = new StringBuilder();
        //填充数据,后面会用到
        refundRecord.setInvalidType(RefundRecord.OPERATOR_TYPE_EMPLOYEE);
        refundRecord.setInvalidRemarks(refundCancelVo.getRemarks());
        //****更新退款单信息,更新完退款单再去做剩下的操作
        values.put("refundStatus",RefundStatus.STATUS_REFUND_CANCEL);
        values.put("refundStatusOld",RefundStatus.STATUS_SUCC_REFUND);
        values.put("id",refundRecord.getId());
        values.put("invalidTime",new Date());
        values.put("invalidType",RefundRecord.OPERATOR_TYPE_EMPLOYEE);
        values.put("invalidRemarks",refundCancelVo.getRemarks());
        sql.append("refundStatus = #{m.refundStatus},invalidTime = #{m.invalidTime},invalidRemarks = #{m.invalidRemarks},invalidType = #{m.invalidType}");
        if(employee != null){
            refundRecord.setInvalidId(employee.getId());
            refundRecord.setInvalidNo(employee.getEmployeeNo());
            refundRecord.setInvalidName(employee.getCnName());
            values.put("invalidId",employee.getId());
            values.put("invalidNo",employee.getEmployeeNo());
            values.put("invalidName",employee.getCnName());
            sql.append(",invalidId = #{m.invalidId},invalidNo = #{m.invalidNo},invalidName = #{m.invalidName}");
        }
        if(employeeRole != null){
            refundRecord.setInvalidRoleNo(employeeRole.getRoleUniqueStr());
            refundRecord.setInvalidRoleName(employeeRole.getRoleName());
            values.put("invalidRoleNo",employeeRole.getRoleUniqueStr());
            values.put("invalidRoleName",employeeRole.getRoleName());
            sql.append(",invalidRoleNo = #{m.invalidRoleNo},invalidRoleName = #{m.invalidRoleName}");
        }
        if(thirtApplication != null){
            refundRecord.setInvalidAppId(thirtApplication.getAppId());
            refundRecord.setInvalidAppCode(thirtApplication.getAppIdCode());
            refundRecord.setInvalidAppName(thirtApplication.getName());
            values.put("invalidAppId",thirtApplication.getAppId());
            values.put("invalidAppCode",thirtApplication.getAppIdCode());
            values.put("invalidAppName",thirtApplication.getName());
            sql.append(",invalidAppId = #{m.invalidAppId},invalidAppCode = #{m.invalidAppCode},invalidAppName = #{m.invalidAppName}");
        }
        sql.append(" WHERE id = #{m.id} AND refundStatus = #{m.refundStatusOld}");
        sqlSentence.sqlUpdate(sql.toString(),values);
        if(refundRecordMapper.updateWhere(sqlSentence) != 1){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"操作失败,退款单状态已发生变化!");
        }
        //判断订单类型
        if(OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){
            //充值订单
            OrderRefundCancelTool.rechargeHandle(ordersTotal,refundRecord,1,commonService);
        }else{
            //处理子单
            OrderRefundCancelTool.refundRecordItemHandle(ordersTotal,refundRecord,commonService);
        }
        //处理订单活动
        OrderRefundCancelTool.activityRuleHandle(ordersTotal,refundRecord,1,commonService);
        //处理订单优惠券
        OrderRefundCancelTool.handCoupon(ordersTotal,refundRecord,commonService);
        //处理总退款方式
        OrderRefundCancelTool.refundRecordMotnedHandle(refundRecord,ordersTotal,commonService);
        //****更改总订单退款状态
        values.clear();
        values.put("orderId",ordersTotal.getId());
        sqlSentence.sqlSentence("select * from order_item WHERE orderId=#{m.orderId} and isDel=0",values);
        List<OrderItem> orderItemList=commonService.selectList(OrderItemMapper.class,sqlSentence);
        values.clear();
        values.put("oldStatus",ordersTotal.getStatus());
        values.put("oldRefundStatus",ordersTotal.getRefundStatus());
        List<Integer> collect = orderItemList.stream().map(OrderItem::getRefundStatus).collect(Collectors.toList());
        if(collect.contains(OrderTotalConstants.STATUS_REFUND_PART)){
            ordersTotal.setStatus(OrderTotalConstants.STATUS_PAY);
            ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
            ordersTotal.setReTotal(orderItemList.stream().map(OrderItem::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
        }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE) && collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
            ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
            ordersTotal.setStatus(OrderTotalConstants.STATUS_PAY);
            ordersTotal.setReTotal(orderItemList.stream().map(OrderItem::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
        }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_NONE)){
            ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
            ordersTotal.setStatus(OrderTotalConstants.STATUS_PAY);
            ordersTotal.setReTotal(BigDecimal.ZERO);
        }else if (collect.contains(OrderTotalConstants.STATUS_REFUND_FINSH)){
            ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_FINSH);
            ordersTotal.setStatus(OrderTotalConstants.STATUS_CANCEL);
            ordersTotal.setReTotal(orderItemList.stream().map(OrderItem::getReTotal).reduce(BigDecimal.ZERO,BigDecimal::add));
        }else {
            if(OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())){
                if(ordersTotal.getReTotal().compareTo(refundRecord.getRefundTotal()) == 0){
                    ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
                    ordersTotal.setStatus(OrderTotalConstants.STATUS_PAY);
                    ordersTotal.setReTotal(ordersTotal.getReTotal().subtract(refundRecord.getRefundTotal()));
                }else if(ordersTotal.getReTotal().compareTo(refundRecord.getRefundTotal()) > 0){
                    ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_PART);
                    ordersTotal.setStatus(OrderTotalConstants.STATUS_PAY);
                    ordersTotal.setReTotal(ordersTotal.getReTotal().subtract(refundRecord.getRefundTotal()));
                }
            }else{
                ordersTotal.setRefundStatus(OrderTotalConstants.STATUS_REFUND_NONE);
                ordersTotal.setStatus(OrderTotalConstants.STATUS_PAY);
                ordersTotal.setReTotal(ordersTotal.getReTotal().subtract(refundRecord.getRefundTotal()));
            }
        }
        ordersTotal.setIsSyncOrder(BaseEntity.NO);
        values.put("isSyncOrder",BaseEntity.NO);
        values.put("status",ordersTotal.getStatus());
        values.put("refundStatus",ordersTotal.getRefundStatus());
        values.put("reTotal",ordersTotal.getReTotal());
        values.put("id",ordersTotal.getId());
        sqlSentence.sqlUpdate("isSyncOrder = #{m.isSyncOrder},status = #{m.status},refundStatus = #{m.refundStatus},reTotal = #{m.reTotal}" +
                " WHERE id = #{m.id} AND status = #{m.oldStatus} AND refundStatus = #{m.oldRefundStatus}",values);
        if(commonService.updateWhere(OrdersTotalMapper.class,sqlSentence) != 1){
            throw new TipsException("作废失败,订单状态已发生改变!");
        }
    }
}