package com.zd.laboratory.controller;

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

import cn.hutool.core.collection.CollUtil;
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 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.LabSubjectLayout;
import com.zd.laboratory.service.ILabSubjectLayoutService;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.zd.common.core.web.page.TableDataInfo;

/**
 * 实验室布局Controller
 *
 * @author zd
 * @date 2021-09-23
 */
@RestController
@Api(tags = "【实验室布局】")
@RequestMapping("/layout")
public class LabSubjectLayoutController extends BaseController {
    @Autowired
    private ILabSubjectLayoutService labSubjectLayoutService;

    /**
     * 查询实验室布局列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_LAYOUT + PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询实验室布局列表")
    public TableDataInfo<LabSubjectLayout> list(LabSubjectLayout labSubjectLayout) {
        startPage();
        List<LabSubjectLayout> list = labSubjectLayoutService.selectLabSubjectLayoutList(labSubjectLayout);
        return getDataTable(list);
    }

    /**
     * 查询楼层下的布局信息-列表-不分页
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_LAYOUT + PerFun.LIST)
    @GetMapping("/{floorId}/list")
    @ApiOperation(value = "查询楼层下的布局信息")
    public ResultData listByFloorId(@PathVariable("floorId") Long floorId) {
        LabSubjectLayout subjectLayout = new LabSubjectLayout();
        subjectLayout.setFloorId(floorId);
        List<LabSubjectLayout> list = labSubjectLayoutService.selectLabSubjectLayoutList(subjectLayout);
        return ResultData.success(list);
    }

    /**
     * 查询楼层下的未分配布局信息-列表-不分页
     */
    @GetMapping("/{floorId}/noUse/list")
    @ApiOperation(value = "查询楼层下的布局信息")
    public ResultData listByFloorIdNoUse(@PathVariable("floorId") Long floorId) {
        LabSubjectLayout subjectLayout = new LabSubjectLayout();
        subjectLayout.setFloorId(floorId);
        subjectLayout.setIsSubject(1);
        List<LabSubjectLayout> list = labSubjectLayoutService.selectLabSubjectLayoutNoUseList(subjectLayout);
        return ResultData.success(list);
    }

    /**
     * 查询楼层下的未分配布局信息-列表-不分页 排除ID(排除自身)
     */
    @GetMapping("/{floorId}/{ex}/noUse/list")
    @ApiOperation(value = "查询楼层下的未分配布局信息-列表-不分页 排除ID(排除自身)")
    public ResultData listByFloorIdNoUseExId(@PathVariable("floorId") Long floorId, @PathVariable("ex") Long exId) {
        LabSubjectLayout subjectLayout = new LabSubjectLayout();
        subjectLayout.setFloorId(floorId);
        subjectLayout.setId(exId);
//        subjectLayout.setSubjectId(-1L);
        List<LabSubjectLayout> list = labSubjectLayoutService.selectLabSubjectLayoutNoUseList(subjectLayout);
        return ResultData.success(list);
    }

    /**
     * 导出实验室布局列表
     */
    @ApiOperation(value = "导出实验室布局列表")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_LAYOUT + PerFun.EXPORT)
    @Log(title = "实验室布局", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LabSubjectLayout labSubjectLayout) throws IOException {
        List<LabSubjectLayout> list = labSubjectLayoutService.selectLabSubjectLayoutList(labSubjectLayout);
        ExcelUtil<LabSubjectLayout> util = new ExcelUtil<LabSubjectLayout>(LabSubjectLayout.class);
        util.exportExcel(response, list, "实验室布局数据");
    }

    /**
     * 获取实验室布局详细信息
     */
    @ApiOperation(value = "获取实验室布局详细信息")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_LAYOUT + PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public ResultData<LabSubjectLayout> getInfo(@PathVariable("id") Long id) {
        return ResultData.success(labSubjectLayoutService.selectLabSubjectLayoutById(id));
    }

    /**
     * 获取实验室布局详细信息-通过实验室ID（如果有多个只获取其中一个）
     */
    @ApiOperation(value = "获取实验室布局详细信息-通过实验室ID（如果有多个只获取其中一个）")
//    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_LAYOUT + PerFun.QUERY)
    @GetMapping(value = "/subId/{subId}")
    public ResultData<LabSubjectLayout> getInfoBySubId(@PathVariable("subId") Long id) {
        LabSubjectLayout labSubjectLayout = new LabSubjectLayout();
        labSubjectLayout.setSubjectId(id);
        List<LabSubjectLayout> labSubjectLayouts = labSubjectLayoutService.selectLabSubjectLayoutList(labSubjectLayout);
        if (CollUtil.isEmpty(labSubjectLayouts)) return ResultData.success();
        return ResultData.success(labSubjectLayouts.get(0));
    }


    /**
     * 新增实验室布局
     */
    @ApiOperation(value = "新增实验室布局")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_LAYOUT + PerFun.ADD)
    @Log(title = "实验室布局", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody LabSubjectLayout labSubjectLayout) {
        return ResultData.result(labSubjectLayoutService.insertLabSubjectLayout(labSubjectLayout));
    }

    /**
     * 修改实验室布局
     */
    @ApiOperation(value = "修改实验室布局")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_LAYOUT + PerFun.EDIT)
    @Log(title = "实验室布局", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody LabSubjectLayout labSubjectLayout) {
        return ResultData.result(labSubjectLayoutService.updateLabSubjectLayout(labSubjectLayout));
    }

    /**
     * 删除实验室布局
     */
    @ApiOperation(value = "删除实验室布局")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_LAYOUT + PerFun.REMOVE)
    @Log(title = "实验室布局", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids) {
        return ResultData.result(labSubjectLayoutService.deleteLabSubjectLayoutByIds(ids));
    }


    /**
     * 获取实验室布局详细信息
     */
    @ApiOperation(value = "获取实验室布局详细信息")
    @GetMapping(value = "/noauth/{id}")
    public ResultData<LabSubjectLayout> getNoauthInfo(@PathVariable("id") Long id) {
        return ResultData.success(labSubjectLayoutService.selectLabSubjectLayoutById(id));
    }

}
