package com.xhpc.pp.logic;

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 com.xhpc.pp.utils.security.CRCCalculator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Map;

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

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

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

    public static final String[] stable = {"空闲", "故障"};

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

        Map<String, Object> req = sp.getParameters();
        String pileNo = (String) req.get("pileNo");
        String gunId = (String) req.get("gunId");
        String gunStatus = (String) req.get("gunStatus");
        Long gunStatusInt = Long.parseLong(gunStatus);
        String gunkey = "gun:".concat(pileNo).concat(gunId);
        Map<String, Object> cacheGun = REDIS.getCacheMap(gunkey);
        cacheGun.put("statusInt", gunStatusInt);
        cacheGun.put("hbtime", Calendar.getInstance().getTimeInMillis());
        REDIS.setCacheMap(gunkey, cacheGun);
        String skey = gunkey.concat(".seqhex");
        String seq = seqHex(skey);
        String resultStr = "680D".concat(seq).concat("0004").concat(pileNo).concat(gunId).concat(ServiceResult.HEX_00);
        resultStr = resultStr.concat(CRCCalculator.calcCrc(resultStr));
        return new ServiceResult(HexUtils.toBytes(resultStr), ServiceResult.OK);
    }

}
