package com.iformall.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.IdWorker;
import com.iformall.domain.dto.WxCUserBasicInfoDto;
import com.iformall.domain.po.WxCUserCar;
import com.iformall.mapper.WxCUserCarMapper;
import com.iformall.service.WxCUserCarService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxCUserCarServiceImpl implements WxCUserCarService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    WxCUserCarMapper wxCUserCarMapper;


    @Override
    public PageInfo<WxCUserCar> listAsPage(WxCUserCar record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxCUserCarMapper.findList(record));
    }

    @Override
    public List<WxCUserCar> getList(WxCUserCar record) {
        return wxCUserCarMapper.findList(record);
    }

    @Override
    public Integer countUserCar(WxCUserCar record) {
        return wxCUserCarMapper.countList(record);
    }

    @Override
    public Integer countUser(WxCUserBasicInfoDto dto) {
        return wxCUserCarMapper.countUser(dto);
    }

    @Override
    public WxCUserCar getOne(WxCUserCar record) {
        List<WxCUserCar> userCarList = wxCUserCarMapper.findList(record);
        if (userCarList.size() > 0) {
            return userCarList.get(0);
        }
        return null;
    }

    @Override
    public WxCUserCar getOnlyOne(WxCUserCar record) {
        List<WxCUserCar> userCarList = wxCUserCarMapper.findList(record);
        if (userCarList.size() == 1) {
            return userCarList.get(0);
        }
        return null;
    }


    @Override
    public WxCUserCar getById(Long id) {
        return wxCUserCarMapper.selectById(id);
    }

    @Override
    public Long getNewCarID() {
        final IdWorker idWorker = IdWorker.get();
        return idWorker.nextId();
    }

    @Override
    public void save(WxCUserCar record) {
        wxCUserCarMapper.insert(record);
    }

    @Override
    public void saveOrUpdate(WxCUserCar record) {
        if (record.getId() == null) {
            final IdWorker idWorker = IdWorker.get();
        	record.setId(idWorker.nextId());
            wxCUserCarMapper.insert(record);
        } else {
            wxCUserCarMapper.updateById(record);
        }
    }

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

    @Override
    public void deleteByObj(WxCUserCar record) {
        wxCUserCarMapper.delete(new UpdateWrapper<>(record));
    }
    
}
