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

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.device.domain.entity.TsbDevice;
import com.ruoyi.device.mapper.TsbDeviceMapper;
import com.ruoyi.device.mqtt.annotation.JsonCmdUpHandler;
import com.ruoyi.device.mqtt.constants.MqttConstants;
import com.ruoyi.device.mqtt.domain.BaseJsonBody;
import com.ruoyi.device.mqtt.domain.decoder.DevicePtRequest;
import com.ruoyi.device.mqtt.domain.encoder.DevicePtResponse;
import com.ruoyi.device.mqtt.enums.CmdTypeEnum;
import com.ruoyi.device.mqtt.handler.decoder.json.IJsonCmdUpHandler;
import com.ruoyi.device.mqtt.vo.CommonHeader;
import com.ruoyi.device.mqtt.vo.CommonTopic;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 调试宝产测：tsb:pt:up → tsb:pt:down
 *
 * @author lwm
 */
@JsonCmdUpHandler(cmdType = CmdTypeEnum.TSB_PT)
public class DevicePtService implements IJsonCmdUpHandler
{
    private static final Logger log = LoggerFactory.getLogger(DevicePtService.class);

    @Resource
    private TsbDeviceMapper tsbDeviceMapper;

    @Override
    public BaseJsonBody handle(CommonTopic topic, CommonHeader header, String bodyJson, CmdTypeEnum cmd)
    {
        DevicePtRequest request = JSON.parseObject(bodyJson, DevicePtRequest.class);
        if (request == null)
        {
            log.info("调试宝产测报文体解析失败");
            return BaseJsonBody.fail(topic.getDeviceType(), topic.getDeviceSn(), cmd.getCmdDownType(), "调试宝产测报文体解析失败");
        }

        // 1、设备查询
        if (StringUtils.isEmpty(request.getImei()))
        {
            log.info("IMEI 不能为空");
            return BaseJsonBody.fail(request.getDeviceType(), request.getDeviceSn(), cmd.getCmdDownType(), "IMEI 不能为空");
        }
        TsbDevice device = tsbDeviceMapper.selectTsbDeviceByImei(request.getImei());
        if (device == null)
        {
            log.info("未找到 IMEI:{} 对应设备", request.getImei());
            return BaseJsonBody.fail(request.getDeviceType(), request.getDeviceSn(), cmd.getCmdDownType(), "未找到 IMEI 对应设备");
        }
        if (UserStatus.DELETED.getCode().equals(device.getDelFlag()))
        {
            log.info("设备 IMEI:{} 已被删除", request.getImei());
            return BaseJsonBody.fail(request.getDeviceType(), request.getDeviceSn(), cmd.getCmdDownType(), "设备 IMEI 对应设备已被删除");
        }
        if (UserStatus.DISABLE.getCode().equals(device.getStatus()))
        {
            log.info("设备 IMEI:{} 已被停用", request.getImei());
            return BaseJsonBody.fail(request.getDeviceType(), request.getDeviceSn(), cmd.getCmdDownType(), "设备 IMEI 对应设备已被停用");
        }

        // 2、返回结果
        DevicePtResponse response = new DevicePtResponse();
        response.setCmdType(cmd.getCmdDownType());
        response.setImei(request.getImei());
        DevicePtResponse.DeviceConfig config = new DevicePtResponse.DeviceConfig();
        config.setDeviceType(device.getDeviceType());
        config.setDeviceSn(device.getDeviceSn());
        if (device.getDeviceProduceDate() != null)
        {
            config.setDeviceProduceDate(DateUtils.parseDateToStr(DateUtils.YYYYMMDD, device.getDeviceProduceDate()));
        }
        response.setDeviceConfig(config);
        response.setCode(MqttConstants.SUCCESS_CODE);
        response.setMsg("操作成功");
        // 模拟下发topic所需的数据
        response.setDeviceType(device.getDeviceType());
        response.setDeviceSn(Long.parseLong(request.getImei()));
        return response;
    }

}
