package com.fourdage.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.fourdage.base.dao.IBaseDao;
import com.fourdage.base.service.impl.BaseServiceImpl;
import com.fourdage.system.dao.IDataDictionaryDao;
import com.fourdage.system.entity.DataDictionary;
import com.fourdage.system.service.IDataDictionaryService;

/**
 * <p>
 *  数据字典 服务实现类
 * </p>
 *
 * @author ajay peng
 * @since 2016-12-28
 */
@Service
public class DataDictionaryServiceImpl extends BaseServiceImpl<DataDictionary, Integer> implements IDataDictionaryService {

    @Autowired
    private IDataDictionaryDao dataDictionaryDao;

    @Override
    public IBaseDao<DataDictionary, Integer> getBaseDao() {
        return this.dataDictionaryDao;
    }
    
    @Override
    public void saveOrUpdate(DataDictionary dataDictionary) {
        if (dataDictionary.getId() != null) {
        	DataDictionary dbDictionary = find(dataDictionary.getId());
        	dbDictionary.setDataKey(dataDictionary.getDataKey());
        	dbDictionary.setDataValue(dataDictionary.getDataValue());
        	dbDictionary.setCode(dataDictionary.getCode());
        	dbDictionary.setDescription(dataDictionary.getDescription());
        	dbDictionary.setParam1(dataDictionary.getParam1());
        	dbDictionary.setParam2(dataDictionary.getParam2());
        	dbDictionary.setParam3(dataDictionary.getParam3());
            update(dbDictionary);
        } else {
            save(dataDictionary);
        }
    }

	@Override
	public DataDictionary findByCodeAndDataKey(String code, String dataKey) {
		return dataDictionaryDao.findByCodeAndDataKey(code, dataKey);
	}
	
	@Override
	public List<DataDictionary> findByCode(String code){
		return dataDictionaryDao.findByCode(code);
	}

	@Override
	public List<DataDictionary> findByCondition(final DataDictionary dataDictionary) {
		return dataDictionaryDao.findAll(new Specification<DataDictionary>(){
			@Override
			public Predicate toPredicate(Root<DataDictionary> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<Predicate>();
				if(dataDictionary.getId() != null){ 
					predicates.add(cb.equal(root.get("id").as(Integer.class), dataDictionary.getId()));
				}
				if (dataDictionary.getCode() != null){
					predicates.add(cb.equal(root.get("code").as(String.class), dataDictionary.getCode()));
				}
				if (dataDictionary.getDataKey() != null){
					predicates.add(cb.equal(root.get("data_key").as(String.class), dataDictionary.getDataKey()));
				}
				if (dataDictionary.getDataValue() != null){
					predicates.add(cb.equal(root.get("data_value").as(String.class), dataDictionary.getDataValue()));
				}
				if (dataDictionary.getDescription() != null){
					predicates.add(cb.equal(root.get("description").as(String.class), dataDictionary.getDescription()));
				}
				if (dataDictionary.getParam1() != null){
					predicates.add(cb.equal(root.get("param1").as(String.class), dataDictionary.getParam1()));
				}
				if (dataDictionary.getParam2() != null){
					predicates.add(cb.equal(root.get("param2").as(String.class), dataDictionary.getParam2()));
				}
				if (dataDictionary.getParam3() != null){
					predicates.add(cb.equal(root.get("param3").as(String.class), dataDictionary.getParam3()));
				}
				return cb.and(predicates.toArray(new Predicate[predicates.size()]));
			}
		});
	}
}
