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

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

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.training.domain.TTraining;
import com.ruoyi.project.training.newstaff.domain.*;
import com.ruoyi.project.training.newstaff.mapper.TTnFirstplanMapper;
import com.ruoyi.project.training.newstaff.mapper.TTnSchoolplanMapper;
import com.ruoyi.project.training.newstaff.mapper.TTnTransferplanMapper;
import com.ruoyi.project.training.newstaff.service.ITTnFirstplanService;
import com.ruoyi.project.training.newstaff.service.ITTnFirstplanTmplService;
import com.ruoyi.project.training.spec.domain.TStSuccessor;
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.newstaff.service.ITTnNewService;
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 ruoyi
 * @date 2023-06-29
 */
@RestController
@RequestMapping("/newstaff/tnNew")
public class TTnNewController extends BaseController {
    @Autowired
    private ITTnNewService tTnNewService;
    @Autowired
    private ITStaffmgrService tStaffmgrService;
    @Resource
    private TTnFirstplanMapper tTnFirstplanMapper;
    @Resource
    private TTnSchoolplanMapper tTnSchoolplanMapper;
    @Resource
    private TTnTransferplanMapper tTnTransferplanMapper;
    @Autowired
    private ITTnFirstplanService tTnFirstplanService;

    /**
     * 查询新员工培训列表
     */
    @PreAuthorize("@ss.hasPermi('newstaff:tnNew:list')")
    @GetMapping("/list")
    public TableDataInfo list(TTnNew tTnNew) {
        startPage();
        List<TTnNew> list = tTnNewService.selectTTnNewList(tTnNew);
        //线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        final CountDownLatch latch = new CountDownLatch(list.size()); //相同线程数量的计数
        for (TTnNew t : list
        ) {
            executorService.execute(() -> {
                try {
                    //岗前培训
                    TTnFirstplan tTnFirstplan = new TTnFirstplan();
                    tTnFirstplan.setNewId(t.getNewId());
                    List<TTnFirstplan> firstplanAll = tTnFirstplanMapper.selectTTnFirstplanListByNewId(tTnFirstplan);
                    tTnFirstplan.setPlanStatus(1l);
                    List<TTnFirstplan> firstplanNow = tTnFirstplanMapper.selectTTnFirstplanListByNewId(tTnFirstplan);
                    Integer firstAll = firstplanAll.size();
                    Integer firstNow = firstplanNow.size();
                    Integer secAll = 0;
                    Integer secNow = 0;
                    if (t.getPlanType() == 1) {
                        TTnSchoolplan tTnSchoolplan = new TTnSchoolplan();
                        tTnSchoolplan.setNewId(t.getNewId());
                        List<TTnSchoolplan> schoolplansAll = tTnSchoolplanMapper.selectTTnSchoolplanListByNewId(tTnSchoolplan);
                        tTnSchoolplan.setPlanStatus(1l);
                        List<TTnSchoolplan> schoolplansNow = tTnSchoolplanMapper.selectTTnSchoolplanListByNewId(tTnSchoolplan);
                        secAll = schoolplansAll.size();
                        secNow = schoolplansNow.size();
                    } else {
                        TTnTransferplan tTnTransferplan = new TTnTransferplan();
                        tTnTransferplan.setNewId(t.getNewId());
                        List<TTnTransferplan> transferplanAll = tTnTransferplanMapper.selectTTnTransferplanListByNewId(tTnTransferplan);
                        tTnTransferplan.setPlanStatus(1l);
                        List<TTnTransferplan> transferplanNow = tTnTransferplanMapper.selectTTnTransferplanListByNewId(tTnTransferplan);
                        secAll = transferplanAll.size();
                        secNow = transferplanNow.size();
                    }
                    if (secAll + firstAll != 0) {
                        String finsh = String.valueOf((secNow + firstNow) * 100 / (secAll + firstAll));
                        t.setPlanFinish(finsh);
                    } else {
                        t.setPlanFinish("0");
                    }
                } catch (Exception e) {
                    logger.error("e:", e);
                }finally {
                    latch.countDown(); //线程计数
                }
            });
        }

        try {
            latch.await(); //线程计数完毕后继续执行
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        executorService.shutdown();
        return getDataTable(list);
    }

    @GetMapping("/trainNewstaffCount")
    public AjaxResult trainNewstaffCount(TTnNew tTnNew) {
        tTnNew.setPlanStatus(1l);
        List<TTnNew> list1 = tTnNewService.selectTTnNewList(tTnNew);
        tTnNew.setPlanStatus(0l);
        List<TTnNew> list0 = tTnNewService.selectTTnNewList(tTnNew);
        HashMap<String, Object> result = new HashMap<>();
        result.put("count1", list1.size());
        result.put("count1Name", "已完成");
        result.put("count0", list0.size());
        result.put("count0Name", "未完成");
        return AjaxResult.success(result);
    }

    /**
     * 查询下拉
     */
    @GetMapping("/listStaff")
    public AjaxResult listStaff() {
        List<TStaffmgr> list = tStaffmgrService.selectAllTStaffmgrList(new TStaffmgr());
        return AjaxResult.success(list);
    }

    /**
     * 导出新员工培训列表
     */
    @PreAuthorize("@ss.hasPermi('newstaff:tnNew:export')")
    @Log(title = "新员工培训", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TTnNew tTnNew) {
        List<TTnNew> list = tTnNewService.selectTTnNewList(tTnNew);
        ExcelUtil<TTnNew> util = new ExcelUtil<TTnNew>(TTnNew.class);
        return util.exportExcel(list, "tnNew");
    }

    /**
     * 获取新员工培训详细信息
     */
    @PreAuthorize("@ss.hasPermi('newstaff:tnNew:query')")
    @GetMapping(value = "/{newId}")
    public AjaxResult getInfo(@PathVariable("newId") Long newId) {
        return AjaxResult.success(tTnNewService.selectTTnNewById(newId));
    }

    /**
     * 新增新员工培训
     */
    @PreAuthorize("@ss.hasPermi('newstaff:tnNew:add')")
    @Log(title = "新员工培训", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTnNew tTnNew) {
        tTnNewService.insertTTnNew(tTnNew);

        return toAjax(1);
    }

    /**
     * 修改新员工培训
     */
    @PreAuthorize("@ss.hasPermi('newstaff:tnNew:edit')")
    @Log(title = "新员工培训", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTnNew tTnNew) {
        return toAjax(tTnNewService.updateTTnNew(tTnNew));
    }

    /**
     * 删除新员工培训
     */
    @PreAuthorize("@ss.hasPermi('newstaff:tnNew:remove')")
    @Log(title = "新员工培训", businessType = BusinessType.DELETE)
    @DeleteMapping("/{newIds}")
    public AjaxResult remove(@PathVariable Long[] newIds) {
        return toAjax(tTnNewService.deleteTTnNewByIds(newIds));
    }
}
