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

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.project.plant.domain.TStaffmgr;
import com.ruoyi.project.plant.mapper.TStaffmgrMapper;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.project.training.domain.TWorkcertificateCbps;
import com.ruoyi.project.training.elearn.mapper.TElRecordMapper;
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.elearn.domain.TElRecord;
import com.ruoyi.project.training.elearn.service.ITElRecordService;
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 ssy
 * @date 2024-06-25
 */
@RestController
@RequestMapping("/elearn/record")
public class TElRecordController extends BaseController
{
    @Autowired
    private ITElRecordService tElRecordService;
    @Resource
    private TElRecordMapper tElRecordMapper;
    @Resource
    private TStaffmgrMapper tStaffmgrMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    /**
     * 查询岗位培训统计列表
     */
    @PreAuthorize("@ss.hasPermi('elearn:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(TElRecord tElRecord)
    {
        try {
            //  同步人员
            syncUser();
        }catch (Exception e) {
            e.printStackTrace();
        };
        startPage();
        List<TElRecord> list = tElRecordService.selectTElRecordList(tElRecord);
        return getDataTable(list);
    }

    /**
     * 查询岗位培训统计列表
     */
    @PreAuthorize("@ss.hasPermi('elearn:record:list')")
    @GetMapping("/syncRecord")
    public AjaxResult syncRecord(TElRecord tElRecord)
    {
        syncRecord();
        return AjaxResult.success();
    }

    private void syncUser() {
       List<String> delList = tElRecordMapper.queryNeedDeleteIds();
        if (delList.size() > 0){
            for (String staffId : delList
            ) {
                tElRecordMapper.deleteTElRecordByStaffId(staffId);
            }
        }
       List<String> addList = tElRecordMapper.queryNeedInsertIds();
        if (addList.size() > 0) {
            for (String id: addList
            ) {

                TStaffmgr staffmgr = tStaffmgrMapper.selectTStaffmgrByStaffId(id);
                SysUser sysUser = sysUserMapper.selectUserByStaffId(staffmgr.getStaffid());

                TElRecord t = new TElRecord();
                t.setStaffId(staffmgr.getStaffid());
                t.setClasses(staffmgr.getTeam());
                t.setName(staffmgr.getName());
                t.setDeptId(staffmgr.getDeptId());
                t.setPlantCode(staffmgr.getPlantCode());
                if (sysUser != null) {
                    t.setUserId(sysUser.getUserId());
                }
                tElRecordMapper.insertTElRecord(t);
            }
        }


    }

    private void syncRecord(){
        TElRecord tElRecord = new TElRecord();
        List<TElRecord> list = tElRecordService.selectTElRecordList(tElRecord);

        for (TElRecord t: list
             ) {
            if (t.getUserId() != null) {
                TElRecord quData = tElRecordMapper.queryQuData(t.getUserId());
                TElRecord exmaData = tElRecordMapper.queryExamData(t.getUserId());
                t.setQuTotal(quData.getQuTotal());
                t.setQuRight(quData.getQuRight());
                t.setExamTotal(exmaData.getExamTotal());
                t.setExamPassed(exmaData.getExamPassed());
                tElRecordMapper.updateTElRecord(t);
            }
        }



    }
    /**
     * 导出岗位培训统计列表
     */
    @PreAuthorize("@ss.hasPermi('elearn:record:export')")
    @Log(title = "岗位培训统计", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TElRecord tElRecord)
    {
        List<TElRecord> list = tElRecordService.selectTElRecordList(tElRecord);
        ExcelUtil<TElRecord> util = new ExcelUtil<TElRecord>(TElRecord.class);
        return util.exportExcel(list, "record");
    }

    /**
     * 获取岗位培训统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('elearn:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tElRecordService.selectTElRecordById(id));
    }

    /**
     * 新增岗位培训统计
     */
    @PreAuthorize("@ss.hasPermi('elearn:record:add')")
    @Log(title = "岗位培训统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TElRecord tElRecord)
    {
        return toAjax(tElRecordService.insertTElRecord(tElRecord));
    }

    /**
     * 修改岗位培训统计
     */
    @PreAuthorize("@ss.hasPermi('elearn:record:edit')")
    @Log(title = "岗位培训统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TElRecord tElRecord)
    {
        return toAjax(tElRecordService.updateTElRecord(tElRecord));
    }

    /**
     * 删除岗位培训统计
     */
    @PreAuthorize("@ss.hasPermi('elearn:record:remove')")
    @Log(title = "岗位培训统计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tElRecordService.deleteTElRecordByIds(ids));
    }
}
