package com.ylcm.sys.service.impl;

import java.util.List;

import com.ylcm.sys.enums.AdSpecialTypeEnum;
import com.ylcm.sys.form.AdPlatformForm;
import com.ylcm.sys.service.AdSpecialService;
import com.ylcm.sys.service.AdTheatreService;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
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.domain.AdSpecial;
import com.ylcm.sys.domain.AdTheatre;
import com.ylcm.sys.excel.model.AdPlatformImportDTO;
import com.ylcm.sys.exception.BaseException;
import com.ylcm.sys.mapper.AdPlatformMapper;
import com.ylcm.sys.mapper.AdSpecialMapper;
import com.ylcm.sys.mapper.AdTheatreMapper;
import com.ylcm.sys.service.AdPlatformService;

import javax.annotation.Resource;

/**
 * <p>
 * 栏目管理表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-01
 */
@Service
public class AdPlatformServiceImpl extends ServiceImpl<AdPlatformMapper, AdPlatform> implements AdPlatformService {

	@Resource
	private AdPlatformMapper adPlatformMapper;
	@Resource
	private AdSpecialService adSpecialService;
	@Resource
	private AdSpecialMapper adSpecialMapper;
	@Resource
    private AdTheatreMapper adTheatreMapper;
	@Resource
    private AdTheatreService adTheatreService;
	
	@Override
	public AdPlatform getByName(String name) {
		LambdaQueryWrapper<AdPlatform> queryWrapper = new LambdaQueryWrapper<AdPlatform>().eq(AdPlatform::getPlatformName, name)
				                                                                          .last("LIMIT 1");
		AdPlatform adPlatform = baseMapper.selectOne(queryWrapper);
		return adPlatform;
	}
	
	@Override
	public AdPlatform getByName(String name,String brandName) {
	
		return adPlatformMapper.getByName(name, StringUtils.isNotBlank(brandName) ? brandName.trim() : ""); 
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String importBatch(List<AdPlatformImportDTO> importDTOs) {
		int index = 2; //表格从第二行开始
		for (AdPlatformImportDTO adPlatformImportDTO : importDTOs) {
			index ++;
			AdPlatform adPlatform = adPlatformImportDTO.toAdPlatform();
		    AdPlatform oldAdPlatform = getByName(adPlatform.getPlatformName(),adPlatform.getBrandName());
			if (oldAdPlatform != null) {
				throw new BaseException("第"+ index + "行，重复栏目名称");
			}
			baseMapper.insert(adPlatform);
		}
		return "ok";
	}

	@Override
	public Integer getSelectPlatformNameTotal(String platformName) {
        
		return adPlatformMapper.getSelectPlatformNameTotal(platformName);
	}

	@Override
	public List<String> getSelectPlatformNameList(String platformName,Integer pageNo, Integer pageSize) {
		
		return adPlatformMapper.getSelectPlatformNameList(platformName, pageNo, pageSize);
	}
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updateByForm(AdPlatformForm upAdPlatformForm) {
		AdPlatform adPlatform = getById(upAdPlatformForm.getId());
		if (!adPlatform.getPlatformName().equals(upAdPlatformForm.getPlatformName())
				|| upAdPlatformForm.isCanSyscUpBrandName(adPlatform.getBrandName())) {
			adSpecialService.upSyscAdSpecialProductName(AdSpecialTypeEnum.PLATFORM.getCode(), upAdPlatformForm.getPlatformName(), upAdPlatformForm.getPlatformName(), adPlatform.getPlatformName(), adPlatform.getBrandName());
			adTheatreService.updateProductName(AdSpecialTypeEnum.PLATFORM.getCode(), adPlatform.getPlatformName(), upAdPlatformForm.getPlatformName());
		}
		AdPlatform upAdPlatform = new AdPlatform();
		BeanUtils.copyProperties(upAdPlatformForm, upAdPlatform);
		this.updateById(upAdPlatform);
	}

	@Override
	public String deleteById(Integer id) {
	
		AdPlatform adPlatform = getById(id);
		AdSpecial adSpecial = adSpecialMapper.selectOne(
				new LambdaQueryWrapper<AdSpecial>().eq(AdSpecial::getProductName, adPlatform.getPlatformName())
						                           .eq(AdSpecial:: getType, AdSpecialTypeEnum.PLATFORM.getCode())
						                           .last("limit 1"));
		if (adSpecial != null) {
			return "该栏目在专题中使用不能删除";
		}
		AdTheatre adTheatre = adTheatreMapper.selectOne(
				new LambdaQueryWrapper<AdTheatre>().eq(AdTheatre::getTypeName, adPlatform.getPlatformName())
												   .eq(AdTheatre:: getType, AdSpecialTypeEnum.PLATFORM.getCode())                                   
												   .last("limit 1"));
		if (adTheatre != null) {
			return "该栏目在剧场中使用不能删除";
		}
		baseMapper.deleteById(id);
		return null;
	}

	@Override
	public Integer getBrandNameSelectTotalByName(String name) {
		if(StringUtils.isBlank(name)) {
			return 0;
		}
		LambdaQueryWrapper<AdPlatform> queryWrapper = new LambdaQueryWrapper<AdPlatform>();
		queryWrapper.eq(AdPlatform::getPlatformName, name);
		return baseMapper.selectCount(queryWrapper);
	}
}
