package com.ylcm.sys.service.impl;

import java.util.List;

import com.ylcm.sys.enums.AdSpecialTypeEnum;
import com.ylcm.sys.form.AdProductForm;
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.AdProduct;
import com.ylcm.sys.domain.AdSpecial;
import com.ylcm.sys.domain.AdTheatre;
import com.ylcm.sys.excel.model.AdProductImportDTO;
import com.ylcm.sys.exception.BaseException;
import com.ylcm.sys.mapper.AdProductMapper;
import com.ylcm.sys.mapper.AdSpecialMapper;
import com.ylcm.sys.mapper.AdTheatreMapper;
import com.ylcm.sys.service.AdProductService;

import javax.annotation.Resource;

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

	@Resource
	private AdProductMapper adProductMapper;
	@Resource
	private AdSpecialService adSpecialService;
	@Resource
	private AdSpecialMapper adSpecialMapper;
	@Resource
    private AdTheatreMapper adTheatreMapper;
	@Resource
	private AdTheatreService adTheatreService;
	
	@Override
	public AdProduct getByName(String name) {
		
		LambdaQueryWrapper<AdProduct> queryWrapper = new LambdaQueryWrapper<AdProduct>().eq(AdProduct::getProductName, name)
		                                                                                .last("LIMIT 1");
		AdProduct adProduct = baseMapper.selectOne(queryWrapper);
		return adProduct;
	}
	
	@Override
	public AdProduct getByName(String name, String brandName) {
		
		return adProductMapper.getByName(name, StringUtils.isNotBlank(brandName) ? brandName.trim() : null);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String importBatch(List<AdProductImportDTO> importDTOs) {
		int index = 2; //表格从第二行开始
		for (AdProductImportDTO adProductImportDTO : importDTOs) {
			index ++;
			AdProduct adProduct = adProductImportDTO.toAdProduct();
			AdProduct oldAdProduct = getByName(adProduct.getProductName(),adProduct.getBrandName());
			if (oldAdProduct != null) {
				throw new BaseException("第"+ index + "行，重复产品名称");
			}
			baseMapper.insert(adProduct);
		}
		return "ok";
	}

	@Override
	public Integer getSelectProductNameTotal(String productName) {

		return adProductMapper.getSelectProductNameTotal(productName);
	}

	@Override
	public List<String> getSelectProductNameList(String productName, Integer pageNo, Integer pageSize) {
		
		return adProductMapper.getSelectProductNameList(productName, pageNo, pageSize);
	}
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updateByForm(AdProductForm upAdProductForm) {
		AdProduct adProduct = getById(upAdProductForm.getId());
		if (!adProduct.getProductName().equals(upAdProductForm.getProductName())
				|| upAdProductForm.isCanSyscUpBrandName(adProduct.getBrandName())){
			adSpecialService.upSyscAdSpecialProductName(AdSpecialTypeEnum.PRODUCT.getCode(), upAdProductForm.getProductName(), upAdProductForm.getBrandName(), adProduct.getProductName() , adProduct.getBrandName());
			adTheatreService.updateProductName(AdSpecialTypeEnum.PRODUCT.getCode(), adProduct.getProductName(), upAdProductForm.getProductName());
		}
		AdProduct addAdProduct = new AdProduct();
		BeanUtils.copyProperties(upAdProductForm, addAdProduct);
		this.updateById(addAdProduct);
	}

	@Override
	public String deleteById(Integer id) {
		AdProduct adProduct = getById(id);
		AdSpecial adSpecial = adSpecialMapper.selectOne(
				new LambdaQueryWrapper<AdSpecial>().eq(AdSpecial::getProductName, adProduct.getProductName())
						                           .eq(AdSpecial:: getType, AdSpecialTypeEnum.PRODUCT.getCode())
						                           .last("limit 1"));
		if (adSpecial != null) {
			return "该产品在专题中使用不能删除";
		}
		AdTheatre adTheatre = adTheatreMapper.selectOne(
				new LambdaQueryWrapper<AdTheatre>().eq(AdTheatre::getTypeName, adProduct.getProductName())
												   .eq(AdTheatre:: getType, AdSpecialTypeEnum.PRODUCT.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<AdProduct> queryWrapper = new LambdaQueryWrapper<AdProduct>();
		queryWrapper.eq(AdProduct::getProductName, name);
		return baseMapper.selectCount(queryWrapper);
	}

}
