package com.sifang.modules.sys.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;

import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.entity.SysCapitalDict;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.stereotype.Repository;

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

@Repository
public class SysCapitalDictDao extends DjxBaseDao<SysCapitalDict> {

    /**
     * 根据字典码和资方id获取子类字典项
     * @param code
     * @param capitalId
     * @return
     */
    public List<Record> getDictionaryList(String code ,String capitalId){
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT id,capital_id,code,dict_key,dict_value,sort FROM sys_capital_dict ")
                .line(" where status = '1' and  is_deleted = '0'and  dict_key != '-1' and code = ? ",code)
                .line(" and capital_id = ? ",capitalId)
                .line(" order by  CONVERT(sort, SIGNED) ");
        return Db.find(builder.build(),builder.params());
    }


    /**
     * 获取字典项列表分页
     * @return
     */
    public List<Record> getParentDictionaryList(LayuiPage<Record> page,Map<String, String> map){
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT s.id,s.capital_id,s.parent_id,s.code,s.dict_key,s.dict_value,s.sort,s.status,p.name FROM sys_capital_dict s ")
                .line(" LEFT JOIN crm_partner p ON p.id = s.capital_id ")
                .line(" where s.is_deleted = '0'")
                .line(selectConditions(map))
                .line(" order by s.sort asc limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(builder.build(),builder.params());
    }

    /**
     * 获取父类字典项列表计数
     * @return
     */
    public Long queryParentDictCount( Map<String, String> map) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT count(*) FROM sys_capital_dict s ")
                .line(" LEFT JOIN crm_partner p ON p.id = s.capital_id ")
                .line(" where s.is_deleted = '0' ")
                .line(selectConditions(map));
        return Db.queryLong(builder.build(),builder.params());
    }

    public String selectConditions(Map<String, String> map ) {
        SqlBuilder builder = new SqlBuilder();
        if (map == null || map.size() == 0) {
            return "";
        }
        if (StringUtil.isNotBlank(map.get("name"))) {
            builder.line("and s.name like" + likeQuot(map.get("name").trim()));
        }
        if (StringUtil.isNotBlank(map.get("dictValue"))) {
            builder.line("and s.dict_value like" + likeQuot(map.get("dictValue").trim()));
        }
        if (StringUtil.isNotBlank(map.get("dictKey"))) {
            if(StringUtil.isNotBlank(map.get("parentId"))){
                builder.line("and  s.dict_key !="+map.get("dictKey"));
                builder.line("and  s.parent_id='"+map.get("parentId")+"'");
            }else{
                builder.line("and  s.dict_key="+map.get("dictKey"));
            }
        }
        if (StringUtil.isNotBlank(map.get("status"))) {
            builder.line("and  s.status="+map.get("status"));
        }
        if (StringUtil.isNotBlank(map.get("code"))) {
            builder.line("and  s.code='"+map.get("code")+"'");
        }
        if (StringUtil.isNotBlank(map.get("capitalId"))) {
            builder.line("and  s.capital_id="+map.get("capitalId"));
        }

        return builder.toString();
    }

    public String getDictId(String id) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT id FROM sys_capital_dict ")
                .line(" where status = '1' and  is_deleted = '0' ")
                .line(" and id = '"+id+"'");

        return Db.queryStr(builder.build());
    }
    public String getDictValue(String partnerId,String code,String dictKey ) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" select dict_value from sys_capital_dict  ")
                .line(" where capital_id = ? ",partnerId)
                .line(" and code = ?  and dict_key = ? ",code,dictKey)
                .line(" and status = '1' and  is_deleted = '0'  ");
        return Db.queryStr(builder.build(),builder.params());
    }
    public String getDictKey(String partnerId,String code,String dictValue ) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" select dict_key from sys_capital_dict  ")
                .line(" where capital_id = ? ",partnerId)
                .line(" and code = ?  and dict_value = ? ",code,dictValue)
                .line(" and status = '1' and  is_deleted = '0'  ");
        return Db.queryStr(builder.build(),builder.params());
    }

    //可以匹配禁用的
    public String getDictKey2(String partnerId,String code,String dictValue ) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" select dict_key from sys_capital_dict  ")
                .line(" where capital_id = ? ",partnerId)
                .line(" and code = ?  and dict_value = ? ",code,dictValue)
                .line(" and  is_deleted = '0'  ");
        return Db.queryStr(builder.build(),builder.params());
    }

}
