package com.fuyuanshen.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyuanshen.app.service.AppDeviceBizService;
import com.fuyuanshen.equipment.domain.Device;
import com.fuyuanshen.equipment.domain.DeviceAssignments;
import com.fuyuanshen.equipment.mapper.DeviceAssignmentsMapper;
import com.fuyuanshen.equipment.mapper.DeviceMapper;
import com.fuyuanshen.web.service.WEBDeviceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Author: WY
 * @Date: 2025/5/16
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class WEBDeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements WEBDeviceService {

    private final AppDeviceBizService appDeviceService;

    private final DeviceAssignmentsMapper deviceAssignmentsMapper;


    /**
     * WEB端解绑设备
     *
     * @param id
     * @return
     */
    @Override
    public int webUnBindDevice(Long id, Long userId) {
        // 设备端解绑 0:设备端解绑 1:web端解绑
        int type = 1;
        if (userId == null) {
            DeviceAssignments deviceAssignments = deviceAssignmentsMapper.selectById(id);
            if (deviceAssignments == null) {
                throw new RuntimeException("请先将设备入库！！！");
            }
            id = deviceAssignments.getDeviceId();
            type = 0;
        }
        return appDeviceService.unBindDevice(id, userId, type);
    }


}
