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.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 java.util.concurrent.TimeUnit;

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 final Logger log = LoggerFactory.getLogger(RateModelConfigReplyDataLogic.class);

    @Resource
    XhpcDeviceMessageMapper deviceMessageMapper;

    @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();

        String result = FAIL;
        if (HEX_01.equals(configResult)) {
            final String pk = "pile:".concat(pileRateModelConfigReplyData.getPileNo());
            Map<String, Object> cachePile = REDIS.getCacheMap(pk);
            ChargingStationDto cacheStation = REDIS.getCacheObject("station:".concat(cachePile.get("stationId").toString()));
            String version = (String) cachePile.get("version");
            Integer rateModelId = pileRateModelConfigReplyData.getRateModelId();
//            if (!"0A".equals(version) && !"0B".equals(version)) {
//                Long stationRateModelId = cacheStation.getRateModelId();
//                if (stationRateModelId.intValue() == rateModelId) {
//                    cachePile.put("rateModelId", cacheStation.getRateModelId());
//                    result = OK;
//                }
//            } else {
//                cachePile.put("rateModelId", cacheStation.getRateModelId());
//                result = OK;
//            }
            if ( "0F".equals(version)) {
                //System.out.println("==========0X57======");
                cachePile.put("rateModelId", cacheStation.getRateModelId());
                result = OK;
            } else if("0C".equals(version)){
                Long stationRateModelId = cacheStation.getRateModelId();
                if (stationRateModelId.intValue() == rateModelId) {
                    cachePile.put("rateModelId", cacheStation.getRateModelId());
                    result = OK;
                }
            }else {
                cachePile.put("rateModelId", cacheStation.getRateModelId());
                result = OK;
            }
            REDIS.setCacheMap(pk, cachePile);
            String configRateReply = pk.concat(".configRateReply");
            REDIS.setCacheObject(configRateReply, "校费设置成功,费率模型:".concat(String.valueOf(rateModelId)), 30L, TimeUnit.MINUTES);
        }

        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(result);
    }

}
