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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.entity.*;
import com.zhgd.xmgl.modules.basicdata.service.*;
import com.zhgd.xmgl.util.ParamEnum;
import io.swagger.annotations.Api;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Title: Controller
 * @Description: 设备外部对接接口
 * @author： pds
 * @date：   2023-02-20
 * @version： V1.0
 */
@RestController
@RequestMapping("/xmgl/api")
@Slf4j
@Api(tags = "设备外部对接接口")
public class HardWareCallbackController {

    @Autowired
    private IHardWareAlarmService hardWareAlarmService;

    @Autowired
    private IEnvironmentDevService environmentDevService;

    @Autowired
    private IDustNoiseDataService dustNoiseDataService;

    @Autowired
    private IEnvironmentAlarmService environmentAlarmService;

    @Autowired
    private IAiMonitorDevService aiMonitorDevService;

    @Autowired
    private IAiMonitorAlarmService aiMonitorAlarmService;

    @Autowired
    private IAiMonitorQuestionService iAiMonitorQuestionService;

    @Autowired
    private ISystemDictDataService iSystemDictDataService;

    @Autowired
    private RedisRepository redisRepository;

    /**
     *   新增设备报警信息
     * @param hardWareAlarm
     * @return
     */
    @ApiOperation(value = " 新增设备报警信息", notes = "新增设备报警信息" , httpMethod="POST")
    @PostMapping(value = "/saveHardWareAlarm")
    public Result<HardWareAlarm> saveHardWareAlarm(@RequestBody HardWareAlarm hardWareAlarm) {
        Result<HardWareAlarm> result = new Result<HardWareAlarm>();
        // TODO  查询设备相关的项目信息

        hardWareAlarmService.save(hardWareAlarm);
        result.success("添加成功！");
        return result;
    }

    /**
     *   扬尘噪声实时数据上报
     * @param dustNoiseData
     * @return
     */
    @ApiOperation(value = " 扬尘噪声实时数据上报", notes = "扬尘噪声实时数据上报" , httpMethod="POST")
    @PostMapping(value = "/dustNoiseData/add")
    public Result<Object> addDustNoiseData(@RequestBody DustNoiseData dustNoiseData) {
        // TODO  查询设备相关的项目信息
        EnvironmentDev environmentDev = environmentDevService.getOne(Wrappers.<EnvironmentDev>lambdaQuery().eq(EnvironmentDev::getCode, dustNoiseData.getDeviceCode()));
        if (environmentDev == null) {
            return Result.error("该设备未绑定!");
        }
        dustNoiseData.setCreateTime(new Date());
        dustNoiseData.setProjectSn(environmentDev.getProjectSn());
        dustNoiseData.setEngineeringSn(environmentDev.getEngineeringSn());
        dustNoiseDataService.save(dustNoiseData);
        redisRepository.set(CacheConstants.DUST_NOISE_DATA + environmentDev.getCode(), dustNoiseData);
        return Result.ok("操作成功!");
    }

    /**
     *   扬尘报警数据上报
     * @param environmentAlarm
     * @return
     */
    @ApiOperation(value = " 扬尘报警数据上报", notes = "扬尘报警数据上报" , httpMethod="POST")
    @PostMapping(value = "/environmentAlarm/add")
    public Result<Object> addEnvironmentAlarm(@RequestBody EnvironmentAlarm environmentAlarm) {
        // TODO  查询设备相关的项目信息
        EnvironmentDev environmentDev = environmentDevService.getOne(Wrappers.<EnvironmentDev>lambdaQuery().eq(EnvironmentDev::getCode, environmentAlarm.getDeviceCode()));
        if (environmentDev == null) {
            return Result.error("该设备未绑定!");
        }
        environmentAlarm.setCreateTime(new Date());
        environmentAlarm.setProjectSn(environmentDev.getProjectSn());
        environmentAlarm.setEngineeringSn(environmentDev.getEngineeringSn());
        environmentAlarmService.save(environmentAlarm);
        long num = redisRepository.leftPush(CacheConstants.ENVIRONMENT_ALARM_LIST + environmentDev.getCode(), JSONObject.toJSON(environmentAlarm).toString());
        if (num > 10) {
            redisRepository.rightPop(CacheConstants.ENVIRONMENT_ALARM_LIST + environmentDev.getCode());
        }
        return Result.success("添加成功!");
    }


    /**
     *   AI监控报警信息上报
     * @param aiMonitorAlarm
     * @return
     */
    @ApiOperation(value = " AI监控报警信息上报", notes = "AI监控报警信息上报" , httpMethod="POST")
    @PostMapping(value = "/aiMonitorAlarm/add")
    public Result<Object> addEnvironmentAlarm(@RequestBody AiMonitorAlarm aiMonitorAlarm) {
        // 查询字典类型确认报警类型
        List<SystemDictData> systemDictDataList = iSystemDictDataService.getByType(ParamEnum.SysDictType.AI_ALARM_TYPE.getValue());
        List<SystemDictData> dictData = systemDictDataList.stream().filter(d -> d.getDictLabel().equals(aiMonitorAlarm.getType().toString())).collect(Collectors.toList());
        if (dictData.size() == 0) {
            return Result.error("报警类型错误!");
        }
        // TODO  查询设备相关的项目信息
        AiMonitorDev aiMonitorDev = aiMonitorDevService.getOne(Wrappers.<AiMonitorDev>lambdaQuery()
                .eq(AiMonitorDev::getCode, aiMonitorAlarm.getDeviceCode())
                .eq(AiMonitorDev::getHardwareId, aiMonitorAlarm.getHardwareId()));
        if (aiMonitorDev == null) {
            return Result.error("该设备未绑定!");
        }
        aiMonitorAlarm.setCreateTime(new Date());
        aiMonitorAlarm.setProjectSn(aiMonitorDev.getProjectSn());
        aiMonitorAlarm.setEngineeringSn(aiMonitorDev.getEngineeringSn());
        aiMonitorAlarmService.save(aiMonitorAlarm);
        long num = redisRepository.leftPush(CacheConstants.AI_MONITOR_ALARM_LIST + aiMonitorDev.getCode(), aiMonitorAlarm);
        if (num > 10) {
            redisRepository.rightPop(CacheConstants.AI_MONITOR_ALARM_LIST + aiMonitorDev.getCode());
        }
        // 判断将部分类型报警信息添加到问题整改记录
        if (dictData.get(0).getRemark().equals("true")) {
            AiMonitorQuestion aiMonitorQuestion = new AiMonitorQuestion();
            BeanUtils.copyProperties(aiMonitorAlarm, aiMonitorQuestion);
            aiMonitorQuestion.setCreateBy("自动报警");
            iAiMonitorQuestionService.save(aiMonitorQuestion);
        }
        return Result.success("添加成功!");
    }
}
