package com.xhpc.pp.logic;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xhpc.common.api.dto.ChargingStationDto;
import com.xhpc.common.data.up.PileConfigReplyData;
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 java.util.Map;

import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
import static com.xhpc.pp.tx.ServiceResult.*;

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

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

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

        Map<String, Object> req = sp.getParameters();
        ObjectMapper objectMapper = new ObjectMapper();
        PileConfigReplyData pileRateModelConfigReplyData = objectMapper.convertValue(req, PileConfigReplyData.class);
        String configResult = pileRateModelConfigReplyData.getConfigResult();
        if (HEX_01.equals(configResult)) {
//            确定设置成功的rateModelId 可能涉及协议修改
            final String pkey = "pile:".concat(pileRateModelConfigReplyData.getPileNo());
            Map<String, Object> cachePile = REDIS.getCacheMap(pkey);
            ChargingStationDto cacheStation = REDIS.getCacheObject("station:".concat(cachePile.get("stationId").toString()));
            cachePile.put("rateModelId", cacheStation.getRateModelId());
            REDIS.setCacheMap(pkey, cachePile);
            return new ServiceResult(OK);
        }
        return new ServiceResult(FAIL);
    }

}
