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

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

import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import com.ruoyi.project.training.spec.domain.TStFeedback;
import com.ruoyi.project.training.spec.domain.TStPlan;
import com.ruoyi.project.training.spec.domain.vo.TStYearPlanExportVO;
import com.ruoyi.project.training.spec.mapper.TStPlanMapper;
import com.ruoyi.project.training.spec.mapper.TStYearplanMapper;
import com.ruoyi.project.training.spec.service.ITStFeedbackService;
import com.ruoyi.project.training.spec.service.ITStPlanService;
import com.ruoyi.project.training.spec.service.ITStSuccessorService;
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.TStYearplan;
import com.ruoyi.project.training.spec.service.ITStYearplanService;
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;

import javax.annotation.Resource;

/**
 * 专项培训年度计划Controller
 *
 * @author ly
 * @date 2022-04-24
 */
@RestController
@RequestMapping("/spec/yearplan")
public class TStYearplanController extends BaseController
{
    @Autowired
    private ITStYearplanService tStYearplanService;

    @Autowired
    private ITStSuccessorService tStSuccessorService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ITStFeedbackService feedbackService;

    @Resource
    private TStYearplanMapper tStYearplanMapper;
    @Autowired
    private ITStPlanService tStPlanService;
    /**
     * 查询专项培训年度计划列表
     */
    @PreAuthorize("@ss.hasPermi('spec:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(TStYearplan tStYearplan)
    {
        Long userId = getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        String staffId = sysUser.getStaffId();
        tStYearplan.setMentorStaffId(staffId);
        startPage();
        List<TStYearplan> list = tStYearplanService.selectTStYearplanList(tStYearplan);
        return getDataTable(list);
    }

    /**
     * 导出专项培训年度计划列表
     */
    @PreAuthorize("@ss.hasPermi('spec:plan:export')")
    @Log(title = "专项培训年度计划", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TStYearplan tStYearplan)
    {
        // 年度计划
        List<TStYearplan> yearPlans = tStYearplanService.selectTStYearplanList(tStYearplan);
        // 年度计划导出内容
        List<TStYearPlanExportVO> yearPlanVOs = new ArrayList<>();
        for (TStYearplan yearPlan : yearPlans) {
            TStYearPlanExportVO yearPlanVO = new TStYearPlanExportVO();
            yearPlanVO.setStaffId(yearPlan.getStaffId());
            yearPlanVO.setStaffName(yearPlan.getStaffName());
            yearPlanVO.setName(yearPlan.getName());
            yearPlanVO.setPlanYear(yearPlan.getPlanYear());
            yearPlanVO.setDeptName(yearPlan.getDeptName());
            yearPlanVO.setApproveStatus(yearPlan.getApproveStatus());
            yearPlanVOs.add(yearPlanVO);
        }
        ExcelUtil<TStYearPlanExportVO> util = new ExcelUtil<TStYearPlanExportVO>(TStYearPlanExportVO.class);
        return util.exportExcel(yearPlanVOs, "年度培养计划");
    }

    /**
     * 获取专项培训年度计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('spec:plan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tStYearplanService.selectTStYearplanById(id));
    }

    /**
     * 新增专项培训年度计划
     */
    @PreAuthorize("@ss.hasPermi('spec:plan:add')")
    @Log(title = "专项培训年度计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TStYearplan tStYearplan)
    {
        List<TStYearplan> plantExistList = tStYearplanMapper.selectTStYearPlanExist(tStYearplan);
        if (plantExistList.size() != 0) {
            return AjaxResult.error("无法新增，当前数据已存在");
        }
        // 王子文 2022年4月28日 修改
        Long planYear = tStYearplan.getPlanYear();
        String staffId = tStYearplan.getStaffId();
        Long userId = getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        String mentorStaffId = sysUser.getStaffId();
        // 生成月度反馈
        for (int i = 1; i <=12; i++) {
            TStFeedback feedback = new TStFeedback();
            feedback.setFeedbackType(2L);
            feedback.setFeedbackStatus(0L);
            feedback.setFeedbackYear(planYear+"");
            feedback.setFeedbackMonth(i+"");
            feedback.setSuccessorId(staffId);
            feedback.setMentorId(mentorStaffId);
            feedbackService.insertTStFeedback(feedback);
        }
        // 生成季度反馈
        for (int i = 1; i <=4; i++) {
            TStFeedback feedback = new TStFeedback();
            feedback.setFeedbackType(3L);
            feedback.setFeedbackStatus(0L);
            feedback.setFeedbackYear(planYear+"");
            feedback.setFeedbackSeason(i+"");
            feedback.setSuccessorId(staffId);
            feedback.setMentorId(mentorStaffId);
            feedbackService.insertTStFeedback(feedback);
        }
        return toAjax(tStYearplanService.insertTStYearplan(tStYearplan));
    }

    /**
     * 修改专项培训年度计划
     */
    @PreAuthorize("@ss.hasPermi('spec:plan:edit')")
    @Log(title = "专项培训年度计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TStYearplan tStYearplan)
    {
        return toAjax(tStYearplanService.updateTStYearplan(tStYearplan));
    }

    /**
     * 删除专项培训年度计划
     */
    @PreAuthorize("@ss.hasPermi('spec:plan:remove')")
    @Log(title = "专项培训年度计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        // 王子文 2022年5月31日 修改
        for (int i = 0; i < ids.length; i++) {
            TStYearplan yearplan = tStYearplanService.selectTStYearplanById(ids[i]);
            TStFeedback queryObj = new TStFeedback();
            queryObj.setSuccessorId(yearplan.getStaffId());
            queryObj.setFeedbackYear(yearplan.getPlanYear().toString());
            List<TStFeedback> result = feedbackService.selectTStFeedbackList(queryObj);
            for (TStFeedback feedback : result) {
                feedbackService.deleteTStFeedbackById(feedback.getId());
            }
        }
        // 删除关联培训
        for (int i = 0; i < ids.length; i++) {
            TStPlan t = new TStPlan();
            t.setYearPlanId(ids[i]);
            List<TStPlan> list = tStPlanService.selectTStPlanList(t);
            for (TStPlan tp: list
                 ) {
                tStPlanService.deleteTStPlanById(tp.getId());
            }
        }
        return toAjax(tStYearplanService.deleteTStYearplanByIds(ids));
    }
}
