package com.ho.business.controller;

import com.ho.business.service.CockpitService;
import com.ho.business.service.LargeScreenService;
import com.ho.business.vo.resp.cockpit.ConditionMonitoringRespVO;
import com.ho.business.vo.resp.cockpit.RevenueRespVO;
import com.ho.business.vo.resp.largescreen.GroupDataResp;
import com.ho.business.vo.resp.largescreen.MainDateResp;
import com.ho.business.vo.resp.largescreen.MonthlyStatisticResp;
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.RedisKeyConstant;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.util.BigDecimalUtil;
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.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/3/21 10:52
 */
@RestController
@RequestMapping(ContextConstant.BUSINESS + "largeScreen")
@Api(tags = "大屏展示")
@Slf4j
public class LargeScreenController {

    @Autowired
    RedisService redisService;

    @Autowired
    CockpitService cockpitService;

    @Autowired
    LargeScreenService largeScreenService;

    @Autowired
    BigDecimalUtil bigDecimalUtil;


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

    @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 = largeScreenService.getConditionMonitoring(user);
        bigDecimalUtil.ifIsNUll(respVO);
        return DataResult.success(respVO);
    }

    @PostMapping("monthlyStatistics")
    @ApiOperation(value = "当年月累计充放电量统计")
    @TokenIgnore
    @LargeScreenToken
    public DataResult<List<MonthlyStatisticResp>> getMonthlyStatistics(HttpServletRequest request) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByBigScreenToken(token);
        List<MonthlyStatisticResp> statisticResps =  largeScreenService.getMonthlyStatistics(user);
        for (MonthlyStatisticResp statisticResp : statisticResps) {
            bigDecimalUtil.keepTwoDecimalPlaces(statisticResp);
            bigDecimalUtil.ifIsNUll(statisticResp);
        }
        return DataResult.success(statisticResps);
    }

    @PostMapping("mainData")
    @ApiOperation(value = "主要数据展示")
    @TokenIgnore
    @LargeScreenToken
    public DataResult<MainDateResp> getMainDate(HttpServletRequest request) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByBigScreenToken(token);
        MainDateResp resp = largeScreenService.getMainDate(user);
        bigDecimalUtil.keepTwoDecimalPlaces(resp);
        bigDecimalUtil.ifIsNUll(resp);
        return DataResult.success(resp);
    }


    @PostMapping("yearIncomeRevenue")
    @ApiOperation(value = "站年收益排行")
    @TokenIgnore
    @LargeScreenToken
    public DataResult<List<RevenueRespVO>> getYearIncomeRevenue(HttpServletRequest request) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByBigScreenToken(token);
        List<RevenueRespVO> list = largeScreenService.getYearIncomeRevenue(user);
        return DataResult.success(list);
    }
}
