package com.bocloud.ims.service.config;

import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.utils.Common;
import com.bocloud.ims.entity.common.Dictionary;
import com.bocloud.ims.interfaces.config.DictService;
import com.bocloud.ims.repository.resource.DictionaryRepository;
import com.bocloud.service.lock.LockFactory;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 字典服务实现类
 *
 * @author dmw
 */
@Service("dictService")
public class DictServiceImpl implements DictService {

    private static Logger logger = LoggerFactory.getLogger(DictServiceImpl.class);
    @Autowired
    private DictionaryRepository dictionaryRepository;
    @Autowired
    private LockFactory lockFactory;

    @Override
    public BocloudResult<List<Map<String, Object>>> list(String value) {
        try {
            List<Map<String, Object>> data = Lists.newArrayList();
            List<Dictionary> dicts = dictionaryRepository.dictionaries(value);
            for (Dictionary dict : dicts) {
                Map<String, Object> map = Maps.newHashMap();
                map.put(Common.ID, dict.getId());
                map.put(Common.NAME, dict.getName());
                map.put(Common.VALUE, dict.getValue());
                map.put("pvalue", dict.getPvalue());
                List<Dictionary> children = dictionaryRepository.dictionaries(dict.getValue());
                if (null != children && !children.isEmpty()) {
                    map.put("children", "[]");
                }
                data.add(map);
            }
            return new BocloudResult<>(true, data, "查询成功");
        } catch (Exception e) {
            logger.error("list dictionary fail:", e);
            return new BocloudResult<>(false, "查询失败", "", "");
        }
    }

    @Override
    public BocloudResult<List<Dictionary>> listChild(String pid) {
        try {
            List<Dictionary> dicts = this.dictionaryRepository.listChildren(pid);
            return new BocloudResult<>(true, dicts, "success");
        } catch (Exception e) {
            logger.error("List Children Dictionary exception:", e);
            return new BocloudResult<>(false, "查询失败", "505", "");
        }
    }

    @Override
    public BocloudResult<List<Dictionary>> childTree(String pid) {
        try {
            List<Dictionary> dicts = this.dictionaryRepository.listChildren(pid);
            List<Dictionary> result = Lists.newArrayList();
            for (Object object : dicts) {
                Dictionary dictionary = (Dictionary) object;
                List<Dictionary> list = dictionaryRepository.listChildren(dictionary.getValue());
                if (!list.isEmpty()) {
                    dictionary.setChildren(new ArrayList());
                }
                result.add(dictionary);
            }
            return new BocloudResult<>(true, result, "success");
        } catch (Exception e) {
            logger.error("List Children Dictionary exception:", e);
            return new BocloudResult<>(false, "查询失败", "505", "");
        }
    }
}