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.DisEventUrge;
import com.zksy.dispatch.domain.DisEventUrgeDetail;
import com.zksy.dispatch.dto.EventUrgePageInDTO;
import com.zksy.dispatch.mapper.DisEventUrgeDetailMapper;
import com.zksy.dispatch.mapper.DisEventUrgeMapper;
import com.zksy.dispatch.service.DisEventUrgeService;
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 DisEventUrgeServiceImpl extends ServiceImpl<DisEventUrgeMapper, DisEventUrge>
        implements DisEventUrgeService {

    @Autowired
    private DisEventUrgeDetailMapper urgeDetailMapper;

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean doUrge(DisEventUrgeDetail detail) {
        if (detail.getUrgeId() == null) {
            log.warn("关联催办ID为空");
            return false;
        }
        DisEventUrge urge = this.getById(detail.getUrgeId());
        if (urge == null) {
            log.warn("催办记录不存在: id={}", detail.getUrgeId());
            return false;
        }
        urge.setUrgeCount(urge.getUrgeCount() == null ? 1 : urge.getUrgeCount() + 1);
        urge.setLastUrgeTime(LocalDateTime.now());
        urge.setStatus("催办中");
        urge.setUpdateTime(LocalDateTime.now());
        this.updateById(urge);

        detail.setEventNo(urge.getEventNo());
        detail.setUrgeTime(LocalDateTime.now());
        detail.setCreateTime(LocalDateTime.now());
        urgeDetailMapper.insert(detail);
        return true;
    }

    @Override
    public boolean closeUrge(Long id) {
        DisEventUrge urge = this.getById(id);
        if (urge == null) {
            log.warn("催办记录不存在: id={}", id);
            return false;
        }
        urge.setStatus("已完成");
        urge.setUpdateTime(LocalDateTime.now());
        return this.updateById(urge);
    }

    @Override
    public List<DisEventUrgeDetail> getUrgeDetails(Long urgeId) {
        LambdaQueryWrapper<DisEventUrgeDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DisEventUrgeDetail::getUrgeId, urgeId);
        wrapper.orderByDesc(DisEventUrgeDetail::getUrgeTime);
        return urgeDetailMapper.selectList(wrapper);
    }
}
