package com.yanzhu.jh.bigscreen.web.controller;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.SysRoleEnum;
import com.ruoyi.system.service.ISysDeptService;
import com.yanzhu.jh.video.domain.DevAiProjectData;
import com.yanzhu.jh.video.domain.SurProjectVideoConfig;
import com.yanzhu.jh.video.service.IDevAiProjectDataService;
import com.yanzhu.jh.video.service.ISurProjectVideoConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 实时视频Conller
 */
@RestController
@RequestMapping("/bgscreen/aiBoxVideo")
public class VideoAiBoxController extends BaseController {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private IDevAiProjectDataService devAiProjectDataService;

    @Autowired
    private ISurProjectVideoConfigService surProjectVideoConfigService;

    /**
     * 查询AI预警列表
     */
    @GetMapping("/listView")
    public AjaxResult listView(String deptId, String projectId)
    {
        DevAiProjectData devAiProjectData = new DevAiProjectData();
        devAiProjectData.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(devAiProjectData.getNowRole())){
            devAiProjectData.setNowDept(Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            devAiProjectData.setNowDept(Convert.toStr(getDeptId()));
        }
        devAiProjectData.setNowUser(Convert.toStr(getUserId()));
        String key="bgscreen_aiBoxVideo_listView_"+deptId+"_"+projectId+"_"+devAiProjectData.getNowRole()+"_"+devAiProjectData.getNowDept()+"_"+devAiProjectData.getNowUser();
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return success(obj);
        }
        if(deptId!=null && !"0".equals(deptId)){
            devAiProjectData.setProjectDeptId(deptId);
        }
        if(projectId!=null && !"0".equals(projectId)){
            devAiProjectData.setProjectId(Convert.toLong(projectId));
        }
        //默认查询当天数据
        /*Map<String, Object> params = new HashMap<>();
        params.put("date",new Date());
        devAiProjectData.setParams(params);*/
        startPage();
        List<DevAiProjectData> list = devAiProjectDataService.selectDevAiProjectDataList(devAiProjectData);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return success(list);
    }

    /**
     * 获取视频配置详细信息
     */
    @GetMapping(value = "/getVideoPassage")
    public AjaxResult getVideoPassage(String deptId, String projectId, String passageType, String importance)
    {
        Map<String, Object> map = new HashMap<>();
        String nowRole = Convert.toStr(getUserFirstRole());
        map.put("nowRole",nowRole);
        if(SysRoleEnum.ZGS.getCode().equals(nowRole)){
            map.put("nowDept",Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            map.put("nowDept",Convert.toStr(getDeptId()));
        }
        map.put("nowUser",Convert.toStr(getUserId()));
        String key="bgscreen_aiBoxVideo_getVideoPassage_"+deptId+"_"+projectId+"_"+passageType+"_"+importance+"_"+map.get("nowRole")+"_"+map.get("nowDept")+"_"+map.get("nowUser");
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return success(obj);
        }
        if(deptId!=null && !"0".equals(deptId)){
            map.put("deptId",deptId);
        }
        if(projectId!=null && !"0".equals(projectId)){
            map.put("projectId",projectId);
        }
        if(passageType!=null && !"".equals(passageType)){
            map.put("passageType",passageType);
        }
        if(importance!=null && !"".equals(importance)){
            map.put("importance",importance);
        }
        clearPage();
        List<SurProjectVideoConfig> list = surProjectVideoConfigService.selectSurProjectVideoConfigByParams(map);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return success(list);
    }

    /**
     * 查询视频配置列表
     */
    @GetMapping("/groupCountByAlarmType")
    public AjaxResult groupCountByAlarmType(String deptId,String projectId,String now)
    {
        DevAiProjectData devAiProjectData = new DevAiProjectData();
        devAiProjectData.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(devAiProjectData.getNowRole())){
            devAiProjectData.setNowDept(Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            devAiProjectData.setNowDept(Convert.toStr(getDeptId()));
        }
        devAiProjectData.setNowUser(Convert.toStr(getUserId()));
        String key="bgscreen_aiBoxVideo_groupCountByAlarmType_"+deptId+"_"+projectId+"_"+now+"_"+devAiProjectData.getNowRole()+"_"+devAiProjectData.getNowDept()+"_"+devAiProjectData.getNowUser();
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return success(obj);
        }
        if(deptId!=null && !"0".equals(deptId)){
            devAiProjectData.setProjectDeptId(deptId);
        }
        if(projectId!=null && !"0".equals(projectId)){
            devAiProjectData.setProjectId(Convert.toLong(projectId));
        }
        //查询当天数据
        if(now!=null && "Y".equals(now)){
            Map<String, Object> params = new HashMap<>();
            params.put("date",new Date());
            devAiProjectData.setParams(params);
        }
        List<Map<String, Object>> list = devAiProjectDataService.groupCountByAlarmType(devAiProjectData);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return success(list);
    }

    /**
     * 查询视频配置列表汇总
     */
    @GetMapping("/selectGroupCountVideoConfig")
    public AjaxResult selectGroupCountVideoConfig(String deptId,String projectId)
    {
        Map<String, Object> map = new HashMap<>();
        String nowRole = Convert.toStr(getUserFirstRole());
        map.put("nowRole",nowRole);
        if(SysRoleEnum.ZGS.getCode().equals(nowRole)){
            map.put("nowDept",Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            map.put("nowDept",Convert.toStr(getDeptId()));
        }
        map.put("nowUser",Convert.toStr(getUserId()));
        String key="bgscreen_aiBoxVideo_selectGroupCountVideoConfig_"+deptId+"_"+projectId+"_"+map.get("nowRole")+"_"+map.get("nowDept")+"_"+map.get("nowUser");
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return success(obj);
        }
        if(deptId!=null && !"0".equals(deptId)){
            map.put("deptId",deptId);
        }
        if(projectId!=null && !"0".equals(projectId)){
            map.put("projectId",projectId);
        }
        List<Map<String, Object>> list = surProjectVideoConfigService.selectGroupCountVideoConfig(map);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return success(list);
    }

}
