package com.jsowell.netty.handler.yunkuaichong;

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.DateUtils;
import com.jsowell.common.util.YKCUtils;
import com.jsowell.netty.factory.YKCOperateFactory;
import com.jsowell.pile.domain.OrderBasicInfo;
import com.jsowell.pile.service.OrderBasicInfoService;
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.Date;
import java.util.Objects;

/**
 * 充电结束Handler
 * 0x19
 * GBT-27930 充电桩与 BMS 充电结束阶段报文
 * @author JS-ZZA
 * @date 2022/9/19 13:27
 */
@Slf4j
@Component
public class ChargeEndHandler extends AbstractHandler{
    private final String type = YKCUtils.frameType2Str(YKCFrameTypeCode.CHARGE_END_CODE.getBytes());

    @Autowired
    private OrderBasicInfoService orderBasicInfoService;

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

    @Override
    public byte[] supplyProcess(YKCDataProtocol ykcDataProtocol, Channel channel) {
        // log.info("[===执行充电结束逻辑===] param:{}, channel:{}", JSON.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);

        // 保存时间
        saveLastTimeAndCheckChannel(pileSn, channel);

        // 枪号
        startIndex += length;
        length = 1;
        byte[] connectorCodeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String connectorCode = BytesUtil.bcd2Str(connectorCodeByteArr);

        // BMS中止荷电状态 SOC   1%/位， 0%偏移量； 数据范围： 0~100%
        startIndex += length;
        byte[] BMSStopChargingSOC = BytesUtil.copyBytes(msgBody, startIndex, length);
        String stopSoc = BytesUtil.binary(BMSStopChargingSOC, 10);

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

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

        // BMS 动力蓄电池最低温度   1ºC/位， -50 ºC 偏移量；数据范围： -50 ºC ~+200 ºC
        startIndex += length;
        length = 1;
        byte[] BMSBatteryMinTemperatureByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

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

        // 电桩累计充电时间   1 min/位， 0 min 偏移量； 数据范围： 0~600 min
        startIndex += length;
        length = 2;
        byte[] pileSumChargingTimeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 电桩输出能量   0.1 kWh/位， 0 kWh 偏移量； 数据范围： 0~1000 kWh
        startIndex += length;
        byte[] pileOutputEnergyByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 电桩充电机编号   充电机编号，  1/位，  1偏移量 ，数据范围 ： 0 ～ 0xFFFFFFFF
        startIndex += length;
        length = 4;
        byte[] pileChargedCodeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 查询订单 将结束soc传入
        OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByTransactionCode(transactionCode);
        if (Objects.nonNull(orderInfo)) {
            Date nowDate = DateUtils.getNowDate(); // 当前时间

            // 只更新个别字段
            OrderBasicInfo updateOrder = new OrderBasicInfo();
            updateOrder.setId(orderInfo.getId());
            updateOrder.setEndSoc(stopSoc);
            if (orderInfo.getChargeEndTime() == null) {
                updateOrder.setChargeEndTime(nowDate); // 结束充电时间
            }
            updateOrder.setUpdateTime(nowDate);
            orderBasicInfoService.updateOrderBasicInfo(updateOrder);
        }

        return null;
    }
}
