package com.jpsoft.shinestar.modules.sys.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.sys.dao.DataDictionaryDAO;
import com.jpsoft.shinestar.modules.sys.entity.DataDictionary;
import com.jpsoft.shinestar.modules.sys.service.DataDictionaryService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Component(value="dataDictionaryService")
public class DataDictionaryServiceImpl implements DataDictionaryService {
	@Resource(name="dataDictionaryDAO")
	private DataDictionaryDAO dataDictionaryDAO;

	@Override
	public DataDictionary get(String id) {
		// TODO Auto-generated method stub
		return dataDictionaryDAO.get(id);
	}

	@Override
	public int insert(DataDictionary model) {
		// TODO Auto-generated method stub
		//model.setId(UUID.randomUUID().toString());

		return dataDictionaryDAO.insert(model);
	}

	@Override
	public int update(DataDictionary model) {
		// TODO Auto-generated method stub
		return dataDictionaryDAO.update(model);
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return dataDictionaryDAO.delete(id);
	}

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = dataDictionaryDAO.exist(id);

		return count > 0 ? true : false;
	}

	@Override
	public String getName(String id){
		return dataDictionaryDAO.getName(id);
	}
	@Override
	public String getValue(String id){
		return dataDictionaryDAO.getValue(id);
	}

	@Override
	public String findParentId(String parentId,String value){
		return dataDictionaryDAO.findParentId(parentId,value);
	}

	@Override
	public List<DataDictionary> list() {
		// TODO Auto-generated method stub
		return dataDictionaryDAO.list();
	}

	@Override
	public List<Map<String, Object>> queryChildren(String parentId){
		return dataDictionaryDAO.queryChildren(parentId);
	}

	@Override
	public Page<DataDictionary> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,List<Sort> sortList) {
		Page<DataDictionary> page = PageHelper.startPage(pageNumber,pageSize).doSelectPage(()->{
			dataDictionaryDAO.search(searchParams,sortList);
		});

		return page;
	}

	@Override
	public List<DataDictionary> findByCatalogName(String catalogName) {
		return dataDictionaryDAO.findByCatalogName(catalogName);
	}

	@Override
	public String findNameByCatalogNameAndValue(String catalogName,String value) {
//		Map<String,Object> map = new HashMap<>();
//		map.put("catalogName",catalogName);
//		map.put("value",value);

		return dataDictionaryDAO.findNameByCatalogNameAndValue(catalogName,value);
	}

	@Override
	public Map<String, DataDictionary> findMapByCatalogName(String catalogName) {
		Map<String,DataDictionary> ddMap = new HashMap<>();

		List<DataDictionary> ddList = dataDictionaryDAO.findByCatalogName(catalogName);

		for (DataDictionary dd : ddList) {
			ddMap.put(dd.getValue(),dd);
		}

		return ddMap;
	}
}