package com.ylcm.sys.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylcm.sys.domain.AdPlatform;
import com.ylcm.sys.excel.model.AdPlatformImportDTO;
import com.ylcm.sys.exception.BaseException;
import com.ylcm.sys.mapper.AdPlatformMapper;
import com.ylcm.sys.service.AdPlatformService;

/**
 * <p>
 * 栏目管理表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-01
 */
@Service
public class AdPlatformServiceImpl extends ServiceImpl<AdPlatformMapper, AdPlatform> implements AdPlatformService {
	
	@Override
	public AdPlatform getByName(String name) {
		LambdaQueryWrapper<AdPlatform> queryWrapper = new LambdaQueryWrapper<AdPlatform>().eq(AdPlatform::getPlatformName, name);
		AdPlatform adPlatform = baseMapper.selectOne(queryWrapper);
		return adPlatform;
	}

	@Override
	@Transactional
	public String importBatch(List<AdPlatformImportDTO> importDTOs) {
		for (AdPlatformImportDTO adPlatformImportDTO : importDTOs) {
			AdPlatform adPlatform = adPlatformImportDTO.toAdPlatform();
		    AdPlatform oldAdPlatform = getByName(adPlatform.getPlatformName());
			if (oldAdPlatform != null) {
				throw new BaseException("重复栏目名称");
			}
			baseMapper.insert(adPlatform);
		}
		return "ok";
	}

	@Override
	public Integer getTotalByName(String name) {
		LambdaQueryWrapper<AdPlatform> queryWrapper = new LambdaQueryWrapper<AdPlatform>();
		if(StringUtils.isNotBlank(name)) {
			queryWrapper.like(AdPlatform::getPlatformName, name);
		}
		return baseMapper.selectCount(queryWrapper);
	}

}
