package com.xhpc.pp.logic;

import com.xhpc.common.enums.StationDeviceEnum;
import com.xhpc.pp.domain.XhpcDeviceMessage;
import com.xhpc.pp.mapper.XhpcDeviceMessageMapper;
import com.xhpc.pp.tx.ServiceParameter;
import com.xhpc.pp.tx.ServiceResult;
import com.xhpc.pp.tx.logic.ServiceLogic;
import com.xhpc.pp.utils.HexUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Lazy
@Component("BmsChargingDataLogic")
public class BmsChargingDataLogic implements ServiceLogic {

    private static final Logger log = LoggerFactory.getLogger(BmsChargingDataLogic.class);
    @Resource
    XhpcDeviceMessageMapper deviceMessageMapper;

    @Override
    public ServiceResult service(ServiceParameter sp) throws Exception {

        Map<String, Object> req = sp.getParameters();
//todo test only
//        String hex = (String) req.get("hex");
//        int idxStart = 60;
//        int hibsn = Integer.parseInt(hex.substring(idxStart, 2 + idxStart), 16);//BMS 最高单体动力蓄电池电压所在编号
//        idxStart = 2 + idxStart;
//        int hibtemp = Integer.parseInt(hex.substring(idxStart, 2 + idxStart), 16) - 50;//BMS 最高动力蓄电池温度
//        idxStart = 2 + idxStart;
//        int hibtempchkpt = Integer.parseInt(hex.substring(idxStart, 2 + idxStart), 16);//最高温度检测点编号
//        idxStart = 2 + idxStart;
//        int lobtemp = Integer.parseInt(hex.substring(idxStart, 2 + idxStart), 16) - 50;//最低动力蓄电池温度
//        idxStart = 2 + idxStart;
//        int lobtempchkpt = Integer.parseInt(hex.substring(idxStart, 2 + idxStart), 16);//最低动力蓄电池温度检测点编号
//        idxStart = 2 + idxStart;
//        String flags1 = HexUtils.toBinaryString(hex.substring(idxStart, 2 + idxStart));//前4检测标识位
//        idxStart = 2 + idxStart;
//        String flags2 = HexUtils.toBinaryString(hex.substring(idxStart, 2 + idxStart));//后4检测标识位
//        idxStart = 0;
//        String flag = flags1.substring(idxStart, 2 + idxStart);
//        idxStart = 2 + idxStart;
//        String sb = flag.equals("00") ? "正常" : (flag.equals("01") ? "过高" : "正常");//BMS 单体动力蓄电池电压过高/过低
//        flag = flags1.substring(idxStart, 2 + idxStart);
//        idxStart = 2 + idxStart;
//        String wb = flag.equals("00") ? "正常" : (flag.equals("01") ? "过高" : "正常");//BMS 整车动力蓄电池荷电状态SOC 过高/过低
//        flag = flags1.substring(idxStart, 2 + idxStart);
//        idxStart = 2 + idxStart;
//        String pbovcurrency = flag.equals("00") ? "正常" : (flag.equals("01") ? "过流" : "不可信状态");//BMS 动力蓄电池充电过电流
//        flag = flags1.substring(idxStart, 2 + idxStart);
//        String pbtemphi = flag.equals("00") ? "正常" : (flag.equals("01") ? "过高" : "不可信状态");//BMS 动力蓄电池温度过高
//        idxStart = 0;
//        flag = flags1.substring(idxStart, 2 + idxStart);
//        idxStart = 2 + idxStart;
//        String pbinsulation = flag.equals("00") ? "正常" : (flag.equals("01") ? "不正常" : "不可信状态");//BMS 动力蓄电池绝缘状态
//        flag = flags2.substring(idxStart, 2 + idxStart);
//        idxStart = 2 + idxStart;
//        String pboconn = flag.equals("00") ? "正常" : (flag.equals("01") ? "不正常" : "不可信状态");//BMS 动力蓄电池组输出连接器连接状态
//        flag = flags2.substring(idxStart, 2 + idxStart);
//        idxStart = 2 + idxStart;
//        String chargingForbidden = flag.equals("00") ? "禁止" : "允许";//充电禁止
//        flag = flags2.substring(idxStart, 2 + idxStart);
//        String reserved = flag;//预留位

        //682382A90025808360000100100122041109221317998083600001001001FF57FF56FF85F14D05
        XhpcDeviceMessage deviceMessage = new XhpcDeviceMessage();
        deviceMessage.setType(StationDeviceEnum.PILE.getCode());
        deviceMessage.setSerialNumber(sp.getPileNo());
        deviceMessage.setRemark("充电桩与 BMS 充电过程 BMS 信息");
        deviceMessage.setStatus(0);
        deviceMessage.setContent((String) req.get("hex"));
        deviceMessage.setChargeOrderNo((String) req.get("orderNo"));
        deviceMessageMapper.insertByDomain(deviceMessage);

        return new ServiceResult(false);
    }

}
