package com.zd.exam.controller;

import java.util.List;
import java.io.IOException;
import java.util.Optional;
import javax.servlet.http.HttpServletResponse;

import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.exam.domain.ElSafeTrain;
import com.zd.exam.domain.vo.ElSafeTrainVO;
import com.zd.exam.service.IElSafeTrainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.utils.poi.ExcelUtil;
import com.zd.common.core.web.page.TableDataInfo;

/**
 * 安全培训Controller
 *
 * @author zd
 * @date 2021-09-07
 */
@RestController
@RequestMapping("/train")
@Api(tags = {"安全培训"})
public class ElSafeTrainController extends BaseController {
    @Autowired
    private IElSafeTrainService elSafeTrainService;

    /**
     * 查询安全培训列表  "laboratory:train:list"
     */
    @ApiOperation(value = "查询安全培训列表" ,notes="权限字符："+PerPrefix.LABORATORY_TRAIN+ PerFun.LIST)
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_TRAIN+ PerFun.LIST)
    @GetMapping("/list")
    public TableDataInfo list(ElSafeTrain elSafeTrain) {
        startPage();
        List<ElSafeTrain> list = elSafeTrainService.selectElSafeTrainList(elSafeTrain);
        return getDataTable(list);
    }

    /**
     * 查询安全培训下拉框
     */
    @ApiOperation(value = "查询安全培训下拉框")
    @PostMapping("/queryOption")
    public AjaxResult queryOption(@RequestBody ElSafeTrain elSafeTrain) {
        List<ElSafeTrain> list = elSafeTrainService.selectElSafeTrainList(elSafeTrain);
        return AjaxResult.success(list);
    }


    /**
     * 查询我的安全培训列表-增加已学标识
     */
    @ApiOperation(value = "查询我的安全培训列表-增加已学标识")
    @GetMapping("/list/my")
    public TableDataInfo mylist(ElSafeTrain elSafeTrain) {
        startPage();
        List<ElSafeTrainVO> list = elSafeTrainService.selectElSafeTrainVOList(elSafeTrain);
        return getDataTable(list);
    }


    /**
     * 导出安全培训列表  "laboratory:train:export"
     */
    @ApiOperation(value = "导出安全培训列表 ")
    @PreAuthorize(hasPermi =PerPrefix.LABORATORY_TRAIN+ PerFun.EXPORT)
    @Log(title = "安全培训", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElSafeTrain elSafeTrain) throws IOException {
        List<ElSafeTrain> list = elSafeTrainService.selectElSafeTrainList(elSafeTrain);
        ExcelUtil<ElSafeTrain> util = new ExcelUtil<ElSafeTrain>(ElSafeTrain.class);
        util.exportExcel(response, list, "安全培训数据");
    }

    /**
     * 获取安全培训详细信息
     */
//    @PreAuthorize(hasPermi = "laboratory:train:query")
    @ApiOperation(value = "获取安全培训详细信息 ")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(elSafeTrainService.selectElSafeTrainById(id));
    }

    /**
     * 获取安全培训详细信息-增加已学信息  "laboratory:train:query"
     */
    @ApiOperation(value = "获取安全培训详细信息-增加已学信息",notes="权限字符："+PerPrefix.LABORATORY_TRAIN+ PerFun.QUERY)
    @PreAuthorize(hasPermi =PerPrefix.LABORATORY_TRAIN+ PerFun.QUERY)
    @GetMapping(value = "/{id}/learn")
    public ElSafeTrainVO getInfoAndLeanr(@PathVariable("id") Long id) {
        ElSafeTrain elSafeTrain = new ElSafeTrain();
        elSafeTrain.setId(id);
        List<ElSafeTrainVO> list = elSafeTrainService.selectElSafeTrainVOList(elSafeTrain);
        return Optional.ofNullable(list.get(0)).orElseGet(null);

    }

    /**
     * 新增安全培训  "laboratory:train:add"
     */
    @ApiOperation(value = "新增安全培训 ",notes="权限字符："+PerPrefix.LABORATORY_TRAIN+ PerFun.ADD)
    @PreAuthorize(hasPermi =PerPrefix.LABORATORY_TRAIN+ PerFun.ADD)
    @Log(title = "安全培训", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElSafeTrain elSafeTrain) {
        return toAjax(elSafeTrainService.insertElSafeTrain(elSafeTrain));
    }

    /**
     * 修改安全培训  "laboratory:train:edit"
     */
    @ApiOperation(value = "修改安全培训",notes="权限字符："+PerPrefix.LABORATORY_TRAIN+ PerFun.EDIT)
    @PreAuthorize(hasPermi =PerPrefix.LABORATORY_TRAIN+ PerFun.EDIT)
    @Log(title = "安全培训", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElSafeTrain elSafeTrain) {
        return toAjax(elSafeTrainService.updateElSafeTrain(elSafeTrain));
    }

    /**
     * 删除安全培训  "laboratory:train:remove"
     */
    @ApiOperation(value = "删除安全培训 ",notes="权限字符："+PerPrefix.LABORATORY_TRAIN+ PerFun.REMOVE)
    @PreAuthorize(hasPermi =PerPrefix.LABORATORY_TRAIN+ PerFun.REMOVE)
    @Log(title = "安全培训", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(elSafeTrainService.deleteElSafeTrainByIds(ids));
    }
}
