package com.njcn.csdevice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.csdevice.mapper.CsDeviceRegistryMapper;
import com.njcn.csdevice.pojo.po.CsDeviceRegistry;
import com.njcn.csdevice.service.ICsDeviceRegistryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 徐扬
 */
@Service
public class CsDeviceRegistryServiceImpl extends ServiceImpl<CsDeviceRegistryMapper, CsDeviceRegistry> implements ICsDeviceRegistryService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(List<CsDeviceRegistry> list) {
        return this.saveBatch(list);
    }

    @Override
    public boolean update(CsDeviceRegistry csDeviceRegistry) {
        return this.updateById(csDeviceRegistry);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBatch(List<CsDeviceRegistry> list) {
        return this.updateBatchById(list);
    }

    @Override
    public void deleteByCurrentNdid(String currentNdid) {
        LambdaQueryWrapper<CsDeviceRegistry> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CsDeviceRegistry::getCurrentNdid, currentNdid);
        this.remove(queryWrapper);
    }

    @Override
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    public boolean updateIsAccessByCurrentNdid(String currentNdid, Integer isAccess) {
        LambdaUpdateWrapper<CsDeviceRegistry> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CsDeviceRegistry::getCurrentNdid, currentNdid)
                .set(CsDeviceRegistry::getIsAccess, isAccess);
        return this.update(updateWrapper);
    }

    @Override
    public List<CsDeviceRegistry> queryByCurrentNdid(String currentNdid) {
        LambdaQueryWrapper<CsDeviceRegistry> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CsDeviceRegistry::getCurrentNdid, currentNdid);
        return this.list(queryWrapper);
    }

    @Override
    public CsDeviceRegistry queryByCurrentNdidAndClDid(String currentNdid, Integer clDid) {
        LambdaQueryWrapper<CsDeviceRegistry> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CsDeviceRegistry::getCurrentNdid, currentNdid)
                .eq(CsDeviceRegistry::getClDid, clDid);
        return this.getOne(queryWrapper);
    }

}
