package com.jsowell.netty.handler;

import com.alibaba.fastjson2.JSONObject;
import com.jsowell.common.core.domain.ykc.YKCDataProtocol;
import com.jsowell.common.core.domain.ykc.YKCFrameTypeCode;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.common.util.YKCUtils;
import com.jsowell.netty.factory.YKCOperateFactory;
import com.jsowell.pile.domain.OrderBasicInfo;
import com.jsowell.pile.service.IOrderBasicInfoService;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 参数配置 Handler
 *
 * GBT-27930 充电桩与 BMS 参数配置阶段报文
 * @author JS-ZZA
 * @date 2022/9/19 13:24
 */
@Slf4j
@Component
public class ParameterConfigurationHandler extends AbstractHandler{
    @Autowired
    private IOrderBasicInfoService orderBasicInfoService;

    private final String type = YKCUtils.frameType2Str(YKCFrameTypeCode.PARAMETER_CONFIGURATION_CODE.getBytes());

    @Override
    public void afterPropertiesSet() throws Exception {
        YKCOperateFactory.register(type, this);
    }

    @Override
    public byte[] supplyProcess(YKCDataProtocol ykcDataProtocol, Channel channel) {
        log.info("[===参数配置===] param:{}, channel:{}", JSONObject.toJSONString(ykcDataProtocol), channel.toString());
        // 获取消息体
        byte[] msgBody = ykcDataProtocol.getMsgBody();

        int startIndex = 0;
        int length = 16;

        // 交易流水号
        byte[] serialNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String transactionCode = BytesUtil.bcd2Str(serialNumByteArr);

        // 桩编码
        startIndex += length;
        length = 7;
        byte[] pileSnByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String pileSn = BytesUtil.bcd2Str(pileSnByteArr);

        // 保存时间
        saveLastTime(pileSn);

        // 枪号
        startIndex += length;
        length = 1;
        byte[] pileConnectorNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 单体动力蓄电池最高允许充电电压   0.01 V/位， 0 V 偏移量；  数据范围： 0~24 V
        startIndex += length;
        length = 2;
        byte[] BMSMaxVoltageByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 最高允许充电电流   0.1 A/位， -400A 偏移量
        startIndex += length;
        byte[] BMSMaxCurrentByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 动力蓄电池标称总能量   0.1 kWh/位， 0 kWh 偏移量；  数据范围： 0~1000 kWh
        startIndex += length;
        byte[] BMSSumEnergyByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 最高允许充电总电压    0.1 V/位， 0 V 偏移量
        startIndex += length;
        byte[] BMSMaxChargingVoltageByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 最高允许温度   1ºC/位， -50 ºC 偏移量；数据范 围： -50 ºC ~+200 ºC
        startIndex += length;
        length = 1;
        byte[] BMSMaxTemperatureByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // BMS 整车动力 蓄电池荷电状态(soc)   0.1%/位， 0%偏移量；数据范围： 0~100%
        startIndex += length;
        length = 2;
        byte[] BMSSocByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String soc = YKCUtils.convertVoltageCurrent(BMSSocByteArr);


        // BMS 整车动力蓄电池当前电池电压   整车动力蓄电池总电压
        startIndex += length;
        byte[] BMSRealTimeVoltageByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 电桩最高输出电压   0.1 V /位， 0 V 偏移量
        startIndex += length;
        byte[] pileMaxOutputVoltageByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 电桩最低输出电压   0.1 V /位， 0 V 偏移量
        startIndex += length;
        byte[] pileMinOutputVoltageByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 电桩最大输出电流   0.1 A/位， -400 A 偏移量
        startIndex += length;
        byte[] pileMaxOutputCurrentByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 电桩最小输出电流   0.1 A/位， -400 A 偏移量
        startIndex += length;
        byte[] pileMinOutputCurrentByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        log.info("参数配置, 起始SOC:{}", soc);

        // 查询该订单下信息，将起始soc传入
        OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByTransactionCode(transactionCode);
        if (Objects.nonNull(orderInfo)) {
            OrderBasicInfo orderBasicInfo = OrderBasicInfo.builder()
                    .id(orderInfo.getId())
                    .startSOC(soc)
                    .build();

            orderBasicInfoService.updateOrderBasicInfo(orderBasicInfo);
        }

        return null;
    }
}
