package com.srs.teacher.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.srs.common.core.domain.AjaxResult;
import com.srs.common.doman.dto.ProcessResultDto;
import com.srs.common.exception.ServiceException;
import com.srs.common.utils.SecurityUtils;
import com.srs.flowable.service.IFlowDefinitionService;
import com.srs.teacher.domain.SysTeacherKpiFillingNegativeList;
import org.flowable.engine.IdentityService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.teacher.mapper.SysTeacherKpiFillingStuMgtMapper;
import com.srs.teacher.domain.SysTeacherKpiFillingStuMgt;
import com.srs.teacher.service.ISysTeacherKpiFillingStuMgtService;
import lombok.extern.slf4j.Slf4j;


/**
 * 业绩考核-个人填报-学生管理Service业务层处理
 *
 * @author chc
 * @date 2024-06-05
 */
@Slf4j
@Service
public class SysTeacherKpiFillingStuMgtServiceImpl extends ServiceImpl<SysTeacherKpiFillingStuMgtMapper,SysTeacherKpiFillingStuMgt> implements ISysTeacherKpiFillingStuMgtService {
    @Autowired
    private SysTeacherKpiFillingStuMgtMapper sysTeacherKpiFillingStuMgtMapper;

    /**
     * 查询业绩考核-个人填报-学生管理
     *
     * @param id 业绩考核-个人填报-学生管理主键
     * @return 业绩考核-个人填报-学生管理
     */
    @Override
    public SysTeacherKpiFillingStuMgt selectSysTeacherKpiFillingStuMgtById(Long id) {
        return sysTeacherKpiFillingStuMgtMapper.selectSysTeacherKpiFillingStuMgtById(id);
    }

    /**
     * 查根据辅导员名称、年份 月份 查询
     *
     * @param fdyName 业绩考核-个人填报-学生管理
     * @return 业绩考核-个人填报-学生管理详细
     */
    @Override
    public SysTeacherKpiFillingStuMgt selectSysTeacherKpiFillingStuMgtByFdyName(String fdyName, String fillingYear, String fillingMonth) {
        return sysTeacherKpiFillingStuMgtMapper.selectSysTeacherKpiFillingStuMgtByFdyName(fdyName, fillingYear, fillingMonth);
    }

    /**
     * 查询业绩考核-个人填报-学生管理列表
     *
     * @param sysTeacherKpiFillingStuMgt 业绩考核-个人填报-学生管理
     * @return 业绩考核-个人填报-学生管理
     */
    @Override
    public List<SysTeacherKpiFillingStuMgt> selectSysTeacherKpiFillingStuMgtList(SysTeacherKpiFillingStuMgt sysTeacherKpiFillingStuMgt) {
        sysTeacherKpiFillingStuMgt.setFdyName(SecurityUtils.getLoginUser().getUser().getNickName());
        return sysTeacherKpiFillingStuMgtMapper.selectSysTeacherKpiFillingStuMgtList(sysTeacherKpiFillingStuMgt);
    }

    /**
     * 新增业绩考核-个人填报-学生管理
     *
     * @param sysTeacherKpiFillingStuMgt 业绩考核-个人填报-学生管理
     * @return 结果
     */
    @Override
    public int insertSysTeacherKpiFillingStuMgt(SysTeacherKpiFillingStuMgt sysTeacherKpiFillingStuMgt) {
        if (sysTeacherKpiFillingStuMgt.getFdyName() == null) {
            sysTeacherKpiFillingStuMgt.setFdyName(SecurityUtils.getLoginUser().getUser().getNickName());
        }
        if (sysTeacherKpiFillingStuMgt.getId() != null) {
            return sysTeacherKpiFillingStuMgtMapper.updateSysTeacherKpiFillingStuMgt(sysTeacherKpiFillingStuMgt);
        }
//        if (sysTeacherKpiFillingStuMgt.getAuditStatus() != null && sysTeacherKpiFillingStuMgt.getAuditStatus() == 1) {
//
//            // 开启工作流
//            ProcessResultDto processResultDto = startLeaveProcess(sysTeacherKpiFillingStuMgt);
//
////            if (processResultDto != null) {
////                sysTeacherKpiFillingStuMgt.setProcessInstanceId(processResultDto.getProcessInstanceId());
////                sysTeacherKpiFillingStuMgt.setDeployId(processResultDto.getDeploymentId());
////            }
//        }
        return sysTeacherKpiFillingStuMgtMapper.insertSysTeacherKpiFillingStuMgt(sysTeacherKpiFillingStuMgt);
    }

    /**
     * 修改业绩考核-个人填报-学生管理
     *
     * @param sysTeacherKpiFillingStuMgt 业绩考核-个人填报-学生管理
     * @return 结果
     */
    @Override
    public int updateSysTeacherKpiFillingStuMgt(SysTeacherKpiFillingStuMgt sysTeacherKpiFillingStuMgt) {
        List<SysTeacherKpiFillingStuMgt> sysTeacherKpiFillingStuMgts = sysTeacherKpiFillingStuMgtMapper.selectSysTeacherKpiFillingStuMgtByFdyNameAndFillingYear(sysTeacherKpiFillingStuMgt.getDepartmentName(), sysTeacherKpiFillingStuMgt.getFdyName(), sysTeacherKpiFillingStuMgt.getFillingYear(), sysTeacherKpiFillingStuMgt.getFillingMonth());
        if (sysTeacherKpiFillingStuMgts != null && !sysTeacherKpiFillingStuMgts.isEmpty()) {
            throw new ServiceException("已提交，请勿重复提交", 500);
        }
        if (sysTeacherKpiFillingStuMgt.getFdyName() == null) {
            sysTeacherKpiFillingStuMgt.setFdyName(SecurityUtils.getLoginUser().getUser().getNickName());
        }
        return sysTeacherKpiFillingStuMgtMapper.updateSysTeacherKpiFillingStuMgt(sysTeacherKpiFillingStuMgt);
    }

    /**
     * 批量删除业绩考核-个人填报-学生管理
     *
     * @param ids 需要删除的业绩考核-个人填报-学生管理主键
     * @return 结果
     */
    @Override
    public int deleteSysTeacherKpiFillingStuMgtByIds(Long[] ids) {
        return sysTeacherKpiFillingStuMgtMapper.deleteSysTeacherKpiFillingStuMgtByIds(ids);
    }

    /**
     * 删除业绩考核-个人填报-学生管理信息
     *
     * @param id 业绩考核-个人填报-学生管理主键
     * @return 结果
     */
    @Override
    public int deleteSysTeacherKpiFillingStuMgtById(Long id) {
        return sysTeacherKpiFillingStuMgtMapper.deleteSysTeacherKpiFillingStuMgtById(id);
    }


    @Autowired
    IFlowDefinitionService flowDefinitionService;

    @Autowired
    TaskService taskService;

    @Autowired
    IdentityService identityService;
    /**
     * 开启处分工作流
     *
     * @param sysTeacherKpiFillingStuMgt
     */
    private ProcessResultDto startLeaveProcess(SysTeacherKpiFillingStuMgt sysTeacherKpiFillingStuMgt) {

        Map<String, Object> variables = new HashMap<>();
//        variables.put("id", sysTeacherKpiFillingStuMgt.getId().toString());
//        variables.put("auditStatus", sysTeacherKpiFillingStuMgt.getAuditStatus());
//        variables.put("stuId", sysTeacherKpiFillingStuMgt.getStuId());
        // 把辅导员的userId放到approval中
        variables.put("approval", SecurityUtils.getUserId());
        variables.put("deptId", SecurityUtils.getDeptId());

        AjaxResult ajaxResult = flowDefinitionService.startProcessInstanceById("flow_kzjdarkf:6", variables);
        String code = ajaxResult.get("code").toString();
        if (code.equals("200")) {

            log.info("流程启动成功");

            Object obj = ajaxResult.get("data");
            ProcessResultDto dto = (ProcessResultDto) obj;

            // 操作工作流
            // 设置当前用户
            identityService.setAuthenticatedUserId(SecurityUtils.getUserId().toString());
            // 查询待办任务列表
            List<Task> tasks = taskService.createTaskQuery()
                    .processInstanceId(dto.getProcessInstanceId())
                    .taskAssignee(SecurityUtils.getUserId().toString())
                    .active()
                    .list();
            // 保存审核结果到任务变量中
            variables.put("approved", true);
            // 完成待办任务列表
            for (Task task : tasks) {
                String taskId = task.getId();
                String applicationId = taskService.getVariable(taskId, "id").toString();
                if (applicationId.equals(sysTeacherKpiFillingStuMgt.getId().toString())) {
                    taskService.complete(task.getId(), variables);
                }
            }

            return dto;
        } else {
            return null;
        }
    }
}
