package com.xhpc.pp.logic;

import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xhpc.common.api.PileOrderService;
import com.xhpc.common.data.up.RemoteStartReplyData;
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.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

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

import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMAT;
import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
import static com.xhpc.pp.tx.ServiceResult.HEX_01;

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

    private static Logger log = LoggerFactory.getLogger(RemoteStartReplyDataLogic.class);
    private static Map<String, String> frs;

    static {
        frs = new HashMap<>();
        frs.put("00", "无");
        frs.put("01", "设备编号不匹配");
        frs.put("02", "枪已在充电");
        frs.put("03", "设备故障");
        frs.put("04", "设备离线");
        frs.put("05", "未插枪");
    }

    @Autowired
    private PileOrderService pileOrderService;

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

        Map<String, Object> req = sp.getParameters();
        ObjectMapper objectMapper = new ObjectMapper();
        RemoteStartReplyData remoteStartReplyData = objectMapper.convertValue(req, RemoteStartReplyData.class);
        String orderNo = remoteStartReplyData.getOrderNo();
        String orderkey = "order:".concat(orderNo);
        Map<String, Object> cacheOrder = REDIS.getCacheMap(orderkey);
        String startResult = remoteStartReplyData.getStartResult();
        cacheOrder.put("startResult", startResult);
        String gunkey = "gun:".concat(remoteStartReplyData.getPileNo()).concat(remoteStartReplyData.getGunId());
        Map<String, Object> cacheGun = REDIS.getCacheMap(gunkey);
        REDIS.setCacheMap(orderkey, cacheOrder);
        REDIS.setCacheMap(gunkey, cacheGun);
        if (HEX_01.equals(remoteStartReplyData.getStartResult())) {
            final String orderstarttime = DateUtil.format(Calendar.getInstance().getTime(), NORM_DATETIME_FORMAT);
            cacheGun.put("orderstarttime", orderstarttime);
            cacheGun.put("orderstoptime", null);
            cacheGun.put("orderkey", orderkey);
            cacheOrder.put("orderstarttime", orderstarttime);
            REDIS.setCacheMap(gunkey, cacheGun);
            REDIS.setCacheMap(orderkey, cacheOrder);
            pileOrderService.pileStartup(orderNo, 1, "启动充电成功");
        } else {

            final String remark = frs.get(remoteStartReplyData.getFailReason());
            pileOrderService.pileStartup(orderNo, 2, remark == null ? "未知错误" : remark);
        }
        return new ServiceResult(false);
    }

}
