package com.ruoyi.project.training.spec.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.project.plant.domain.TStaffmgr;
import com.ruoyi.project.plant.service.ITStaffmgrService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import com.ruoyi.project.training.spec.domain.TStSuccessorScore;
import com.ruoyi.project.training.spec.domain.vo.TStSuccessorExportVO;
import com.ruoyi.project.training.spec.mapper.TStYearplanMapper;
import com.ruoyi.project.training.spec.service.ITStSuccessorScoreService;
import com.ruoyi.project.training.spec.service.ITStYearplanService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.training.spec.domain.TStSuccessor;
import com.ruoyi.project.training.spec.service.ITStSuccessorService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 继任者清单Controller
 *
 * @author ruoyi
 * @date 2022-04-11
 */
@RestController
@RequestMapping("/spec/successor")
public class TStSuccessorController extends BaseController
{
    @Autowired
    private ITStSuccessorService tStSuccessorService;

    @Autowired
    private ITStaffmgrService tStaffmgrService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ITStSuccessorScoreService tStSuccessorScoreService;

    @Autowired
    private TStYearplanMapper tStYearplanMapper;

    /**
     * 根据学员id查询继任者清单详细信息
     */
    @PreAuthorize("@ss.hasPermi('spec:successor:query')")
    @GetMapping("/getInfoBySuccessorId/{staffId}")
    public AjaxResult getInfoBySuccessorId(@PathVariable("staffId") Long staffId)
    {
        return AjaxResult.success(tStSuccessorService.selectTStSuccessorBySuccessorId(staffId));
    }

    /**
     * 查询导师列表
     */
    @PreAuthorize("@ss.hasPermi('spec:successor:list')")
    @GetMapping("/listMentors")
    public AjaxResult listMentors(TStSuccessor tStSuccessor)
    {
        Long userId = getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        String staffId = sysUser.getStaffId();
        // 处理前的导师列表
        List<TStSuccessor> list = tStSuccessorService.selectTStSuccessorList(tStSuccessor);
        // 去重后的导师列表
        List<TStSuccessor> newList = new ArrayList<>();
        // 处理前的导师列表中上一个导师id的标识
        String temp = "";
        for (TStSuccessor item : list) {
            String mentorStaffId = item.getMentorStaffId();
            if (staffId.equals(mentorStaffId)) {    // 导师id=当前登录导师id
                continue;
            } else
                if (temp.equals(mentorStaffId)) {   // 导师id重复
                continue;
            } else {
                newList.add(item);
                temp = mentorStaffId;
            }
        }
        return AjaxResult.success(newList);
    }

    /**
     * 查询继任者清单列表
     */
    @PreAuthorize("@ss.hasPermi('spec:successor:list')")
    @GetMapping("/list")
    public TableDataInfo list(TStSuccessor tStSuccessor)
    {
        startPage();
        List<TStSuccessor> list = tStSuccessorService.selectTStSuccessorList(tStSuccessor);
        for (TStSuccessor t: list
             ) {
            TStaffmgr staffmgr = tStaffmgrService.selectTStaffmgrByStaffId(t.getStaffId());
            t.setStaff(staffmgr);

            TStSuccessorScore tStSuccessorScore = new TStSuccessorScore();
            tStSuccessorScore.setStaffId(t.getStaffId());
            List<TStSuccessorScore> scoreList = tStSuccessorScoreService.selectTStSuccessorScoreList(tStSuccessorScore);
            t.setScoreList(scoreList);
        }
        return getDataTable(list);
    }

    /**
     * 查询学员下拉
     */
    @GetMapping("/listPostStaff")
    public AjaxResult listPostStaff(TStSuccessor tStSuccessor)
    {
        Long userId = getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        List<TStaffmgr> list = tStaffmgrService.selectTStaffmgrByPost(sysUser);
        return AjaxResult.success(list);
    }

    /**
     * 查询学员下拉
     */
    @GetMapping("/listPostMentorStaff")
    public AjaxResult listPostMentorStaff(TStSuccessor tStSuccessor)
    {
        Long userId = getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        List<TStaffmgr> list = tStaffmgrService.selectTMentorStaffmgrByPost(sysUser);
        return AjaxResult.success(list);
    }

    /**
     * 导出继任者清单列表
     */
    @PreAuthorize("@ss.hasPermi('spec:successor:export')")
    @Log(title = "继任者清单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TStSuccessor tStSuccessor)
    {
        // 继任者清单
        List<TStSuccessor> successors = tStSuccessorService.selectTStSuccessorList(tStSuccessor);
        // 继任者清单导出内容
        List<TStSuccessorExportVO> successorVOs = new ArrayList<TStSuccessorExportVO>();
        for (TStSuccessor successor: successors) {
            // 继任者清单导出内容VO
            TStSuccessorExportVO successorVO = new TStSuccessorExportVO();
            successorVO.setStaffId(successor.getStaffId());
            successorVO.setStaffName(successor.getStaffName());
            successorVO.setCurrentPost(successor.getActualpost());
            successorVO.setMentorStaffName(successor.getMentorStaffName());
            successorVO.setState(successor.getState());
            successorVO.setDeptName(successor.getDeptName());
            successorVO.setRemarks(successor.getRemarks());
            TStaffmgr staffmgr = tStaffmgrService.selectTStaffmgrByStaffId(successor.getStaffId());
            successorVO.setFuturePost(staffmgr.getActualpost());
            successorVO.setTeam(staffmgr.getTeam());
            TStSuccessorScore queryObj = new TStSuccessorScore();
            queryObj.setStaffId(successor.getStaffId());
            List<TStSuccessorScore> scoreList = tStSuccessorScoreService.selectTStSuccessorScoreList(queryObj);
            if (scoreList.size() != 0) {
                TStSuccessorScore stSuccessorScore = scoreList.get(0);
                successorVO.setQuarterOne(stSuccessorScore.getQuarterOne());
                successorVO.setQuarterTwo(stSuccessorScore.getQuarterTwo());
                successorVO.setQuarterThree(stSuccessorScore.getQuarterThree());
                successorVO.setQuarterFour(stSuccessorScore.getQuarterFour());
                successorVO.setYear(stSuccessorScore.getYear());
            }
            successorVOs.add(successorVO);
        }
        ExcelUtil<TStSuccessorExportVO> util = new ExcelUtil<TStSuccessorExportVO>(TStSuccessorExportVO.class);
        return util.exportExcel(successorVOs, "专项培养清单");
    }

    /**
     * 获取继任者清单详细信息
     */
    @PreAuthorize("@ss.hasPermi('spec:successor:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tStSuccessorService.selectTStSuccessorById(id));
    }

    /**
     * 新增继任者清单
     */
    @PreAuthorize("@ss.hasPermi('spec:successor:add')")
    @Log(title = "继任者清单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TStSuccessor tStSuccessor)
    {
        return toAjax(tStSuccessorService.insertTStSuccessor(tStSuccessor));
    }

    /**
     * 修改继任者清单
     */
    @PreAuthorize("@ss.hasPermi('spec:successor:edit')")
    @Log(title = "继任者清单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TStSuccessor tStSuccessor)
    {
        return toAjax(tStSuccessorService.updateTStSuccessor(tStSuccessor));
    }

    /**
     * 删除继任者清单
     */
    @PreAuthorize("@ss.hasPermi('spec:successor:remove')")
    @Log(title = "继任者清单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        //删除对应年度计划
        for (Long id: ids
             ) {
            TStSuccessor t = tStSuccessorService.selectTStSuccessorById(id);
            tStYearplanMapper.deleteTStYearplanByStaffId(t.getStaffId());
        }
        return toAjax(tStSuccessorService.deleteTStSuccessorByIds(ids));
    }
}
