package com.sifang.modules.sunshine.SystemSetting.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmLinkNumberSetting;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.utils.StringUtil;
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 LinkNumberSettingDao extends BaseDao<CrmLinkNumberSetting> {

    /**
     * 获取设置列表
     */
    public List<Record> findList(Map<String, String> params, LayuiPage page) {
        SqlBuilder sb = new SqlBuilder();
        sb.line("SELECT rs.id, rs.bank_name, rs.number, rs.create_time");
        sb.line("FROM crm_link_number_setting rs");
        sb.line("WHERE 1=1");
        queryCondition(sb, params);
        sb.line("ORDER BY rs.create_time DESC");
        if(page != null){
            sb.line(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        }
        return Db.find(sb.build(), sb.params());
    }

    /**
     * 获取列表数据
     */
    public List<Record> findPageList(Map<String, String> params, LayuiPage page) {
        SqlBuilder sb = new SqlBuilder();
        sb.line("SELECT rs.id, rs.bank_name, rs.number, rs.create_time, rs.capitals");
        sb.line("FROM crm_link_number_setting rs");
        sb.line("WHERE 1=1");
        queryCondition(sb, params);
        sb.line("ORDER BY rs.create_time DESC");
        if (page != null) {
            sb.line(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        }
        return Db.find(sb.build(), sb.params());
    }

    /**
     * 获取列表总数
     */
    public long queryCount(Map<String, String> params) {
        SqlBuilder sb = new SqlBuilder();
        sb.line("SELECT COUNT(1)");
        sb.line("FROM crm_link_number_setting rs");
        sb.line("WHERE 1=1");
        queryCondition(sb, params);
        return Db.queryLong(sb.build(), sb.params());
    }

    /**
     * 查询变更类型设置详情
     */
    public Record findOneById(String id) {
        SqlBuilder sb = new SqlBuilder();
        sb.line("SELECT rs.id, rs.number, rs.bank_name, rs.capitals");
        sb.line("FROM crm_link_number_setting rs");
        sb.line("WHERE rs.id = ?", id);
        return Db.findFirst(sb.build(), sb.params());
    }

    /**
     * 根据名称查询是否存在
     */
    public long queryCountByName(String name, String id) {
        SqlBuilder sb = new SqlBuilder();
        sb.line("SELECT COUNT(1)");
        sb.line("FROM crm_link_number_setting rs");
        sb.line("WHERE rs.number = ?", name);
        if (StringUtil.isNotBlank(id)) {
            sb.line("AND rs.id != ?", id);
        }
        return Db.queryLong(sb.build(), sb.params());
    }

    public void deleteById(String id) {
        Db.deleteById("crm_link_number_setting", id);
    }

    /**
     * 查询条件
     */
    private void queryCondition(SqlBuilder sql, Map<String, String> params) {
        if (params == null || params.size() == 0) {
            return;
        }
        if (StringUtil.isNotBlank(params.get("bankName"))) {
            sql.like("AND bank_name LIKE ?", params.get("bankName"));
        }
        if (StringUtil.isNotBlank(params.get("number"))) {
            sql.like("AND number LIKE ?", params.get("number"));
        }
        if (StringUtil.isNotBlank(params.get("bankBranch"))) {
            sql.line("AND bank_belong = ?", params.get("bankBranch"));
        }
        if (StringUtil.isNotBlank(params.get("province"))) {
            sql.line("AND province = ?", params.get("province"));
        }
        if (StringUtil.isNotBlank(params.get("city"))) {
            sql.line("AND city = ?", params.get("city"));
        }
        if (StringUtil.isNotBlank(params.get("startTime"))) {
            sql.line("AND create_time >= ?", params.get("startTime") + " 00:00:00");
        }
        if (StringUtil.isNotBlank(params.get("endTime"))) {
            sql.line("AND create_time <= ?", params.get("endTime") + " 23:59:59");
        }
        if (StringUtil.isNotBlank(params.get("capitals"))) {
            sql.line("AND FIND_IN_SET("+"'"+params.get("capitals")+"'"+",rs.capitals)" );
        }
    }

    public String findBankNUmberName(String number){
        return Db.queryStr("SELECT bank_name FROM `crm_link_number_setting` WHERE number=?",number);
    }

    /**
     * 获取联行号
     */
    public List<Record> findBankList(String name, String partner) {
        SqlBuilder sb = new SqlBuilder();
        sb.line("SELECT rs.id, rs.bank_name, rs.number");
        sb.line("FROM crm_link_number_setting rs");
        sb.like("WHERE bank_name LIKE ?", name);
        sb.line("AND FIND_IN_SET("+"'"+partner+"'"+",rs.capitals)");
        sb.line("LIMIT 5000");
        return Db.find(sb.build(), sb.params());
    }
}
