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.PortfolioDO;
import com.nailart.dataobj.TagDO;
import com.nailart.mapper.PortfolioMapper;
import com.nailart.service.PortfolioService;
import com.nailart.service.TagInfoService;
import com.nailart.utils.DateUtils;
import org.springframework.stereotype.Service;

/**
 *
 * @Author: xiaowang
 * @Date: 2021/5/10 14:48
 */
@Service
public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, PortfolioDO> implements PortfolioService {

    private final PortfolioMapper portfolioMapper;

    public PortfolioServiceImpl(PortfolioMapper portfolioMapper ) {

        this.portfolioMapper = portfolioMapper;
    }

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

    @Override
    public Integer addPortfolio(PortfolioDO portfolioDO) {
        if (portfolioDO.getCreateTime()==null){
            portfolioDO.setCreateTime(DateUtils.getCurrentDateTime());
        }
        return portfolioMapper.insert(portfolioDO);
    }

    @Override
    public Integer updatePortfolio(PortfolioDO portfolioDO) {
        return portfolioMapper.updateById(portfolioDO);
    }

    @Override
    public Integer deletePortfolio(Integer id) {
        return portfolioMapper.deleteById(id);
    }

    @Override
    public Integer addLoves(Integer id, Integer changes) {
        PortfolioDO portfolioDO = portfolioMapper.selectById(id);
        if (portfolioDO == null) {
            return 0;
        }
        portfolioDO.setLoves(portfolioDO.getLoves() + changes);
        return portfolioMapper.updateById(portfolioDO);
    }

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