package com.zd.laboratory.controller;

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

import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.laboratory.domain.vo.LabBigViewGroupVO;
import com.zd.laboratory.domain.vo.LabControlVO;
import com.zd.laboratory.domain.vo.LabRiskPlanListVO;
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.LabControl;
import com.zd.laboratory.service.ILabControlService;
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("/control")
public class LabControlController extends BaseController {
    @Autowired
    private ILabControlService labControlService;

    /**
     * 查询控制记录列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CONTROL + PerFun.LIST)
    @GetMapping("/list")
    public TableDataInfo list(LabControlVO labControl) {
        startPage();
        List<LabControlVO> list = labControlService.selectLabControlList(labControl);
        return getDataTable(list);
    }

    /**
     * 导出控制记录列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CONTROL + PerFun.EXPORT)
    @Log(title = "控制记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LabControlVO labControl) throws IOException {
        List<LabControlVO> list = labControlService.selectLabControlList(labControl);
        ExcelUtil<LabControlVO> util = new ExcelUtil<LabControlVO>(LabControlVO.class);
        util.exportExcel(response, list, "控制记录数据");
    }

    /**
     * 获取控制记录详细信息
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CONTROL + PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(labControlService.selectLabControlById(id));
    }

    /**
     * 新增控制记录
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CONTROL + PerFun.ADD)
    @Log(title = "控制记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LabControl labControl) {
        return toAjax(labControlService.insertLabControl(labControl));
    }

    /**
     * 修改控制记录
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CONTROL + PerFun.EDIT)
    @Log(title = "控制记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LabControl labControl) {
        return toAjax(labControlService.updateLabControl(labControl));
    }

    /**
     * 删除控制记录
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CONTROL + PerFun.REMOVE)
    @Log(title = "控制记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(labControlService.deleteLabControlByIds(ids));
    }

    /**
     * 查询根据当年12个月的信息统计
     */
    @GetMapping("/getYearControlInfo")
    public AjaxResult getYearControlInfo(LabControl labControl) {
        LinkedHashMap <String,Object> list = labControlService.getYearControlInfo(labControl);
        return AjaxResult.success(list);
    }

    /**
     * 查询预案统计次数列表
     */
    @GetMapping("/getRiskPlanList")
    public TableDataInfo getRiskPlanList(LabControlVO labControlVO) {
        startPage();
        List<LabRiskPlanListVO> list = labControlService.getRiskPlanList(labControlVO);
        return getDataTable(list);
    }
}
