package com.jsowell.netty.handler;

import com.alibaba.fastjson2.JSONObject;
import com.jsowell.common.core.domain.ykc.YKCDataProtocol;
import com.jsowell.common.core.domain.ykc.YKCFrameTypeCode;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.common.util.YKCUtils;
import com.jsowell.netty.factory.YKCOperateFactory;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 离线卡数据查询应答
 *
 * @author JS-ZZA
 * @date 2022/9/27 10:20
 */
@Slf4j
@Component
public class OfflineCardDataQueryResponseHandler extends AbstractHandler{
    private final String type = YKCUtils.frameType2Str(YKCFrameTypeCode.OFFLINE_CARD_DATA_QUERY_ANSWER_CODE.getBytes());

    @Override
    public void afterPropertiesSet() throws Exception {
        YKCOperateFactory.register(type, this);
    }

    @Override
    public byte[] supplyProcess(YKCDataProtocol ykcDataProtocol, Channel channel) {
        log.info("[===离线卡数据查询应答===] param:{}, channel:{}", JSONObject.toJSONString(ykcDataProtocol), channel.toString());
        // 消息体
        byte[] msgBody = ykcDataProtocol.getMsgBody();

        int startIndex = 0;
        int length = 7;

        // 桩编号
        byte[] pileSnByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String pileSn = BytesUtil.bcd2Str(pileSnByteArr);

        // 保存时间
        saveLastTime(pileSn);

        // 第1个卡物理卡号
        startIndex += length;
        length = 8;
        byte[] firstCardNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 查询结果
        startIndex += length;
        length = 1;
        byte[] firstCardResultByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);

        // 第N+1个卡物理卡号
        // 查询结果

        return null;
    }
}
