package com.seer.teach.iot.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.iot.api.req.DeviceReqDTO;
import com.seer.teach.iot.api.resp.DeviceRespDTO;
import com.seer.teach.iot.convert.DevicesConvert;
import com.seer.teach.iot.module.entity.DevicesEntity;
import com.seer.teach.iot.module.entity.UserDevicesEntity;
import com.seer.teach.iot.service.DevicesService;
import com.seer.teach.iot.service.UserDevicesService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

/**
 * 设备信息接口实现类
 * <p>
 * 设备信息接口实现类
 * </p>
 *
 * @author Captain
 * @version 1.0
 * @since 2025-07-16 10:42
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class DeviceInfoServiceApiImpl implements DeviceInfoServiceApi {

    private final DevicesService devicesService;

    private final UserDevicesService userDevicesService;

    /**
     * 根据设备号获取设备信息
     *
     * @param device 设备号
     * @return 设备信息 DTO
     */
    @Override
    public DeviceRespDTO getDevice(String device) {
        DevicesEntity one = devicesService.getOne(new LambdaQueryWrapper<DevicesEntity>()
                .eq(DevicesEntity::getUnionId, device));
        DeviceRespDTO deviceDTO = DevicesConvert.INSTANCE.toDeviceDTO(one);
        log.info("设备信息：{}", deviceDTO);
        return deviceDTO;
    }

    /**
     * 根据设备 ID 获取设备信息
     *
     * @param id 设备 ID
     * @return 设备信息 DTO
     */
    @Override
    public DeviceRespDTO getById(Integer id) {
        DevicesEntity one = devicesService.getById(id);
        AssertUtils.notNull(one, ResultCodeEnum.DEVICE_NOT_FOUND);
        return DevicesConvert.INSTANCE.toDeviceDTO(one);
    }

    /**
     * 根据设备号获取设备 ID
     *
     * @param device 设备号
     * @return 设备 ID
     */
    @Override
    public Integer getDeviceIdByDevice(String device) {
        DevicesEntity one = devicesService.getOne(new LambdaQueryWrapper<DevicesEntity>()
                .eq(DevicesEntity::getUnionId, device));
        AssertUtils.notNull(one, ResultCodeEnum.DEVICE_NOT_FOUND);
        return one.getId();
    }


    @Override
    public void updateDeviceById(DeviceReqDTO reqDTO) {
        DevicesEntity deviceEntity = DevicesConvert.INSTANCE.toDeviceEntity(reqDTO);
        devicesService.updateById(deviceEntity);
    }

    @Override
    public Integer getCityIdByUserId(Integer userId) {
        UserDevicesEntity userDevice = userDevicesService.lambdaQuery()
                .eq(UserDevicesEntity::getUserId, userId)
                .one();
        DevicesEntity entity = devicesService.getOne(new LambdaQueryWrapper<DevicesEntity>()
                .eq(DevicesEntity::getId, userDevice.getDeviceId()));
        AssertUtils.notNull(entity, ResultCodeEnum.DEVICE_NOT_FOUND);
        return entity.getCityId();
    }

    @Override
    public void updateDeviceCityByUserId(Integer userId, Integer cityId) {
        Integer unionId = userDevicesService.getDeviceIdByUserId(userId);
        AssertUtils.notNull(unionId, ResultCodeEnum.DEVICE_NOT_FOUND);
        boolean update = devicesService.lambdaUpdate()
                .eq(DevicesEntity::getId, unionId)
                .set(DevicesEntity::getCityId, cityId)
                .update();
        log.info("设备:[{}]更改城市信息：{} :{}",unionId,cityId, update);
    }


}
