package com.ruoyi.project.training.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.training.mapper.TWorkcertificateCbpsMapper;
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.TWorkcertificateCbps;
import com.ruoyi.project.training.service.ITWorkcertificateCbpsService;
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-07-14
 */
@RestController
@RequestMapping("/training/workcertificateCbps")
public class TWorkcertificateCbpsController extends BaseController
{
    @Autowired
    private ITWorkcertificateCbpsService tWorkcertificateCbpsService;
    @Resource
    private TWorkcertificateCbpsMapper tWorkcertificateCbpsMapper;
    @Resource
    private TStaffmgrMapper tStaffmgrMapper;

    /**
     * 查询作业证书一览列表
     */
    @PreAuthorize("@ss.hasPermi('training:workcertificate:list')")
    @GetMapping("/list")
    public TableDataInfo list(TWorkcertificateCbps tWorkcertificateCbps)
    {
        try {
            //  同步人员
            syncData();
        }catch (Exception e) {
            logger.error(JSON.toJSONString(e));
        };
        startPage();
        List<TWorkcertificateCbps> list = tWorkcertificateCbpsService.selectTWorkcertificateCbpsList(tWorkcertificateCbps);
        return getDataTable(list);
    }

    public AjaxResult syncData() {
        List<String> delList = tWorkcertificateCbpsMapper.queryNeedDeleteIds();
        if (delList.size() > 0){
            for (String id : delList
                 ) {
                tWorkcertificateCbpsMapper.deleteTWorkcertificateCbpsByStaffId(id);
            }
        }

        List<String> addList = tWorkcertificateCbpsMapper.queryNeedInsertIds();
        if (addList.size() > 0) {
            for (String id: addList
                 ) {
                TStaffmgr staffmgr = tStaffmgrMapper.selectTStaffmgrByStaffId(id);
                TWorkcertificateCbps t = new TWorkcertificateCbps();
                t.setEmployeeid(staffmgr.getStaffid());
                t.setClasses(staffmgr.getTeam());
                t.setName(staffmgr.getName());
                t.setDeptId(staffmgr.getDeptId());
                t.setPlantCode(staffmgr.getPlantCode());
                tWorkcertificateCbpsMapper.insertTWorkcertificateCbps(t);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 导出作业证书一览列表
     */
    @PreAuthorize("@ss.hasPermi('training:workcertificate:export')")
    @Log(title = "作业证书一览", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TWorkcertificateCbps tWorkcertificateCbps)
    {
        List<TWorkcertificateCbps> list = tWorkcertificateCbpsService.selectTWorkcertificateCbpsList(tWorkcertificateCbps);
        ExcelUtil<TWorkcertificateCbps> util = new ExcelUtil<TWorkcertificateCbps>(TWorkcertificateCbps.class);
        return util.exportExcel(list, "workcertificateCbps");
    }

    /**
     * 获取作业证书一览详细信息
     */
    @PreAuthorize("@ss.hasPermi('training:workcertificate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tWorkcertificateCbpsService.selectTWorkcertificateCbpsById(id));
    }

    /**
     * 新增作业证书一览
     */
    @PreAuthorize("@ss.hasPermi('training:workcertificate:add')")
    @Log(title = "作业证书一览", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TWorkcertificateCbps tWorkcertificateCbps)
    {
        return toAjax(tWorkcertificateCbpsService.insertTWorkcertificateCbps(tWorkcertificateCbps));
    }

    /**
     * 修改作业证书一览
     */
    @PreAuthorize("@ss.hasPermi('training:workcertificate:edit')")
    @Log(title = "作业证书一览", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TWorkcertificateCbps tWorkcertificateCbps)
    {
        return toAjax(tWorkcertificateCbpsService.updateTWorkcertificateCbps(tWorkcertificateCbps));
    }

    /**
     * 删除作业证书一览
     */
    @PreAuthorize("@ss.hasPermi('training:workcertificate:remove')")
    @Log(title = "作业证书一览", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tWorkcertificateCbpsService.deleteTWorkcertificateCbpsByIds(ids));
    }
}
