package com.zd.laboratory.controller;

import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.poi.ExcelUtil;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.core.web.page.TableDataInfo;
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.LabHazardBook;
import com.zd.laboratory.service.ILabHazardBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 危化品安全技术说明书Controller
 *
 * @author liubo
 * @date 2021-11-19
 */
@RestController
@Api(tags = "【危化品安全技术说明书】")
@RequestMapping("/hazard_book")
public class LabHazardBookController extends BaseController {
    @Autowired
    private ILabHazardBookService labHazardBookService;

    /**
     * 查询危化品安全技术说明书列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_HAZARD_BOOK + PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询危化品安全技术说明书列表")
    public TableDataInfo<LabHazardBook> list(LabHazardBook labHazardBook) {
        startPage();
        List<LabHazardBook> list = labHazardBookService.selectLabHazardBookList(labHazardBook);
        return getDataTable(list);
    }

    /**
     * 一体机查询危化品(不分页)
     */
    @GetMapping("/queryOption")
    @ApiOperation(value = "一体机查询危化品")
    public List<LabHazardBook> queryOption(LabHazardBook labHazardBook) {
        List<LabHazardBook> list = labHazardBookService.selectLabHazardBookList(labHazardBook);
        return list;
    }

    /**
     * 导出危化品安全技术说明书列表
     */
    @ApiOperation(value = "导出危化品安全技术说明书列表")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_HAZARD_BOOK + PerFun.EXPORT)
    @Log(title = "危化品安全技术说明书", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LabHazardBook labHazardBook) throws IOException {
        List<LabHazardBook> list = labHazardBookService.selectLabHazardBookList(labHazardBook);
        ExcelUtil<LabHazardBook> util = new ExcelUtil<LabHazardBook>(LabHazardBook.class);
        util.exportExcel(response, list, "危化品安全技术说明书数据");
    }

    /**
     * 小程序二维码 查询数据详情 并记录count
     */
    @ApiOperation(value = "二维码 查询数据详情 并记录count")
    @GetMapping(value = "/lookUp/{id}")
    public ResultData<LabHazardBook> lookUp(@PathVariable("id") Long id) {
        LabHazardBook hazardBook = labHazardBookService.selectLabHazardBookById(id);
        if(hazardBook != null){
            hazardBook.setScanCount(hazardBook.getScanCount() + 1);
            labHazardBookService.updateLabHazardBook(hazardBook);
        }
        return ResultData.success(hazardBook);
    }

    /**
     * 获取危化品安全技术说明书详细信息
     */
    @ApiOperation(value = "获取危化品安全技术说明书详细信息")
    @GetMapping(value = "/{id}")
    public ResultData<LabHazardBook> getInfo(@PathVariable("id") Long id) {
        return ResultData.success(labHazardBookService.selectLabHazardBookById(id));
    }

    /**
     * 新增危化品安全技术说明书
     */
    @ApiOperation(value = "新增危化品安全技术说明书")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_HAZARD_BOOK + PerFun.ADD)
    @Log(title = "危化品安全技术说明书", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody LabHazardBook labHazardBook) {

        labHazardBookService.insertLabHazardBook(labHazardBook);
        return ResultData.result(1);
    }

    /**
     * 修改危化品安全技术说明书
     */
    @ApiOperation(value = "修改危化品安全技术说明书")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_HAZARD_BOOK + PerFun.EDIT)
    @Log(title = "危化品安全技术说明书", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody LabHazardBook labHazardBook) {
        labHazardBookService.updateLabHazardBook(labHazardBook);
        return ResultData.result(1);
    }

    /**
     * 删除危化品安全技术说明书
     */
    @ApiOperation(value = "删除危化品安全技术说明书")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_HAZARD_BOOK + PerFun.REMOVE)
    @Log(title = "危化品安全技术说明书", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids) {
        int count = labHazardBookService.deleteLabHazardBookByIds(ids);
        return ResultData.result(count);
    }


    /**
     * 查询危化品安全技术说明书关联的化学品列表
     */
    @GetMapping("/hazardBookJoinChemical")
    @PreAuthorize(hasPermi = PerPrefix.HPX_CHEMICAL + PerFun.MSDSLIST)
    @ApiOperation(value = "查询危化品安全技术说明书关联的化学品列表")
    public TableDataInfo<LabHazardBook> hazardBookJoinChemical(LabHazardBook labHazardBook) {
        startPage();
        List<LabHazardBook> list = labHazardBookService.hazardBookJoinChemical(labHazardBook);
        return getDataTable(list);
    }
}
