package com.platform.service.custom;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.platform.entity.TmTaskLog;
import com.platform.enums.IdStarterEnum;
import com.platform.service.impl.TmTaskLogServiceImpl;
import com.platform.utils.UUidGenerator;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author abnerhou
 * @date 2020/9/3 15:27
 * @desciption
 */
@Service
@Slf4j
public class TaskLogService extends TmTaskLogServiceImpl {

    public int insertTaskLog(TmTaskLog tmTaskLog){
        if(null == tmTaskLog){
            log.info("task的入参为空，无法新增");
            return -1;
        }
        tmTaskLog.setCreateTime(LocalDateTime.now());
        tmTaskLog.setUpdateTime(LocalDateTime.now());
        tmTaskLog.setTaskId(UUidGenerator.generatorUuid(IdStarterEnum.DEFAULT_ID.getStarter()));
        tmTaskLog.setEnable(1);
        return  getBaseMapper().insert(tmTaskLog);
    }

    public int updateTaskLog(TmTaskLog tmTaskLog){
        if(null == tmTaskLog || StringUtils.isBlank(tmTaskLog.getTaskId())){
            return -1;
        }
        UpdateWrapper<TmTaskLog> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("task_id" , tmTaskLog.getTaskId());
        tmTaskLog.setUpdateTime(LocalDateTime.now());
        return getBaseMapper().update(tmTaskLog , updateWrapper);
    }

    public TmTaskLog getTaskById(String taskId){
        if(StringUtils.isBlank(taskId)){
            return null;
        }
        QueryWrapper<TmTaskLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id" , taskId);
        queryWrapper.eq("enable" , 1);
        queryWrapper.last("limit 1");
        return getBaseMapper().selectOne(queryWrapper);

    }

    public TmTaskLog getTaskByDate(LocalDate curDate){
        if(null == curDate){
            return null;
        }
        QueryWrapper<TmTaskLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_date" , curDate);
        queryWrapper.eq("enable" , 1);
        queryWrapper.last("limit 1");
        return getBaseMapper().selectOne(queryWrapper);

    }
}
