package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.oa.domain.bo.OaProgressBo;
import com.ruoyi.oa.domain.bo.SysOaProjectBo;
import com.ruoyi.oa.domain.bo.SysOaTaskBo;
import com.ruoyi.oa.domain.vo.OaProgressVo;
import com.ruoyi.oa.domain.vo.SysOaProjectVo;
import com.ruoyi.oa.domain.vo.SysOaTaskVo;
import com.ruoyi.oa.service.IOaProgressService;
import com.ruoyi.oa.service.ISysOaProjectService;
import com.ruoyi.oa.service.ISysOaTaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.SysOaRemindBo;
import com.ruoyi.oa.domain.vo.SysOaRemindVo;
import com.ruoyi.oa.domain.SysOaRemind;
import com.ruoyi.oa.mapper.SysOaRemindMapper;
import com.ruoyi.oa.service.ISysOaRemindService;

import java.util.*;

/**
 * 事件提醒Service业务层处理
 *
 * @author huangxing
 * @date 2024-04-02
 */
@RequiredArgsConstructor
@Service
public class SysOaRemindServiceImpl implements ISysOaRemindService {

    private final SysOaRemindMapper baseMapper;

    private final ISysOaTaskService sysOaTaskService;

    private final ISysOaProjectService projectService;

    private final IOaProgressService progressService;

    /**
     * 查询事件提醒
     */
    @Override
    public SysOaRemindVo queryById(Long remindId){
        return baseMapper.selectVoById(remindId);
    }

    /**
     * 查询代办事件列表
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public R<List<SysOaRemindVo>> queryRemindList(SysOaRemindBo bo, PageQuery pageQuery){

        // 任务的逾期情况，项目的逾期情况 目前就这个两个需要提醒
        // 开始
        // 首先查询任务存在不存在逾期问题
        SysOaTaskBo sysOaTaskBo = new SysOaTaskBo();
        sysOaTaskBo.setState(0L);
        sysOaTaskBo.setWorkerId(LoginHelper.getUserId());
        List<SysOaTaskVo> tasks = sysOaTaskService.queryListByRemain(sysOaTaskBo,pageQuery);
        ArrayList<SysOaRemindVo> sysOaRemindVos = new ArrayList<>();
        for (SysOaTaskVo task : tasks) {
            SysOaRemindVo sysOaRemindVo = new SysOaRemindVo();
            sysOaRemindVo.setType("task");
            sysOaRemindVo.setAccessory(task.getAccessory());
            sysOaRemindVo.setContent(task.getContent());
            sysOaRemindVo.setTaskTime(task.getFinishTime());
            sysOaRemindVo.setRemainTime(task.getOverDays());
            sysOaRemindVos.add(sysOaRemindVo);
        }
        // 查询报工
        sysOaTaskBo.setItemStatus(0L);
        tasks = sysOaTaskService.queryListByRemain(sysOaTaskBo, pageQuery);
        for (SysOaTaskVo task : tasks) {
            SysOaRemindVo sysOaRemindVo = new SysOaRemindVo();
            sysOaRemindVo.setType("taskItem");
            sysOaRemindVo.setAccessory(task.getFiles());
            sysOaRemindVo.setContent(task.getContent());
            sysOaRemindVo.setRemainTime(task.getOverDays());
            sysOaRemindVo.setTaskTime(task.getFinishTime());
            sysOaRemindVos.add(sysOaRemindVo);
        }

        // 查询项目情况
        SysOaProjectBo sysOaProjectBo = new SysOaProjectBo();
        sysOaProjectBo.setProjectStatus("0");
        sysOaProjectBo.setCreateBy(LoginHelper.getNickName());
        List<SysOaProjectVo> rows = projectService.queryPageList(sysOaProjectBo, pageQuery).getRows();
        for (SysOaProjectVo row : rows) {
            SysOaRemindVo sysOaRemindVo = new SysOaRemindVo();
            sysOaRemindVo.setType("project");
            sysOaRemindVo.setAccessory(row.getAccessory());
            sysOaRemindVo.setContent(row.getProjectName());
            sysOaRemindVo.setRemainTime(row.getRemainTime());
            sysOaRemindVo.setTaskTime(row.getFinishTime());
            sysOaRemindVos.add(sysOaRemindVo);
        }

        // 查询邻近过期进度
//        OaProgressBo oaProgressBo = new OaProgressBo();
//        oaProgressBo.setType(1L);
//        List<OaProgressVo> rows1 = progressService.queryPageList(oaProgressBo, pageQuery).getRows();
//        for (OaProgressVo row : rows1) {
//            if (row.getRemainTime()!=null && row.getStatus() ==0L && row.getRemainTime()<3L){
//                SysOaRemindVo sysOaRemindVo = new SysOaRemindVo();
//                sysOaRemindVo.setType("progress-project");
//                sysOaRemindVo.setContent(row.getProgressName());
//                sysOaRemindVo.setRemainTime(row.getRemainTime());
//                sysOaRemindVo.setTaskTime(row.getEndTime());
//                sysOaRemindVos.add(sysOaRemindVo);
//            }
//
//        }

        return R.ok(sysOaRemindVos);
    }

    @Override
    public Long queryRemindCount(SysOaRemindBo bo){
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysOaRemind> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTaskStatus()), SysOaRemind::getTaskStatus, bo.getTaskStatus());
        lqw.between(params.get("beginTime") != null && params.get("endTime") != null, SysOaRemind::getTaskTime, params.get("beginTime"), params.get("endTime"));
        Long aLong = baseMapper.selectCount(lqw);
        return  aLong;
    }

    /**
     * 查询事件提醒列表
     */
    @Override
    public TableDataInfo<SysOaRemindVo> queryPageList(SysOaRemindBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysOaRemind> lqw = buildQueryWrapper(bo);
        Page<SysOaRemindVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询事件提醒列表
     */
    @Override
    public List<SysOaRemindVo> queryList(SysOaRemindBo bo) {
        LambdaQueryWrapper<SysOaRemind> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysOaRemind> buildQueryWrapper(SysOaRemindBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysOaRemind> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTaskTitle()), SysOaRemind::getTaskTitle, bo.getTaskTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskStatus()), SysOaRemind::getTaskStatus, bo.getTaskStatus());
        lqw.between(params.get("beginTime") != null && params.get("endTime") != null, SysOaRemind::getTaskTime, params.get("beginTime"), params.get("endTime"));
        lqw.orderByDesc(SysOaRemind::getCreateTime);
        return lqw;
    }

    /**
     * 新增事件提醒
     */
    @Override
    public Boolean insertByBo(SysOaRemindBo bo) {
        bo.setCreateBy(LoginHelper.getUsername());
        bo.setCreateTime(new Date());
        SysOaRemind add = BeanUtil.toBean(bo, SysOaRemind.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRemindId(add.getRemindId());
        }
        return flag;
    }

    /**
     * 修改事件提醒
     */
    @Override
    public Boolean updateByBo(SysOaRemindBo bo) {
        bo.setUpdateBy(LoginHelper.getUsername());
        bo.setUpdateTime(new Date());
        SysOaRemind update = BeanUtil.toBean(bo, SysOaRemind.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysOaRemind entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除事件提醒
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
