package com.ho.business.controller;

import com.ho.business.common.BusiTool;
import com.ho.business.feignclient.FileCenterFeignClient;
import com.ho.business.service.CockpitService;
import com.ho.business.service.ReallyCockpitService;
import com.ho.business.service.StationService;
import com.ho.business.vo.resp.StationRespVO;
import com.ho.business.vo.resp.cockpit.*;
import com.ho.common.tools.annotation.LargeScreenToken;
import com.ho.common.tools.annotation.TokenIgnore;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.EnvConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.common.tools.entity.WeatherRespVo;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.util.BigDecimalUtil;
import com.ho.common.tools.util.IPUtils;
import com.ho.common.tools.vo.req.WeatherReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description 驾驶舱的真实数据界面
 * Author yule
 * Date 2023/4/12 13:47
 */
@RestController
@RequestMapping(ContextConstant.BUSINESS + "reallyCockpit")
@Api(tags = "真实驾驶舱")
@Slf4j
public class ReallyCockpitController {


    @Autowired
    RedisService redisService;

    @Autowired
    CockpitService cockpitService;

    @Autowired
    ReallyCockpitService reallyCockpitService;

    @Autowired
    BigDecimalUtil bigDecimalUtil;

    @Autowired
    StationService stationService;

    @Autowired
    FileCenterFeignClient fileCenterFeignClient;

    @Autowired
    BusiTool busiTool;

    @Value("${spring.profiles.active}")
    String env;

    @Value("${event.currentMaxDay}")
    Integer currentMaxDay;


    @PostMapping("/weather")
    @ApiOperation(value = "大屏天气接口")
    @TokenIgnore
    @LargeScreenToken
    public DataResult<WeatherRespVo> weather(HttpServletRequest request) {
        String ipAddr = IPUtils.getIpAddr(request);
        log.info("获取到IP: {}", ipAddr);
        if (env.equals("dev") || env.equals("exp")) {
            ipAddr = "114.222.185.168";
        }
        //通合的先显示南通的地址IP,后续再优化
        else if(EnvConstant.TONG_HE.equals(env)){
            ipAddr = "221.227.154.59";
        }
        //调用file-center的天气接口
        WeatherReq weatherReq = new WeatherReq();
        weatherReq.setIp(ipAddr);
        DataResult<WeatherRespVo> weatherResult = fileCenterFeignClient.queryWeather(weatherReq);
        log.info("天气接口数据:" + weatherResult);
        return DataResult.success(weatherResult.getData());
    }

    @PostMapping("/groupData")
    @ApiOperation(value = "集团数据")
    @TokenIgnore
    @LargeScreenToken
    public DataResult<GroupDataRespVo> groupData(HttpServletRequest request){
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByBigScreenToken(token);
        GroupDataRespVo groupDataRespVo = reallyCockpitService.getGroupData(user);
        bigDecimalUtil.keepTwoDecimalPlaces(groupDataRespVo);
        bigDecimalUtil.ifIsNUll(groupDataRespVo);
        return DataResult.success(groupDataRespVo);
    }

    @PostMapping("/cumulative")
    @ApiOperation(value = "累计总和")
    @TokenIgnore
    @LargeScreenToken
    public DataResult<CumulativeRespVO> cumulative(HttpServletRequest request) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByBigScreenToken(token);
        CumulativeRespVO respVO = reallyCockpitService.getCumulative(user);
        bigDecimalUtil.keepTwoDecimalPlaces(respVO);
        bigDecimalUtil.ifIsNUll(respVO);
        return DataResult.success(respVO);
    }

    @PostMapping("/stationMap")
    @ApiOperation(value = "电站地图")
    @TokenIgnore
    @LargeScreenToken
    public DataResult<List<StationMapRespVO>> stationMap(HttpServletRequest request){
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByBigScreenToken(token);
        List<StationMapRespVO> respVO = cockpitService.getStationMap(user);
        return DataResult.success(respVO);
    }

    @PostMapping("/conditionMonitoring")
    @ApiOperation(value = "状态监测")
    @TokenIgnore
    @LargeScreenToken
    public DataResult<ConditionMonitoringRespVO> conditionMonitoring(HttpServletRequest request) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByBigScreenToken(token);
        ConditionMonitoringRespVO respVO = cockpitService.getConditionMonitoring(user);
        bigDecimalUtil.ifIsNUll(respVO);
        return DataResult.success(respVO);
    }

    @PostMapping("/stationOperation")
    @ApiOperation(value = "电站运行数据")
    @TokenIgnore
    @LargeScreenToken
    public DataResult<List<StationOperationRespVO>> stationOperation(HttpServletRequest request){
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByBigScreenToken(token);
        List<StationOperationRespVO> list = cockpitService.getStationOperation(user);
        for (StationOperationRespVO stationOperationRespVO : list) {
            bigDecimalUtil.keepTwoDecimalPlaces(stationOperationRespVO);
            bigDecimalUtil.ifIsNUll(stationOperationRespVO);
        }
        return DataResult.success(list);
    }

    @PostMapping("/capacityProportion")
    @ApiOperation(value = "装机占比")
    @TokenIgnore
    @LargeScreenToken
    public DataResult<CapacityProportionResp> capacityProportion(HttpServletRequest request){
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByBigScreenToken(token);
        CapacityProportionResp capacityProportionResp = reallyCockpitService.capacityProportion(user);
        bigDecimalUtil.keepTwoDecimalPlaces(capacityProportionResp);
        bigDecimalUtil.ifIsNUll(capacityProportionResp);
        return DataResult.success(capacityProportionResp);
    }

    @PostMapping("/monitoringStation")
    @ApiOperation(value = "电站实时监控")
    @TokenIgnore
    @LargeScreenToken
    public DataResult<List<StationRespVO>> monitoringStation(HttpServletRequest request){
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByBigScreenToken(token);
        List<StationRespVO> list = cockpitService.monitoringStation(user);
        return DataResult.success(list);
    }
}
