package com.platform.service.custom;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.entity.ApiTmSlideShowVo;
import com.platform.service.impl.ApiTmSlideShowServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;

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



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

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

    public List<ApiTmSlideShowVo> getTopSixSlideShow(){
        QueryWrapper<ApiTmSlideShowVo> firstQueryWrapper = new QueryWrapper<>();
        firstQueryWrapper.eq("enable" , 1);
        firstQueryWrapper.eq("is_show" , 1);
        firstQueryWrapper.isNotNull("sort_order");
        firstQueryWrapper.orderByAsc("sort_order");
        firstQueryWrapper.last("limit 6");
        List<ApiTmSlideShowVo> topSixList = getBaseMapper().selectList(firstQueryWrapper);
        if(!CollectionUtils.isEmpty(topSixList)){
            if(topSixList.size() < 6){
                int size = 6 - topSixList.size();
                QueryWrapper<ApiTmSlideShowVo> secondQueryWrapper = new QueryWrapper<>();
                secondQueryWrapper.eq("enable" , 1);
                secondQueryWrapper.eq("is_show" , 1);
                secondQueryWrapper.isNull("sort_order");
                secondQueryWrapper.orderByDesc("create_time");
                secondQueryWrapper.last("limit " + size);
                List<ApiTmSlideShowVo> theLastTopSixList = getBaseMapper().selectList(secondQueryWrapper);
                if(!CollectionUtils.isEmpty(theLastTopSixList)){
                    topSixList.addAll(theLastTopSixList);
                }
            }
        }
        return topSixList;
    }

}

