package com.nbclass.activity.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.nbclass.activity.model.ContentTags;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.nbclass.activity.mapper.DataDictItemMapper;
import com.nbclass.activity.mapper.DataDictMapper;
import com.nbclass.activity.model.DataDict;
import com.nbclass.activity.model.DataDictItem;
import com.nbclass.activity.service.DataDictService;
import com.nbclass.exception.ParameterException;

/**
 *
 * @author Leon
 * @datetime 2020年5月28日 下午3:23:26
 */
@Service
public class DataDictServiceImpl implements DataDictService {

	@Resource
	private DataDictMapper mapper;

	@Resource
	DataDictItemMapper dataDictItemMapper;

	private void baseSaveVerify(DataDict entity) {

	}

	@Override
	public void add(DataDict entity) {
		baseSaveVerify(entity);
		mapper.insertSelective(entity);
	}

	@Override
	public void update(DataDict entity) {
		baseSaveVerify(entity);
		mapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public void delete(List<Long> ids) {
		if(ids == null || ids.isEmpty()){
			throw new ParameterException("id不能为空");
		}
		Long cnt = mapper.delCheck(ids);
		if(cnt!=null && cnt>0) {
			throw new ParameterException("请先删除子项再删除字典！");
		}
		List<Long> dataDictID = mapper.getDataDictID();
		for (Long id : ids) {
			if(dataDictID.equals(id)) throw new ParameterException("该字典不可删除!");
		}
		List<Long> tids = dataDictItemMapper.getContentDict();
		for (Long id : ids) {
			for (Long tid : tids) {
				if(id.equals(tid)){
					throw new ParameterException("该字典已被案例引用!");
				}
			}
		}
		mapper.deleteByIds(ids);
	}

	@Override
	public DataDict findById(Long id) {
		if(id == null || id < 1){
			throw new ParameterException("id不能为空");
		}
		DataDict search = new DataDict();
		search.setId(id);
		DataDict entity = mapper.selectOne(search);
		return entity;
	}

	@Override
	public List<DataDict> getList(DataDict entity) {
		return mapper.getList(entity);
	}

	@Override
	public List<DataDictItem> getItemsByKey(String datakey) {
		if(StringUtils.isBlank(datakey)) {
			throw new ParameterException("参数不能为空");
		}
		return mapper.getItemsByKey(datakey);
	}

	@Override
	public List<DataDictItem> getItemList(DataDictItem entity) {
		return dataDictItemMapper.getList(entity);
	}

	@Override
	public void mergeDataDictItem(DataDictItem entity) {
		if(entity.getDictid() == null || entity.getDictid() < 1) {
			throw new ParameterException("字典ID不能为空");
		}
		if(StringUtils.isBlank(entity.getValue())) {
			throw new ParameterException("编码不能为空");
		}
		if(StringUtils.isBlank(entity.getName())) {
			throw new ParameterException("名称不能为空");
		}

		Long cnt = dataDictItemMapper.checkExist(entity);
		if(cnt!=null && cnt>0) {
			throw new ParameterException("该记录已存在");
		}
		if(entity.getId()!=null && entity.getId()>0) {
			dataDictItemMapper.updateByPrimaryKeySelective(entity);
		}else {
			entity.setId(null);
			dataDictItemMapper.insertSelective(entity);
		}
	}

	@Override
	public void deleteDataDictItem(Long id) {
		if(id == null || id < 1) {
			throw new ParameterException("ID不能为空");
		}
		Long cnt = dataDictItemMapper.checkContentTag(id);
		if(cnt!=null && cnt>0) {
			throw new ParameterException("该标签正在使用，请先从案例中解除此标签");
		}

		Long cnt2 = dataDictItemMapper.checkContentFrom(id);
		if(cnt2!=null && cnt2>0) {
			throw new ParameterException("该案例来源正在使用，请先从案例中解除此来源");
		}

		dataDictItemMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<ContentTags> getDropDownBox() {
		return dataDictItemMapper.getDropDownBox();
	}

	@Override
	public String[] getTagId(String name) {
		return dataDictItemMapper.getTagId(name);
	}


}
