package com.template.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.template.mapper.SmartDeviceMapper;
import com.template.model.pojo.SmartDevice;
import com.template.model.pojo.SmartDevice;
import com.template.mapper.SmartDeviceMapper;
import com.template.model.result.PageUtils;
import com.template.services.SmartDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 应用管理 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2024-03-26
 */
@Service
public class SmartDeviceServiceImpl extends ServiceImpl<SmartDeviceMapper, SmartDevice> implements SmartDeviceService {
    @Autowired
    private SmartDeviceMapper smartDeviceMapper;

    @Override
    public int insertSmartDevice(SmartDevice sa) {
        int result = smartDeviceMapper.insert(sa);
        return result;
    }

    @Override
    public int updateSmartDevice(SmartDevice sa) {
        int result = smartDeviceMapper.updateById(sa);
        return result;
    }

    @Override
    public PageUtils<SmartDevice> queryPageSmartDevices(int currentPage, int pageCount, String name) {
        Page<SmartDevice> page = new Page<>(currentPage, pageCount);
        QueryWrapper<SmartDevice> queryWrapper = new QueryWrapper<>();
        //queryWrapper.like(StringUtils.hasText(name), "name", name);
        IPage<SmartDevice> result = smartDeviceMapper.selectPage(page,queryWrapper);
        return new PageUtils<>(result);
    }

    @Override
    public int deleteSmartDeviceById(int id) {
        int result = smartDeviceMapper.deleteById(id);
        return result;
    }

    @Override
    public SmartDevice getSmartById(int id) {
        SmartDevice result = smartDeviceMapper.selectById(id);
        return result;
    }

    @Override
    public List<SmartDevice> queryOnLineDevice() {
        QueryWrapper<SmartDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq( "state", 1);
        List<SmartDevice> devices = smartDeviceMapper.selectList(queryWrapper);
        return devices;
    }

    @Override
    public SmartDevice queryDeviceByNum(String num) {
        QueryWrapper<SmartDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq( StringUtils.hasText(num),"num", num);
        SmartDevice result = smartDeviceMapper.selectOne(queryWrapper);
        return result;
    }
}
