package com.zd.laboratory.controller;

import com.zd.common.core.domain.R;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.SecurityUtils;
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.LabCheckOption;
import com.zd.laboratory.domain.vo.LabCheckOptionExcelVo;
import com.zd.laboratory.service.ILabCheckOptionService;
import com.zd.laboratory.service.impl.WordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 检查项选项Controller
 *
 * @author liubo
 * @date 2021-12-30
 */
@RestController
@Api(tags = "【检查项管理】")
@RequestMapping("/checkOption")
public class LabCheckOptionController extends BaseController {
    @Autowired
    private ILabCheckOptionService labCheckOptionService;
    @Autowired
    private WordService wordService;

    /**
     * 查询检查项选项列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CHECKOPTION + PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询检查项选项列表")
    public R<List<LabCheckOption>> list(LabCheckOption labCheckOption) {
        List<LabCheckOption> list = labCheckOptionService.selectLabCheckOptionList(labCheckOption);
        return R.ok(list);
    }

    /***
     * 查询检查项选项列表 - 小程序
     * @param labCheckOption
     * @return
     */
    @GetMapping("/listApp")
    @ApiOperation(value = "查询检查项选项列表-小程序")
    public R<List<LabCheckOption>> listApp(LabCheckOption labCheckOption) {
        List<LabCheckOption> list = labCheckOptionService.selectLabCheckOptionList(labCheckOption);
        return R.ok(list);
    }

    /**
     * 导出检查项选项模板
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CHECKOPTION + PerFun.EXPORT)
    @ApiOperation(value = "导出检查项选项列表")
    @Log(title = "检查项选项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<LabCheckOptionExcelVo> list = new ArrayList<>();
        ExcelUtil<LabCheckOptionExcelVo> util = new ExcelUtil<>(LabCheckOptionExcelVo.class);
        util.exportExcel(response, list, "检查项模板");
    }

    /**
     * 导入检查项选项列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CHECKOPTION + PerFun.IMPORT)
    @ApiOperation(value = "导入检查项选项列表")
    @Log(title = "检查项选项", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public R importData(MultipartFile file) {
        labCheckOptionService.importData(file);
        return R.ok();
    }

    /**
     * 新增检查项选项
     */
    @ApiOperation(value = "新增检查项选项")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CHECKOPTION + PerFun.ADD)
    @Log(title = "检查项选项", businessType = BusinessType.INSERT)
    @PostMapping
    public R<LabCheckOption> add(@RequestBody LabCheckOption labCheckOption) {
        labCheckOption.setType(1);
        return R.ok(labCheckOptionService.insertLabCheckOption(labCheckOption));
    }

    /**
     * 修改检查项选项
     */
    @ApiOperation(value = "修改检查项选项")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CHECKOPTION + PerFun.EDIT)
    @Log(title = "检查项选项", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody LabCheckOption labCheckOption) {
        return ResultData.result(labCheckOptionService.updateLabCheckOption(labCheckOption));
    }

    /**
     * 删除检查项选项
     */
    @ApiOperation(value = "删除检查项选项")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CHECKOPTION + PerFun.REMOVE)
    @Log(title = "检查项选项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids) {
        return ResultData.result(labCheckOptionService.deleteLabCheckOptionByIds(ids));
    }


    //--------------------------------------检查项提交记录----------------------------------------//

    /**
     * 查询检查项选项列表  -- 临时目录
     * @return
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CHECKOPTION + PerFun.LIST)
    @GetMapping("/temp/list")
    @ApiOperation(value = "查询检查项选项列表临时目录")
    public TableDataInfo tempList(LabCheckOption labCheckOption) {
        startPage();
        List<LabCheckOption> list = labCheckOptionService.selectLabCheckOptionTempList(labCheckOption);
        return getDataTable(list);
    }

    /**
     * 新增检查项选项 -- 临时目录
     */
    @ApiOperation(value = "新增检查项选项临时目录")
    @Log(title = "检查项选项", businessType = BusinessType.INSERT)
    @PostMapping("/temp")
    public R<LabCheckOption> addTemp(@RequestBody LabCheckOption labCheckOption) {
        labCheckOption.setType(2);
        labCheckOption.setUserId(SecurityUtils.getUserIdEx());
        return R.ok(labCheckOptionService.insertLabCheckOption(labCheckOption));
    }


    /**
     * PC多选 生成整改告知书
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CHECKOPTION + PerFun.EXPORT)
    @ApiOperation(value = "生成整改告知书")
    @RequestMapping("/genNotify/{ids}")
    public void genNotify(@PathVariable("ids") Long[] ids, HttpServletResponse response) {
        wordService.genNotify(ids, response);
    }

    /**
     * 小程序单个 生成整改通知书
     */
    //@PreAuthorize(hasPermi = PerPrefix.LABORATORY_CHECKOPTION + PerFun.CHECKNOTIFY)
    @ApiOperation(value = "生成整改通知书")
    @RequestMapping("/genNotice/{id}")
    public void genNotice(@PathVariable("id") Long id, HttpServletResponse response, HttpServletRequest request) {
        wordService.genNotice(id, response, request);
    }

    /**
     * 小程序单个 生成整改报告
     */
    //@PreAuthorize(hasPermi = PerPrefix.LABORATORY_CHECKOPTION + PerFun.CHECKREPORT)
    @ApiOperation(value = "生成整改报告")
    @RequestMapping("/genReport/{id}")
    public void genReport(@PathVariable("id") Long id, HttpServletResponse response, HttpServletRequest request) {
        wordService.genReport(id, response, request);
    }

}
