package com.xhpc.pp.logic;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xhpc.common.data.up.PileConfigReplyData;
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 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;

import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;

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

    private static final Logger log = LoggerFactory.getLogger(PileConfigReplyDataLogic.class);

    @Resource
    XhpcDeviceMessageMapper deviceMessageMapper;

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

        Map<String, Object> req = sp.getParameters();
        String hex = (String) req.get("hex");
        int idxStart = 12;
        String serialNumber = hex.substring(idxStart, idxStart + 14);
        idxStart =idxStart+14;
        int hibsn = Integer.parseInt(hex.substring(idxStart, 2 + idxStart), 16);
        try{
            if(hibsn==1){
                log.error("进入入库步骤");
                System.out.println();
                String nacosServer = REDIS.getCacheObject("pile:"+serialNumber+":powerNumber").toString();
                deviceMessageMapper.updateXhpcChargingPile(serialNumber,Integer.valueOf(nacosServer));
            }
        }catch (Exception e){
            log.error("桩："+serialNumber+"入库错误");
        }

        //todo
        String remark = "充电桩工作参数设置";
        XhpcDeviceMessage deviceMessage = new XhpcDeviceMessage();
        deviceMessage.setType(StationDeviceEnum.PILE.getCode());
        deviceMessage.setSerialNumber(sp.getPileNo());
        deviceMessage.setRemark(remark);
        deviceMessage.setStatus(0);
        deviceMessage.setContent((String) req.get("hex"));
        deviceMessageMapper.insertByDomain(deviceMessage);
        return new ServiceResult(false);
    }


    public static void main(String[] args) {
        String hex = "680C000000518083600040000101F309";
        int idxStart = 12;
        String substring = hex.substring(idxStart, idxStart + 14);
        idxStart =idxStart+14;
        int hibsn = Integer.parseInt(hex.substring(idxStart, 2 + idxStart), 16);


        System.out.println("substring :"+substring);
        System.out.println("hibsn :"+hibsn);
    }
}
