package com.zhgd.xmgl.modules.basicdata.controller.government;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.redis.lock.RedisRepository;
import com.zhgd.xmgl.modules.basicdata.constant.CacheConstants;
import com.zhgd.xmgl.modules.basicdata.dto.AiMonitorDevDto;
import com.zhgd.xmgl.modules.basicdata.dto.AiMonitorDevStatDto;
import com.zhgd.xmgl.modules.basicdata.dto.EngineeringDeviceDto;
import com.zhgd.xmgl.modules.basicdata.dto.ProjectDeviceDto;
import com.zhgd.xmgl.modules.basicdata.entity.AiMonitorAlarm;
import com.zhgd.xmgl.modules.basicdata.entity.AiMonitorDev;
import com.zhgd.xmgl.modules.basicdata.service.IAiMonitorAlarmService;
import com.zhgd.xmgl.modules.basicdata.service.IAiMonitorDevService;
import com.zhgd.xmgl.modules.basicdata.service.IEngineeringService;
import com.zhgd.xmgl.modules.basicdata.service.IProjectService;
import com.zhgd.xmgl.security.SecurityUser;
import com.zhgd.xmgl.security.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @Title: Controller
 * @Description: AI监控设备管理
 * @author： pds
 * @date： 2023-03-01
 * @version： V1.0
 */
@RestController
@RequestMapping("/gov/aiMonitorDev")
@Slf4j
@Api(tags = "AI预警管理")
public class GovAiMonitorDevController {
    @Autowired
    private IAiMonitorAlarmService aiMonitorAlarmService;

    @Autowired
    private  IAiMonitorDevService aiMonitorDevService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IEngineeringService engineeringService;

    @Autowired
    private RedisRepository redisRepository;

    /**
     * 分页列表查询
     *
     * @return
     */
    @OperLog(operModul = "AI预警管理", operType = "分页查询", operDesc = "分页列表查询AI预警工程信息")
    @ApiOperation(value = " 分页列表查询AI预警工程信息", notes = "分页列表查询AI预警工程信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "engineeringName", value = "工程名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "页数", paramType = "query", required = true, defaultValue = "1", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", required = true, defaultValue = "10", dataType = "Integer")
    })
    @PostMapping(value = "/engineeringPage")
    public Result<IPage<EngineeringDeviceDto>> engineeringPageList(@RequestBody Map<String, Object> map) {
        return Result.success(aiMonitorAlarmService.engineeringPageList(map));
    }

    /**
     * 分页列表查询
     *
     * @return
     */
    @OperLog(operModul = "AI预警管理", operType = "分页查询", operDesc = "分页列表查询AI预警项目信息")
    @ApiOperation(value = " 分页列表查询AI预警项目信息", notes = "分页列表查询AI预警项目信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectName", value = "项目名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "页数", paramType = "query", required = true, defaultValue = "1", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", required = true, defaultValue = "10", dataType = "Integer")
    })
    @PostMapping(value = "/projectPage")
    public Result<IPage<ProjectDeviceDto>> projectPageList(@RequestBody Map<String, Object> map) {
        return Result.success(aiMonitorAlarmService.projectPageList(map));
    }


    /**
     * 列表查询
     *
     * @return
     */
    @OperLog(operModul = "AI预警管理", operType = "列表查询", operDesc = "列表查询AI预警设备管理信息")
    @ApiOperation(value = " 列表查询AI预警设备管理信息", notes = "列表查询AI预警设备管理信息", httpMethod = "POST")
    @PostMapping(value = "/list")
    public Result<List<AiMonitorDev>> queryList(@RequestBody AiMonitorDev aiMonitorDev) {
        LambdaQueryWrapper<AiMonitorDev> wrapper = Wrappers.<AiMonitorDev>lambdaQuery();
        if (aiMonitorDev.getProjectSn() != null) {
            wrapper.eq(AiMonitorDev::getEngineeringSn, aiMonitorDev.getEngineeringSn());
        }
        List<AiMonitorDev> list = aiMonitorDevService.list(wrapper);
        return Result.success(list);
    }

    /**
     * 数据统计
     *
     * @return
     */
    @OperLog(operModul = "AI预警管理", operType = "统计", operDesc = "统计AI预警设备数据")
    @ApiOperation(value = " 统计AI预警设备数据", notes = "统计AI预警设备数据", httpMethod = "GET")
    @GetMapping(value = "/statistics")
    public Result<AiMonitorDevStatDto> statistics() {
        Result<AiMonitorDevStatDto> result = new Result<AiMonitorDevStatDto>();
        AiMonitorDevStatDto aiMonitorDev = new AiMonitorDevStatDto();
        SecurityUser user = SecurityUtil.getUser();
        List<String> engineeringSnList = engineeringService.getSnListByGov(user.getSn());
        int installNum = aiMonitorDevService.list(Wrappers.<AiMonitorDev>lambdaQuery()
                .in(AiMonitorDev::getEngineeringSn, engineeringSnList)
                .groupBy(AiMonitorDev::getEngineeringSn)).size();
        Integer engineeringNum = engineeringService.getSnListByGov(user.getSn()).size();
        aiMonitorDev.setInstallNum(installNum);
        aiMonitorDev.setUseRate(engineeringNum == 0 ? 0 : NumberUtil.round(installNum * 100F / engineeringNum, 2).floatValue());
        aiMonitorDev.setTotalAlarm(aiMonitorAlarmService.count(Wrappers.<AiMonitorAlarm>lambdaQuery()
                .in(AiMonitorAlarm::getProjectSn, projectService.getSnListForGov(user.getSn()))));
        return Result.success(aiMonitorDev);
    }

    /**
     * 通过设备编码查询设备详细信息
     *
     * @return
     */
    @OperLog(operModul = "AI预警管理", operType = "查询", operDesc = "通过设备ID查询设备详细信息")
    @ApiOperation(value = "通过设备ID查询设备详细信息", notes = "通过设备ID查询设备详细信息", httpMethod = "POST")
    @ApiImplicitParam(name = "aiMonitorId", value = "AI预警设备id", paramType = "query", required = true, dataType = "String")
    @PostMapping(value = "/queryDetailById")
    public Result<AiMonitorDevDto> queryById(@RequestBody AiMonitorDev aiMonitorDev) {
        Result<AiMonitorDevDto> result = new Result<AiMonitorDevDto>();
        aiMonitorDev = aiMonitorDevService.getById(aiMonitorDev.getAiMonitorId());
        if (aiMonitorDev == null) {
            result.error500("未找到对应实体");
        } else {
            AiMonitorDevDto aiMonitorDevDto = new AiMonitorDevDto();
            BeanUtils.copyProperties(aiMonitorDev, aiMonitorDevDto);
			// 最近报警信息
            Object objList = redisRepository.getList(CacheConstants.AI_MONITOR_ALARM_LIST + aiMonitorDev.getCode());
            if (objList != null) {
                aiMonitorDevDto.setAiMonitorAlarmList((List<AiMonitorAlarm>) objList);
            }
            aiMonitorDevDto.setCountGroupByType(aiMonitorAlarmService.getCountGroupByType(aiMonitorDev.getCode()));
            result.setResult(aiMonitorDevDto);
            result.setSuccess(true);
        }
        return result;
    }
}
