package com.ylcm.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylcm.sys.domain.AdSelectType;
import com.ylcm.sys.form.AdSelectForm;
import com.ylcm.sys.mapper.AdSelectTypeMapper;
import com.ylcm.sys.service.AdSelectTypeService;

/**
 * <p>
 * 媒体管理表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-01
 */
@Service
public class AdSelectTypeServiceImpl extends ServiceImpl<AdSelectTypeMapper, AdSelectType> implements AdSelectTypeService {

	@Override
	public List<AdSelectType> getTopSelectCategoryList(AdSelectForm form) {
		form.page();
		LambdaQueryWrapper<AdSelectType> queryWrapper = new LambdaQueryWrapper<AdSelectType>().eq(AdSelectType::getParent, 0) //一级
		                                                                                      .eq(AdSelectType::getType, form.getType())
                                                                                              .last("ORDER BY id LIMIT " + form.getPageNo() +" , "+ form.getPageSize());
		if(StringUtils.isNotBlank(form.getName())) {
			queryWrapper.like(AdSelectType::getName, form.getName());
		}
		return baseMapper.selectList(queryWrapper);
				
	}

	@Override
	public Integer getTopSelectCategoryTotal(AdSelectForm form) {
		LambdaQueryWrapper<AdSelectType> queryWrapper = new LambdaQueryWrapper<AdSelectType>().eq(AdSelectType::getParent, 0) //一级
                                                                                              .eq(AdSelectType::getType, form.getType());
		if(StringUtils.isNotBlank(form.getName())) {
			queryWrapper.like(AdSelectType::getName, form.getName());
		}
		return baseMapper.selectCount(queryWrapper);
	}

	
	@Override
	public List<AdSelectType> getSecondSelectCategoryList(AdSelectForm form) {
		form.page();
		AdSelectType adSelectType = getAdSelectTypeByName(form.getParentName(), form.getType());
		if(adSelectType == null) {
			return new ArrayList<AdSelectType>();
		}
		LambdaQueryWrapper<AdSelectType> queryWrapper = new LambdaQueryWrapper<AdSelectType>().eq(AdSelectType::getParent, adSelectType.getId()) //父类id
																							  .eq(AdSelectType::getType, form.getType())
                                                                                              .last("ORDER BY id LIMIT " + form.getPageNo() +" , "+ form.getPageSize());
      	if(StringUtils.isNotBlank(form.getName())) {
			queryWrapper.like(AdSelectType::getName, form.getName());
		}
		return baseMapper.selectList(queryWrapper);
	}

	@Override
	public Integer getSecondSelectCategoryTotal(AdSelectForm form) {
		AdSelectType adSelectType = getAdSelectTypeByName(form.getParentName(), form.getType());
		if(adSelectType == null) {
			return 0;
		}
		LambdaQueryWrapper<AdSelectType> queryWrapper = new LambdaQueryWrapper<AdSelectType>().eq(AdSelectType::getParent, adSelectType.getId()) //父类id
																							  .eq(AdSelectType::getType, form.getType())
																			                  .last("ORDER BY id LIMIT " + form.getPageNo() +" , "+ form.getPageSize());
		if(StringUtils.isNotBlank(form.getName())) {
		    queryWrapper.like(AdSelectType::getName, form.getName());
		}
		return baseMapper.selectCount(queryWrapper);
	}

	@Override
	public AdSelectType getAdSelectTypeByName(String name, Integer type) {
	    if(StringUtils.isBlank(name) || type == null) {
	    	return null;
	    }
		return baseMapper.selectOne(new LambdaQueryWrapper<AdSelectType>().eq(AdSelectType::getName, name)
                                                                          .eq(AdSelectType::getType, type)
                                                                          .last("LIMIT 1"));                                                          
	}

	@Override
	public AdSelectType getAdSecondSelectTypeByName(String name ,Integer parent, Integer type) {
		if(parent == null || type == null || StringUtils.isBlank(name)) {
		    return null;
		}
		return baseMapper.selectOne(new LambdaQueryWrapper<AdSelectType>().eq(AdSelectType::getName, name)
				                                                          .eq(AdSelectType::getParent, parent)
														                  .eq(AdSelectType::getType, type)
														                  .last("LIMIT 1")); 
	}

}
