package com.zd.laboratory.controller.bigview;

import com.zd.common.core.domain.R;
import com.zd.common.core.echarts.Builder;
import com.zd.common.core.echarts.EchartsBuilder;
import com.zd.common.core.echarts.Option;
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.laboratory.domain.bigview.HardwareState;
import com.zd.laboratory.domain.bigview.HazardTotal;
import com.zd.laboratory.domain.bigview.SenseState;
import com.zd.laboratory.domain.vo.*;
import com.zd.laboratory.service.ILabCheckOptionService;
import com.zd.laboratory.service.ILabGradeManageRecordService;
import com.zd.laboratory.service.ILabWarnPushMessageService;
import com.zd.laboratory.service.*;
import com.zd.laboratory.service.impl.BigViewServiceImpl;
import com.zd.laboratory.service.impl.LabSubjectManagerService;
import com.zd.system.api.RemoteUserService;
import com.zd.system.api.laboratory.domain.LabSubject;
import com.zd.system.api.laboratory.domain.LabSubjectControllerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 大屏cpntroller
 *
 * @Author: zhoupan
 * @Date: 2021/09/13/10:23
 * @Description:
 */
@Api("数据大屏接口")
@RestController
@RequestMapping("/bigview")
public class BigViewController extends BaseController {

    @Autowired
    BigViewServiceImpl bigViewService;

    @Autowired
    ILabWarnPushMessageService warnPushMessageService;

    @Autowired
    LabSubjectManagerService subjectManagerService;
    @Autowired
    private ILabCheckOptionService labCheckOptionService;
    @Autowired
    private ILabGradeManageRecordService labGradeManageRecordService;

    @Autowired
    ILabNotifyplanService labNotifyplanService;

    @Autowired
    ILabCheckRecordDetailsService iLabCheckRecordDetailsService;

    @Autowired
    private RemoteUserService remoteUserService;

    /**
     * echarts-条形图：设备概览
     *
     * @return
     */
    @ApiOperation("echarts-条形图：设备概览")
    @GetMapping("/hardware")
    public Option echartsHardwareOverview() {
        List<HardwareState> list = bigViewService.queryHardwareState();
        Builder builder = new EchartsBuilder<>(list);
        return builder.build();
    }

    /**
     * echarts-条形图：传感器状态统计
     *
     * @return
     */
    @ApiOperation("echarts-条形图：设备概览")
    @GetMapping("/senseState")
    public Option echartsSenseStateview() {
        List<SenseState> list = bigViewService.querySenseState();
        Builder builder = new EchartsBuilder<>(list);
        return builder.build();
    }

    /**
     * echarts-条形图：传感器状态统计
     *
     * @return
     */
    @ApiOperation("echarts-条形图：危险源")
    @GetMapping("/HazardTotal")
    public Option echartsHazardTotalview() {
        List<HazardTotal> list = bigViewService.queryHazardRank();
        Builder builder = new EchartsBuilder<>(list);
        return builder.build();
    }

    /**
     * 查询重点实验室 - 根据权限
     *
     */
    @ApiOperation("查询重点实验室")
    @GetMapping("/my/sub")
    public  List<LabSubjectControllerVO> mySubview(com.zd.laboratory.domain.LabSubject labSubject) {
        return bigViewService.queryMySubview(labSubject);
    }

    /**
     * 查询重点实验室
     *
     * @return
     */
    @ApiOperation("查询重点实验室")
    @GetMapping("/sub/{size}")
    public  List<LabSubjectControllerVO> echartsSubview(@PathVariable("size") Integer size, @RequestParam(required = false) Long buildId) {
        return bigViewService.queryFocusSub(size, buildId);
    }

    /**
     * 实验室人数
     * 学号  userName
     * 姓名 nickName
     * 所属院系 deptId
     * 实验室分类 subDept
     * 实验室名称 name
     * @return
     */
    @ApiOperation("查询实验室人数")
    @PostMapping("/sub/online")
    public TableDataInfo querySubOnline(@RequestBody Map<String, Object> params) {
        if(params.get("pageNum") != null && params.get("pageSize") != null){
            startPage();
        }
        List<com.zd.system.api.laboratory.domain.LabSubject> list = bigViewService.querySubOnline(params);

        long allNum = 0;
        for (LabSubject sub : list) {
            allNum += Optional.ofNullable(sub.getOnline()).orElse(0L);
        }

        TableDataInfo dataInfo = getDataTable(list);
        dataInfo.setCount(allNum);
        return dataInfo;
    }


    /**
     * 实验室在线人数(首页大屏)
     * @return
     */
    @ApiOperation("查询实验室人数")
    @GetMapping("/sub/onlineByBigView")
    public TableDataInfo onlineByBigView(LabSubjectVO labSubjectVO) {
        if(labSubjectVO.getPageNumStr() != null && labSubjectVO.getPageSizeStr() != null){
            startPage(labSubjectVO.getPageNumStr(),labSubjectVO.getPageSizeStr());
        }
        return getDataTable(bigViewService.onlineByBigView(labSubjectVO));
    }

    /**
     * 实验室在线人数总数(首页大屏)
     * @return
     */
    @ApiOperation("查询实验室人数")
    @GetMapping("/sub/onlineSumByBigView")
    public AjaxResult onlineSumByBigView(LabSubjectVO labSubjectVO) {
        return AjaxResult.success(bigViewService.onlineSumByBigView(labSubjectVO));
    }

    /**
     * 查询实验室人员信息
     * 实验室id subId
     * @return
     */
    @ApiOperation("查询实验室人员信息")
    @PostMapping("/sub/online/user")
    public TableDataInfo querySubOnlineUser(@RequestBody Map<String, Object> params) {
        if(params.get("pageNum") != null && params.get("pageSize") != null){
            startPage();
        }
        return getDataTable(bigViewService.querySubOnlineUser(params));
    }

    /**
     * 实验室负责人信息
     *
     * @return
     */
    @ApiOperation("查询实验室简要信息")
    @GetMapping("/{id}/sub")
    public  SubSimpleVO querySubInfo(@PathVariable("id") Long id ) {

        return subjectManagerService.querySubSimpleVOById(id);
    }

    /**
     * 查询风险分组内消息推送记录
     *
     * @return
     */
    @ApiOperation("查询风险分组内消息推送记录")
    @GetMapping("/{group}/warn/message")
    public  List<LabWarnPushMessageListVO> querySubOnline(@PathVariable("group") Long groupId) {
        List<LabWarnPushMessageVO> labWarnPushMessageVOS = warnPushMessageService.selectLabWarnPushMessageList(groupId);

        return Optional.ofNullable(labWarnPushMessageVOS)
                .orElseGet(Collections::emptyList)
                .stream()
                .map(a->{
                    LabWarnPushMessageListVO warnPushMessageVO = new LabWarnPushMessageListVO();
                    BeanUtils.copyProperties(a,warnPushMessageVO);
                    return warnPushMessageVO;
                })
                .collect(Collectors.toList());
    }


    /**
     * 安全隐患统计 柱状图2.4需求
     */
    @ApiOperation("安全隐患统计信息")
    @GetMapping("/check/option/charts")
    public R<Map<String, Object>> queryCheckOptionStatistics(LabCheckOptionStatisticsVo labCheckOptionStatisticsVo) {

        return R.ok(labCheckOptionService.queryCheckOptionStatistics(labCheckOptionStatisticsVo));
    }

    /**
     * 安全隐患统计 详情页列表2.4需求
     * @return
     */
    @ApiOperation("安全隐患统计列表")
    @GetMapping("/check/option/chartDetails")
    public TableDataInfo queryCheckOptionStatisticDtails(LabCheckOptionStatisticsVo labCheckOptionStatisticsVo) {
        return getDataTable(labCheckOptionService.queryCheckOptionStatisticDtails(labCheckOptionStatisticsVo));
    }

    /**
     * 分级管控统计 大屏首页本周数据2.4需求
     */
    @ApiOperation("分级管控统计 大屏首页")
    @GetMapping("/manage/work/info")
    public R<Map<String, Object>> queryManageWorkInfo(LabManageWorkChartVo labManageWorkChartVo) {

        return R.ok(labGradeManageRecordService.queryManageWorkInfo(labManageWorkChartVo));
    }

    /**
     * 分级管控统计 柱状图2.4需求
     */
    @ApiOperation("分级管控统计 柱状图")
    @GetMapping("/manage/work/charts")
    public R<List<List<Object>>> queryManageWorkCharts(LabManageWorkChartVo labManageWorkChartVo) {

        return R.ok(labGradeManageRecordService.queryManageWorkCharts(labManageWorkChartVo));
    }

    /**
     * 分级管控统计 详情页列表2.4需求
     */
    @ApiOperation("分级管控统计 详情页列表")
    @GetMapping("/manage/work/chartDetails")
    public TableDataInfo queryManageWorkChartDetails(LabManageWorkChartVo labManageWorkChartVo) {
        startPage();
        return getDataTable(labGradeManageRecordService.queryManageWorkChartDetails(labManageWorkChartVo));
    }
}
