DataDictionaryServiceImpl.java 4.9 KB
package com.diligrp.website.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.diligrp.website.domain.DataDictionary;
import com.diligrp.website.domain.DataDictionaryValue;
import com.diligrp.website.manager.DataDictionaryManager;
import com.diligrp.website.service.DataDictionaryService;
import com.diligrp.website.util.dao.BaseQuery;
import com.diligrp.website.util.domain.Result;
import com.diligrp.website.util.web.PageTemplate;
import com.google.common.base.Objects;

@Component("dataDictionaryService")
public class DataDictionaryServiceImpl implements DataDictionaryService {

	@Resource
	private DataDictionaryManager dataDictionaryManager;

	@Override
	public DataDictionary getDataDictionaryByCode(String code) {
		return dataDictionaryManager.getDataDictionaryByCode(code);
	}

	@Override
	public DataDictionaryValue getDataDicValById(Long id) {
		return dataDictionaryManager.getDataDicValById(id);
	}

	@Override
	public List<DataDictionaryValue> getDataDictionaryValue(Long id) {
		return dataDictionaryManager.getDataDictionaryValue(id);
	}

	@Override
	public PageTemplate query(BaseQuery query) {
		return dataDictionaryManager.query(query);
	}

	@Override
	public Result deleteValue(Long id) {
		boolean flag = dataDictionaryManager.deleteValue(id);
		return Result.create(flag);
	}

	@Override
	public Result delete(Long id) {
		List<DataDictionaryValue> list = dataDictionaryManager.getDataDictionaryValue(id);
		if (!CollectionUtils.isEmpty(list)) {
			// code 1000 有字典值,不允许删除
			return Result.create(false).setCode(1000);
		}
		boolean flag = dataDictionaryManager.delete(id);
		return Result.create(flag);
	}

	@Override
	public Result save(DataDictionary dic) {
		if (dic == null) {
			// 10000 : 参数错误
			return Result.create(false).setCode(10000);
		}
		if (StringUtils.isEmpty(dic.getCode()) || StringUtils.isEmpty(dic.getName())) {
			return Result.create(false).setCode(10000);
		}
		DataDictionary dbDic = dataDictionaryManager.getDataDictionaryByCode(dic.getCode().trim());
		if (dbDic != null) {
			// 10100 : 字典已经存在
			return Result.create(false).setCode(10100);
		}
		boolean flag = dataDictionaryManager.save(dic);
		return Result.create(flag);
	}

	@Override
	public Result saveValue(DataDictionaryValue dicv) {
		if (dicv == null) {
			// 10000 : 参数错误
			return Result.create(false).setCode(10000);
		}
		if (StringUtils.isEmpty(dicv.getCode()) || StringUtils.isEmpty(dicv.getName())
				|| StringUtils.isEmpty(dicv.getRemark()) || dicv.getSort() == null) {

			return Result.create(false).setCode(10000);
		}
		DataDictionaryValue dbDic = dataDictionaryManager.getDataDicValByCode(dicv.getCode(), dicv.getDdId());
		if (dbDic != null) {
			// 10100 : 字典已经存在
			return Result.create(false).setCode(10100);
		}
		dbDic = dataDictionaryManager.getDataDicValBySort(dicv.getSort(), dicv.getDdId());
		if (dbDic != null) {
			// 10200 : 顺序值已经存在
			return Result.create(false).setCode(10300);
		}
		/*
		 * 值名称不需要判断是否重复 dbDic =
		 * dataDictionaryManager.getDataDicValByName(dicv.getName(),
		 * dicv.getDdId()); if(dbDic != null){ // 10100 : 字典已经存在 return
		 * Result.create(false).setCode(10100); }
		 */
		boolean flag = dataDictionaryManager.saveValue(dicv);
		return Result.create(flag);
	}

	@Override
	public Result edit(DataDictionary dic) {
		if (dic == null || StringUtils.isEmpty(dic.getName())) {
			// 10000 : 参数错误
			return Result.create(false).setCode(10000);
		}
		DataDictionary dbDic = dataDictionaryManager.getDataDictionaryById(dic.getId());
		if (dbDic == null) {
			// 10300 : 字典已经不存在
			return Result.create(false).setCode(10100);
		}
		boolean flag = dataDictionaryManager.edit(dic);
		return Result.create(flag).setMsg(dbDic.getCode());
	}

	@Override
	public Result editValue(DataDictionaryValue dicv) {
		if (dicv == null) {
			// 10000 : 参数错误
			return Result.create(false).setCode(10000);
		}
		if (dicv.getId() == null || StringUtils.isEmpty(dicv.getName()) || StringUtils.isEmpty(dicv.getRemark())
				|| dicv.getSort() == null) {

			return Result.create(false).setCode(10000);
		}
		DataDictionaryValue dbDic = dataDictionaryManager.getDataDicValById(dicv.getId());
		if (dbDic == null) {
			// 10200 : 字典已经不存在
			return Result.create(false).setCode(10100);
		}
		dbDic = dataDictionaryManager.getDataDicValBySort(dicv.getSort(), dbDic.getDdId());
		if (dbDic != null && !Objects.equal(dbDic.getId(), dicv.getId())) {
			// 10300 : 顺序值已经存在
			return Result.create(false).setCode(10300);
		}
		boolean flag = dataDictionaryManager.editValue(dicv);
		return Result.create(flag);
	}

	@Override
	public DataDictionary findById(Long id) {
		return this.dataDictionaryManager.getDataDictionaryById(id);
	}

}