package com.hssx.pcbms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hssx.pcbms.constant.Constant;
import com.hssx.pcbms.entity.Task;
import com.hssx.pcbms.entity.TaskDynamic;
import com.hssx.pcbms.entity.TaskExamine;
import com.hssx.pcbms.mapper.TaskDynamicMapper;
import com.hssx.pcbms.mapper.TaskExamineMapper;
import com.hssx.pcbms.mapper.TaskMapper;
import com.hssx.pcbms.mapper.UserMapper;
import com.hssx.pcbms.service.TaskExamineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hssx.pcbms.util.HttpRespMsg;
import com.hssx.pcbms.util.PageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 吴涛涛
 * @since 2019-11-12
 */
@Service
public class TaskExamineServiceImpl extends ServiceImpl<TaskExamineMapper, TaskExamine> implements TaskExamineService {
    @Resource
    private TaskExamineMapper taskExamineMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private TaskDynamicMapper taskDynamicMapper;
    @Resource
    private TaskMapper taskMapper;

    @Override
    public HttpRespMsg addOrUpdate(TaskExamine taskExamine, Integer type) {
        HttpRespMsg msg = new HttpRespMsg();
        TaskDynamic dynamic = new TaskDynamic();
        Task task = new Task();
        if (taskExamine.getId() == null) {
            //添加操作
            taskExamine.setPublisher(userMapper.selectById(taskExamine.getPublisherId()).getName());
            taskExamineMapper.insert(taskExamine);
            dynamic.setTaskId(taskExamine.getTaskId());
            //添加动态
            dynamic.setTaskState(3);
            dynamic.setContent(taskExamine.getPublisher()+Constant.EXAMINE_SUBMIT);
            dynamic.setStateContent(Constant.TASK_SUBMIT);
            taskDynamicMapper.insert(dynamic);
            task.setId(taskExamine.getTaskId());
            task.setState(3);
            taskMapper.updateById(task);
        } else {
            //回复操作
            taskExamine.setResponder(userMapper.selectById(taskExamine.getResponderId()).getName());
            taskExamine.setRespondDate(LocalDateTime.now());
            taskExamineMapper.updateById(taskExamine);
            TaskExamine newTaskExamine = taskExamineMapper.selectById(taskExamine.getId());
            task.setId(newTaskExamine.getTaskId());
            dynamic.setTaskId(newTaskExamine.getTaskId());
            if (taskExamine.getIsPass() == 2) {
                //添加通过任务处理动态
                dynamic.setTaskState(5);
                dynamic.setContent(newTaskExamine.getResponder()+Constant.EXAMINE_PASS);
                dynamic.setStateContent(Constant.TASK_COMPLETED);
                taskDynamicMapper.insert(dynamic);
                task.setState(5);
                taskMapper.updateById(task);
            } else if (taskExamine.getIsPass() == 1) {
                //添加不通过任务处理动态
                dynamic.setTaskState(4);
                dynamic.setContent(newTaskExamine.getResponder()+Constant.EXAMINE_NO_PASS);
                dynamic.setStateContent(Constant.TASK_NO_PASS);
                taskDynamicMapper.insert(dynamic);
                task.setState(4);
                taskMapper.updateById(task);
            }
        }
        return msg;
    }

    @Override
    public HttpRespMsg getList(PageUtil page, String taskId) {
        HttpRespMsg msg = new HttpRespMsg();
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        List<TaskExamine> list = taskExamineMapper.selectList(new QueryWrapper<TaskExamine>().eq("task_id", taskId).orderByDesc("publish_date"));
        PageInfo<TaskExamine> pageInfo = new PageInfo<>(list);
        msg.data = pageInfo;
        return msg;
    }
}
