package com.ruoyi.device.mqtt.handler.encoder.json.service;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.device.domain.model.TsbUserDeviceBind;
import com.ruoyi.device.mqtt.annotation.JsonCmdDownHandler;
import com.ruoyi.device.mqtt.domain.BaseJsonBody;
import com.ruoyi.device.mqtt.domain.encoder.TaxDataDown;
import com.ruoyi.device.mqtt.enums.CmdTypeEnum;
import com.ruoyi.device.mqtt.handler.encoder.json.IJsonCmdDownHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 报税口下行：common:tax:down
 * <p>
 * 收到 WebSocket页面数据并推送到 设备 MQTT 下行应答
 *
 * @author lwm
 */
@JsonCmdDownHandler(cmdType = CmdTypeEnum.COMMON_TAX)
public class TaxDataDownService implements IJsonCmdDownHandler
{
    private static final Logger log = LoggerFactory.getLogger(TaxDataDownService.class);

    @Override
    public BaseJsonBody handle(TsbUserDeviceBind bind, JSONObject data, CmdTypeEnum cmd)
    {
        TaxDataDown taxDataDown = data.to(TaxDataDown.class);
        if (taxDataDown == null)
        {
            log.info("报税口报文体解析失败, deviceSn={}", bind.getDeviceSn());
            return null;
        }
        taxDataDown.setDeviceType(bind.getDeviceType());
        taxDataDown.setDeviceSn(bind.getDeviceSn());
        taxDataDown.setCmdType(cmd.getCmdDownType());
        return taxDataDown;
    }
}
