package com.yanzhu.jh.work.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.DeptTypeEnum;
import com.ruoyi.common.enums.SysRoleEnum;
import com.ruoyi.system.service.ISysDeptService;
import com.yanzhu.jh.project.domain.SurProject;
import com.yanzhu.jh.project.domain.SurProjectUnitInfo;
import com.yanzhu.jh.project.service.ISurProjectService;
import com.yanzhu.jh.project.service.ISurProjectUnitInfoService;
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.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.yanzhu.jh.work.domain.WorkTrain;
import com.yanzhu.jh.work.service.IWorkTrainService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 演练培训Controller
 * 
 * @author JiangYuQi
 * @date 2023-08-09
 */
@RestController
@RequestMapping("/work/workTrain")
public class WorkTrainController extends BaseController
{
    @Autowired
    private IWorkTrainService workTrainService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISurProjectService surProjectService;

    @Autowired
    private ISurProjectUnitInfoService surProjectUnitInfoService;

    /**
     * 查询演练培训列表
     */
    @PreAuthorize("@ss.hasPermi('work:workTrain:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkTrain workTrain)
    {
        startPage();
        workTrain.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(workTrain.getNowRole())){
            workTrain.setNowDept(Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            workTrain.setNowDept(Convert.toStr(getDeptId()));
        }
        workTrain.setNowUser(Convert.toStr(getUserId()));
        workTrain.setNowUserName(getUsername());
        List<WorkTrain> list = workTrainService.selectWorkTrainList(workTrain);
        return getDataTable(list);
    }

    /**
     * 导出演练培训列表
     */
    @PreAuthorize("@ss.hasPermi('work:workTrain:export')")
    @Log(title = "演练培训", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkTrain workTrain)
    {
        workTrain.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(workTrain.getNowRole())){
            workTrain.setNowDept(Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            workTrain.setNowDept(Convert.toStr(getDeptId()));
        }
        workTrain.setNowUser(Convert.toStr(getUserId()));
        workTrain.setNowUserName(getUsername());
        List<WorkTrain> list = workTrainService.selectWorkTrainList(workTrain);
        ExcelUtil<WorkTrain> util = new ExcelUtil<WorkTrain>(WorkTrain.class);
        util.exportExcel(response, list, "演练培训数据");
    }

    /**
     * 获取演练培训详细信息
     */
    @PreAuthorize("@ss.hasPermi('work:workTrain:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(workTrainService.selectWorkTrainById(id));
    }

    /**
     * 新增演练培训
     */
    @PreAuthorize("@ss.hasPermi('work:workTrain:add')")
    @Log(title = "演练培训", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkTrain workTrain)
    {
        return toAjax(workTrainService.insertWorkTrain(workTrain));
    }

    /**
     * 修改演练培训
     */
    @PreAuthorize("@ss.hasPermi('work:workTrain:edit')")
    @Log(title = "演练培训", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkTrain workTrain)
    {
        return toAjax(workTrainService.updateWorkTrain(workTrain));
    }

    /**
     * 删除演练培训
     */
    @PreAuthorize("@ss.hasPermi('work:workTrain:remove')")
    @Log(title = "演练培训", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(workTrainService.deleteWorkTrainByIds(ids));
    }

    /**
     * 查询协同演练部门列表
     */
    @PreAuthorize("@ss.hasPermi('work:workTrain:query')")
    @GetMapping("/projectUnitList")
    public AjaxResult projectUnitList(SurProjectUnitInfo surProjectUnitInfo)
    {
        SysDept sysDept = sysDeptService.selectDeptById(getDeptId());
        if(DeptTypeEnum.JTGS.getCode().equals(sysDept.getTypeFlag()) || DeptTypeEnum.ZGS.getCode().equals(sysDept.getTypeFlag())){
            SurProject surProject = surProjectService.selectSurProjectById(surProjectUnitInfo.getProjectId());
            surProjectUnitInfo.setNowDept(Convert.toStr(surProject.getDeptId()));
        }else{
            surProjectUnitInfo.setNowDept(Convert.toStr(getDeptId()));
        }
        return success(surProjectUnitInfoService.selectSurProjectUnitInfoList(surProjectUnitInfo));
    }

}
