package com.seer.teach.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.enums.TvVersionCategoryEnum;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.iot.common.resp.TvVersionResp;
import com.seer.teach.iot.convert.TvVersionsConvert;
import com.seer.teach.iot.module.entity.BeatsEntity;
import com.seer.teach.iot.module.entity.DevicesEntity;
import com.seer.teach.iot.module.entity.IotVersionsEntity;
import com.seer.teach.iot.module.mapper.DevicesMapper;
import com.seer.teach.iot.module.mapper.IotVersionsMapper;
import com.seer.teach.iot.service.BeatsService;
import com.seer.teach.iot.service.IotVersionsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Captain
 * @since 2025-05-09
 */
@Service
@RequiredArgsConstructor
public class IotVersionsServiceImpl extends ServiceImpl<IotVersionsMapper, IotVersionsEntity> implements IotVersionsService {

    private final DevicesMapper devicesMapper;

    private final BeatsService beatsService;

    /**
     * 根据设备标识获取 TV 版本信息。
     * <p>
     * 该方法首先验证设备是否已绑定，若已绑定则获取设备当前心跳中的版本信息；
     * 若未绑定，则返回默认的最新版本信息。
     * </p>
     *
     * @param unionId 设备唯一标识（如 MAC 地址或序列号）
     * @return TvVersionResp 返回 TV 版本信息
     */
    @Override
    public TvVersionResp getVersionByDevice(String unionId, Integer versionCode) {
        DevicesEntity deviceEntity = devicesMapper.selectOne(new LambdaQueryWrapper<DevicesEntity>()
                .eq(DevicesEntity::getUnionId, unionId));
        BeatsEntity one = beatsService.getOne(new LambdaQueryWrapper<BeatsEntity>()
                .eq(BeatsEntity::getDeviceId, deviceEntity.getId()));
        if (one != null) {
            IotVersionsEntity beatVersion = getById(one.getVersionId());
            return TvVersionsConvert.INSTANCE.toTvVersionResp(beatVersion);
        }
        IotVersionsEntity latestVersion = getOne(
                new LambdaQueryWrapper<IotVersionsEntity>()
                        .eq(IotVersionsEntity::getVersionCategory, TvVersionCategoryEnum.STABLE.getCode())
                        .orderByDesc(IotVersionsEntity::getVersionCode)
                        .last("limit 1"));
        IotVersionsEntity version = getOne(new LambdaQueryWrapper<IotVersionsEntity>()
                .eq(IotVersionsEntity::getVersionCode, versionCode));
        if (version != null) {
            deviceEntity.setVersionId(version.getId());
            devicesMapper.updateById(deviceEntity);
        }
        return TvVersionsConvert.INSTANCE.toTvVersionResp(latestVersion);
    }

    @Override
    public IotVersionsEntity getByVersionCode(Integer versionCode) {
        IotVersionsEntity one = this.lambdaQuery()
                .eq(IotVersionsEntity::getVersionCode, versionCode)
                .one();
        AssertUtils.notNull(one, ResultCodeEnum.VERSION_NOT_EXIST);
        return one;
    }

}
