package com.liquidnet.service.order.service.impl;

import com.liquidnet.commons.lang.util.CollectionUtil;
import com.liquidnet.service.kylin.dto.vo.mongo.KylinOrderTicketEntitiesVo;
import com.liquidnet.service.kylin.dto.vo.mongo.KylinOrderTicketVo;
import com.liquidnet.service.kylin.dto.vo.mongo.KylinPerformanceVo;
import com.liquidnet.service.kylin.service.IKylinOrderRefundsService;
import com.liquidnet.service.order.utils.DataUtils;
import com.liquidnet.service.order.utils.TaobaoTicketUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 超时支付调用退款申请
 * </p>
 *
 * @author jiangxiulong
 * @since 2021-05-26
 */
@Slf4j
@Service
public class OrderRefundOvertimeServiceImpl implements IKylinOrderRefundsService {
    @Autowired
    private KylinRefundsStatusServiceImpl kylinRefundsStatusServiceImpl;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    private DataUtils dataUtils;

    @Autowired
    private TaobaoTicketUtils taobaoTicketUtils;

    public Boolean refundApply(String orderTicketsId, String paymentId, String paymentType) {
        log.info("订单超时支付refundApply-Start：[orderTicketsId={}, [paymentId={}, paymentType={}]",
                orderTicketsId, paymentId, paymentType);
        KylinOrderTicketVo orderInfo = dataUtils.getOrderTicketVo(orderTicketsId);
        // 金额
        BigDecimal priceActual = orderInfo.getPriceActual();
        BigDecimal priceExpress = orderInfo.getPriceExpress();

        // 查询订单入场人
        List<KylinOrderTicketEntitiesVo> entitiesList = orderInfo.getEntitiesVoList();
        if (!CollectionUtil.isEmpty(entitiesList)) {
            int allEntitiesCount = entitiesList.size();// 总入场人数量 排出未付款的 用来计算单入场人的价格
            BigDecimal onePrice = priceActual.subtract(priceExpress).divide(BigDecimal.valueOf(allEntitiesCount));//单价
            for (KylinOrderTicketEntitiesVo entities : entitiesList) {
                entities.setCanRefundedPrice(onePrice);
            }
        }
        List<String> ticketEntityIds = entitiesList.stream().map(KylinOrderTicketEntities -> KylinOrderTicketEntities.getOrderTicketEntitiesId()).collect(Collectors.toList());
        List<BigDecimal> entitiesPrice = entitiesList.stream().map(KylinOrderTicketEntities -> KylinOrderTicketEntities.getCanRefundedPrice()).collect(Collectors.toList());

        boolean res = kylinRefundsStatusServiceImpl.orderTicketRefunding(
                orderInfo, orderTicketsId,
                priceExpress,
                ticketEntityIds, entitiesPrice, paymentId, paymentType
        );
        if (res) {
            // 优惠券 库存 积分 大美已自己处理
            // 同步大麦 票务平台
            KylinOrderTicketVo orderData = dataUtils.getOrderTicketVo(orderTicketsId);
            KylinPerformanceVo vo = dataUtils.getPerformanceVo(orderData.getPerformanceId());
            taobaoTicketUtils.refundDamaiOrder(orderData, vo);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public String getOrderRefundCode(String orderRefundCode, int type) {
        return null;
    }
}