package com.dsk.system.service.impl;

import cn.hutool.core.bean.BeanException;
import cn.hutool.core.util.ObjectUtil;
import com.dsk.system.domain.business.BusinessBacklog;
import com.dsk.common.exception.base.BaseException;
import com.dsk.common.utils.DateUtils;
import com.dsk.system.domain.business.dto.BusinessBacklogListDto;
import com.dsk.system.domain.business.vo.BusinessBacklogListVo;
import com.dsk.system.mapper.BusinessBacklogMapper;
import com.dsk.system.service.IBusinessBacklogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

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

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

    /**
     * 查询项目工作待办列表
     *
     * @param dto 项目工作待办
     * @return 项目工作待办
     */
    @Override
    public List<BusinessBacklogListVo> selectBusinessBacklogList(BusinessBacklogListDto dto) {
        if (ObjectUtil.isEmpty(dto.getBusinessId())) {
            throw new BeanException("项目id不能为空！");
        }
        if (ObjectUtil.isEmpty(dto.getState())) {
            throw new BeanException("工作代办状态不能为空！");
        }
        switch (dto.getState()){
            case 0:
                dto.setStartTime(new Date());
                break;
            case 1:
                break;
            case 2:
                dto.setState(0);
                dto.setEndTime(new Date());
                break;
            default:
                throw new BeanException("工作代办状态参数错误！");
        }
        return businessBacklogMapper.selectBusinessBacklogList(dto);
    }

    /**
     * 新增项目工作待办
     *
     * @param businessBacklog 项目工作待办
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBusinessBacklog(BusinessBacklog businessBacklog) {
        if(ObjectUtil.isNotEmpty(businessBacklog.getDueTime())){
            if (businessBacklog.getDueTime().before(new Date())) throw new BaseException("到期时间必须大于当前时间");
        }
        return businessBacklogMapper.insertBusinessBacklog(businessBacklog);
    }

    /**
     * 修改项目工作待办
     *
     * @param businessBacklog 项目工作待办
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBusinessBacklog(BusinessBacklog businessBacklog) {
        if (ObjectUtil.isEmpty(businessBacklog.getId())) {
            throw new BeanException("id不能为空！");
        }
        if (ObjectUtil.isEmpty(businessBacklog.getState())) {
            throw new BeanException("状态不能为空！");
        }
        switch (businessBacklog.getState()) {
            case 0:
                businessBacklog.setFinishTime(null);
                break;
            case 1:
                businessBacklog.setFinishTime(new Date());
                break;
            default:
                throw new BeanException("状态参数错误！");
        }
        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);
    }

    @Override
    public Integer overdueCount(Integer businessId) {
        return businessBacklogMapper.overdueCount(businessId);
    }
}
