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.training.domain.TTraining;
import com.ruoyi.project.training.domain.TTrainingRegular;
import com.ruoyi.project.training.service.ITTrainingRegularService;
import com.ruoyi.project.training.service.ITTrainingService;
import org.apache.commons.lang.StringUtils;
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.domain.TTrainingDevice;
import com.ruoyi.project.training.service.ITTrainingDeviceService;
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-06-23
 */
@RestController
@RequestMapping("/training/device")
public class TTrainingDeviceController extends BaseController
{
    @Autowired
    private ITTrainingDeviceService tTrainingDeviceService;

    @Autowired
    private ITStaffmgrService tStaffmgrService;

    @Autowired
    private ITTrainingService tTrainingService;

    @Autowired
    private ITTrainingRegularService tTrainingRegularService;

    /**
     * 查询人员-装置级培训关系列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TTrainingDevice tTrainingDevice)
    {
        startPage();
        List<TTrainingDevice> list = tTrainingDeviceService.selectTTrainingDeviceList(tTrainingDevice);
        return getDataTable(list);
    }

    /**
     * 查询人员-装置级培训关系列表
     */
    @GetMapping("/deviceList")
    public Map<String, Object> deviceList(TTrainingDevice tTrainingDevice)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sdf2 = new SimpleDateFormat("MM");
        List<TTrainingDevice> list = tTrainingDeviceService.selectTTrainingDeviceList(tTrainingDevice);

        TTraining tTraining = new TTraining();
        tTraining.setYear(tTrainingDevice.getYear());
        tTraining.setTrainingType(tTrainingDevice.getTrainingType());
        List<TTraining> tTrainings = tTrainingService.selectTTrainingList(tTraining);
        List<List> deviceLevelList = new ArrayList<>();
        //查询人员 ，客户的需求：units值
        TStaffmgr paramStaff = new TStaffmgr();
        paramStaff.setUnits(tTrainingDevice.getUnits());
        List<TStaffmgr> staffmgrs = tStaffmgrService.selectRecordList(paramStaff);
        try {
            //判断离职
            if (!StringUtils.isEmpty(tTrainingDevice.getYear())) {
                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(tTrainingDevice.getYear())) {
                            logger.debug(t.getName() + "离职年份小于数据");
                            iterator.remove();
                        }
                    }
                }
            }
        }catch (Exception e) {
            logger.error(e.toString());
        }
        logger.info("deviceList:" + staffmgrs.size());
        for (int i = 0; i < tTrainings.size(); i++) {
            //每一类装置级培训数据
            List<String> device = new ArrayList<>();
            List<String> deviceDate = new ArrayList<>();
            if (tTrainings.get(i).getCourseid() != null) {
                device.add(tTrainings.get(i).getCourseid());
                deviceDate.add(tTrainings.get(i).getCourseid());
            }else {
                device.add("");
                deviceDate.add("");
            }
            device.add(tTrainings.get(i).getCourse());
            deviceDate.add(tTrainings.get(i).getCourse());
            for (TStaffmgr s : staffmgrs) {
                //按人员筛
                int m = 0;
                TTrainingDevice d = new TTrainingDevice();
                for (TTrainingDevice t : list) {
                    if (t.getStaffId().equals(s.getStaffid()) && t.getRegularId().equals(tTrainings.get(i).getId())) {
                        if (tTrainings.get(i).getTrainingType().equals("10")) {
                            if (tTrainings.get(i).getStartDate() == null) {
                                m = 2;
                            }else {
                                m = 1;
                                d = t;
                            }
                        }else {
                            m = 1;
                            d = t;
                        }
                    }
                }
                if (m == 0) {
                    device.add("⚪");
                    deviceDate.add("N.A");
                }else if (m == 2) {
                    device.add("╳");
                    deviceDate.add("尚未开始培训");
                }else {
                    if (d.getSupplementary().equals("1")) {
                        device.add("╳");
                        if (d.getStartDate() != null) {
                            deviceDate.add(sdf.format(d.getStartDate()));
                        }else {
                            deviceDate.add("-");
                        }
                    }else if (d.getSupplementary().equals("0")){
                        device.add("╳");
                        if (tTrainings.get(i).getRegularId() != null&&d.getStartDate() != null) {
                            //判断结束日期和开始日期是否同一个月
                            if (tTrainings.get(i).getCourseEnddate()!= null) {
                                Calendar calendar1 = Calendar.getInstance();
                                calendar1.setTime(d.getStartDate());
                                int month1 = calendar1.get(Calendar.MONTH);

                                Calendar calendar2 = Calendar.getInstance();
                                calendar2.setTime(tTrainings.get(i).getCourseEnddate());
                                int month2 = calendar2.get(Calendar.MONTH);
                                if (month2 == month1) {
                                    deviceDate.add(sdf1.format(d.getStartDate()));
                                }else {
                                    deviceDate.add(sdf1.format(d.getStartDate()) + "/" + sdf2.format(tTrainings.get(i).getCourseEnddate()));
                                }
                            }else {
                                deviceDate.add(sdf1.format(d.getStartDate()));
                            }
                        }else {
                            if(d.getStartDate() != null){
                                deviceDate.add(sdf.format(d.getStartDate()));
                            }
                        }
                    }else if (d.getSupplementary().equals("2")){
                        device.add("新员工");
                        if (d.getStartDate() != null) {
                            deviceDate.add(sdf.format(d.getStartDate()));
                        }else {
                            deviceDate.add("-");
                        }
                    }else {
                        device.add("/");
                        deviceDate.add("/");
                    }
                }
            }
            deviceLevelList.add(device);
            deviceLevelList.add(deviceDate);
        }
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("deviceLevelList", deviceLevelList);
        ajaxResult.put("staffmgrs" , staffmgrs);
        return ajaxResult;
    }

    /**
     * 导出人员-装置级培训关系列表
     */
    @Log(title = "人员-装置级培训关系", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TTrainingDevice tTrainingDevice)
    {
        List<TTrainingDevice> list = tTrainingDeviceService.selectTTrainingDeviceList(tTrainingDevice);
        ExcelUtil<TTrainingDevice> util = new ExcelUtil<TTrainingDevice>(TTrainingDevice.class);
        return util.exportExcel(list, "device");
    }

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

    /**
     * 新增人员-装置级培训关系
     */
    @Log(title = "人员-装置级培训关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTrainingDevice tTrainingDevice)
    {
        return toAjax(tTrainingDeviceService.insertTTrainingDevice(tTrainingDevice));
    }

    /**
     * 修改人员-装置级培训关系
     */
    @Log(title = "人员-装置级培训关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTrainingDevice tTrainingDevice)
    {
        return toAjax(tTrainingDeviceService.updateTTrainingDevice(tTrainingDevice));
    }

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