package com.nailart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nailart.dataobj.SampleCollectionDO;
import com.nailart.mapper.SampleCollectionMapper;
import com.nailart.service.SampleCollectionService;
import com.nailart.utils.DateUtils;
import org.springframework.stereotype.Service;

/**
 *
 * @Author: xiaowang
 * @Date: 2021/5/10 14:48
 */
@Service
public class SampleCollectionServiceImpl extends ServiceImpl<SampleCollectionMapper, SampleCollectionDO> implements SampleCollectionService {

    private final SampleCollectionMapper SampleCollectionMapper;

    public SampleCollectionServiceImpl(SampleCollectionMapper SampleCollectionMapper ) {

        this.SampleCollectionMapper = SampleCollectionMapper;
    }

    /**
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public IPage<SampleCollectionDO> listByPage(int page, int size) {
        Page<SampleCollectionDO> objectPage = new Page<>(page, size);
        return SampleCollectionMapper.selectPage(objectPage, null);
    }

    @Override
    public Integer addSampleCollection(SampleCollectionDO SampleCollectionDO) {
        if (SampleCollectionDO.getCreateTime()==null){
            SampleCollectionDO.setCreateTime(DateUtils.getCurrentDateTime());
        }
        return SampleCollectionMapper.insert(SampleCollectionDO);
    }

    @Override
    public Integer updateSampleCollection(SampleCollectionDO SampleCollectionDO) {
        return SampleCollectionMapper.updateById(SampleCollectionDO);
    }

    @Override
    public Integer deleteSampleCollection(Integer id) {
        return SampleCollectionMapper.deleteById(id);
    }


    @Override
    public IPage<SampleCollectionDO> getSampleCollectionByTag(Long tagID,int page, int size) {
        Page<SampleCollectionDO> SampleCollectionDOPage = new Page<>(page, size);
        LambdaQueryWrapper<SampleCollectionDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(SampleCollectionDO::getTagId, tagID);
        return page(SampleCollectionDOPage, wrapper);
    }
}
