package com.ylcm.sys.service.impl;

import com.ylcm.sys.domain.AdMedia;
import com.ylcm.sys.domain.AdSpecial;
import com.ylcm.sys.excel.model.AdSpecialImportDTO;
import com.ylcm.sys.form.AdSpecialForm;
import com.ylcm.sys.form.AdSpecialQueryForm;
import com.ylcm.sys.mapper.AdSpecialMapper;
import com.ylcm.sys.service.AdProductService;
import com.ylcm.sys.service.AdSpecialService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import javax.annotation.Resource;

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

/**
 * <p>
 * 专题管理 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-01
 */
@Service
public class AdSpecialServiceImpl extends ServiceImpl<AdSpecialMapper, AdSpecial> implements AdSpecialService {
	
	@Resource
	private AdProductService adProductService;

	@Override
	public IPage<AdSpecial> queryPage(AdSpecialQueryForm adSpecialQueryForm) {
		LambdaQueryWrapper<AdSpecial> queryWrapper = new LambdaQueryWrapper<AdSpecial>();
        if (StringUtils.isNotBlank(adSpecialQueryForm.getAdvMaster())) {
			queryWrapper.like(AdSpecial::getAdvMaster, adSpecialQueryForm.getAdvMaster());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getBrandName())) {
			queryWrapper.like(AdSpecial::getBrandName, adSpecialQueryForm.getBrandName());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getMediaName())) {
			queryWrapper.like(AdSpecial::getMediaName, adSpecialQueryForm.getMediaName());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getPhone())) {
			queryWrapper.eq(AdSpecial::getPhone, adSpecialQueryForm.getPhone());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getProductName())) {
			queryWrapper.like(AdSpecial::getProductName, adSpecialQueryForm.getProductName());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getShowDateEnd())) {
			queryWrapper.le(AdSpecial::getShowDate, adSpecialQueryForm.getShowDateEnd());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getShowDateStart())) {
			queryWrapper.ge(AdSpecial::getShowDate, adSpecialQueryForm.getShowDateStart());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getShowTimeEnd())) {
			queryWrapper.le(AdSpecial::getShowTime, adSpecialQueryForm.getShowTimeEnd());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getShowDateStart())) {
			queryWrapper.ge(AdSpecial::getShowDate, adSpecialQueryForm.getShowDateStart());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getTvName())) {
			queryWrapper.like(AdSpecial::getTvName, adSpecialQueryForm.getTvName());
		}
        if (adSpecialQueryForm.getPriceMax() != null) {
			queryWrapper.le(AdSpecial::getPrice, adSpecialQueryForm.getPriceMax());
		}
        if (adSpecialQueryForm.getPriceMin() != null) {
			queryWrapper.ge(AdSpecial::getPrice, adSpecialQueryForm.getPriceMin());
		}
        if (adSpecialQueryForm.getShowDurationMax() != null) {
			queryWrapper.le(AdSpecial::getShowDuration, adSpecialQueryForm.getShowDurationMax());
		}
        if (adSpecialQueryForm.getShowDurationMin() != null) {
			queryWrapper.ge(AdSpecial::getShowDuration, adSpecialQueryForm.getShowDurationMin());
		}
        if (adSpecialQueryForm.getShowEnv() != null) {
			queryWrapper.eq(AdSpecial::getShowEnv, adSpecialQueryForm.getShowEnv());
		}
        if (adSpecialQueryForm.getType() != null) {
			queryWrapper.eq(AdSpecial::getType, adSpecialQueryForm.getType());
		}
        
        queryWrapper.orderByDesc(AdSpecial::getId);
        
        IPage<AdSpecial> page = baseMapper.selectPage(new Page<AdSpecial>(adSpecialQueryForm.getPageNo(), adSpecialQueryForm.getPageSize()), queryWrapper);
		return page;
	}

	@Override
	public AdSpecial getOneByForm(AdSpecialForm form) {
		LambdaQueryWrapper<AdSpecial> queryWrapper = new LambdaQueryWrapper<AdSpecial>();
		queryWrapper.eq(AdSpecial::getProductName, form.getProductName());
		queryWrapper.eq(AdSpecial::getMediaName, form.getMediaName());
		queryWrapper.eq(AdSpecial::getShowEnv, form.getShowEnv());
		queryWrapper.eq(AdSpecial::getTvName, form.getTvName());
		queryWrapper.eq(AdSpecial::getShowDate, form.getShowDate());
		queryWrapper.eq(AdSpecial::getShowTime, form.getShowTime());
		return baseMapper.selectOne(queryWrapper);
	}

	@Transactional
	@Override
	public void importBatch(List<AdSpecialImportDTO> importDTOs) throws Exception{
		if (CollectionUtils.isEmpty(importDTOs)) {
			return;
		}
		for (AdSpecialImportDTO adSpecialImportDTO : importDTOs) {
			AdSpecial adSpecial = adSpecialImportDTO.toSpecial(adProductService);
			AdSpecial oldSpecial = getOneByForm(adSpecialImportDTO.toSpecialForm());
			if (oldSpecial != null) {
				throw new Exception("数据重复");
			}
			baseMapper.insert(adSpecial);
		}
	}
	
	
	

}
