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.echarts.EchartsBuilder;
import com.zd.common.core.utils.poi.ExcelUtil;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
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.LabRiskPlanAbnormalLog;
import com.zd.laboratory.domain.SubDiyAppVO;
import com.zd.laboratory.domain.vo.LabRiskPlanAbnormalLogVO;
import com.zd.laboratory.service.ILabRiskPlanAbnormalLogService;
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.Collections;
import java.util.List;
import java.util.Map;

/**
 * 风险异常日志Controller
 *
 * @author liubo
 * @date 2021-09-06
 */
@RestController
@RequestMapping("/plan/log")
@Api(tags = {" 风险异常以及统计"})
public class LabRiskPlanAbnormalLogController extends BaseController {
    @Autowired
    private ILabRiskPlanAbnormalLogService labRiskPlanAbnormalLogService;

    /**
     * 查询风险异常日志列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_PLANLOG + PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询风险异常日志列表")
    public TableDataInfo list(LabRiskPlanAbnormalLog labRiskPlanAbnormalLog) {
        startPage();
        List<LabRiskPlanAbnormalLog> list = labRiskPlanAbnormalLogService.selectLabRiskPlanAbnormalLogList(labRiskPlanAbnormalLog);
        return getDataTable(list);
    }

    /**
     * 导出风险异常日志列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_PLANLOG + PerFun.EXPORT)
    @Log(title = "风险异常日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出风险异常日志列表")
    public void export(HttpServletResponse response, LabRiskPlanAbnormalLog labRiskPlanAbnormalLog) throws IOException {
        List<LabRiskPlanAbnormalLog> list = labRiskPlanAbnormalLogService.selectLabRiskPlanAbnormalLogList(labRiskPlanAbnormalLog);
        ExcelUtil<LabRiskPlanAbnormalLog> util = new ExcelUtil<LabRiskPlanAbnormalLog>(LabRiskPlanAbnormalLog.class);
        util.exportExcel(response, list, "风险异常日志数据");
    }

    /**
     * 获取风险异常日志详细信息
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_PLANLOG + PerFun.QUERY)
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取风险异常日志详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(labRiskPlanAbnormalLogService.selectLabRiskPlanAbnormalLogById(id));
    }

    /**
     * 根据预案id和组级id查询风险异常日志列表排序
     */
    @GetMapping("/abnormalLogOrderByList")
    @ApiOperation(value = "根据预案id和组级id查询风险异常日志列表排序")
    public AjaxResult selectLabRiskPlanAbnormalLogOrderByList(LabRiskPlanAbnormalLog labRiskPlanAbnormalLog) {
        List<LabRiskPlanAbnormalLog> list = labRiskPlanAbnormalLogService.selectLabRiskPlanAbnormalLogOrderByList(labRiskPlanAbnormalLog);
        Collections.reverse(list);
        return AjaxResult.success(list);
    }

    /**
     * 新增风险异常日志
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_PLANLOG + PerFun.ADD)
    @Log(title = "风险异常日志", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增风险异常日志")
    public AjaxResult add(@RequestBody LabRiskPlanAbnormalLog labRiskPlanAbnormalLog) {
        return toAjax(labRiskPlanAbnormalLogService.insertLabRiskPlanAbnormalLog(labRiskPlanAbnormalLog));
    }

    /**
     * 修改风险异常日志
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_PLANLOG + PerFun.EDIT)
    @Log(title = "风险异常日志", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改风险异常日志")
    public AjaxResult edit(@RequestBody LabRiskPlanAbnormalLog labRiskPlanAbnormalLog) {
        return toAjax(labRiskPlanAbnormalLogService.updateLabRiskPlanAbnormalLog(labRiskPlanAbnormalLog));
    }

    /**
     * 删除风险异常日志
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_PLANLOG + PerFun.REMOVE)
    @Log(title = "风险异常日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除风险异常日志")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(labRiskPlanAbnormalLogService.deleteLabRiskPlanAbnormalLogByIds(ids));
    }

    /**
     *  风险发生数折线统计图
     */
    @PostMapping("/lineChart")
    @ApiOperation(value = "风险发生数折线统计图")
    public AjaxResult lineChart(@RequestBody LabRiskPlanAbnormalLog labRiskPlanAbnormalLog) {
        List<Map<String, Object>> maps = labRiskPlanAbnormalLogService.queryLineChart(labRiskPlanAbnormalLog);
        EchartsBuilder builder = new EchartsBuilder<>();
        builder.setListData(maps, "date", "count");
        return AjaxResult.success(builder.build());
    }

    /**
     * 风险发生占比图
     */
    @PostMapping("/happenChart")
    @ApiOperation(value = "风险发生占比图")
    public AjaxResult happenChart(@RequestBody LabRiskPlanAbnormalLog labRiskPlanAbnormalLog) {
        return AjaxResult.success(labRiskPlanAbnormalLogService.queryHappenChart(labRiskPlanAbnormalLog));
    }

    /**
     * 院系风险统计图
     */
    @PostMapping("/statisticsChartDept")
    @ApiOperation(value = "院系风险统计图")
    public AjaxResult statisticsChartDept(@RequestBody LabRiskPlanAbnormalLog labRiskPlanAbnormalLog) {
        return AjaxResult.success(labRiskPlanAbnormalLogService.queryStatisticsChartDept(labRiskPlanAbnormalLog));
    }

    /**
     * 近30天预案执行数
     */
    @PostMapping("/queryPlanCount")
    @ApiOperation(value = "近30天预案执行数")
    public AjaxResult queryPlanCount() {
        return AjaxResult.success(labRiskPlanAbnormalLogService.queryPlanCount());
    }

    /**
     * 风险趋势统计图
     */
    @PostMapping("/queryRiskTrend")
    @ApiOperation(value = "风险趋势统计图")
    public AjaxResult queryRiskTrend(@RequestBody Map<String, Object> params) {
        return AjaxResult.success(labRiskPlanAbnormalLogService.queryRiskTrend(params));
    }


    /**
     * 小程序查询当前发生的实验室风险异常日志列表
     */
    @RequestMapping("/app/subBySensorList")
    @ApiOperation(value = "小程序查询当前发生的实验室风险异常日志列表")
    public TableDataInfo subBySensorList(@RequestBody LabRiskPlanAbnormalLogVO labRiskPlanAbnormalLogVo) {
        startPage(labRiskPlanAbnormalLogVo.getAppPageNum(),labRiskPlanAbnormalLogVo.getAppPageSize());
        List<SubDiyAppVO> list = labRiskPlanAbnormalLogService.selectLabRiskPlanAppList(labRiskPlanAbnormalLogVo);
        return getDataTable(list);
    }


    /**
     * 小程序查询当前发生的实验室风险异常日志统计
     */
    @RequestMapping("/app/subBySensorCount")
    @ApiOperation(value = "小程序查询当前发生的实验室风险异常日志统计")
    public AjaxResult subBySensorCount() {
        LabRiskPlanAbnormalLogVO labRiskPlanAbnormalLogVo = new LabRiskPlanAbnormalLogVO();
        return AjaxResult.success(labRiskPlanAbnormalLogService.selectLabRiskPlanAppCount(labRiskPlanAbnormalLogVo));
    }




    /**
     * 小程序查询登录人管理实验室下的预案记录列表
     */
    @RequestMapping("/app/adminSubByRiskPlan")
    @ApiOperation(value = "小程序查询登录人管理实验室下的预案记录列表")
    public TableDataInfo adminSubByRiskPlan(@RequestBody LabRiskPlanAbnormalLogVO labRiskPlanAbnormalLogVo) {
        startPage(labRiskPlanAbnormalLogVo.getAppPageNum(),labRiskPlanAbnormalLogVo.getAppPageSize());
        List<SubDiyAppVO> list = labRiskPlanAbnormalLogService.adminSubByRiskPlan(labRiskPlanAbnormalLogVo);
        return getDataTable(list);
    }

}
