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.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.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
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 final Logger log = LoggerFactory.getLogger(RemoteStartReplyDataLogic.class);
    private static final 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;
    @Resource
    XhpcDeviceMessageMapper deviceMessageMapper;

    @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);
        String pushOrderKey = orderkey.replace("order:", "pushOrder:");
        Map<String, Object> pushOrder = REDIS.getCacheMap(pushOrderKey);
        if (HEX_01.equals(remoteStartReplyData.getStartResult())) {
            final String orderstarttime = DateUtil.format(Calendar.getInstance().getTime(), NORM_DATETIME_FORMAT);
            cacheGun.put("pileStartTime", orderstarttime);
            cacheGun.put("orderstoptime", null);
            cacheGun.put("orderkey", orderkey);
            cacheOrder.put("pileStartTime", orderstarttime);
            String substring = "gun:"+orderNo.substring(0, 16);

            REDIS.setCacheMap(substring, cacheGun);
            REDIS.setCacheMap(orderkey, cacheOrder);
            pileOrderService.pileStartup(orderNo, 1, "启动充电成功");
            if (pushOrder != null) {
                REDIS.setCacheMapValue(pushOrderKey, "startChargeSeqStat", 2);
            }  else {
                Map map = new HashMap<>();
                map.put("startChargeSeqStat", Integer.valueOf(2));
                REDIS.setCacheMap(pushOrderKey, map);
            }
        } else {

            final String remark = frs.get(remoteStartReplyData.getFailReason());
            pileOrderService.pileStartup(orderNo, 2, remark == null ? "未知错误" : remark);
            if (pushOrder != null) {
                REDIS.setCacheMapValue(pushOrderKey, "startChargeSeqStat", 4);
            } else {
                Map map = new HashMap<>();
                map.put("startChargeSeqStat", Integer.valueOf(4));
                REDIS.setCacheMap(pushOrderKey, map);
            }
        }
        XhpcDeviceMessage deviceMessage = new XhpcDeviceMessage();
        deviceMessage.setType(StationDeviceEnum.PILE.getCode());
        deviceMessage.setSerialNumber(sp.getPileNo());
        deviceMessage.setRemark("远程启动充电命令回复");
        deviceMessage.setStatus(0);
        deviceMessage.setContent((String) req.get("hex"));
        deviceMessage.setChargeOrderNo((String) req.get("orderNo"));
        deviceMessageMapper.insertByDomain(deviceMessage);
        return new ServiceResult(false);
    }

    public static void main(String[] args) {
        String st ="80836000100009012401271201045239";
        System.out.println(st.substring(0,16));
    }
}
