package com.zksy.dispatch.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.dispatch.domain.DisEventTrack;
import com.zksy.dispatch.domain.DisEventTrackLog;
import com.zksy.dispatch.dto.EventTrackPageInDTO;
import com.zksy.dispatch.mapper.DisEventTrackLogMapper;
import com.zksy.dispatch.mapper.DisEventTrackMapper;
import com.zksy.dispatch.service.DisEventTrackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class DisEventTrackServiceImpl extends ServiceImpl<DisEventTrackMapper, DisEventTrack>
        implements DisEventTrackService {

    @Autowired
    private DisEventTrackLogMapper trackLogMapper;

    @Override
    public Page<DisEventTrack> findByPage(EventTrackPageInDTO dto) {
        Page<DisEventTrack> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<DisEventTrack> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(dto.getEventNo()), DisEventTrack::getEventNo, dto.getEventNo());
        wrapper.eq(StrUtil.isNotBlank(dto.getCategory()), DisEventTrack::getCategory, dto.getCategory());
        wrapper.eq(StrUtil.isNotBlank(dto.getLevel()), DisEventTrack::getLevel, dto.getLevel());
        wrapper.eq(StrUtil.isNotBlank(dto.getStatus()), DisEventTrack::getStatus, dto.getStatus());
        wrapper.eq(StrUtil.isNotBlank(dto.getHandler()), DisEventTrack::getHandler, dto.getHandler());
        if (StrUtil.isNotBlank(dto.getBeginTime()) && StrUtil.isNotBlank(dto.getEndTime())) {
            wrapper.between(DisEventTrack::getCreateTime,
                    LocalDateTime.parse(dto.getBeginTime()),
                    LocalDateTime.parse(dto.getEndTime()));
        }
        wrapper.orderByDesc(DisEventTrack::getCreateTime);
        return this.page(page, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addTrackLog(DisEventTrackLog log) {
        if (log.getTrackId() == null) {
            return false;
        }
        DisEventTrack track = this.getById(log.getTrackId());
        if (track == null) {
            return false;
        }
        log.setEventNo(track.getEventNo());
        log.setActionTime(LocalDateTime.now());
        log.setCreateTime(LocalDateTime.now());
        trackLogMapper.insert(log);

        track.setLastVisitTime(LocalDateTime.now());
        track.setUpdateTime(LocalDateTime.now());
        this.updateById(track);
        return true;
    }

    @Override
    public boolean release(Long id) {
        DisEventTrack track = this.getById(id);
        if (track == null) {
            log.warn("跟踪记录不存在: id={}", id);
            return false;
        }
        track.setStatus("已解除");
        track.setUpdateTime(LocalDateTime.now());
        return this.updateById(track);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean close(Long id) {
        DisEventTrack track = this.getById(id);
        if (track == null) {
            log.warn("跟踪记录不存在: id={}", id);
            return false;
        }
        track.setStatus("已关闭");
        track.setUpdateTime(LocalDateTime.now());
        this.updateById(track);

        DisEventTrackLog logEntity = new DisEventTrackLog();
        logEntity.setTrackId(track.getId());
        logEntity.setEventNo(track.getEventNo());
        logEntity.setTitle("事件关闭");
        logEntity.setContent("跟踪已关闭");
        logEntity.setType("success");
        logEntity.setActionTime(LocalDateTime.now());
        logEntity.setCreateTime(LocalDateTime.now());
        trackLogMapper.insert(logEntity);
        return true;
    }

    @Override
    public List<DisEventTrackLog> getTrackLogs(Long trackId) {
        LambdaQueryWrapper<DisEventTrackLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DisEventTrackLog::getTrackId, trackId);
        wrapper.orderByDesc(DisEventTrackLog::getActionTime);
        return trackLogMapper.selectList(wrapper);
    }
}
