package com.ho.business.controller;

import com.alibaba.fastjson.JSON;
import com.ho.business.entity.Device;
import com.ho.business.service.DeviceService;
import com.ho.business.service.ModelDeviceService;
import com.ho.business.vo.req.point.PointReqVO;
import com.ho.business.vo.resp.point.BmsCellResp;
import com.ho.business.vo.resp.point.BmsClusterResp;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
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.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.validation.Valid;
import java.util.HashMap;

/**
 * @author fancl
 * @desc: BMS点表页面
 * @date 2023/4/25
 */
@RestController
@RequestMapping(ContextConstant.BUSINESS + "bmsPcs")
@Api(tags = "BMS PCS点表")
@Slf4j
public class BmsController {

    @Autowired
    DeviceService deviceService;

    @Autowired
    RedisService redisService;

    @Autowired
    ModelDeviceService modelDeviceService;

    @PostMapping("getBmsCluster")
    @ApiOperation(value = "BMS簇页面数据")
    public DataResult<BmsClusterResp> getBmsCluster(@RequestBody @Valid PointReqVO pointReq) {
        if (pointReq.getStationId() == null || pointReq.getSrcId() == null) {
            throw new BusinessException(BaseResponseCode.METHODARGUMENTNOTVALIDEXCEPTION);
        }
        //根据设备查询设备类型
        Device device = deviceService.selectByStationIdAndSrcId(pointReq.getStationId(), pointReq.getSrcId());
        if (device == null) {
            throw new BusinessException(BaseResponseCode.CORRESPONDING_DEVICE_DOES_NOT_EXIST);
        }
        if (device.getDeviceType() == null) {
            throw new BusinessException(BaseResponseCode.DEVICE_TYPE_NOT_CONFIG);
        }
        String deviceType = device.getDeviceType();
        //从redis取数据
        String key = deviceType + ":" + pointReq.getStationId() + ":" + pointReq.getSrcId();
        Object o = redisService.hgetall(key);
        HashMap<String, Object> map = (HashMap) o;
        BmsClusterResp bmsClusterResp = JSON.parseObject(JSON.toJSONString(map), BmsClusterResp.class);
        return DataResult.success(bmsClusterResp);
    }

    @PostMapping("getPcsCell")
    @ApiOperation(value = "Pcs单体电池页面数据")
    public DataResult<BmsCellResp> getPcsCell(@RequestBody @Valid PointReqVO pointReq) {
        if (pointReq.getStationId() == null || pointReq.getSrcId() == null) {
            throw new BusinessException(BaseResponseCode.METHODARGUMENTNOTVALIDEXCEPTION);
        }
        //根据设备查询设备类型
        Device device = deviceService.selectByStationIdAndSrcId(pointReq.getStationId(), pointReq.getSrcId());
        if (device == null) {
            throw new BusinessException(BaseResponseCode.CORRESPONDING_DEVICE_DOES_NOT_EXIST);
        }
        String deviceType = device.getDeviceType();
        //从redis取数据
        String key = deviceType + ":" + pointReq.getStationId() + ":" + pointReq.getSrcId();
        Object o = redisService.hgetall(key);
        HashMap<String, Object> map = (HashMap) o;
        BmsCellResp bmsCellResp = JSON.parseObject(JSON.toJSONString(map), BmsCellResp.class);
        return DataResult.success(bmsCellResp);
    }
}
