package com.ruoyi.project.training.controller;

import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.project.plant.domain.TStaffmgr;
import com.ruoyi.project.plant.service.ITStaffmgrService;
import com.ruoyi.project.system.service.ISysDeptService;
import com.ruoyi.project.system.service.ISysDictTypeService;
import com.ruoyi.project.training.domain.TTrainingCompanylevel;
import com.ruoyi.project.training.domain.TTrainingMatrix;
import com.ruoyi.project.training.service.ITTrainingCompanylevelService;
import com.ruoyi.project.training.service.ITTrainingMatrixService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.training.domain.TTrainingParticipants;
import com.ruoyi.project.training.service.ITTrainingParticipantsService;
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 2021-05-30
 */
@RestController
@RequestMapping("/training/participants")
public class TTrainingParticipantsController extends BaseController
{
    @Autowired
    private ITTrainingParticipantsService tTrainingParticipantsService;
    @Autowired
    private ITTrainingCompanylevelService tTrainingCompanylevelService;
    @Autowired
    private ITStaffmgrService tStaffmgrService;
    @Autowired
    private ISysDeptService iSysDeptService;
    @Autowired
    private ISysDictTypeService iSysDictTypeService;
    @Autowired
    private ITTrainingMatrixService tTrainingMatrixService;

    /**
     * 查询人员-公司级培训关系列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TTrainingParticipants tTrainingParticipants)
    {
        startPage();
        List<TTrainingParticipants> list = tTrainingParticipantsService.selectTTrainingParticipantsList(tTrainingParticipants);
        return getDataTable(list);
    }



    /**
     * 查询人员-公司级培训关系列表
     */
    @GetMapping("/companyList")
    public Map<String, Object> companyList(TTrainingParticipants tTrainingParticipants)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<TTrainingParticipants> list = tTrainingParticipantsService.selectTTrainingParticipantsList(tTrainingParticipants);
        List<List> companyLevelList = new ArrayList<>();
        List<TTrainingCompanylevel> tTrainingCompanylevels = tTrainingCompanylevelService.selectTTrainingCompanylevelList(new TTrainingCompanylevel());
        TStaffmgr staffmgr = new TStaffmgr();
        staffmgr.setUnits("10,18,20,30");
        staffmgr.setLeftYear(String.valueOf(Calendar.getInstance().YEAR));
        List<TStaffmgr> staffmgrs = tStaffmgrService.selectRecordList(staffmgr);
        try {
            //判断离职
            if (!StringUtils.isEmpty(staffmgr.getLeftYear())) {
                Iterator<TStaffmgr> iterator = staffmgrs.iterator();
                while (iterator.hasNext()) {
                    TStaffmgr t = iterator.next();
                    if (t.getLeftDate() != null && t.getDelFlag() == 9){
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(t.getLeftDate());
                        Integer year = cal.get(Calendar.YEAR);//获取年
                        if (year < Integer.parseInt(staffmgr.getLeftYear())) {
                            logger.debug(t.getName() + "离职年份小于数据");
                            iterator.remove();
                        }
                    }
                }
            }
        }catch (Exception e) {
            logger.error(e.toString());
        }
        for (int i = 0; i < tTrainingCompanylevels.size(); i++) {
            //每一类公司级培训数据
            List<String> company = new ArrayList<>();
            List<String> companyDate = new ArrayList<>();
            company.add(tTrainingCompanylevels.get(i).getCourseCode());
            companyDate.add(tTrainingCompanylevels.get(i).getCourseCode());
            company.add(tTrainingCompanylevels.get(i).getItem());
            companyDate.add(tTrainingCompanylevels.get(i).getItem());
            if (tTrainingCompanylevels.get(i).getFrequency() != null) {
                company.add("每" + tTrainingCompanylevels.get(i).getFrequency() + "年一次");
                companyDate.add("每" + tTrainingCompanylevels.get(i).getFrequency() + "年一次");
            }else {
                TTrainingMatrix matrix = new TTrainingMatrix();
                matrix.setCourseCode(tTrainingCompanylevels.get(i).getCourseCode());
                matrix.setDeptId(tTrainingCompanylevels.get(i).getDeptId());
                List<TTrainingMatrix> matrixList = tTrainingMatrixService.selectTTrainingMatrixList(matrix);
                if (matrixList.size() > 0) {
                    company.add(matrixList.get(0).getFrequency());
                    companyDate.add(matrixList.get(0).getFrequency());
                }else {
                    company.add("一次");
                    companyDate.add("一次");
                }
            }
            for (TStaffmgr s : staffmgrs) {
                //按人员筛
                int m = 0;
                TTrainingParticipants f = new TTrainingParticipants();
                for (TTrainingParticipants t : list) {
                    if (t.getStaffId().equals(s.getStaffid()) && t.getCompanyId().equals(tTrainingCompanylevels.get(i).getId())) {
                        m = 1;
                        f = t;
                    }
                }
                if (m == 0) {
                    company.add("⚪");
                    companyDate.add("N.A");
                }else {
                    company.add("╳");
                    if (f.getStartDate() != null) {
                        companyDate.add(sdf.format(f.getStartDate()));
                    }else {
                        companyDate.add("-");
                    }
                }
            }
            companyLevelList.add(company);
            companyLevelList.add(companyDate);
        }
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("companyLevelList", companyLevelList);
        ajaxResult.put("staffmgrs" , staffmgrs);
        return ajaxResult;
    }

    /**
     * 导出人员-公司级培训关系列表
     */
    @Log(title = "人员-公司级培训关系", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TTrainingParticipants tTrainingParticipants)
    {
        List<TTrainingParticipants> list = tTrainingParticipantsService.selectTTrainingParticipantsList(tTrainingParticipants);
        ExcelUtil<TTrainingParticipants> util = new ExcelUtil<TTrainingParticipants>(TTrainingParticipants.class);
        return util.exportExcel(list, "participants");
    }

    /**
     * 获取人员-公司级培训关系详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tTrainingParticipantsService.selectTTrainingParticipantsById(id));
    }

    /**
     * 新增人员-公司级培训关系
     */
    @Log(title = "人员-公司级培训关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTrainingParticipants tTrainingParticipants)
    {
        return toAjax(tTrainingParticipantsService.insertTTrainingParticipants(tTrainingParticipants));
    }

    /**
     * 修改人员-公司级培训关系
     */
    @Log(title = "人员-公司级培训关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTrainingParticipants tTrainingParticipants)
    {
        return toAjax(tTrainingParticipantsService.updateTTrainingParticipants(tTrainingParticipants));
    }

    /**
     * 删除人员-公司级培训关系
     */
    @Log(title = "人员-公司级培训关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tTrainingParticipantsService.deleteTTrainingParticipantsByIds(ids));
    }

    /**
     * 删除所有历史人员-公司级培训关系
     */
    @Log(title = "人员-公司级培训关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteAll")
    public AjaxResult deleteAll()
    {
        return toAjax(tTrainingParticipantsService.deleteTTrainingParticipantsAll());
    }


}
