package com.ylcm.sys.service.impl;

import java.util.List;

import com.ylcm.sys.form.AdTvForm;
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.AdSpecial;
import com.ylcm.sys.domain.AdTheatre;
import com.ylcm.sys.domain.AdTv;
import com.ylcm.sys.excel.model.AdTvImportDTO;
import com.ylcm.sys.exception.BaseException;
import com.ylcm.sys.mapper.AdSpecialMapper;
import com.ylcm.sys.mapper.AdTheatreMapper;
import com.ylcm.sys.mapper.AdTvMapper;
import com.ylcm.sys.service.AdTvService;

import javax.annotation.Resource;

/**
 * <p>
 * 电视剧管理表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-01
 */
@Service
public class AdTvServiceImpl extends ServiceImpl<AdTvMapper, AdTv> implements AdTvService {

	@Resource
	private AdSpecialService adSpecialService;
	@Resource
	private AdSpecialMapper adSpecialMapper;
	@Resource
    private AdTheatreMapper adTheatreMapper;
	@Resource
	private AdTheatreService adTheatreService;
	
	@Override
	public AdTv getByName(String name) {
		LambdaQueryWrapper<AdTv> queryWrapper = new LambdaQueryWrapper<AdTv>().eq(AdTv::getName, name)
				                                                              .last("LIMIT 1");
		AdTv adTv = baseMapper.selectOne(queryWrapper);
		return adTv;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String importBatch(List<AdTvImportDTO> importDTOs) {
		int index = 2; //表格从第二行开始
		for (AdTvImportDTO adTvImportDTO : importDTOs) {
			index ++;
			AdTv adTv = adTvImportDTO.toAdTv();
			AdTv oldAdTv = getByName(adTv.getName());
			if (oldAdTv != null) {
				throw new BaseException("第"+ index + "行，重复电视剧名称");
			}
			baseMapper.insert(adTv);
		}
	    return "ok";
	}

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

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updateByForm(AdTvForm adTvForm) {
		AdTv adTv = baseMapper.selectById(adTvForm.getId());
		if (!adTv.getName().equals(adTvForm.getName())
				|| adTvForm.isCanSyscUpTheme(adTv.getFirstTheme(), adTv.getSecondTheme())) {
			adSpecialService.updateTvName(adTv.getName(), adTvForm.getName());
			adTheatreService.updateTvName(adTv.getName(), adTvForm.getName(), adTvForm.getFirstTheme(), adTv.getFirstTheme(), adTvForm.getSecondTheme());
		}

		AdTv upAdTv = new AdTv();
		BeanUtils.copyProperties(adTvForm, upAdTv);
		this.updateById(upAdTv);
	}

	@Override
	public String deleteById(Integer id) {
		AdTv adTv = getById(id);
		AdSpecial adSpecial = adSpecialMapper.selectOne(
				new LambdaQueryWrapper<AdSpecial>().eq(AdSpecial::getTvName, adTv.getName())
						                           .last("limit 1"));
		if (adSpecial != null) {
			return "该节目在专题中使用不能删除";
		}
		AdTheatre adTheatre = adTheatreMapper.selectOne(
				new LambdaQueryWrapper<AdTheatre>().eq(AdTheatre::getName, adTv.getName())
												   .last("limit 1"));
		if (adTheatre != null) {
			return "该节目在剧场中使用不能删除";
		}
		baseMapper.deleteById(id);
		return null;
	}

	@Override
	public List<AdTv> getListByIds(List<Integer> ids) {
		return baseMapper.selectBatchIds(ids);
	}
}
