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.AdTv;
import com.ylcm.sys.excel.model.AdTvImportDTO;
import com.ylcm.sys.exception.BaseException;
import com.ylcm.sys.mapper.AdTvMapper;
import com.ylcm.sys.service.AdTvService;

/**
 * <p>
 * 电视剧管理表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-01
 */
@Service
public class AdTvServiceImpl extends ServiceImpl<AdTvMapper, AdTv> implements AdTvService {

	@Override
	public AdTv getByName(String name) {
	
		LambdaQueryWrapper<AdTv> queryWrapper = new LambdaQueryWrapper<AdTv>().eq(AdTv::getName, name);
		AdTv adTv = baseMapper.selectOne(queryWrapper);
		return adTv;
	}

	@Override
	@Transactional
	public String importBatch(List<AdTvImportDTO> importDTOs) {
		for (AdTvImportDTO adTvImportDTO : importDTOs) {
			AdTv adTv = adTvImportDTO.toAdTv();
			AdTv oldAdTv = getByName(adTv.getName());
			if (oldAdTv != null) {
				throw new BaseException("重复电话剧名称");
			}
			baseMapper.insert(adTv);
		}
	    return "ok";
	}

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