package com.njzscloud.common.sichen.service;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.core.ex.Exceptions;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.common.sichen.contant.TaskLogLevel;
import com.njzscloud.common.sichen.mapper.TaskExecuteLogMapper;
import com.njzscloud.common.sichen.pojo.entity.TaskExecuteLogEntity;
import com.njzscloud.common.sichen.pojo.param.SearchTaskExecuteLogParam;
import org.springframework.transaction.annotation.Transactional;

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

public class TaskExecuteLogService extends ServiceImpl<TaskExecuteLogMapper, TaskExecuteLogEntity> implements IService<TaskExecuteLogEntity> {

    /**
     * 新增
     */
    public void add(TaskExecuteLogEntity taskExecuteLogEntity) {
        this.save(taskExecuteLogEntity);
    }

    /**
     * 修改
     */
    public void modify(TaskExecuteLogEntity taskExecuteLogEntity) {
        this.updateById(taskExecuteLogEntity);
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     */
    public TaskExecuteLogEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     */
    public PageResult<TaskExecuteLogEntity> paging(PageParam pageParam, SearchTaskExecuteLogParam searchTaskExecuteLogParam) {
        Long scheduleId = searchTaskExecuteLogParam.getScheduleId();
        TaskLogLevel logLevel = searchTaskExecuteLogParam.getLogLevel();
        LocalDateTime startLogTime = searchTaskExecuteLogParam.getStartLogTime();
        LocalDateTime endLogTime = searchTaskExecuteLogParam.getEndLogTime();
        Assert.notNull(scheduleId, () -> Exceptions.clierr("未指调度记录"));
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<TaskExecuteLogEntity>lambdaQuery()
            .eq(TaskExecuteLogEntity::getScheduleId, scheduleId)
            .eq(logLevel != null, TaskExecuteLogEntity::getLogLevel, logLevel)
            .ge(startLogTime != null, TaskExecuteLogEntity::getLogTime, startLogTime)
            .le(endLogTime != null, TaskExecuteLogEntity::getLogTime, endLogTime)
        ));
    }
}
