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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liquidnet.client.admin.common.utils.DateUtils;
import com.liquidnet.client.admin.common.utils.ShiroUtils;
import com.liquidnet.commons.lang.util.BeanUtil;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.kylin.constant.KylinTableStatusConst;
import com.liquidnet.service.kylin.dao.KylinOrderRefundsEntitiesDao;
import com.liquidnet.service.kylin.dao.KylinOrderTicketEntitiesDao;
import com.liquidnet.service.kylin.dao.OrderRefundDao;
import com.liquidnet.service.kylin.dto.param.RefundApplyParam;
import com.liquidnet.service.kylin.dto.param.RefundCallbackParam;
import com.liquidnet.service.kylin.dto.param.RefundSearchParam;
import com.liquidnet.service.kylin.dto.vo.mongo.KylinOrderTicketVo;
import com.liquidnet.service.kylin.dto.vo.mongo.KylinPerformanceVo;
import com.liquidnet.service.kylin.dto.vo.returns.KylinOrderRefundsVo;
import com.liquidnet.service.kylin.dto.vo.returns.RefundOrderDetailsVo;
import com.liquidnet.service.kylin.entity.*;
import com.liquidnet.service.kylin.mapper.*;
import com.liquidnet.service.kylin.service.IKylinOrderRefundsService;
import com.mongodb.BasicDBObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单退款表 服务实现类 处理逻辑判断
 * </p>
 *
 * @author jiangxiulong
 * @since 2021-05-26
 */
@Slf4j
@Service
public class KylinOrderRefundsServiceImpl extends ServiceImpl<KylinOrderRefundsMapper, KylinOrderRefunds> implements IKylinOrderRefundsService {
    @Autowired
    private KylinOrderTicketsMapper kylinOrderTicketsMapper;

    @Autowired
    private KylinRefundsStatusServiceImpl kylinRefundsStatusServiceImpl;

    @Autowired
    private KylinOrderRefundsMapper kylinOrderRefundsMapper;

    @Autowired
    private KylinOrderTicketStatusMapper kylinOrderTicketStatusMapper;

    @Autowired
    private KylinOrderTicketEntitiesMapper kylinOrderTicketEntitiesMapper;

    @Autowired
    private KylinOrderTicketRelationsMapper kylinOrderTicketRelationsMapper;

    @Autowired
    private KylinPerformancesMapper kylinPerformancesMapper;

    @Autowired
    private KylinOrderRefundsEntitiesMapper kylinOrderRefundsEntitiesMapper;

    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    private MongoConverter mongoConverter;

    public ResponseDto refundApply(RefundApplyParam refundApplyParam) {
        String orderTicketsId = refundApplyParam.getOrderTicketsId();
        List<String> ticketEntityIds = refundApplyParam.getTicketEntityIds();
        List<Double> entitiesPrice = refundApplyParam.getEntitiesPrice();
        Double RefundPriceExpress = refundApplyParam.getRefundPriceExpress();
        if (null == RefundPriceExpress) {
            RefundPriceExpress = 0.0;
        }

        KylinOrderTickets orderInfo = kylinOrderTicketsMapper.selectOne(
                new QueryWrapper<KylinOrderTickets>().eq("order_tickets_id", orderTicketsId)
        );
        KylinOrderTicketStatus orderStatus = kylinOrderTicketStatusMapper.selectOne(
                new QueryWrapper<KylinOrderTicketStatus>().eq("order_id", orderTicketsId)
        );
        int thisOrderStatus = orderStatus.getStatus();
        int thisPayStatus = orderStatus.getPayStatus();
        double priceExpress = orderInfo.getPriceExpress().doubleValue();

        // todo 转增是否能退
        // 订单状态需已付款
        if (thisOrderStatus != KylinTableStatusConst.ORDER_STATUS1 && thisOrderStatus != KylinTableStatusConst.ORDER_STATUS3 && thisOrderStatus != KylinTableStatusConst.ORDER_STATUS6) {
            return ResponseDto.failure("当前订单状态不支持退款申请");
        }
        // 订单支付状态需为已支付
        if (thisPayStatus != KylinTableStatusConst.ORDER_PAY_STATUS1) {
            return ResponseDto.failure("订单支付状态需为已支付");
        }
        // TODO: 2021/6/16 是否能单独退快递飞 批量退款是否退快递费 现在做的是自动退剩余快递费
        // 传的快递费不能大于实际的快递费=（支付的快递费-已退的快递费）
        Double refundPriceExpressSum = kylinOrderRefundsMapper.RefundPriceExpressSum(// 已退快递费
                orderTicketsId,
                KylinTableStatusConst.ORDER_REFUND_STATUS_CANCEL
        );
        if (null == refundPriceExpressSum) {
            refundPriceExpressSum = 0.0;
        }
        if (null == refundApplyParam.getOrderRefundBatchesId() || refundApplyParam.getOrderRefundBatchesId().isEmpty()) { // 不是批量退款
            if (RefundPriceExpress > (priceExpress - refundPriceExpressSum)) {
                return ResponseDto.failure("快递费不能大于实际减去已退的快递费");
            }
        } else {
            RefundPriceExpress = priceExpress - refundPriceExpressSum;
        }

        // todo 出票未出票
        // 选择退款的入场人是否正确
        Integer[] entitiesStatus = new Integer[]{KylinTableStatusConst.ENTITIES_IS_PAYMENT1, KylinTableStatusConst.ENTITIES_IS_PAYMENT2, KylinTableStatusConst.ENTITIES_IS_PAYMENT4};
        int choiceCount = kylinOrderTicketEntitiesMapper.selectCount(
                new QueryWrapper<KylinOrderTicketEntities>()
                        .eq("order_id", orderTicketsId)
                        .in("is_payment", entitiesStatus)
                        .in("order_ticket_entities_id", ticketEntityIds)
        );
        if (choiceCount < 0 || choiceCount != ticketEntityIds.size()) {
            return ResponseDto.failure("入场人订单有误");
        }
        // 该订单正在退款或已有退款 因支持填入价格所有取消判断
        // 各个入场人订单填写的退款金额是否正确
        double priceActual = orderInfo.getPriceActual().doubleValue();
        int allEntitiesCount = kylinOrderTicketEntitiesMapper.selectCount(// 总入场人数量 排出未付款的 用来计算单入场人的价格
                new QueryWrapper<KylinOrderTicketEntities>()
                        .eq("order_id", orderTicketsId)
                        .ne("is_payment", KylinTableStatusConst.ENTITIES_IS_PAYMENT0)
        );
        double onePrice = (priceActual - priceExpress) / allEntitiesCount;//单价
        List<Double> realRefundPriceList = new ArrayList<>();
        if (null == refundApplyParam.getOrderRefundBatchesId() || refundApplyParam.getOrderRefundBatchesId().isEmpty()) { // 不是批量退款
            for (int i = 0; i <= ticketEntityIds.size() - 1; i++) {
                Double price = entitiesPrice.get(i);
                if (price < 0) {
                    return ResponseDto.failure("退款金额不能为0");
                } else {
                    Double refundedPrice = kylinOrderTicketEntitiesMapper.getRefundEntitiesPrice(//已退
                            orderTicketsId,
                            KylinTableStatusConst.ORDER_REFUND_STATUS_CANCEL,
                            ticketEntityIds.get(i)
                    );
                    if (null == refundedPrice) {
                        refundedPrice = 0.0;
                    }
                    if (price > (onePrice - refundedPrice)) {
                        return ResponseDto.failure("超过可退款金额");
                    }
                }
            }
            realRefundPriceList = entitiesPrice;
        } else { // 批量 计算入场人订单可退金额
            List<String> ticketEntityIdsNew = new ArrayList<>();
            for (int i = 0; i <= ticketEntityIds.size() - 1; i++) {
                Double refundedPrice = kylinOrderTicketEntitiesMapper.getRefundEntitiesPrice(//已退 包含退款中
                        orderTicketsId,
                        KylinTableStatusConst.ORDER_REFUND_STATUS_CANCEL,
                        ticketEntityIds.get(i)
                );
                if (null == refundedPrice) {
                    refundedPrice = 0.0;
                }
                Double price = onePrice - refundedPrice; // 计算可退金额
                if (price > 0) {
                    realRefundPriceList.add(price);
                    ticketEntityIdsNew.add(ticketEntityIds.get(i));
                }
            }
            // 剔除掉已经退完的入场人订单 可能包含正在退的
            ticketEntityIds = ticketEntityIdsNew;
        }

        boolean res = kylinRefundsStatusServiceImpl.orderTicketRefunding(
                refundApplyParam, orderInfo, orderTicketsId,
                RefundPriceExpress,
                ticketEntityIds, realRefundPriceList
        );

//        KylinOrderTicketVo vo = new KylinOrderTicketVo();
        HashMap<String,Object> map = new HashMap<>();
        map.put("isHaveRefundDetails",1);
        BasicDBObject obj = new BasicDBObject("$set", mongoConverter.convertToMongoType(map));
        mongoTemplate.getCollection(KylinOrderTicketVo.class.getSimpleName()).updateOne(
                Query.query(Criteria.where("orderTicketsId").is(orderTicketsId)).getQueryObject(),
                obj);

        if (res) {
            return ResponseDto.success();
        } else {
            return ResponseDto.failure("操作失败");
        }
    }

    /*public Boolean refundPrice(List<String> ticketEntityIds, List<BigDecimal> entitiesPrice, String orderTicketsId) {
        // 该订单的所有退款单
        List<KylinOrderRefunds> refundList = kylinOrderRefundsMapper.selectList(
                new QueryWrapper<KylinOrderRefunds>()
                        .eq("order_tickets_id", orderTicketsId)
                        .ne("status", KylinTableStatusConst.ORDER_REFUND_STATUS_CANCEL)
        );
    }*/

    public ResponseDto refundCheckStatus(RefundApplyParam refundApplyParam) {
        String type = refundApplyParam.getType();
        List<String> orderRefundsIdList = refundApplyParam.getIds();
        if (orderRefundsIdList.size() <= 0) {
            return ResponseDto.failure("无退款订单");
        }
        Integer[] orderRefundStatus = null;
        Integer orderType = null;
        switch (type) {
            default:
                return ResponseDto.failure("type类型错误");
            case "cancel":
                orderRefundStatus = new Integer[]{KylinTableStatusConst.ORDER_REFUND_STATUS_APPLY, KylinTableStatusConst.ORDER_REFUND_STATUS_REJECT, KylinTableStatusConst.ORDER_REFUND_STATUS_REFUSE};
                orderType = KylinTableStatusConst.ORDER_REFUND_TYPE_APPLY;
                break;
            case "reapply":
                orderRefundStatus = new Integer[]{KylinTableStatusConst.ORDER_REFUND_STATUS_REJECT, KylinTableStatusConst.ORDER_REFUND_STATUS_REFUSE};
                break;
            case "approved":
                orderRefundStatus = new Integer[]{KylinTableStatusConst.ORDER_REFUND_STATUS_APPLY, KylinTableStatusConst.ORDER_REFUND_STATUS_REJECT, KylinTableStatusConst.ORDER_REFUND_STATUS_REFUSE};
                break;
            case "reject":
                orderRefundStatus = new Integer[]{KylinTableStatusConst.ORDER_REFUND_STATUS_APPLY, KylinTableStatusConst.ORDER_REFUND_STATUS_APPROVED, KylinTableStatusConst.ORDER_REFUND_STATUS_REFUSE};
                break;
            case "unfilled":
                orderRefundStatus = new Integer[]{KylinTableStatusConst.ORDER_REFUND_STATUS_APPROVED, KylinTableStatusConst.ORDER_REFUND_STATUS_ERROR};
                break;
            case "refuse":
                orderRefundStatus = new Integer[]{KylinTableStatusConst.ORDER_REFUND_STATUS_APPROVED, KylinTableStatusConst.ORDER_REFUND_STATUS_ERROR};
                break;
        }

        QueryWrapper<KylinOrderRefunds> listQuery = new QueryWrapper();
        listQuery.in("order_refunds_id", orderRefundsIdList).in("status", orderRefundStatus);
        if (null != orderType) {
            listQuery.eq("type", orderType);
        }
        List<KylinOrderRefunds> refundList = kylinOrderRefundsMapper.selectList(listQuery);
        if (orderRefundsIdList.size() != refundList.size()) {
            return ResponseDto.failure("当前状态不允许操作");
        }

        boolean res = false;
        String reject = refundApplyParam.getReject();
        String refuse = refundApplyParam.getRefuse();
        switch (type) {
            case "cancel":
                res = kylinRefundsStatusServiceImpl.orderTicketRefundCancel(refundList);
                break;
            case "reapply":
                res = kylinRefundsStatusServiceImpl.orderRefundChangeStatus(refundList, type, "", "");
                break;
            case "approved":
                res = kylinRefundsStatusServiceImpl.orderRefundChangeStatus(refundList, type, reject, "");
                break;
            case "reject":
                res = kylinRefundsStatusServiceImpl.orderRefundChangeStatus(refundList, type, reject, "");
                break;
            case "unfilled":
                res = kylinRefundsStatusServiceImpl.orderTicketRefundUnfilled(refundList, refuse);
                break;
            case "refuse":
                res = kylinRefundsStatusServiceImpl.orderRefundChangeStatus(refundList, type, "", refuse);
                break;
        }
        if (res) {
            return ResponseDto.success();
        } else {
            return ResponseDto.failure("操作失败");
        }
    }

    public ResponseDto refundCompleted(RefundApplyParam refundApplyParam) {
        List<String> orderRefundsIdList = refundApplyParam.getIds();

        KylinOrderRefunds refundInfo = kylinOrderRefundsMapper.selectOne(
                new QueryWrapper<KylinOrderRefunds>()
                        .in("order_refunds_id", orderRefundsIdList)
        );
        if (null == refundInfo) {
            return ResponseDto.failure("退款订单查询失败");
        }
        if (refundInfo.getStatus() == KylinTableStatusConst.ORDER_REFUND_STATUS_CANCEL) {
            return ResponseDto.failure("退款订单已取消");
        }
        if (refundInfo.getStatus() == KylinTableStatusConst.ORDER_REFUND_STATUS_REFUNDED) {
            return ResponseDto.failure("退款订单已完成");
        }

        RefundCallbackParam refundCallbackParam = new RefundCallbackParam();
        refundCallbackParam.setRefund_code(refundInfo.getRefundCode());
        double refundPrice = refundInfo.getPrice().doubleValue() + refundInfo.getPriceExpress().doubleValue();
        refundCallbackParam.setRefund_price(refundPrice);
        refundCallbackParam.setRefund_type("UNKNOWN");
        refundCallbackParam.setRefund_id(refundInfo.getRefundId());
        refundCallbackParam.setRefund_at(DateUtils.dateTimeNow());
        refundCallbackParam.setRefund_error(refundInfo.getRefundError());
        boolean res = kylinRefundsStatusServiceImpl.orderTicketRefunded(refundCallbackParam, refundInfo);
        if (res) {
            String authId = ShiroUtils.getUserId().toString();
            String authName = ShiroUtils.getLoginName();
            KylinOrderRefunds kylinOrderRefunds = new KylinOrderRefunds();
            if (refundInfo.getAuditorId().isEmpty()) {
                kylinOrderRefunds.setAuditorAt(LocalDateTime.now());
                kylinOrderRefunds.setAuditorId(authId);
                kylinOrderRefunds.setAuditorName(authName);
            }
            kylinOrderRefunds.setExecutorAt(LocalDateTime.now());
            kylinOrderRefunds.setExecutorId(authId);
            kylinOrderRefunds.setExecutorName(authName);
            kylinOrderRefundsMapper.update(
                    kylinOrderRefunds,
                    new UpdateWrapper<KylinOrderRefunds>().in("order_refunds_id", refundInfo.getOrderRefundsId())
            );
            return ResponseDto.success();
        } else {
            return ResponseDto.failure("fail");
        }
    }

    public KylinOrderRefundsVo detail(String orderRefundId) {
        KylinOrderRefunds data = kylinOrderRefundsMapper.selectOne(
                new QueryWrapper<KylinOrderRefunds>()
                        .eq("order_refunds_id", orderRefundId)
        );

        List<KylinOrderRefundEntities> refundEntities = kylinOrderRefundsEntitiesMapper.selectList(
                new QueryWrapper<KylinOrderRefundEntities>().eq("order_refunds_id", orderRefundId)
        );
        List<String> orderTicketEntitiesIdsArr = refundEntities.stream().map(KylinOrderTicketEntities -> KylinOrderTicketEntities.getOrderTicketEntitiesId()).collect(Collectors.toList());
        List<KylinOrderRefundsEntitiesDao> entities = kylinOrderTicketEntitiesMapper.selectEntitiesList(orderTicketEntitiesIdsArr);
        for (KylinOrderRefundsEntitiesDao item : entities) {
            item.setPaymentName(item.getIsPayment());
        }

        KylinOrderRefundsVo kylinOrderRefundsVo = new KylinOrderRefundsVo();
        kylinOrderRefundsVo.setStatusName(data.getStatus());
        kylinOrderRefundsVo.setEntities(entities);
        BeanUtils.copyProperties(data, kylinOrderRefundsVo);

        return kylinOrderRefundsVo;
    }

    public PageInfo<OrderRefundDao> refundList(RefundSearchParam refundSearchParam) {
        PageInfo<OrderRefundDao> pageInfoTmp = null;
        try {
            PageHelper.startPage(refundSearchParam.getPageNum(), refundSearchParam.getPageSize());
            List<OrderRefundDao> voList = kylinOrderRefundsMapper.misRefundList(BeanUtil.convertBeanToMap(refundSearchParam));
            for (OrderRefundDao item : voList) {
                item.setStatusName(item.getStatus());
            }
            pageInfoTmp = new PageInfo(voList);
        } catch (Exception e) {
            return new PageInfo(new ArrayList());
        }
        return pageInfoTmp;
    }

    public RefundOrderDetailsVo orderDetails(String orderCode) {
        // 订单
        KylinOrderTickets kylinOrderTickets = kylinOrderTicketsMapper.selectOne(
                new QueryWrapper<KylinOrderTickets>().eq("order_code", orderCode)
        );
        // 传的快递费不能大于实际的快递费=（支付的快递费-已退的快递费）
        Double refundPriceExpressSum = kylinOrderRefundsMapper.RefundPriceExpressSum(// 已退快递费
                kylinOrderTickets.getOrderTicketsId(),
                KylinTableStatusConst.ORDER_REFUND_STATUS_CANCEL
        );
        if (null == refundPriceExpressSum) {
            refundPriceExpressSum = 0.0;
        }
        Double RefundPriceExpress = kylinOrderTickets.getPriceExpress().doubleValue() - refundPriceExpressSum;
        kylinOrderTickets.setPriceExpress(BigDecimal.valueOf(RefundPriceExpress));

        // 订单关联 获取演出id
        KylinOrderTicketRelations kylinOrderTicketRelations = kylinOrderTicketRelationsMapper.selectOne(
                new QueryWrapper<KylinOrderTicketRelations>().eq("order_id", kylinOrderTickets.getOrderTicketsId())
        );

        // 演出
        KylinPerformances kylinPerformances = kylinPerformancesMapper.selectOne(
                new QueryWrapper<KylinPerformances>().eq("performances_id", kylinOrderTicketRelations.getPerformanceId())
        );

        List<KylinOrderTicketEntitiesDao> entitiesListTemp = kylinOrderTicketEntitiesMapper.getRefundEntitiesList(kylinOrderTickets.getOrderTicketsId());
        List<KylinOrderTicketEntitiesDao> entitiesList = new ArrayList<>();
        if (entitiesListTemp.size() > 0) {
            double priceActual = kylinOrderTickets.getPriceActual().doubleValue();
            double priceExpress = kylinOrderTickets.getPriceExpress().doubleValue();
            int allEntitiesCount = kylinOrderTicketEntitiesMapper.selectCount(// 总入场人数量 排出未付款的 用来计算单入场人的价格
                    new QueryWrapper<KylinOrderTicketEntities>()
                            .eq("order_id", kylinOrderTickets.getOrderTicketsId())
                            .ne("is_payment", KylinTableStatusConst.ENTITIES_IS_PAYMENT0)
            );
            double onePrice = (priceActual - priceExpress) / allEntitiesCount;//单价
            for (KylinOrderTicketEntitiesDao entities : entitiesListTemp) {
                Double refundedPrice = kylinOrderTicketEntitiesMapper.getRefundEntitiesPrice(//已退 包含退款中
                        kylinOrderTickets.getOrderTicketsId(),
                        KylinTableStatusConst.ORDER_REFUND_STATUS_CANCEL,
                        entities.getOrderTicketEntitiesId()
                );
                double canRefundedPrice;
                if (null != refundedPrice) {
                    canRefundedPrice = onePrice - refundedPrice;
                } else {
                    canRefundedPrice = onePrice;
                }
                entities.setCanRefundedPrice(BigDecimal.valueOf(canRefundedPrice));
                if (canRefundedPrice > 0) { // 退款中但是可退款金额为0不展示
                    entitiesList.add(entities);
                }
            }
        }

        KylinOrderTicketVo kylinOrderTicketVo = new KylinOrderTicketVo();
        BeanUtil.copy(kylinOrderTickets, kylinOrderTicketVo);

        KylinPerformanceVo performanceVo = new KylinPerformanceVo();
        BeanUtil.copy(kylinPerformances, performanceVo);

        RefundOrderDetailsVo refundOrderDetailsVo = new RefundOrderDetailsVo();
        refundOrderDetailsVo.setOrderTicketVo(kylinOrderTicketVo);
        refundOrderDetailsVo.setKylinPerformanceVo(performanceVo);
        refundOrderDetailsVo.setKylinOrderTicketEntitiesDao(entitiesList);

        return refundOrderDetailsVo;
    }
}
