package com.iformall.service.kw.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.IdWorker;
import com.iformall.domain.po.KwMeter;
import com.iformall.mapper.KwMeterMapper;
import com.iformall.service.kw.KwMeterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class KwMeterServiceImpl implements KwMeterService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    KwMeterMapper kwMeterMapper;

    @Override
    public PageInfo<KwMeter> listAsPage(KwMeter record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> kwMeterMapper.findVoList(record));
    }

    @Override
    public KwMeter getById(Long id) {
        return kwMeterMapper.selectById(id);
    }


    @Override
    public void saveOrUpdate(KwMeter record) {
        if (record.getId() == null) {
            final IdWorker idWorker = IdWorker.get();
        	record.setId(idWorker.nextId());
        	record.setCreateTime(new Date());
            record.setUpdateTime(new Date());
            kwMeterMapper.insert(record);
        } else {
            record.setUpdateTime(new Date());
            kwMeterMapper.updateById(record);
        }
    }

    @Override
    public int onlineBatch(List<KwMeter> record) {
        return kwMeterMapper.onlineBatch(record);
    }

    @Override
    public int offlineBatch(KwMeter record) {
        return kwMeterMapper.offlineBatch(record);
    }

    @Override
    public void deleteById(Long id) {
        kwMeterMapper.deleteById(id);
    }

    @Override
    public List<KwMeter> findList(KwMeter record) {
        return kwMeterMapper.findList(record);
    }

    @Override
    public List<KwMeter> findVoList(KwMeter record) {
        return kwMeterMapper.findVoList(record);
    }

    @Override
    public int selectCount(KwMeter record) {
        return kwMeterMapper.selectCount(new QueryWrapper(record));
    }

    @Override
    public int bind(Long boxId, Long meterId) {
        if (meterId == null || boxId == null)
            return 0;
        return kwMeterMapper.bind(boxId, meterId);
    }

    @Override
    public int bindBatch(Long boxId, List<Long> meterIds) {
        if (meterIds == null || boxId == null)
            return 0;
        return kwMeterMapper.bindBatch(boxId, meterIds);
    }

    @Override
    public int unbind(Long meterId) {
        if (meterId == null)
            return 0;
        return kwMeterMapper.unbind(meterId);
    }

    @Override
    public int unbindBatch(Long boxId) {
        if (boxId == null)
            return 0;
        return kwMeterMapper.unbindBatch(boxId);
    }

}
