package com.ruoyi.system.service.impl;

import com.ruoyi.common.biz.BaseServiceImpl;
import com.ruoyi.system.core.ClientExecute;
import com.ruoyi.system.core.ClientExecuteFactory;
import com.ruoyi.system.domain.SysDevice;
import com.ruoyi.system.mapper.SysDeviceMapper;
import com.ruoyi.system.service.ISysDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-14
 */
@Service
public class SysDeviceServiceImpl extends BaseServiceImpl<SysDevice, Long, SysDeviceMapper> implements ISysDeviceService {
    private ClientExecuteFactory clientExecuteFactory;

    @Autowired
    public SysDeviceServiceImpl(ClientExecuteFactory clientExecuteFactory) {
        this.clientExecuteFactory = clientExecuteFactory;
    }

    @Override
    public Mono<SysDevice> selectById(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public Mono<Long> countList(SysDevice cond) {
        return mapper.countList(cond);
    }

    @Override
    public Mono<List<SysDevice>> selectList(SysDevice cond) {
        return mapper.selectList(cond).collectList();
    }

    /**
     * 新增设备
     *
     * @param sysDevice 设备
     * @return 结果
     */
    @Transactional
    @Override
    public Mono<Long> insert(SysDevice sysDevice) {
        ClientExecute clientExecute = clientExecuteFactory.getClientExecute(sysDevice.getConnectionType());
        if (clientExecute == null) {
            return Mono.error(new RuntimeException("设备类型不符"));
        }
        try {
            clientExecute.createClient(sysDevice);
        }catch (Exception e){
            e.printStackTrace();
        }
        return mapper.insert(sysDevice);
    }

    /**
     * 修改设备
     *
     * @param sysDevice 设备
     * @return 结果
     */
    @Transactional
    @Override
    public Mono<Long> update(SysDevice sysDevice) {
        return mapper.update(sysDevice);
    }

    @Override
    public Mono<Long> deleteById(Long id) {
        return null;
    }

    @Override
    public Mono<Long> deleteByIds(Long[] idList) {
        return null;
    }

    @Override
    public  Mono<SysDevice> selectByDeviceNo(String deviceNo) {
        return mapper.selectByDeviceNo(deviceNo);
    }
}
