package com.zd.laboratory.controller;

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

import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.web.domain.ResultData;
import com.zd.laboratory.domain.LabSecuritySubjectmaterial;
import com.zd.laboratory.domain.vo.LabSecurityMaterialDataVO;
import com.zd.laboratory.service.ILabSecuritySubjectmaterialService;
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.laboratory.domain.LabSecurityMaterialData;
import com.zd.laboratory.service.ILabSecurityMaterialDataService;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.zd.common.swagger.config.Knife4jConfiguration;

import com.zd.common.core.web.page.TableDataInfo;

/**
 * 安全准入申请材料配置Controller
 *
 * @author zd
 * @date 2022-01-24
 */
@RestController
@Api(tags = "【安全准入申请材料配置】")
@RequestMapping("/materialdata")
public class LabSecurityMaterialDataController extends BaseController
{
    @Autowired
    private ILabSecurityMaterialDataService labSecurityMaterialDataService;
    @Autowired
    private ILabSecuritySubjectmaterialService iLabSecuritySubjectmaterialService;
    /**
     * 查询安全准入申请材料配置列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_MATERIALDATA + PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询安全准入申请材料配置列表")
    public TableDataInfo<LabSecurityMaterialData> list(LabSecurityMaterialData labSecurityMaterialData)
    {
        startPage();
        List<LabSecurityMaterialData> list = labSecurityMaterialDataService.selectLabSecurityMaterialDataList(labSecurityMaterialData);
        return getDataTable(list);
    }

    /**
     * 导出安全准入申请材料配置列表
     */
    @ApiOperation(value = "导出安全准入申请材料配置列表")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_MATERIALDATA + PerFun.EXPORT)
    @Log(title = "安全准入申请材料配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LabSecurityMaterialData labSecurityMaterialData) throws IOException
    {
        List<LabSecurityMaterialData> list = labSecurityMaterialDataService.selectLabSecurityMaterialDataList(labSecurityMaterialData);
        ExcelUtil<LabSecurityMaterialData> util = new ExcelUtil<LabSecurityMaterialData>(LabSecurityMaterialData.class);
        util.exportExcel(response, list, "安全准入申请材料配置数据");
    }

    /**
     * 获取安全准入申请材料配置详细信息
     */
    @ApiOperation(value = "获取安全准入申请材料配置详细信息")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_MATERIALDATA + PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public ResultData<LabSecurityMaterialData> getInfo(@PathVariable("id") Long id)
    {
        return ResultData.success(labSecurityMaterialDataService.selectLabSecurityMaterialDataById(id));
    }

    /**
     * 新增安全准入申请材料配置
     */
    @ApiOperation(value = "新增安全准入申请材料配置")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_MATERIALDATA + PerFun.ADD)
    @Log(title = "安全准入申请材料配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public ResultData add(@RequestBody LabSecurityMaterialData labSecurityMaterialData)
    {
        return ResultData.result(labSecurityMaterialDataService.insertLabSecurityMaterialData(labSecurityMaterialData));
    }

    /**
     * 修改安全准入申请材料配置
     */
    @ApiOperation(value = "修改安全准入申请材料配置")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_MATERIALDATA + PerFun.EDIT)
    @Log(title = "安全准入申请材料配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody LabSecurityMaterialData labSecurityMaterialData)
    {
        return  ResultData.result(labSecurityMaterialDataService.updateLabSecurityMaterialData(labSecurityMaterialData));
    }

    /**
     * 删除安全准入申请材料配置
     */
    @ApiOperation(value = "删除安全准入申请材料配置")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_MATERIALDATA + PerFun.REMOVE)
    @Log(title = "安全准入申请材料配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids)
    {
        //删除前校验改模板是否有实验室在使用
        LabSecuritySubjectmaterial lss = new LabSecuritySubjectmaterial();
        lss.setMaterialId(ids[0]);
        List<LabSecuritySubjectmaterial> list= iLabSecuritySubjectmaterialService.selectLabSecuritySubjectmaterialList(lss);
        if(list.size()>0){
            return ResultData.fail("该模板已有实验室在使用不可删除！");
        }
        return ResultData.result(labSecurityMaterialDataService.deleteLabSecurityMaterialDataByIds(ids));
    }

    /***
     * 查询实验室的配置模板
     * @param subjectId
     * @return
     */
    @GetMapping("/listTemplate")
    public AjaxResult  listTemplate(Long subjectId) {
        Map<String,Object> map = new HashMap<String,Object>();
        List<LabSecurityMaterialDataVO> listTeacher= labSecurityMaterialDataService.selectLabSecurityMaterialDataTeacher(subjectId);
        List<LabSecurityMaterialDataVO> listStudent= labSecurityMaterialDataService.selectLabSecurityMaterialDataStudent(subjectId);

        /* //教职工模板
        List<LabSecuritySubjectmaterialVO> listTeacher=  labSecuritySubjectmaterialService.selectTeacherList();
        //学生模板
        List<LabSecuritySubjectmaterialVO> listStudent=  labSecuritySubjectmaterialService.selectStudentList();*/
        map.put("listTeacher",listTeacher);
        map.put("listStudent",listStudent);
        return AjaxResult.success(map);
    }
}
