package com.dsk.system.service.impl;

import com.dsk.common.core.domain.entity.BusinessBacklog;
import com.dsk.common.exception.base.BaseException;
import com.dsk.common.utils.DateUtils;
import com.dsk.system.mapper.BusinessBacklogMapper;
import com.dsk.system.service.IBusinessBacklogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 项目工作待办Service业务层处理
 *
 * @author lxl
 * @date 2023-05-17
 */
@Service
@Slf4j
public class BusinessBacklogServiceImpl implements IBusinessBacklogService
{
    @Autowired
    private BusinessBacklogMapper businessBacklogMapper;

    /**
     * 查询项目工作待办
     *
     * @param id 项目工作待办主键
     * @return 项目工作待办
     */
    @Override
    public BusinessBacklog selectBusinessBacklogById(Integer id)
    {
        return businessBacklogMapper.selectBusinessBacklogById(id);
    }

    /**
     * 查询项目工作待办列表
     *
     * @param businessBacklog 项目工作待办
     * @return 项目工作待办
     */
    @Override
    public List<BusinessBacklog> selectBusinessBacklogList(BusinessBacklog businessBacklog)
    {
        return businessBacklogMapper.selectBusinessBacklogList(businessBacklog);
    }

    /**
     * 新增项目工作待办
     *
     * @param businessBacklog 项目工作待办
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBusinessBacklog(BusinessBacklog businessBacklog)
    {
        if(businessBacklog.getFinishTime().before(new Date())) throw new BaseException("完成时间必须大于当前时间");
        businessBacklog.setCreateTime(DateUtils.getNowDate());
        //新增记录，状态默认 进行中
        businessBacklog.setState(1);
        return businessBacklogMapper.insertBusinessBacklog(businessBacklog);
    }

    /**
     * 修改项目工作待办
     *
     * @param businessBacklog 项目工作待办
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBusinessBacklog(BusinessBacklog businessBacklog)
    {
        //从完成状态修改到为完成，判断是否已逾期
        BusinessBacklog backlog = selectBusinessBacklogById(businessBacklog.getId());
        if (businessBacklog.getState() == 1 && backlog.getState() == 2 && backlog.getFinishTime().before(new Date())) {
            businessBacklog.setState(0);
            businessBacklog.setUpdateTime(new Date());
            return businessBacklogMapper.updateBusinessBacklog(businessBacklog);
        }
        businessBacklog.setUpdateTime(new Date());
        return businessBacklogMapper.updateBusinessBacklog(businessBacklog);
    }

    /**
     * 批量删除项目工作待办
     *
     * @param ids 需要删除的项目工作待办主键
     * @return 结果
     */
    @Override
    public int deleteBusinessBacklogByIds(Long[] ids)
    {
        return businessBacklogMapper.deleteBusinessBacklogByIds(ids);
    }

    /**
     * 删除项目工作待办信息
     *
     * @param id 项目工作待办主键
     * @return 结果
     */
    @Override
    public int deleteBusinessBacklogById(Long id)
    {
        return businessBacklogMapper.deleteBusinessBacklogById(id);
    }

    /**
     * 定时修改工作待办逾期状态
     */
    @Scheduled(cron = "0 0 0 * * ?")//每天凌晨0点执行一次
    public void judgeOverdue() {
        int updateBacklogState = businessBacklogMapper.updateBacklogState();
        if (updateBacklogState > 1) log.info("定时修改工作待办逾期状态");
    }
}
