package com.njcn.relational.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.relational.mapper.SyncTableLogsMapper;
import com.njcn.relational.pojo.po.SyncTableLogs;
import com.njcn.relational.service.SyncTableLogsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class SyncTableLogsServiceImpl extends ServiceImpl<SyncTableLogsMapper, SyncTableLogs>
        implements SyncTableLogsService {

    @Override
    public IPage<SyncTableLogs> pageQuery(Integer pageNum, Integer pageSize, LocalDate timeId, Integer result) {
        Page<SyncTableLogs> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SyncTableLogs> wrapper = new LambdaQueryWrapper<>();

        if (timeId != null) {
            wrapper.eq(SyncTableLogs::getTimeId, timeId);
        }
        if (result != null) {
            wrapper.eq(SyncTableLogs::getResult, result);
        }

        // 按创建时间倒序
        wrapper.orderByDesc(SyncTableLogs::getCreateTime);

        return this.page(page, wrapper);
    }

    @Override
    public List<SyncTableLogs> listByTimeId(LocalDate timeId) {
        LambdaQueryWrapper<SyncTableLogs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SyncTableLogs::getTimeId, timeId)
                .orderByDesc(SyncTableLogs::getCreateTime);
        return this.list(wrapper);
    }

    @Override
    public List<SyncTableLogs> listByTimeIdAndResult(LocalDate timeId, Integer result) {
        LambdaQueryWrapper<SyncTableLogs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SyncTableLogs::getTimeId, timeId)
                .eq(SyncTableLogs::getResult, result)
                .orderByDesc(SyncTableLogs::getCreateTime);
        return this.list(wrapper);
    }

    @Override
    public boolean existsByTimeId(LocalDate timeId) {
        LambdaQueryWrapper<SyncTableLogs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SyncTableLogs::getTimeId, timeId);
        return this.count(wrapper) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchInsert(List<SyncTableLogs> logs) {
        if (logs == null || logs.isEmpty()) {
            return false;
        }
        // 设置创建时间
        logs.forEach(log -> {
            if (log.getCreateTime() == null) {
                log.setCreateTime(LocalDateTime.now());
            }
        });
        return this.saveBatch(logs);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByTimeId(LocalDate timeId) {
        LambdaQueryWrapper<SyncTableLogs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SyncTableLogs::getTimeId, timeId);
        return this.remove(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByTimeIdAndResult(LocalDate timeId, Integer result) {
        LambdaQueryWrapper<SyncTableLogs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SyncTableLogs::getTimeId, timeId)
                .eq(SyncTableLogs::getResult, result);
        return this.remove(wrapper);
    }

    @Override
    public int countByTimeIdAndResult(LocalDate timeId, Integer result) {
        return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateLog(SyncTableLogs log) {
        if (log == null) {
            return false;
        }

        // 先查询是否存在
        LambdaQueryWrapper<SyncTableLogs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SyncTableLogs::getTimeId, log.getTimeId())
                .eq(SyncTableLogs::getResult, log.getResult());

        SyncTableLogs exist = this.getOne(wrapper);

        if (exist == null) {
            // 不存在则新增
            if (log.getCreateTime() == null) {
                log.setCreateTime(LocalDateTime.now());
            }
            return this.save(log);
        } else {
            // 存在则更新（保留原创建时间）
            log.setCreateTime(exist.getCreateTime());
            return this.update(log, wrapper);
        }
    }
}