/**
 * 
 */
package link.at17.mid.tushare.service;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.base.MPJBaseMapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.interfaces.MPJBaseJoin;

/**   
 * <b>BaseServiceImpl</b>
 * <br>基本业务实现
 * <br>支持联查 Wrapper: {@code MPJLambdaWrapper}
 * @author Doghole
 */
public abstract class BaseServiceImpl<M extends MPJBaseMapper<T>, T> extends MPJBaseServiceImpl<M, T>  {

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public T getOne(Wrapper<T> wrapper) {
        return (wrapper instanceof MPJBaseJoin) ? 
                (T) selectJoinOne(currentModelClass(), (MPJBaseJoin)wrapper) : super.getOne(wrapper);
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public long count(Wrapper<T> wrapper) {
        return (wrapper instanceof MPJBaseJoin) ? 
                baseMapper.selectJoinCount((MPJBaseJoin) wrapper): super.count(wrapper);
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public List<T> list(Wrapper<T> wrapper) {
        return (wrapper instanceof MPJBaseJoin) ? 
                baseMapper.selectJoinList(currentModelClass(), (MPJBaseJoin)wrapper): super.list(wrapper);
    }
    
    
    /**
     * 给子类用的方法
     * @param <E>
     * @param clazz
     * @param wrapper
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public <E extends T, X> E getOneJoin(Class<E> clazz, MPJBaseJoin<X> wrapper) {
        return (E) baseMapper.selectJoinOne(clazz, (MPJBaseJoin)wrapper);
    }
    
    /**
     * 给子类用的方法
     * @param <E>
     * @param clazz
     * @param wrapper
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public <E extends T, X> List<E> listJoin(Class<E> clazz, MPJBaseJoin<X> wrapper) {
        return baseMapper.selectJoinList(clazz, (MPJBaseJoin)wrapper);
    }
    
    /**   
     * 基于 Mybatis-Plus 的分页，返回对应 Page<T> 和 Wrapper<T> 的 Page
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public <E extends IPage<T>> E  page(E page, Wrapper<T> wrapper) {
        return (wrapper instanceof MPJBaseJoin) ? 
                (E) baseMapper.selectJoinPage(page, this.currentModelClass(), (MPJBaseJoin)wrapper):
                super.page(page, wrapper);
    }
    
    /**   
     * 基于 Mybatis-Plus 的分页，返回对应 Page&lt;T> 和 Wrapper&lt;T> 的 Page<br>
     * 给子类用的方法
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public <E extends IPage<S>, S extends T> E joinPage(E page, Class<S> clazz, MPJBaseJoin<S> wrapper) {
        return (E) baseMapper.selectJoinPage(page, clazz, (MPJBaseJoin)wrapper);
    }
    
}
