package com.platform.service.custom;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.entity.TmSlideShow;
import com.platform.enums.IdStarterEnum;
import com.platform.service.impl.TmSlideShowServiceImpl;
import com.platform.utils.UUidGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author abnerhou
 * @date 2020/9/10 16:36
 * @desciption
 */
@Service
@Slf4j
public class TmSlideShowService extends TmSlideShowServiceImpl {

    public int insertSlideShow(TmSlideShow tmSlideShow){
        if(null == tmSlideShow){
            return  -1;
        }
        if(StringUtils.isBlank(tmSlideShow.getSlideId())){
            tmSlideShow.setSlideId(UUidGenerator.generatorUuid(IdStarterEnum.DEFAULT_ID.getStarter()));
        }
        tmSlideShow.setCreateTime(LocalDateTime.now());
        tmSlideShow.setUpdateTime(LocalDateTime.now());
        tmSlideShow.setEnable(1);
        return getBaseMapper().insert(tmSlideShow);
    }

    public int updateSlideShow(TmSlideShow tmSlideShow){
        if(null == tmSlideShow){
            return -1;
        }
        if(StringUtils.isBlank(tmSlideShow.getSlideId())){
            log.error("轮播图的id为空，无法更新轮播图");
            return -1;
        }
        tmSlideShow.setUpdateTime(LocalDateTime.now());
        UpdateWrapper<TmSlideShow> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("slide_id" , tmSlideShow.getSlideId());
        updateWrapper.last("limit 1");
        return getBaseMapper().update(tmSlideShow , updateWrapper);
    }

    public TmSlideShow getSlideShowById(String slideId){
        if(StringUtils.isBlank(slideId)){
            return new TmSlideShow();
        }
        QueryWrapper<TmSlideShow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("slide_id" , slideId);
        queryWrapper.eq("enable" , 1);
        queryWrapper.last("limit 1");
        return getBaseMapper().selectOne(queryWrapper);
    }

    public TmSlideShow getSlideShowByTitle(String title){
        if(StringUtils.isBlank(title)){
            return new TmSlideShow();
        }
        QueryWrapper<TmSlideShow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title" , title);
        queryWrapper.eq("enable" , 1);
        queryWrapper.last("limit 1");
        return getBaseMapper().selectOne(queryWrapper);
    }

    public IPage<TmSlideShow> getSlideShowByPage(String title , long pageNum , long pageSize){
        IPage<TmSlideShow> page = new Page<>(pageNum , pageSize);
        QueryWrapper<TmSlideShow> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(title)){
            //TODO：看看后面如何解决这个慢查询的问题
            queryWrapper.like("title" , title);
        }
        queryWrapper.eq("enable" , 1);
        queryWrapper.orderByAsc("sort_order").orderByDesc("create_time");
        return getBaseMapper().selectPage(page , queryWrapper);
    }

}

