package com.ruoyi.project.training.controller;

import java.security.acl.LastOwnerException;
import java.text.SimpleDateFormat;
import java.util.List;

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.TTrainingDevice;
import com.ruoyi.project.training.service.ITTrainingDeviceService;
import com.ruoyi.project.training.service.ITTrainingService;
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.TTrainingNon;
import com.ruoyi.project.training.service.ITTrainingNonService;
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-26
 */
@RestController
@RequestMapping("/training/trainingNon")
public class TTrainingNonController extends BaseController
{
    @Autowired
    private ITTrainingNonService tTrainingNonService;
    @Autowired
    private ITTrainingService tTrainingService;
    @Autowired
    private ITStaffmgrService tStaffmgrService;
    @Autowired
    private ITTrainingDeviceService tTrainingDeviceService;

    /**
     * 查询培训计划未参加人员列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TTrainingNon tTrainingNon)
    {
        startPage();
        List<TTrainingNon> list = tTrainingNonService.selectTTrainingNonList(tTrainingNon);
        return getDataTable(list);
    }

    /**
     * 导出培训计划未参加人员列表
     */
    @Log(title = "培训计划未参加人员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TTrainingNon tTrainingNon)
    {
        List<TTrainingNon> list = tTrainingNonService.selectTTrainingNonList(tTrainingNon);
        ExcelUtil<TTrainingNon> util = new ExcelUtil<TTrainingNon>(TTrainingNon.class);
        return util.exportExcel(list, "trainingNon");
    }

    /**
     * 获取培训计划未参加人员详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tTrainingNonService.selectTTrainingNonById(id));
    }

    /**
     * 新增培训计划未参加人员
     */
    @Log(title = "培训计划未参加人员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTrainingNon tTrainingNon)
    {
        logger.info("trainingNon:" + tTrainingNon);
        List<TTrainingNon> list = tTrainingNonService.selectTTrainingNonList(tTrainingNon);
        TTraining tTraining = tTrainingService.selectTTrainingById(tTrainingNon.getTrainingId());
        TStaffmgr staffmgr = tStaffmgrService.selectTStaffmgrByStaffId(tTrainingNon.getStaffId());
        //为参培需补培人员
        if (tTrainingNon.getIsNew().equals("0")) {
            String nonParticipants = null;
            //O为所有未参会人员都参加过补培，1为存在未补培的未参会人员
            int isFinish = 0;
            //判断新增的这条数据信息是否完整，缺失数据则变成1
            if (tTrainingNon.getTrainingDate() == null || tTrainingNon.getTrainingStaff() == null) {
                logger.info("新增数据缺失");
                isFinish = 1;
                if (tTraining.getNonParticipants() == null) {
                    logger.info("此前没有未参会人员尚未参加补培");
                    nonParticipants = staffmgr.getName();
                }else {
                    logger.info("此前存在未参会人员尚未参加补培");
                    nonParticipants = tTraining.getNonParticipants() + "," + staffmgr.getName();
                }
            }else {
                //新增数据信息完整情况下，遍历之前的数据，判断数据信息是否完整，缺失数据则变成1
                if (list.size() > 0) {
                    for (TTrainingNon non : list) {
                        if (non.getTrainingDate() == null || non.getTrainingStaff() == null) {
                            isFinish = 1;
                        }
                    }
                    nonParticipants = tTraining.getNonParticipants();
                }
            }
            if (isFinish == 0) {
                //字段isFinish中1为YES，0为NO
                tTraining.setIsfinish(Long.parseLong("1"));
            }else {
                tTraining.setIsfinish(Long.parseLong("0"));
            }
            tTraining.setNonParticipants(nonParticipants);
            logger.info("isFinish：" + isFinish);
            logger.info("nonParticipants：" + nonParticipants);
            tTrainingService.updateTTraining(tTraining);
            //定期类-关联培训成绩
            TTrainingDevice tTrainingDevice = new TTrainingDevice();
            tTrainingDevice.setRegularId(tTraining.getId());
            List<TTrainingDevice> tTrainingDevices = tTrainingDeviceService.selectTTrainingDeviceList(tTrainingDevice);
            for (TTrainingDevice d : tTrainingDevices) {
                if (d.getStaffId().equals(tTrainingNon.getStaffId())) {
                    if (tTrainingNon.getTrainingDate() != null) {
                        d.setStartDate(tTrainingNon.getTrainingDate());
                    }else {
                        d.setStartDate(null);
                    }
                    d.setSupplementary("1");
                    tTrainingDeviceService.updateTTrainingDevice(d);
                }
            }
        }else {
            //新员工补培
            TTrainingDevice tTrainingDevice = new TTrainingDevice();
            tTrainingDevice.setRegularId(tTraining.getId());
            List<TTrainingDevice> tTrainingDevices = tTrainingDeviceService.selectTTrainingDeviceList(tTrainingDevice);
            //确定之前没有此人对应培训数据，0为没有，1为有
            int neverHave = 0;
            for (TTrainingDevice d : tTrainingDevices) {
                if (d.getStaffId().equals(tTrainingNon.getStaffId())) {
                    neverHave = 1;
                }
            }
            logger.info("是否之前有此人数据:" + neverHave);
            if (neverHave == 0) {
                TTrainingDevice newDevice = new TTrainingDevice();
                newDevice.setStaffId(staffmgr.getStaffid());
                newDevice.setRegularId(tTraining.getId());
                newDevice.setStartDate(tTrainingNon.getTrainingDate());
                //新员工标识为2
                newDevice.setSupplementary("2");
                tTrainingDeviceService.insertTTrainingDevice(newDevice);
            }else {
                for (TTrainingDevice d : tTrainingDevices) {
                    if (d.getStaffId().equals(tTrainingNon.getStaffId())) {
                        d.setStartDate(tTrainingNon.getTrainingDate());
                        d.setSupplementary("2");
                        tTrainingDeviceService.updateTTrainingDevice(d);
                    }
                }
            }
        }
        return toAjax(tTrainingNonService.insertTTrainingNon(tTrainingNon));
    }

    /**
     * 修改培训计划未参加人员
     */
    @Log(title = "培训计划未参加人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTrainingNon tTrainingNon)
    {
        logger.info("tTrainingNon:" + tTrainingNon);
        List<TTrainingNon> list = tTrainingNonService.selectTTrainingNonList(tTrainingNon);
        TTraining tTraining = tTrainingService.selectTTrainingById(tTrainingNon.getTrainingId());
        TStaffmgr staffmgr = tStaffmgrService.selectTStaffmgrByStaffId(tTrainingNon.getStaffId());
        //为参培需补培人员
        if (tTrainingNon.getIsNew().equals("0")) {
            //查询此前是否存在未参会人员尚未参加补培
            String[] nons = null;
            if (tTraining.getNonParticipants() != null) {
                nons = tTraining.getNonParticipants().split(",");
            }
            String nonParticipants = null;
            int isFinish = 0;
            for (TTrainingNon non : list) {
                if (non.getStaffId().equals(tTrainingNon.getStaffId())) {
                    logger.info("循环数据循环到修改的人员信息");
                    if (tTrainingNon.getTrainingDate() == null || tTrainingNon.getTrainingStaff() == null) {
                        logger.info("该条修改数据缺失");
                        isFinish = 1;
                        //此前更改人员是否在要尚未参加补培人员名单中，0为没有，1为有
                        int haveNon = 0;
                        if (nons == null) {
                            logger.info("此前没有未参会人员尚未参加补培");
                            nonParticipants = staffmgr.getName();
                        }else {
                            logger.info("此前存在未参会人员尚未参加补培");
                            for (String n : nons) {
                                if (n.equals(staffmgr.getName())) {
                                    haveNon = 1;
                                }
                            }
                            if (haveNon == 0) {
                                logger.info("此前未参加补培人员不包括更改人员");
                                nonParticipants = tTraining.getNonParticipants() + "," + staffmgr.getName();
                            }else {
                                logger.info("此前未参加补培人员包括更改人员");
                                nonParticipants = tTraining.getNonParticipants();
                            }
                        }
                    }else {
                        logger.info("该条修改数据完整");
                        if (nons != null) {
                            logger.info("此前存在未参会人员尚未参加补培");
                            //此前未补培人员列表循环index
                            int m = 0;
                            //是否需要更改未补培人员名单,0为不需要，1为需要
                            int t = 0;
                            String[] newNons = new String[nons.length - 1];
                            for (String n : nons) {
                                if (n.equals(staffmgr.getName())) {
                                    logger.info("此前未参加补培人员包括更改人员");
                                    t = 1;
                                    //删除该人员的名称
                                    int j = m;
                                    for(int i = 0, k = 0; i < nons.length; i++){
                                        if(i != j){
                                            newNons[k] = nons[i];
                                            k++;
                                        }
                                    }
                                }
                                m++;
                            }
                            logger.info("删除前人员名单：" + nons.toString());
                            logger.info("删除后人员名单：" + newNons.toString());
                            if (t == 1) {
                                for (int i = 0; i < newNons.length; i++) {
                                    if (i == 0) {
                                        nonParticipants = newNons[i];
                                    }else {
                                        nonParticipants = nonParticipants + "," + newNons[i];
                                    }
                                }
                            }else {
                                nonParticipants = tTraining.getNonParticipants();
                            }
                        }
                    }
                }else {
                    if (non.getTrainingDate() == null || non.getTrainingStaff() == null) {
                        isFinish = 1;
                    }
                }
            }
            if (isFinish == 0) {
                tTraining.setIsfinish(Long.parseLong("1"));
            }else {
                tTraining.setIsfinish(Long.parseLong("0"));
            }
            tTraining.setNonParticipants(nonParticipants);
            logger.info("isFinish：" + isFinish);
            logger.info("nonParticipants：" + nonParticipants);
            tTrainingService.updateTTraining(tTraining);
            //定期类-关联培训成绩
            TTrainingDevice tTrainingDevice = new TTrainingDevice();
            tTrainingDevice.setRegularId(tTraining.getId());
            List<TTrainingDevice> tTrainingDevices = tTrainingDeviceService.selectTTrainingDeviceList(tTrainingDevice);
            for (TTrainingDevice d : tTrainingDevices) {
                if (d.getStaffId().equals(tTrainingNon.getStaffId())) {
                    if (tTrainingNon.getTrainingDate() != null) {
                        d.setStartDate(tTrainingNon.getTrainingDate());
                    }else {
                        d.setStartDate(null);
                    }
                    d.setSupplementary("1");
                    tTrainingDeviceService.updateTTrainingDevice(d);
                }
            }
        }else {
            //新员工补培
            TTrainingDevice tTrainingDevice = new TTrainingDevice();
            tTrainingDevice.setRegularId(tTraining.getId());
            tTrainingDevice.setStaffId(tTrainingNon.getStaffId());
            List<TTrainingDevice> tTrainingDevices = tTrainingDeviceService.selectTTrainingDeviceList(tTrainingDevice);
            tTrainingDevices.get(0).setStartDate(tTrainingNon.getTrainingDate());
            tTrainingDeviceService.updateTTrainingDevice(tTrainingDevices.get(0));
        }
        return toAjax(tTrainingNonService.updateTTrainingNon(tTrainingNon));
    }

    /**
     * 删除培训计划未参加人员
     */
    @Log(title = "培训计划未参加人员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id : ids) {
            TTrainingNon tTrainingNon = tTrainingNonService.selectTTrainingNonById(id);
            List<TTrainingNon> list = tTrainingNonService.selectTTrainingNonList(tTrainingNon);
            TTraining tTraining = tTrainingService.selectTTrainingById(tTrainingNon.getTrainingId());
            TStaffmgr staffmgr = tStaffmgrService.selectTStaffmgrByStaffId(tTrainingNon.getStaffId());
            //为参培需补培人员
            if (tTrainingNon.getIsNew().equals("0")) {
                String nonParticipants = null;
                int isFinish = 0;
                for (TTrainingNon non : list) {
                    if (non.getStaffId().equals(tTrainingNon.getStaffId())) {
                        if (tTrainingNon.getTrainingDate() == null || tTrainingNon.getTrainingStaff() == null) {
                            logger.info("删除的该人员存在未补培人员名单中");
                            String[] nons = tTraining.getNonParticipants().split(",");
                            //此前未补培人员列表循环index
                            int m = 0;
                            String[] newNons = new String[nons.length - 1];
                            for (String n : nons) {
                                if (n.equals(staffmgr.getName())) {
                                    //删除该人员的名称
                                    int j = m;
                                    for(int i = 0, k = 0; i < nons.length; i++){
                                        if(i != j){
                                            newNons[k] = nons[i];
                                            k++;
                                        }
                                    }
                                }
                                m++;
                            }
                            for (int i = 0; i < newNons.length; i++) {
                                if (i == 0) {
                                    nonParticipants = newNons[i];
                                }else {
                                    nonParticipants = nonParticipants + "," + newNons[i];
                                }
                            }
                        }else {
                            logger.info("删除的该人员不在未补培人员名单中");
                            nonParticipants = tTraining.getNonParticipants();
                        }
                    }else {
                        if (non.getTrainingDate() == null || non.getTrainingStaff() == null) {
                            isFinish = 1;
                        }
                    }
                }
                if (isFinish == 0) {
                    tTraining.setIsfinish(Long.parseLong("1"));
                }else {
                    tTraining.setIsfinish(Long.parseLong("0"));
                }
                tTraining.setNonParticipants(nonParticipants);
                logger.info("isFinish：" + isFinish);
                logger.info("nonParticipants：" + nonParticipants);
                tTrainingService.updateTTraining(tTraining);
                //定期类-关联培训成绩
                TTrainingDevice tTrainingDevice = new TTrainingDevice();
                tTrainingDevice.setRegularId(tTraining.getId());
                tTrainingDevice.setStaffId(tTrainingNon.getStaffId());
                List<TTrainingDevice> tTrainingDevices = tTrainingDeviceService.selectTTrainingDeviceList(tTrainingDevice);
                if (tTrainingDevices.get(0).getStaffId().equals(tTrainingNon.getStaffId())) {
                    tTrainingDevices.get(0).setStartDate(tTraining.getStartDate());
                    tTrainingDevices.get(0).setSupplementary("0");
                    tTrainingDeviceService.updateTTrainingDevice(tTrainingDevices.get(0));
                }
            }else {
                //新员工补培
                TTrainingDevice tTrainingDevice = new TTrainingDevice();
                tTrainingDevice.setRegularId(tTraining.getId());
                tTrainingDevice.setStaffId(tTrainingNon.getStaffId());
                List<TTrainingDevice> tTrainingDevices = tTrainingDeviceService.selectTTrainingDeviceList(tTrainingDevice);
                if (tTrainingDevices.get(0).getStaffId().equals(tTrainingNon.getStaffId())) {
                    tTrainingDeviceService.deleteTTrainingDeviceById(tTrainingDevices.get(0).getId());
                }
            }
        }
        return toAjax(tTrainingNonService.deleteTTrainingNonByIds(ids));
    }
}
