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

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.entity.Customer;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmTrader;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author hongzq
 */
@Repository
public class TraderDao extends BaseDao<CrmTrader> {

    public boolean checkNameExists(String name, String excludeId){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select count(1)")
                .line("from crm_trader")
                .line("where status='0' and name=?", name);
        if(StrKit.notBlank(excludeId)){
            builder.line("and id != ?", excludeId);
        }
        return Db.queryLong(builder.build(), builder.params()) == 0;
    }

    public boolean checkQqCompanyCodeExists(String qqCompanyCode, String excludeId){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select count(1)")
                .line("from crm_trader")
                .line("where status='0' and qq_company_code=?", qqCompanyCode);
        if(StrKit.notBlank(excludeId)){
            builder.line("and id != ?", excludeId);
        }
        return Db.queryLong(builder.build(), builder.params()) == 0;
    }

    public boolean checkQqProjectCodeExists(String qqProjectCode, String excludeId){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select count(1)")
                .line("from crm_trader")
                .line("where status='0' and qq_project_code=?", qqProjectCode);
        if(StrKit.notBlank(excludeId)){
            builder.line("and id != ?", excludeId);
        }
        return Db.queryLong(builder.build(), builder.params()) == 0;
    }


    public boolean checkMobileExists(String mobile, String excludeId){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select count(1)")
                .line("from sys_user")
                .line("where status='0' and user_type='6' and mobile=?", mobile);
        if(StrKit.notBlank(excludeId)){
            builder.line("and id != ?", excludeId);
        }
        return Db.queryLong(builder.build(), builder.params()) == 0;
    }

    public CrmTrader getTraderBuserId(String userId){
        return getByHql(" from CrmTrader where userId =:p1",new Parameter(userId));
    }

    /**
     * 查找代理商权限列表
     * @return menuId列表
     */
    public List<String> findRolePermission(String roleId){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select m.id ")
                .line("from sys_menu_pro m")
                .line("inner join sys_role_menu r on m.id=r.menu_id")
                .line("where r.role_id=? ",roleId);
        return Db.query(builder.build(),builder.params());
    }


    public List<Record> findTraderListByProvince(String province){
        SqlBuilder sql = new SqlBuilder();
        sql.line("SELECT id,name FROM crm_trader WHERE status='0' ");
        if(StringUtil.isNotBlank(province)){
            sql.inst("AND province IN", province.split(","));
        }
        return Db.find(sql.build(),sql.params());
    }


    public List<Record> findTraderListByProvinces(String province){
        SqlBuilder sql = new SqlBuilder();
        sql.line(" SELECT id,name from crm_trader where status='0'  ");
        if(StringUtil.isNotBlank(province)){
            sql.inst("and id in (SELECT trader_id from crm_trader_pcd where province in  ", province.split(","));
            sql.line(" )");
        }
        return Db.find(sql.build(),sql.params());
    }

    public List<Record> getAllTrader(){
        String sql=" SELECT t.id,t.name,t.actual_contacts,t.telephone FROM `crm_trader` t WHERE t.status<>'1' ";
        return Db.find(sql);
    }

    public List<Record> getAllTopTrader(){
        String sql=" SELECT t.id,t.name,t.actual_contacts,t.telephone FROM `crm_trader` t WHERE t.status<>'1' and t.type='1' ";
        return Db.find(sql);
    }

    public Long getEmpCountByRole(String role,String traderId){
        return Db.queryLong(" SELECT COUNT(e.id) FROM `crm_trader_employe` e WHERE e.status='0' AND role_id=? AND trader_id=?  ", role,traderId);
    }

}
