package com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database;

import java.util.*;

import cn.hutool.core.collection.CollUtil;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.event.EventDetailPageReqVO;
import com.cico.nest.lcdp.boot.core.qxsd.domain.event.ThirdEventStatus;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.EventDetailDO;
import com.cico.nest.lcdp.framework.common.pojo.PageResult;
import com.cico.nest.lcdp.framework.mybatis.core.mapper.BaseMapperX;
import com.cico.nest.lcdp.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;

/**
 * 事件详情 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface EventDetailMapper extends BaseMapperX<EventDetailDO> {

    default PageResult<EventDetailDO> selectPage(EventDetailPageReqVO reqVO) {
        Integer retainedTime = reqVO.getRetainedTime();
        LambdaQueryWrapperX<EventDetailDO> wrapper = new LambdaQueryWrapperX<EventDetailDO>()
                .eqIfPresent(EventDetailDO::getId, reqVO.getId())
                .geIfPresent(EventDetailDO::getEventStartTime,reqVO.getEventStartTimeSearchStart())
                .leIfPresent(EventDetailDO::getEventStartTime,reqVO.getEventStartTimeSearchEnd())
                .eqIfPresent(EventDetailDO::getTunnelId, reqVO.getTunnelId())
                .eqIfPresent(EventDetailDO::getBaseTunnelId, reqVO.getBaseTunnelId())
                .eqIfPresent(EventDetailDO::getHoleId, reqVO.getHoleId())
                .inIfPresent(EventDetailDO::getDirection, reqVO.getDirection())
//                .inIfPresent(EventDetailDO::getEventType, reqVO.getEventType())
                .likeIfPresent(EventDetailDO::getEventCarPlate, reqVO.getEventCarPlate())
                .eqIfPresent(EventDetailDO::getEventStatus,reqVO.getEventStatus());
        wrapper.ne(EventDetailDO::getIsRepeat,1);//只筛选不重复的事件
        // keyword2 桩号 + 隧道方向
        // 桩号 模糊查询 如 k215/k215+838 可以搜的到 215838 数据 参数不为空，去掉k去掉+ 左匹配
        Optional.ofNullable(reqVO.getKeyword2()).ifPresent(
                keyword2 -> wrapper.and(q -> q.likeRight(EventDetailDO::getMilestone,keyword2.replace("k","").replace("K","").replace("zk","").replace("ZK","").replace("+",""))
                        .or().like(EventDetailDO::getDirectionName, keyword2)
                ));

        if (CollectionUtils.isNotEmpty(reqVO.getEventType()) && reqVO.getEventType().size()>0){
            wrapper.inIfPresent(EventDetailDO::getEventType, reqVO.getEventType());
        }

        Optional.ofNullable(reqVO.getEventStatusSearch()).ifPresent(
                eventStatusSearch->{
                    if(eventStatusSearch==0){
                        wrapper.ne(EventDetailDO::getEventStatus,2);
                    }else if(eventStatusSearch==1){
                        wrapper.eq(EventDetailDO::getEventStatus,2);
                    }
                }
        );

        Optional.ofNullable(reqVO.getKeyword()).ifPresent(
                keyword -> wrapper.and(q -> q.like(EventDetailDO::getEventCarPlate, keyword)
                        .or().like(EventDetailDO::getSceneSituation, keyword)
        ));

        Optional.ofNullable(retainedTime).ifPresent(
                time -> wrapper.apply("(UNIX_TIMESTAMP(event_end_time) - UNIX_TIMESTAMP(event_start_time)) <= " + time * 60)
        );

        wrapper.orderByDesc(EventDetailDO::getEventStartTime);
        return selectPage(reqVO, wrapper);
    }

    default EventDetailDO selectByThirdPartyId(String thirdPartyId) {
        return selectOne(EventDetailDO::getThirdPartyId, thirdPartyId);
    }

    default List<EventDetailDO> selectByTunnelIdAndThirdEventStatusNotFinish(String tunnelId) {
        LambdaQueryWrapperX<EventDetailDO> wrapper = new LambdaQueryWrapperX<>();
        wrapper.ne(EventDetailDO::getThirdEventStatus, ThirdEventStatus.finished.type);
        wrapper.eq(EventDetailDO::getTunnelId,tunnelId);
        return selectList(wrapper);
    }

    /**
     * 将所有第三方事件设置为完成状态
     */
    default void updateThirdEventStatusFinish() {
        LambdaQueryWrapperX<EventDetailDO> wrapper = new LambdaQueryWrapperX<>();
        wrapper.ne(EventDetailDO::getThirdEventStatus, ThirdEventStatus.finished.type);
        List<EventDetailDO> eventDetailDOList = selectList(wrapper);
        if(CollUtil.isNotEmpty(eventDetailDOList)){
            for(EventDetailDO eventDetailDO:eventDetailDOList){
                eventDetailDO.setThirdEventStatus(ThirdEventStatus.finished.type);
            }
            updateBatch(eventDetailDOList);
        }
    }

    /**
     * 不同来源的事件判断重复
     * @param tunnelId
     * @param thirdEventId
     * @param eventType
     * @param startMile
     * @param endMill
     * @param eventStartTime
     * @param eventEndTime
     * @param source
     * @return
     */
    default List<EventDetailDO> getSameEvents(String tunnelId,String thirdEventId, Integer eventType,Integer startMile,Integer endMill,Date eventStartTime,Date eventEndTime,int source){
        LambdaQueryWrapperX<EventDetailDO> wrapper = new LambdaQueryWrapperX<EventDetailDO>()
                .eqIfPresent(EventDetailDO::getTunnelId, tunnelId)
                .eqIfPresent(EventDetailDO::getThirdPartyId, thirdEventId)
                .inIfPresent(EventDetailDO::getEventType, eventType)
                .geIfPresent(EventDetailDO::getMilestone,startMile)
                .leIfPresent(EventDetailDO::getMilestone,endMill)
                .geIfPresent(EventDetailDO::getEventStartTime,eventStartTime)
                .leIfPresent(EventDetailDO::getEventStartTime,eventEndTime)
                .neIfPresent(EventDetailDO::getEventSource,source);
        return selectList(wrapper);

    }
}
