package com.bocloud.ims.repository.resource;

import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.common.Dictionary;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典操作数据层接口实现类
 *
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 */
@Component("dictionaryRepository")
public class DictionaryRepository extends BasicGenericDao<Dictionary, Long> {

    public DictionaryRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate) {
        super(jdbcTemplate, npJdbcTemplate);
    }

    public List<Dictionary> dictionaries(String pvalue) throws Exception {
        if (StringUtils.isNotEmpty(pvalue)) {
            String sql = "select * from dictionary where pid = :pvalue order by weight,id";
            Map<String, Object> params = MapTools.simpleMap("pvalue", pvalue);
            return this.list(Dictionary.class, sql, params);
        } else {
            String sql = "select * from dictionary where pvalue = '0' order by weight,id";
            return this.list(Dictionary.class, sql, new HashMap<>(8));
        }
    }

    public List<Dictionary> list() throws Exception {
        return this.list(Dictionary.class, "SELECT * FROM dictionary order by weight,id", new HashMap<>(8));
    }

    public List<Dictionary> listChildren(String pvalue) throws Exception {
        if (!StringUtils.isEmpty(pvalue)) {
            String sql = "SELECT * FROM dictionary a WHERE a.pvalue = :pvalue order by weight,id";
            return this.list(Dictionary.class, sql, MapTools.simpleMap("pvalue", pvalue));
        } else {
            String sql = "SELECT * FROM dictionary a WHERE a.pvalue='0' order by weight,id";
            return this.list(Dictionary.class, sql, null);
        }
    }

    public Dictionary query(Long id) throws Exception {
        String sql = "select * from dictionary where id = :id order by weight,id";
        Map<String, Object> param = MapTools.simpleMap(Common.ID, id);
        List<Dictionary> list = this.list(Dictionary.class, sql, param);
        return ListTool.isEmpty(list) ? null : list.get(0);
    }

    public Dictionary query(String value, String pvalue) throws Exception {
        String sql = "select * from dictionary where value = :value and pvalue = :pvalue order by weight,id";
        Map<String, Object> param = MapTools.simpleMap("value", value);
        param.put("pvalue", pvalue);
        List<Dictionary> list = this.list(Dictionary.class, sql, param);
        return ListTool.isEmpty(list) ? null : list.get(0);
    }

    public boolean existValue(String value, String pvalue) throws Exception {
        String sql = "select * from dictionary where value = :value and pvalue = :pvalue order by weight,id";
        Map<String, Object> params = MapTools.simpleMap("value", value);
        params.put("pvalue", pvalue);
        List<Dictionary> list = this.list(Dictionary.class, sql, params);
        if (list.isEmpty()) {
            return false;
        }
        return true;
    }

    /**
     * 根据value查名称
     *
     * @param value
     * @return
     * @throws Exception
     */

    public String getNameByValue(String value) throws Exception {
        String sql = "select * from dictionary where value = :value order by weight,id";
        Map<String, Object> param = MapTools.simpleMap("value", value);
        List<Dictionary> list = this.list(Dictionary.class, sql, param);
        return ListTool.isEmpty(list) ? null : list.get(0).getName();
    }

}
