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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.client.admin.common.utils.StringUtils;
import com.liquidnet.commons.lang.util.BeanUtil;
import com.liquidnet.commons.lang.util.DateUtil;
import com.liquidnet.commons.lang.util.StringUtil;
import com.liquidnet.service.kylin.constant.KylinTableStatusConst;
import com.liquidnet.service.kylin.dao.OrderTicketsListDao;
import com.liquidnet.service.kylin.dto.param.admin.OrderListReq;
import com.liquidnet.service.kylin.dto.vo.admin.OrderDetailsAdminVo;
import com.liquidnet.service.kylin.dto.vo.middle.KylinTicketVo;
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.entity.*;
import com.liquidnet.service.kylin.mapper.*;
import com.liquidnet.service.kylin.service.IKylinOrderTicketStatusService;
import com.liquidnet.service.kylin.service.admin.IKylinOrderTicketsAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author AnJiabin <anjiabin@zhengzai.tv>
 * @version V1.0
 * @Description: TODO
 * @class: KylinOrderTicketsAdminServiceImpl
 * @Package com.liquidnet.client.admin.zhengzai.kylin.service.impl
 * @Copyright: LightNet @ Copyright (c) 2021
 * @date 2021/6/6 12:01
 */
@Slf4j
@Service
public class KylinOrderTicketsAdminServiceImpl extends ServiceImpl<KylinOrderTicketsMapper, KylinOrderTickets> implements IKylinOrderTicketsAdminService {

    @Autowired
    private KylinOrderTicketsMapper kylinOrderTicketsMapper;

    @Autowired
    private KylinOrderTicketRelationsMapper kylinOrderTicketRelationsMapper;

    @Autowired
    private KylinOrderTicketStatusMapper kylinOrderTicketStatusMapper;

    @Autowired
    private KylinPerformancesMapper kylinPerformancesMapper;

    @Autowired
    private KylinTicketsMapper kylinTicketsMapper;

    @Autowired
    private KylinOrderTicketEntitiesMapper kylinOrderTicketEntitiesMapper;

    @Override
    public List<OrderTicketsListDao> orderList(OrderListReq orderListReq) {
        Map<String, Object> reqParamMap = new HashMap<>();
        try {
            reqParamMap = BeanUtil.convertBeanToMap(orderListReq,false);
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        List<OrderTicketsListDao> list = kylinOrderTicketsMapper.orderList(reqParamMap);
        return list;
    }

    @Override
    public OrderDetailsAdminVo orderDetails(String orderId) {
        OrderDetailsAdminVo vo = new OrderDetailsAdminVo();
        try {
            //查询条件
            LambdaQueryWrapper<KylinOrderTickets> wrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotNull(orderId)) {
                wrapper.eq(KylinOrderTickets::getOrderTicketsId,orderId);
            }
            KylinOrderTicketVo orderTicketVo = new KylinOrderTicketVo();
            KylinOrderTickets kylinOrderTickets = kylinOrderTicketsMapper.selectOne(wrapper);
            //获取订单关联关系
            LambdaQueryWrapper<KylinOrderTicketRelations> relWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotNull(orderId)) {
                relWrapper.eq(KylinOrderTicketRelations::getOrderId,orderId);
            }
            KylinOrderTicketRelations  kylinOrderTicketRelations = kylinOrderTicketRelationsMapper.selectOne(relWrapper);
            BeanUtil.copy(kylinOrderTickets,orderTicketVo);
            orderTicketVo.setPerformanceId(kylinOrderTicketRelations.getPerformanceId());
            orderTicketVo.setTicketId(kylinOrderTicketRelations.getTicketId());
            //获取订单状态
            LambdaQueryWrapper<KylinOrderTicketStatus> orderStatusWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotNull(orderId)) {
                orderStatusWrapper.eq(KylinOrderTicketStatus::getOrderId,orderId);
            }
            KylinOrderTicketStatus  kylinOrderTicketStatus = kylinOrderTicketStatusMapper.selectOne(orderStatusWrapper);
            BeanUtil.copy(kylinOrderTickets,orderTicketVo);
            orderTicketVo.setPerformanceId(kylinOrderTicketRelations.getPerformanceId());
            orderTicketVo.setTicketId(kylinOrderTicketRelations.getTicketId());
            orderTicketVo.setStatus(kylinOrderTicketStatus.getStatus());
            orderTicketVo.setPayStatus(kylinOrderTicketStatus.getPayStatus());
            orderTicketVo.setTransferStatus(kylinOrderTicketStatus.getTransferStatus());
            orderTicketVo.setCreatedAt("");
            orderTicketVo.setTimePay("");
            orderTicketVo.setUseStart("");
            if(StringUtils.isNotNull(kylinOrderTickets.getCreatedAt())){
                orderTicketVo.setCreatedAt(DateUtil.format(kylinOrderTickets.getCreatedAt(),DateUtil.Formatter.yyyyMMddHHmmss));
            }
            if(StringUtils.isNotNull(kylinOrderTickets.getTimePay())){
                orderTicketVo.setTimePay(kylinOrderTickets.getTimePay());
            }

            if (null != orderTicketVo) {
//                Expresses expresses = expressesMapper.selectOne(new UpdateWrapper<Expresses>().eq("order_id", orderId).eq("order_type", "order_ticket"));
                //获取演出信息
                LambdaQueryWrapper<KylinPerformances> perWrapper = new LambdaQueryWrapper<>();
                if (StringUtils.isNotNull(orderTicketVo.getPerformanceId())) {
                    perWrapper.eq(KylinPerformances::getPerformancesId,orderTicketVo.getPerformanceId());
                }
                KylinPerformances kylinPerformances = kylinPerformancesMapper.selectOne(perWrapper);
                KylinPerformanceVo performanceVo = new KylinPerformanceVo();
                BeanUtil.copy(kylinPerformances,performanceVo);
                //获取票种信息
                LambdaQueryWrapper<KylinTickets> ticketsWrapper = new LambdaQueryWrapper<>();
                if (StringUtils.isNotNull(orderTicketVo.getTicketId())) {
                    ticketsWrapper.eq(KylinTickets::getTicketsId,orderTicketVo.getTicketId());
                }
                KylinTickets kylinTickets = kylinTicketsMapper.selectOne(ticketsWrapper);
                KylinTicketVo ticketVo = new KylinTicketVo();
                BeanUtil.copy(kylinTickets,ticketVo);
                if(StringUtils.isNotNull(kylinTickets.getUseStart())){
                    orderTicketVo.setUseStart(DateUtil.format(kylinTickets.getUseStart(),DateUtil.Formatter.yyyyMMddHHmmss));
                }

                //获取订单持票实体信息
                LambdaQueryWrapper<KylinOrderTicketEntities> ticketsEntitiesWrapper = new LambdaQueryWrapper<>();
                if (StringUtils.isNotNull(orderTicketVo.getOrderTicketsId())) {
                    ticketsEntitiesWrapper.eq(KylinOrderTicketEntities::getOrderId,orderTicketVo.getOrderTicketsId());
                }
                List<KylinOrderTicketEntitiesVo> kylinOrderTicketEntitiesVoList = new ArrayList<>();
                List<KylinOrderTicketEntities> ticketEntitiesList = kylinOrderTicketEntitiesMapper.selectList(ticketsEntitiesWrapper);
                for (KylinOrderTicketEntities kylinOrderTicketEntities : ticketEntitiesList) {
                    KylinOrderTicketEntitiesVo ticketEntitiesVo = new KylinOrderTicketEntitiesVo();
                    BeanUtil.copy(kylinOrderTicketEntities,ticketEntitiesVo);
                    kylinOrderTicketEntitiesVoList.add(ticketEntitiesVo);
                }

                // 数据脱敏
                for (KylinOrderTicketEntitiesVo item : kylinOrderTicketEntitiesVoList) {
                    if (item.getEnterIdCode().length() == 18) {
                        item.setEnterIdCode(item.getEnterIdCode().substring(0, 3) + "*************" + item.getEnterIdCode().substring(16));
                    }
                    if (item.getEnterMobile().length() == 11) {
                        item.setEnterMobile(item.getEnterMobile().substring(0, 3) + "****" + item.getEnterMobile().substring(7));
                    }
                }
                //计算 倒计时
                if (orderTicketVo.getStatus() == KylinTableStatusConst.ORDER_STATUS0) {
                    vo.setRestTime(DateUtil.intervalSeconds(
                            DateUtil.parse(orderTicketVo.getOverdueAt(), "yyyy-MM-dd HH:mm:ss"),
                            DateUtil.parse(DateUtil.getNowTime(), "yyyy-MM-dd HH:mm:ss")
                    ));
                } else {
                    vo.setRestTime(0L);
                }
                //快递
                vo.setExpress_number("");
                vo.setExpress_company("");
                vo.setKuaidi_status(-2);
                vo.setTicketsVo(ticketVo);
                vo.setOrderTicketEntitiesVo(kylinOrderTicketEntitiesVoList);
                vo.setPerformanceVo(performanceVo);
                vo.setOrderTicketVo(orderTicketVo);
            }
            return vo;
        } catch (Exception e) {
            e.printStackTrace();
            return vo;
        }
    }
}
