package com.ho.business.controller;

import com.ho.business.entity.StrategyOverviewUserSet;
import com.ho.business.service.DeviceService;
import com.ho.business.service.DynamicConfigService;
import com.ho.business.service.StationHomeService;
import com.ho.business.service.StrategyOverviewUserSetService;
import com.ho.business.vo.req.OperatingStatistic;
import com.ho.business.vo.req.device.DeviceTreeReq;
import com.ho.business.vo.req.device.StrategyOverviewUserSetVo;
import com.ho.business.vo.req.point.PointCurveReq;
import com.ho.business.vo.resp.cabin.TemperatureVoltageResp;
import com.ho.common.tools.constant.CommonConstant;
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 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 javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author yule
 * @Date 2024/3/4 15:32
 * @desc ${}
 */
@RestController
@RequestMapping(ContextConstant.BUSINESS + "strategyOverview")
@Api(tags = "策略总览")
@Slf4j
public class StrategyOverviewController {

    @Autowired
    DeviceService deviceService;

    @Autowired
    StationHomeService stationHomeService;

    @Autowired
    StrategyOverviewUserSetService setService;

    @Autowired
    RedisService redisService;

    @Autowired
    DynamicConfigService dynamicConfigService;

    @PostMapping("cellVoltageAndTemperature")
    @ApiOperation("电芯电压/温度分布")
    public DataResult<TemperatureVoltageResp> getCellVoltageAndTemperature(@RequestBody @Valid DeviceTreeReq vo,HttpServletRequest request) {
        vo.setLang(request.getHeader(CommonConstant.LANG));
        TemperatureVoltageResp temperatureVoltageResp = deviceService.getCellVoltageAndTemperature(vo);
            return DataResult.success(temperatureVoltageResp);
    }

    @PostMapping("cellVoltageAndTemperatureNum")
    @ApiOperation("电芯电压/温度分布按序号")
    public DataResult<TemperatureVoltageResp> getCellVoltageAndTemperatureNum(@RequestBody @Valid DeviceTreeReq vo,HttpServletRequest request) {
        vo.setLang(request.getHeader(CommonConstant.LANG));
        TemperatureVoltageResp temperatureVoltageResp = deviceService.getCellVoltageAndTemperatureNum(vo);
        return DataResult.success(temperatureVoltageResp);
    }

    @PostMapping("defaultValue")
    @ApiOperation("默认值查询(soc,有功功率等)")
    public DataResult<PointCurveReq> getDefaultValue(@RequestBody @Valid DeviceTreeReq vo, HttpServletRequest request) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByToken(token);
        PointCurveReq pointCurveReq = deviceService.getDefaultValue(vo.getStationId(),user);
        return DataResult.success(pointCurveReq);
    }

    @PostMapping("operatingStatistic")
    @ApiOperation("运行统计")
    public DataResult<List<OperatingStatistic>> getOperatingStatistic(@RequestBody @Valid DeviceTreeReq vo) {
        List<OperatingStatistic> list = deviceService.getOperatingStatistic(vo.getStationId());
        return DataResult.success(list);
    }

    @PostMapping("operatingStatistic2")
    @ApiOperation("运行统计(soc,soh)")
    public DataResult<List<OperatingStatistic>> getOperatingStatistic2(@RequestBody @Valid DeviceTreeReq vo,HttpServletRequest request) {
        String lang = request.getHeader(CommonConstant.LANG);
        List<OperatingStatistic> list = deviceService.getOperatingStatistic2(vo.getStationId());
        //多语言切换处理
        list.stream().forEach(s->{
            if(!CommonConstant.langTemp.ZH_CN.equals(lang)){
                s.setName(dynamicConfigService.getNameByLang(s.getName(),lang));
            }
        });
        return DataResult.success(list);
    }

    @PostMapping("getUserSet")
    @ApiOperation("策略总览-获取用户设置值")
    public DataResult<StrategyOverviewUserSet> getUserSet(HttpServletRequest request) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByToken(token);
        StrategyOverviewUserSet strategyOverviewUserSet = setService.selectByUserId(user.getUserId());
        return DataResult.success(strategyOverviewUserSet);
    }

    @PostMapping("setUserSet")
    @ApiOperation("策略总览-保存用户设置值")
    public DataResult<StrategyOverviewUserSet> setUserSet(@RequestBody StrategyOverviewUserSetVo vo,HttpServletRequest request) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByToken(token);
        StrategyOverviewUserSet userSet = new StrategyOverviewUserSet();
        BeanUtils.copyProperties(vo,userSet);
        userSet.setUserId(user.getUserId());
        Integer count = setService.setStrategyOverviewUserSet(userSet);
        return DataResult.success(count);
    }
}
