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.AdProduct;
import com.ylcm.sys.excel.model.AdProductImportDTO;
import com.ylcm.sys.exception.BaseException;
import com.ylcm.sys.mapper.AdProductMapper;
import com.ylcm.sys.service.AdProductService;

/**
 * <p>
 * 产品管理表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-01
 */
@Service
public class AdProductServiceImpl extends ServiceImpl<AdProductMapper, AdProduct> implements AdProductService {

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

	@Override
	@Transactional
	public String importBatch(List<AdProductImportDTO> importDTOs) {
		for (AdProductImportDTO adProductImportDTO : importDTOs) {
			AdProduct adProduct = adProductImportDTO.toAdProduct();
			AdProduct oldAdProduct = getByName(adProduct.getProductName());
			if (oldAdProduct != null) {
				throw new BaseException("重复产品名称");
			}
			baseMapper.insert(adProduct);
		}
		return "ok";
	}

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

}
