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.dto.KwMerchantMeterDataDto;
import com.iformall.domain.dto.KwMerchantMeterDto;
import com.iformall.domain.po.KwMerchantMeter;
import com.iformall.domain.vo.KwMerchantMeterDataVo;
import com.iformall.domain.vo.KwMerchantMeterVo;
import com.iformall.domain.vo.MerchantMeterVo;
import com.iformall.mapper.KwMerchantMeterMapper;
import com.iformall.mapper.KwMeterDataMapper;
import com.iformall.service.kw.KwMerchantMeterService;
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 KwMerchantMeterServiceImpl implements KwMerchantMeterService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    KwMerchantMeterMapper kwMerchantMeterMapper;

    @Autowired
    KwMeterDataMapper kwMeterDataMapper;

    @Override
    public PageInfo<KwMerchantMeterVo> listVoAsPage(KwMerchantMeterDto record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> kwMerchantMeterMapper.findVoList(record));
    }

    @Override
    public KwMerchantMeterVo getById(Long id) {
        return kwMerchantMeterMapper.getById(id);
    }


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

    @Override
    public List<KwMerchantMeterVo> findVoList(KwMerchantMeterDto record) {
        return kwMerchantMeterMapper.findVoList(record);
    }

    @Override
    public List<KwMerchantMeterDataVo> findDataVoList(KwMerchantMeterDataDto record) {
        return kwMerchantMeterMapper.findDataVoList(record);
    }

    @Override
    public int selectCount(KwMerchantMeter record) {
        return kwMerchantMeterMapper.selectCount(new QueryWrapper(record));
    }

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


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

    @Override
    public List<MerchantMeterVo> getMerchantMeter(String tenantId) {
        return kwMerchantMeterMapper.getMerchantMeter(tenantId);
    }

}
