package com.liquidnet.service.kylin.service.impl.admin;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.JsonNode;
import com.liquidnet.service.kylin.constant.KylinTableStatusConst;
import com.liquidnet.service.kylin.entity.KylinOrderRefunds;
import com.liquidnet.service.kylin.entity.KylinOrderTicketStatus;
import com.liquidnet.service.kylin.entity.KylinOrderTickets;
import com.liquidnet.service.kylin.entity.KylinRefunds;
import com.liquidnet.service.kylin.mapper.KylinOrderRefundsMapper;
import com.liquidnet.service.kylin.mapper.KylinOrderTicketStatusMapper;
import com.liquidnet.service.kylin.mapper.KylinOrderTicketsMapper;
import com.liquidnet.service.kylin.mapper.KylinRefundsMapper;
import com.liquidnet.service.kylin.service.IKylinRefundsService;
import org.apache.http.HttpException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 * 退款表 服务实现类
 * </p>
 *
 * @author jiaangxiulong
 * @since 2021-05-26
 */
@Service
public class KylinRefundsStatusServiceImpl extends ServiceImpl<KylinRefundsMapper, KylinRefunds> implements IKylinRefundsService {

    @Autowired
    private KylinOrderTicketsMapper kylinOrderTicketsMapper;

    @Autowired
    private KylinOrderTicketStatusMapper kylinOrderTicketStatusMapper;

    @Autowired
    private KylinOrderRefundsMapper kylinOrderRefundsMapper;


    public void orderTicketRefunding(int authId, String authName, String orderTicketsId, JsonNode ticketEntityIds, String reason, String remark, Integer batch_id) throws HttpException {
        /*KylinOrderTickets orderInfo = kylinOrderTicketsMapper.selectOne(
                new UpdateWrapper<KylinOrderTickets>().eq("order_tickets_id", orderTicketsId)
        );
        KylinOrderTicketStatus orderStatus = kylinOrderTicketStatusMapper.selectOne(
                new UpdateWrapper<KylinOrderTicketStatus>().eq("order_id", orderTicketsId)
        );

        // 订单状态，已支付(其它情况)、已关闭(超时支付、已支付但出票失败等情况)
        Integer[] statusArr = {KylinTableStatusConst.STATUS_PAID, KylinTableStatusConst.STATUS_CLOSE, KylinTableStatusConst.STATUS_DELETE};
        Set<String> set = new HashSet(Arrays.asList(statusArr));

        if (!set.contains(orderStatus.getStatus())) {
            throw new HttpException("订单状态信息有误");
        }
        // 订单支付状态需为已支付
        if (orderStatus.getPayStatus() != 1) {
            throw new HttpException("订单支付信息有误");
        }
        // 该订单正在退款或已有退款
        int refundingCount = kylinOrderRefundsMapper.selectCount(
                new UpdateWrapper<KylinOrderRefunds>().eq("order_id", orderTicketsId)
                        .eq("order_type", "order_ticket")
                        .ne("status", KylinTableStatusConst.ORDER_REFUND_STATUS_CANCEL)
        );
        if (refundingCount > 0) {
            throw new HttpException("该订单正在退款或已有退款");
        }*/
        // 本次退款批次
        /*int maxRefundBatch = kylinOrderRefundsMapper.selectCount(
                new UpdateWrapper<KylinOrderRefunds>().eq("order_id", orderTicketsId)
                        .setSql(max)
                        .ne("status", KylinTableStatusConst.ORDER_REFUND_STATUS_CANCEL)
        );
                $max_refund_batch = OrderRefund::onWriteConnection()->where('order_id', $order_id)->max('batch');
        $batch = $max_refund_batch ? $max_refund_batch + 1 : 1;

        # 本次退款总金额
                $refund_total_price = 0;*/
    }
}
