package com.njcn.csharmonic.controller;

import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.csharmonic.service.RealDataService;
import com.njcn.redis.utils.RedisUtil;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 类的介绍：治理实时数据
 *
 * @author xy
 * @version 1.0.0
 * @createTime 2024/9/30
 */
@Slf4j
@RestController
@RequestMapping("/realData")
@Api(tags = "实时数据")
@AllArgsConstructor
public class RealDataController extends BaseController {

    private final RealDataService realDataService;
    private final RedisUtil redisUtil;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getBaseRealData")
    @ApiOperation("获取基础实时数据")
    @ApiImplicitParam(name = "lineId", value = "监测点id")
    public HttpResult<Boolean> getBaseRealData(@RequestParam("lineId") String lineId) {
        String methodDescribe = getMethodDescribe("getBaseRealData");
        boolean result = realDataService.getBaseRealData(lineId);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getHarmRealData")
    @ApiOperation("获取谐波实时数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "lineId", value = "监测点id"),
        @ApiImplicitParam(name = "target", value = "指标名称 0:谐波电压含有率 1:谐波电流幅值 2:间谐波电压含有率"),
    })
    public HttpResult<Boolean> getHarmRealData(@RequestParam("lineId") String lineId, @RequestParam("target") Integer target) {
        String methodDescribe = getMethodDescribe("getHarmRealData");
        redisUtil.saveByKeyWithExpire("rtDataUserId:"+lineId, RequestUtil.getUserIndex(),600L);
        boolean result = realDataService.getHarmRealData(lineId,target);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }
}
