<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.ibs.list.mapper.SysGroupMapper">
    
    <resultMap type="SysGroup" id="SysGroupResult">
        <result property="id"    column="id"    />
        <result property="groupName"    column="group_name"    />
        <result property="createType"    column="create_type"    />
        <result property="customerNum"    column="customer_num"    />
        <result property="deptId"    column="dept_id"    />
        <result property="userId"    column="user_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateType"    column="update_type"    />
        <result property="status"    column="status"    />
        <result property="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="groupType"    column="group_type"    />
        <result property="groupId"          column="group_id"        />
        <result property="custType"          column="cust_Type"        />
        <result property="createRole"    column="create_role"    />
    </resultMap>

    <resultMap type="SysGroupVo" id="SysGroupVoResult">
        <result property="id"    column="id"    />
        <result property="groupName"    column="group_name"    />
        <result property="createType"    column="create_type"    />
        <result property="customerNum"    column="customer_num"    />
        <result property="deptId"    column="dept_id"    />
        <result property="userId"    column="user_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateType"    column="update_type"    />
        <result property="status"    column="status"    />
        <result property="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="groupType"    column="group_type"    />
        <result property="groupId"          column="group_id"        />
        <result property="custType"          column="cust_Type"        />
        <result property="createRole"    column="create_role"    />
        <result property="campaignId"    column="campaign_id"    />
        <result property="labelIds" column="label_ids"/>
        <result property="labelSql" column="label_sql"/>
    </resultMap>

    <resultMap type="SysGroupRec" id="SysGroupRecResult">
        <result property="id"    column="id"    />
        <result property="groupName"    column="group_name"    />
        <result property="createType"    column="create_type"    />
        <result property="customerNum"    column="customer_num"    />
        <result property="deptId"    column="dept_id"    />
        <result property="userId"    column="user_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateType"    column="update_type"    />
        <result property="status"    column="status"    />
        <result property="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="groupId"          column="group_id"        />
        <result property="custType"          column="cust_Type"        />
        <result property="custId"    column="cust_id"    />
        <result property="custIdc"    column="cust_idc"    />
        <result property="custName"    column="cust_name"    />
        <result property="custPhone"    column="cust_phone"    />
        <result property="pushStatus"          column="push_status"        />
        <result property="gridName"          column="grid_name"        />
        <result property="deptName"          column="dept_name"        />
        <result property="userName"          column="user_name"        />
        <result property="campaignId"    column="campaign_id"    />
        <result property="secondPushStatus"    column="second_push_status"    />
        <result property="gridName2"          column="grid_name2"        />
        <result property="lpName"          column="lp_name"        />
        <result property="tellerId"          column="teller_id"        />
        <result property="labelIds" column="label_ids"/>
        <result property="labelSql" column="label_sql"/>
        <result property="description" column="description"/>
        <result property="updateTypeRelateTime" column="update_type_relate_time"/>
        <result property="relateGroupId" column="relate_group_id"/>
    </resultMap>

    <resultMap type="CustomerList" id="CustomerList">
        <result property="groupName"           column="group_name"          />
        <result property="custId"          column="cust_id"        />
        <result property="custIdc"          column="cust_idc"        />
        <result property="custIsn"          column="cust_isn"        />
        <result property="custName"          column="cust_name"        />
        <result property="custPhone"          column="cust_phone"        />
        <result property="pushStatus"          column="push_status"        />
        <result property="gridName"          column="grid_name"        />
        <result property="gridName2"          column="grid_name2"        />
        <result property="userName"          column="user_name"        />
        <result property="deptName"          column="dept_name"        />
        <result property="deptId"          column="dept_id"        />
        <result property="groupId"          column="group_id"        />
        <result property="outlets"          column="outlets"        />
        <result property="tellerId"          column="teller_id"        />
        <result property="lpName"          column="lp_name"        />
        <result property="socialCreditCode"          column="social_credit_code"        />
        <result property="socialCreditCodeType"          column="social_credit_code_type"        />
        <result property="idType"          column="id_type"        />
        <result property="custType"          column="cust_type"        />
        <result property="tellerNameWithId"          column="teller_name_with_id"        />
        <result property="branchId"          column="branch_id"        />
        <result property="branchName"          column="branch_name"        />
    </resultMap>

    <sql id="selectSysGroup">
        select id,group_id, group_name, create_type, customer_num, dept_id, user_id, create_by, create_time, update_by, update_time, update_type, status, remark, del_flag,group_type,cust_type,create_role , label_ids,label_sql, description, update_type_relate_time, relate_group_id from sys_group
    </sql>
    <sql id="selectSysGroupVo">
        select sg.id,sg.group_id, sg.group_name, sg.create_type, sg.customer_num, sg.dept_id, sg.user_id, concat(su.nick_name,'-',sg.create_by) as create_by, sg.create_time, sg.update_by, sg.update_time, sg.update_type, sg.status, sg.remark, sg.del_flag,sg.group_type,sg.cust_type,sg.create_role
        from sys_group sg left join sys_user su on sg.create_by = su.user_name
    </sql>

    <select id="selectSysGroupList" parameterType="String" resultMap="SysGroupVoResult">
        <include refid="selectSysGroupVo"/>
        <where>
            sg.del_flag = '0'
            <if test="groupName != null  and groupName != ''"> and sg.group_name like concat('%', #{groupName}, '%')</if>
            <if test="deptId != null "> and sg.dept_id div 1000 = #{deptId}</if>
            <if test="createBy != null "> and sg.create_by = #{createBy}</if>
            <if test="type == 0 ">  and sg.cust_type = '2' </if>
            <if test="type == 1 ">  and sg.cust_type != '2' </if>
        </where>
        order by sg.create_time desc
        <if test="updateTimeSort == null ">   ,update_time desc</if>
        <if test="updateTimeSort == 0 ">   ,update_time asc</if>
        <if test="updateTimeSort == 1 ">  , update_time desc</if>

        <if test="customerNumSort == 0 ">  , customer_num asc</if>
        <if test="customerNumSort == 1 ">  , customer_num desc</if>




    </select>

    <select id="selectSysGroupList1" parameterType="String" resultMap="SysGroupVoResult">
        select sg.id,sg.group_id, sg.group_name, sg.create_type, sg.customer_num, sg.dept_id, sg.user_id, concat(su.nick_name,'-',sg.create_by) as create_by, sg.create_time, sg.update_by, sg.update_time, sg.update_type, sg.status, sg.remark, sg.del_flag,sg.group_type,sg.cust_type,sg.create_role
        from sys_group sg left join sys_user su on sg.create_by = su.user_name
        <where>
            sg.del_flag = '0'
            <if test="groupName != null  and groupName != ''"> and sg.group_name like concat('%', #{groupName}, '%')</if>
            <if test="deptId != null "> and sg.dept_id = #{deptId}</if>
            <if test="createBy != null "> and sg.create_by = #{createBy}</if>
        </where>
        order by sg.create_time desc
        <if test="updateTimeSort == null "> , update_time desc</if>
        <if test="updateTimeSort == 0 "> , update_time asc</if>
        <if test="updateTimeSort == 1 ">  ,update_time desc</if>
        <if test="customerNumSort == 0 ">  , customer_num asc</if>
        <if test="customerNumSort == 1 ">  , customer_num desc</if>
    </select>




    <select id="selectSysGroupList5" parameterType="String" resultMap="CustomerList">
        select scgc.group_id,sg.group_name,
        sgc.cust_name,sgc.cust_phone ,scgc.cust_id,sgc.cust_idc,sgc.cust_isn,
        scgc.push_status ,scgc.grid_name,scgc.grid_name2,
        su.nick_name as user_name,sd.dept_name,su.user_name as teller_id,scgc.outlets,sgc.id_type,sg.cust_type,sgc.lp_name,sgc.social_credit_code,sgc.social_credit_code_type
        from sys_campaign_group_customer scgc inner join sys_group_customer sgc on
        scgc.group_id = sgc.group_id and scgc.cust_id = sgc.cust_id
        inner join sys_group sg  on scgc.group_id = sg.group_id  inner join sys_dept sd on scgc.dept_id = sd.dept_id left join sys_user su on scgc.user_id = su.user_id
        <where>
            <if test="groupId != null  and groupId != ''"> and scgc.group_id = #{groupId}</if>
            <if test="deptId != null "> and scgc.dept_id = #{deptId}</if>
            <if test="custId != null  and custId != ''"> and scgc.cust_id like concat('%',  #{custId}, '%') </if>
            <if test="custIdc != null  and custIdc != ''"> and sgc.cust_idc like concat('%',  #{custIdc}, '%') </if>
            <if test="custIsn != null  and custIsn != ''"> and sgc.cust_isn like concat('%',  #{custIsn}, '%') </if>
            <if test="gridName != null  and gridName != ''"> and scgc.grid_name like concat('%',  #{gridName}, '%')</if>
            <if test="gridName2 != null  and gridName2 != ''"> and scgc.grid_name2 like concat('%',  #{gridName2}, '%')</if>
            <if test="custName != null  and custName != ''"> and sgc.cust_name like concat('%',  #{custName}, '%')</if>
            <if test="deptName != null  and deptName != ''"> and sd.dept_name like concat('%',  #{deptName}, '%')</if>
            <if test="outlets != null  and outlets != ''"> and scgc.outlets like concat('%',  #{outlets}, '%')</if>
            <if test="username != null  and username != ''"> and su.nick_name like concat('%',  #{username}, '%')</if>
            <if test="socialCreditCode != null  and socialCreditCode != ''"> and sgc.social_credit_code like concat('%',  #{socialCreditCode}, '%')</if>
            <if test="pushStatus != null  and pushStatus != ''"> and scgc.push_status = #{pushStatus}</if>
        </where>

        union all

        select
        sg.group_id,sg.group_name,
        sgc.cust_name,
        sgc.cust_phone,
        sgc.cust_id,
        sgc.cust_idc,sgc.cust_isn,
        sgc.push_status,
        sgc.grid_name ,
        sgc.grid_name2 ,
        sgc.user_name,
        sgc.dept_name,
        sgc.teller_id,sgc.outlets,sgc.id_type,sg.cust_type,sgc.lp_name,sgc.social_credit_code,sgc.social_credit_code_type
        from
        sys_group_customer sgc
        inner join sys_group sg on
        sgc.group_id = sg.group_id
        left join sys_campaign sc on
        sc.group_id = sgc.group_id
        <where>
            <if test="groupId != null and groupId != ''">
                sgc.group_id = #{groupId}
            </if>
            <if test="deptId != null "> and sgc.dept_id = #{deptId}</if>
            <if test="custId != null and custId != ''">
                and sgc.cust_id like concat('%', #{custId}, '%')
            </if>
            <if test="custIdc != null  and custIdc != ''"> and sgc.cust_idc like concat('%',  #{custIdc}, '%')</if>
            <if test="custIsn != null  and custIsn != ''"> and sgc.cust_isn like concat('%',  #{custIsn}, '%') </if>
            <if test="gridName != null  and gridName != ''"> and sgc.grid_name like concat('%',  #{gridName}, '%')</if>
            <if test="gridName2 != null  and gridName2 != ''"> and sgc.grid_name2 like concat('%',  #{gridName2}, '%')</if>
            <if test="custName != null  and custName != ''"> and sgc.cust_name like concat('%',  #{custName}, '%')</if>
            <if test="deptName != null  and deptName != ''"> and sgc.dept_name like concat('%',  #{deptName}, '%')</if>
            <if test="outlets != null  and outlets != ''"> and sgc.outlets like concat('%',  #{outlets}, '%')</if>
            <if test="username != null  and username != ''"> and sgc.user_name like concat('%',  #{username}, '%')</if>
            <if test="socialCreditCode != null  and socialCreditCode != ''"> and sgc.social_credit_code like concat('%',  #{socialCreditCode}, '%')</if>
            <if test="pushStatus == null or pushStatus ==''">
                and (sgc.push_status = '0' or sgc.push_status = '2')
            </if>
            <if test="pushStatus != null and pushStatus != '' and pushStatus=='1'.toString()">
                and sgc.push_status = '3'
            </if>
            <if test="pushStatus != null and pushStatus != '' and pushStatus!='1'.toString()">
                and sgc.push_status = #{pushStatus}
            </if>
        </where>


    </select>


    <select id="selectSysGroupListImport" parameterType="String" resultMap="CustomerList">
        select
        sg.group_id,sg.group_name,
        sgc.cust_name,
        sgc.cust_phone,
        sgc.cust_id,
        sgc.cust_idc,sgc.cust_isn,
        sgc.push_status,
        sgc.grid_name ,
        sgc.grid_name2 ,
        sgc.user_name,
        sgc.dept_name,
        sgc.dept_id,
        sgc.teller_id,sgc.outlets,sgc.id_type,sg.cust_type,sgc.lp_name,sgc.social_credit_code,sgc.social_credit_code_type,sgc.create_time, sgc.cust_lb, sgc.outlets_id,
        sgc.remark,
        CONCAT(su.nick_name, '-', su.user_name) as teller_name_with_id, dep.parent_id as branch_id, dep.parent_name as branch_name
        from
        sys_group_customer sgc
        inner join sys_group sg on
        sgc.group_id = sg.group_id
        left join sys_user su on sgc.teller_id = su.user_name
        left join
        (SELECT
        sd.dept_id,
        sd.dept_name,
        sd.parent_id,
        sd_parent.dept_name AS parent_name
        FROM sys_dept sd
        LEFT JOIN sys_dept sd_parent ON sd.parent_id = sd_parent.dept_id
        )dep on sgc.outlets_id = dep.dept_id
        <where>
            <if test="groupId != null and groupId != ''">
                sgc.group_id = #{groupId}
            </if>
            <if test="deptId != null "> and sgc.dept_id = #{deptId}</if>
            <if test="custId != null and custId != ''">
                and sgc.cust_id like concat('%', #{custId}, '%')
            </if>
            <if test="custIdc != null  and custIdc != ''"> and sgc.cust_idc like concat('%',  #{custIdc}, '%')</if>
            <if test="custName != null  and custName != ''"> and sgc.cust_name like concat('%',  #{custName}, '%')</if>
            <if test="socialCreditCode != null  and socialCreditCode != ''"> and sgc.social_credit_code like concat('%',  #{socialCreditCode}, '%')</if>
            <if test="tellerId != null  and tellerId != ''"> and sgc.teller_id like concat('%',  #{tellerId}, '%')</if>
            <if test="pushStatus != null  and pushStatus != ''"> and sgc.push_status = #{pushStatus}</if>
        </where>


    </select>

    <select id="selectSysGroupList3" parameterType="String" resultMap="CustomerList">
        select scgc.group_id,sg.group_name,
        sgc.cust_name,sgc.cust_phone ,scgc.cust_id,sgc.cust_idc,sgc.cust_isn,
        scgc.push_status ,scgc.grid_name,scgc.grid_name2,
        su.nick_name as user_name,sd.dept_name,su.user_name as teller_id,scgc.outlets,sgc.id_type,sg.cust_type,sgc.lp_name,sgc.social_credit_code,sgc.social_credit_code_type
        from sys_campaign_group_customer scgc inner join sys_group_customer sgc on
        scgc.group_id = sgc.group_id and scgc.cust_id = sgc.cust_id
        inner join sys_group sg  on scgc.group_id = sg.group_id  inner join sys_dept sd on scgc.dept_id = sd.dept_id left join sys_user su on scgc.user_id = su.user_id
        <where>
            <if test="groupId != null  and groupId != ''"> and scgc.group_id = #{groupId}</if>
            <if test="custId != null  and custId != ''"> and scgc.cust_id like concat('%',  #{custId}, '%') </if>
            <if test="custIdc != null  and custIdc != ''"> and sgc.cust_idc like concat('%',  #{custIdc}, '%') </if>
            <if test="custIsn != null  and custIsn != ''"> and sgc.cust_isn like concat('%',  #{custIsn}, '%') </if>
            <if test="gridName != null  and gridName != ''"> and scgc.grid_name like concat('%',  #{gridName}, '%')</if>
            <if test="gridName2 != null  and gridName2 != ''"> and scgc.grid_name2 like concat('%',  #{gridName2}, '%')</if>
            <if test="custName != null  and custName != ''"> and sgc.cust_name like concat('%',  #{custName}, '%')</if>
            <if test="deptName != null  and deptName != ''"> and sd.dept_name like concat('%',  #{deptName}, '%')</if>
            <if test="outlets != null  and outlets != ''"> and scgc.outlets like concat('%',  #{outlets}, '%')</if>
            <if test="username != null  and username != ''"> and su.nick_name like concat('%',  #{username}, '%')</if>
            <if test="socialCreditCode != null  and socialCreditCode != ''"> and sgc.social_credit_code like concat('%',  #{socialCreditCode}, '%')</if>
            <if test="pushStatus != null  and pushStatus != ''"> and scgc.push_status = #{pushStatus}</if>
            <if test="deptId != null  and deptId != ''">  and scgc.dept_id in
                (select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId},ancestors))
            </if>
        </where>

        union all

        select
        sg.group_id,sg.group_name,
        sgc.cust_name,
        sgc.cust_phone,
        sgc.cust_id,
        sgc.cust_idc,sgc.cust_isn,
        sgc.push_status,
        sgc.grid_name ,
        sgc.grid_name2 ,
        sgc.user_name,
        sgc.dept_name,
        sgc.teller_id,sgc.outlets,sgc.id_type,sg.cust_type,sgc.lp_name,sgc.social_credit_code,sgc.social_credit_code_type
        from
        sys_group_customer sgc
        inner join sys_group sg on
        sgc.group_id = sg.group_id
        left join sys_campaign sc on
        sc.group_id = sgc.group_id
        <where>
            <if test="groupId != null and groupId != ''">
                sgc.group_id = #{groupId}
            </if>
            <if test="custId != null and custId != ''">
                and sgc.cust_id like concat('%', #{custId}, '%')
            </if>
            <if test="custIdc != null  and custIdc != ''"> and sgc.cust_idc like concat('%',  #{custIdc}, '%')</if>
            <if test="custIsn != null  and custIsn != ''"> and sgc.cust_isn like concat('%',  #{custIsn}, '%') </if>
            <if test="gridName != null  and gridName != ''"> and sgc.grid_name like concat('%',  #{gridName}, '%')</if>
            <if test="gridName2 != null  and gridName2 != ''"> and sgc.grid_name2 like concat('%',  #{gridName2}, '%')</if>
            <if test="custName != null  and custName != ''"> and sgc.cust_name like concat('%',  #{custName}, '%')</if>
            <if test="deptName != null  and deptName != ''"> and sgc.dept_name like concat('%',  #{deptName}, '%')</if>
            <if test="outlets != null  and outlets != ''"> and sgc.outlets like concat('%',  #{outlets}, '%')</if>
            <if test="username != null  and username != ''"> and sgc.user_name like concat('%',  #{username}, '%')</if>
            <if test="socialCreditCode != null  and socialCreditCode != ''"> and sgc.social_credit_code like concat('%',  #{socialCreditCode}, '%')</if>
            <if test="pushStatus == null or pushStatus ==''">
                and (sgc.push_status = '0' or sgc.push_status = '2')
            </if>
            <if test="pushStatus != null and pushStatus != '' and pushStatus=='1'.toString()">
                and sgc.push_status = '3'
            </if>
            <if test="pushStatus != null and pushStatus != '' and pushStatus!='1'.toString()">
                and sgc.push_status = #{pushStatus}
            </if>
        </where>

    </select>


    <select id="selectSysGroupList2" parameterType="String" resultMap="CustomerList">
        select scgc.group_id,sg.group_name,
        sgc.cust_name,sgc.cust_phone ,scgc.cust_id,sgc.cust_idc,sgc.cust_isn,
        sgc.remark,
        scgc.push_status ,scgc.grid_name,scgc.grid_name2,
        su.nick_name as user_name,sd.dept_name,su.user_name as teller_id,scgc.outlets,sgc.id_type,sg.cust_type,sgc.lp_name,sgc.social_credit_code,sgc.social_credit_code_type, sgc.create_time, sgc.cust_lb
        from sys_campaign_group_customer scgc inner join sys_group_customer sgc on
        scgc.group_id = sgc.group_id and scgc.cust_id = sgc.cust_id
        inner join sys_group sg  on scgc.group_id = sg.group_id  inner join sys_dept sd on scgc.dept_id = sd.dept_id left join sys_user su on scgc.user_id = su.user_id
        <where>
             scgc.org_claim_status in ('0','1')
            <if test="groupId != null  and groupId != ''"> and scgc.group_id = #{groupId}</if>
            <if test="custId != null  and custId != ''"> and scgc.cust_id like concat('%',  #{custId}, '%') </if>
            <if test="custIdc != null  and custIdc != ''"> and sgc.cust_idc like concat('%',  #{custIdc}, '%') </if>
            <if test="custIsn != null  and custIsn != ''"> and sgc.cust_isn like concat('%',  #{custIsn}, '%') </if>
            <if test="gridName != null  and gridName != ''"> and scgc.grid_name like concat('%',  #{gridName}, '%')</if>
            <if test="gridName2 != null  and gridName2 != ''"> and scgc.grid_name2 like concat('%',  #{gridName2}, '%')</if>
            <if test="custName != null  and custName != ''"> and sgc.cust_name like concat('%',  #{custName}, '%')</if>
            <if test="deptName != null  and deptName != ''"> and sd.dept_name like concat('%',  #{deptName}, '%')</if>
            <if test="outlets != null  and outlets != ''"> and scgc.outlets like concat('%',  #{outlets}, '%')</if>
            <if test="username != null  and username != ''"> and su.nick_name like concat('%',  #{username}, '%')</if>
            <if test="socialCreditCode != null  and socialCreditCode != ''"> and sgc.social_credit_code like concat('%',  #{socialCreditCode}, '%')</if>
            <if test="pushStatus != null  and pushStatus != ''"> and scgc.push_status = #{pushStatus}</if>
            <if test="deptId != null  and deptId != ''">  and scgc.dept_id in
            (select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId},ancestors))
            </if>

        </where>



    </select>

    <select id="selectCustomerList" parameterType="String" resultMap="CustomerList">
        select
        sg.group_id,sg.group_name,
        sgc.cust_name,
        sgc.cust_phone,
        sgc.cust_id,
        sgc.cust_idc,sgc.cust_isn,
        sgc.push_status,
        sgc.grid_name ,
        sgc.grid_name2 ,
        sgc.user_name,
        sgc.dept_name,
        sgc.dept_id,
        sgc.teller_id,sgc.outlets,sgc.id_type,sg.cust_type,sgc.lp_name,sgc.social_credit_code,sgc.social_credit_code_type
        from
        sys_group_customer sgc
        inner join sys_group sg on
        sgc.group_id = sg.group_id
        <where>
            <if test="groupId != null and groupId != ''">
                sgc.group_id = #{groupId}
            </if>
            <if test="custId != null and custId != ''">
                and sgc.cust_id like concat('%', #{custId}, '%')
            </if>
            <if test="custIdc != null  and custIdc != ''"> and sgc.cust_idc like concat('%',  #{custIdc}, '%')</if>
            <if test="custIsn != null  and custIsn != ''"> and sgc.cust_isn like concat('%',  #{custIsn}, '%') </if>
            <if test="gridName != null  and gridName != ''"> and sgc.grid_name like concat('%',  #{gridName}, '%')</if>
            <if test="gridName2 != null  and gridName2 != ''"> and sgc.grid_name2 like concat('%',  #{gridName2}, '%')</if>
            <if test="custName != null  and custName != ''"> and sgc.cust_name like concat('%',  #{custName}, '%')</if>
            <if test="deptName != null  and deptName != ''"> and sgc.dept_name like concat('%',  #{deptName}, '%')</if>
            <if test="outlets != null  and outlets != ''"> and sgc.outlets like concat('%',  #{outlets}, '%')</if>
            <if test="username != null  and username != ''"> and sgc.user_name like concat('%',  #{username}, '%')</if>
            <if test="socialCreditCode != null  and socialCreditCode != ''"> and sgc.social_credit_code like concat('%',  #{socialCreditCode}, '%')</if>
        </where>
    </select>

    <select id="selectUnsuccessCustomerList" parameterType="String" resultMap="CustomerList">
        select
        sg.group_id,sg.group_name,
        sgc.cust_name,
        sgc.cust_phone,
        sgc.cust_id,
        sgc.cust_idc,
        sgc.push_status,
        sgc.grid_name ,
        sgc.grid_name2 ,
        sgc.user_name,
        sgc.dept_name,
        sgc.teller_id
        from
        sys_group_customer sgc
        inner join sys_group sg on
        sgc.group_id = sg.group_id
        left join sys_campaign sc on
        sc.group_id = sgc.group_id
        <where>
            <if test="groupId != null and groupId != ''">
                sgc.group_id = #{groupId}
            </if>
            <if test="custId != null and custId != ''">
                and sgc.cust_id like concat('%', #{custId}, '%')
            </if>
            <if test="custName != null and custName != ''">
                and sgc.cust_name like concat('%', #{custName}, '%')
            </if>
            <if test="pushStatus != null and pushStatus != ''">
                and sgc.push_status = #{pushStatus}
            </if>
            <if test="pushStatus == null or pushStatus ==''">
                and (sgc.push_status = '0' or sgc.push_status = '2')
            </if>
        </where>
    </select>


    
    <select id="selectSysGroupById" parameterType="Long" resultMap="SysGroupResult">
        <include refid="selectSysGroupVo"/>
        where id = #{id}
    </select>

    <select id="selectSysGroupByGroupId" parameterType="String" resultMap="SysGroupResult">
        <include refid="selectSysGroup"/>
        where group_id = #{groupId}
    </select>


    <select id="selectSysGroupNameByGroupId" parameterType="String" >
        select group_name from sys_group
        where group_id = #{groupId}
    </select>



    <select id="selectSysGroupBydeptId" parameterType="Long" resultMap="SysGroupResult">
        <include refid="selectSysGroup"/>

        <where>
            del_flag = '0'
            <if test="deptId != null">and left(dept_id,3) = left(#{deptId},3)</if>
        </where>

    </select>

    <select id="selectSysGroupBydeptId1" parameterType="Long" resultMap="SysGroupResult">
        <include refid="selectSysGroup"/>
        <where>
            del_flag = '0'
            <if test="deptId != null">and dept_id = #{deptId}</if>
        </where>
    </select>

    <select id="selectSysGroupBydeptIdAndUserId" parameterType="Long" resultMap="SysGroupResult">
        <include refid="selectSysGroup"/>
        <where>
            del_flag = '0'
            <if test="deptId != null">and dept_id  = #{deptId}</if>
            <if test="userName != null">and create_by = #{userName}</if>
        </where>
    </select>



    <select id="isHead" parameterType="String" resultType="String">
        select r.role_key from sys_role r left join sys_user_role sur on r.role_id = sur.role_id  left join sys_user u on sur.user_id = u.user_id where u.user_name = #{username}

    </select>

    <insert id="insertSysGroup" parameterType="SysGroup" useGeneratedKeys="true" keyProperty="id">
        insert into sys_group
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="groupName != null">group_name,</if>
            <if test="createType != null">create_type,</if>
            <if test="customerNum != null">customer_num,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateType != null">update_type,</if>
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="groupType != null">group_type,</if>
            <if test="groupId != null">group_id,</if>
            <if test="custType != null">cust_type,</if>
            <if test="createRole != null">create_role,</if>
            <if test="labelIds != null">label_ids,</if>
            <if test="description != null">description,</if>
            <if test="updateTypeRelateTime != null">update_type_relate_time,</if>
            <if test="relateGroupId != null">relate_group_id,</if>
            <if test="labelSql != null">label_sql,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="groupName != null">#{groupName},</if>
            <if test="createType != null">#{createType},</if>
            <if test="customerNum != null">#{customerNum},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateType != null">#{updateType},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="groupType != null">#{groupType},</if>
            <if test="groupId != null">#{groupId},</if>
            <if test="custType != null">#{custType},</if>
            <if test="createRole != null">#{createRole},</if>
            <if test="labelIds != null">#{labelIds},</if>
            <if test="description != null">#{description},</if>
            <if test="updateTypeRelateTime != null">#{updateTypeRelateTime},</if>
            <if test="relateGroupId != null">#{relateGroupId},</if>
            <if test="labelSql != null">#{labelSql},</if>
         </trim>
    </insert>

    <update id="updateSysGroup" parameterType="SysGroup">
        update sys_group
        <trim prefix="SET" suffixOverrides=",">
            <if test="groupName != null">group_name = #{groupName},</if>
            <if test="createType != null">create_type = #{createType},</if>
            <if test="customerNum != null">customer_num = #{customerNum},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateType != null">update_type = #{updateType},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="groupType != null">group_type = #{groupType},</if>
            <if test="createRole != null">create_role = #{createRole},</if>
            <if test="labelIds != null">label_ids = #{labelIds},</if>
            <if test="description != null">description = #{description},</if>
            <if test="updateTypeRelateTime != null">update_type_relate_time = #{updateTypeRelateTime},</if>
            <if test="relateGroupId != null">relate_group_id = #{relateGroupId},</if>
            <if test="labelSql != null">label_sql = #{labelSql},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateSysGroupByIdc" parameterType="SysGroup">
        update sys_group
        <trim prefix="SET" suffixOverrides=",">
            <if test="customerNum != null">customer_num = #{customerNum},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where group_id = #{groupId}
    </update>

    <delete id="deleteSysGroupById" parameterType="Long">
        delete from sys_group where id = #{id}
    </delete>

    <delete id="deleteSysGroupByIds" parameterType="String">
        delete from sys_group where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectListNameByBank" parameterType="Long">
        select distinct a.group_id,a.group_name,a.create_role from sys_group a,sys_campaign_group_customer b  where a.group_id = b.group_id and b.dept_id div 10 = #{countDept}
    </select>

    <select id="selectListNameByUser" parameterType="Long">
        select * from sys_group  where user_id = #{UserId}
    </select>

    <select id="selectCountByGroupName" parameterType="String" resultType="integer">
        select count(*) from sys_group sg
        where del_flag = '0'
        <if test="groupName != null">and group_name  = #{groupName}</if>
        <if test="headId != null">and dept_id like concat(#{headId},'%')</if>
    </select>



</mapper>