package com.ylcm.sys.service.impl;

import com.ylcm.sys.domain.AdSpecial;
import com.ylcm.sys.excel.model.AdSpecialPlatformImportDTO;
import com.ylcm.sys.excel.model.AdSpecialProductImportDTO;
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.lang.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
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 AdSpecialMapper adSpecialMapper;
	@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().trim());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getBrandName())) {
			queryWrapper.like(AdSpecial::getBrandName, adSpecialQueryForm.getBrandName().trim());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getMediaName())) {
			queryWrapper.like(AdSpecial::getMediaName, adSpecialQueryForm.getMediaName().trim());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getPhone())) {
			queryWrapper.eq(AdSpecial::getPhone, adSpecialQueryForm.getPhone().trim());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getProductName())) {
			queryWrapper.like(AdSpecial::getProductName, adSpecialQueryForm.getProductName().trim());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getShowDateEnd())) {
			queryWrapper.le(AdSpecial::getShowDate, adSpecialQueryForm.getShowDateEnd().trim());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getShowDateStart())) {
			queryWrapper.ge(AdSpecial::getShowDate, adSpecialQueryForm.getShowDateStart().trim());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getShowTimeEnd())) {
			queryWrapper.le(AdSpecial::getShowTime, adSpecialQueryForm.getShowTimeEnd().trim());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getShowTimeStart())) {
			queryWrapper.ge(AdSpecial::getShowTime, adSpecialQueryForm.getShowTimeStart().trim());
		}
        if (StringUtils.isNotBlank(adSpecialQueryForm.getTvName())) {
			queryWrapper.like(AdSpecial::getTvName, adSpecialQueryForm.getTvName().trim());
		}
        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::getType, form.getType());
		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());
		if(StringUtils.isNotBlank(form.getBrandName())) {
			queryWrapper.eq(AdSpecial::getBrandName, form.getBrandName());
		}else {
			// 使用 @TableLogic，故查询条件前自动拼接 WHERE status = 0 
			queryWrapper.and(wrapper -> wrapper.isNull(AdSpecial::getBrandName).or().eq(AdSpecial::getBrandName, ""));
		}
		queryWrapper.last("LIMIT 1");
		return baseMapper.selectOne(queryWrapper);
	}

	@CacheEvict(value = "SpecialAnalysis", allEntries=true)
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String importProductBatch(List<AdSpecialProductImportDTO> importDTOs) throws Exception{
	 	int index = 2; //表格从第二行开始
		for (AdSpecialProductImportDTO adSpecialImportDTO : importDTOs) {
			index ++;
			AdSpecial adSpecial = adSpecialImportDTO.toSpecial();
			AdSpecial oldSpecial = getOneByForm(adSpecialImportDTO.toSpecialForm(adSpecial));
			if (oldSpecial != null) {
				throw new Exception("第"+ index + "行，数据重复");
			}
			baseMapper.insert(adSpecial);
		}
		return "ok";
	}


	@CacheEvict(value = "SpecialAnalysis", allEntries=true)
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String importPlatformBatch(List<AdSpecialPlatformImportDTO> importDTOs) throws Exception {
	 	int index = 2; //表格从第二行开始
		for (AdSpecialPlatformImportDTO adSpecialImportDTO : importDTOs) {
			index ++;
			AdSpecial adSpecial = adSpecialImportDTO.toSpecial();
			AdSpecial oldSpecial = getOneByForm(adSpecialImportDTO.toSpecialForm(adSpecial));
			if (oldSpecial != null) {
				throw new Exception("第"+ index + "行，数据重复");
			}
			baseMapper.insert(adSpecial);
		}
		return "ok";
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int upSyscAdSpecialProductName(Integer type, String productName
			, String brandName,  String oldProductName , String oldBrandName) {

		return adSpecialMapper.upSyscAdSpecialProductName(type, productName, brandName, oldProductName, oldBrandName);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updateTvName(String oldName, String newName) {
		AdSpecial adSpecial = new AdSpecial();
		adSpecial.setTvName(newName);

		LambdaQueryWrapper<AdSpecial> queryWrapper = new LambdaQueryWrapper<AdSpecial>();
		queryWrapper.eq(AdSpecial::getTvName, oldName);
		baseMapper.update(adSpecial, queryWrapper);
	}

	@CacheEvict(value = "SpecialAnalysis", allEntries=true)
	@Override
	public void delete(Integer id) {
		baseMapper.deleteById(id);
	}

	@CacheEvict(value = "SpecialAnalysis", allEntries=true)
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String saveOrUpd(AdSpecialForm form) {
		// 更新
		if (form.getId() != null) {
			AdSpecial domainInDb = this.getOneByForm(form);
			if (domainInDb != null && !form.getId().equals(domainInDb.getId())) {
				return "数据重复";
			}

			AdSpecial domain = form.toSpecial();
			this.updateById(domain);

		} else {
			// 新增
			AdSpecial domainInDb = this.getOneByForm(form);
			if (domainInDb != null) {
				return "数据重复";
			}
			AdSpecial domain = form.toSpecial();
			this.save(domain);
		}
		return null;
	}

    @Override
    public void updateMediaName(String oldName, String newName) {
        AdSpecial adSpecial = new AdSpecial();
        adSpecial.setMediaName(newName);

        LambdaQueryWrapper<AdSpecial> queryWrapper = new LambdaQueryWrapper<AdSpecial>();
        queryWrapper.eq(AdSpecial::getMediaName, oldName);
        baseMapper.update(adSpecial, queryWrapper);
    }

}
