<?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.SysGroupCustomerMapper">
    
    <resultMap type="SysGroupCustomer" id="SysGroupCustomerResult">
        <result property="id"    column="id"    />
        <result property="groupId"    column="group_id"    />
        <result property="custId"    column="cust_id"    />
        <result property="custName"    column="cust_name"    />
        <result property="custIdc"    column="cust_idc"    />
        <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="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="gridId"    column="grid_id"    />
        <result property="pushStatus"    column="push_status"    />
        <result property="custPhone"    column="cust_phone"    />
        <result property="gridName"    column="grid_name"    />
        <result property="userName"    column="user_name"    />
        <result property="custIsn"    column="cust_isn"    />
        <result property="deptName"    column="deptName"    />
        <result property="tellerId"    column="teller_id"    />
        <result property="gridName2"    column="grid_name2"    />
        <result property="idType"    column="id_type"    />
        <result property="outlets"    column="outlets"    />
        <result property="lpName"    column="lp_name"    />
        <result property="socialCreditCode"    column="social_credit_code"    />
        <result property="socialCreditCodeType"    column="social_credit_code_type"    />
        <result property="custType"          column="cust_type"        />
        <result property="outletsId" column="outlets_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="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="idType"          column="id_type"        />
        <result property="custType"          column="cust_type"        />
        <result property="socialCreditCodeType"          column="social_credit_code_type"        />
        <result property="createTime"          column="create_time"        />
        <result property="custLb"          column="cust_lb"        />
        <result property="deptId"          column="dept_id"        />
    </resultMap>

    <sql id="selectSysGroupCustomerVo">
        select id, group_id, cust_id, cust_name, cust_idc, dept_id, user_id, create_by, create_time, update_by, update_time, remark, del_flag, grid_id,
        push_status, cust_phone, grid_name, user_name,cust_isn,dept_name,teller_id,grid_name2,id_type,outlets,lp_name,social_credit_code,social_credit_code_type,cust_type,outlets_id
  from sys_group_customer
    </sql>

    <select id="selectSysGroupCustomerList" parameterType="SysGroupCustomer" resultMap="SysGroupCustomerResult">
        <include refid="selectSysGroupCustomerVo"/>
        <where>  
            <if test="groupId != null  and groupId != ''"> and group_id = #{groupId}</if>
            <if test="custId != null  and custId != ''"> and cust_id = #{custId}</if>
            <if test="custName != null  and custName != ''"> and cust_name like concat('%', #{custName}, '%')</if>
            <if test="custIdc != null  and custIdc != ''"> and cust_idc = #{custIdc}</if>
            <if test="deptId != null "> and dept_id in
                (select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId},ancestors))</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="gridId != null "> and grid_id = #{gridId}</if>
            <if test="pushStatus != null  and pushStatus != ''"> and push_status = #{pushStatus}</if>
            <if test="custPhone != null  and custPhone != ''"> and cust_phone = #{custPhone}</if>
            <if test="gridName != null  and gridName != ''"> and grid_name like concat('%', #{gridName}, '%')</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
        </where>
    </select>

    <select id="selectSysGroupCustomerListIds" resultMap="SysGroupCustomerResult">
        select distinct sgc.cust_id, sgc.cust_name, sgc.cust_idc, sgc.dept_id, su.user_id as user_id, sgc.del_flag, sgc.grid_id,
        sgc.push_status, sgc.cust_phone, sgc.grid_name, sgc.user_name,sgc.cust_isn,sgc.dept_name,
        sgc.teller_id,sgc.grid_name2,sgc.id_type,sgc.outlets,sgc.lp_name,sgc.social_credit_code,
        sgc.social_credit_code_type,sgc.cust_type, sgc.outlets_id
        from sys_group_customer sgc
        left join sys_campaign_group scg on scg.group_id = sgc.group_id
        left join sys_campaign_group_customer scgc on scg.campaign_id = scgc.campaign_id
        left join sys_user su on sgc.teller_id = su.user_name
        <where>
            sgc.push_status = '1' and
            sgc.group_id in
                <foreach collection="groupIds"  item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            <if test="custFlag != null and custFlag !='' ">
                 and    sgc.cust_id in
                <foreach collection="custIds"  item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="filter != null  and filter != ''"> and scgc.user_id is null </if>
        </where>
    </select>

    <select id="selectSysGroupCustomerList2" parameterType="SysGroupCustomer" resultMap="SysGroupCustomerResult">
        <include refid="selectSysGroupCustomerVo"/>
        <where>
            <if test="groupId != null  and groupId != ''"> and group_id = #{groupId}</if>
            <if test="custId != null  and custId != ''"> and cust_id = #{custId}</if>
            <if test="custName != null  and custName != ''"> and cust_name like concat('%', #{custName}, '%')</if>
            <if test="custIdc != null  and custIdc != ''"> and cust_idc = #{custIdc}</if>
            <if test="deptId != null "> and dept_id in
                (select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId},ancestors))</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="gridId != null "> and grid_id = #{gridId}</if>
            <if test="pushStatus != null  and pushStatus != ''"> and push_status = #{pushStatus}</if>
            <if test="custPhone != null  and custPhone != ''"> and cust_phone = #{custPhone}</if>
            <if test="gridName != null  and gridName != ''"> and grid_name like concat('%', #{gridName}, '%')</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
        </where>
    </select>

    <select id="selectSysGroupCustomerListExport" 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.teller_id,sgc.outlets,sgc.id_type,sg.cust_type,sgc.lp_name,sgc.social_credit_code
        from
        sys_group_customer sgc
        inner join sys_group sg on
        sgc.group_id = sg.group_id
        <where>
        1 = 1
            <if test="groupId != null and groupId != ''">
               and sgc.group_id = #{groupId}
            </if>
            <if test="pushStatus != null and pushStatus != ''">
               and sgc.push_status = #{pushStatus}
            </if>
        </where>
    </select>

    <select id="selectSysGroupCustomerListExportByCustIds"  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.teller_id,sgc.outlets,sgc.id_type,sg.cust_type,sgc.lp_name,sgc.social_credit_code,sgc.create_time,sgc.dept_id,sgc.social_credit_code_type,
        CASE sgc.cust_lb
        WHEN 0 THEN '是'
        WHEN 1 THEN '否'
        END AS cust_lb
        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>
            and sgc.cust_id in <foreach collection="custIds" index="index" item="item" open="(" separator="," close=")">#{item}</foreach>
        </where>
    </select>

    
    <select id="selectSysGroupCustomerById" parameterType="Long" resultMap="SysGroupCustomerResult">
        <include refid="selectSysGroupCustomerVo"/>
        where id = #{id}
    </select>

    <insert id="insertSysGroupCustomer" parameterType="SysGroupCustomer" useGeneratedKeys="true" keyProperty="id">
        insert into sys_group_customer
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="groupId != null">group_id,</if>
            <if test="custId != null">cust_id,</if>
            <if test="custName != null">cust_name,</if>
            <if test="custIdc != null">cust_idc,</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="remark != null">remark,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="gridId != null">grid_id,</if>
            <if test="pushStatus != null">push_status,</if>
            <if test="custPhone != null">cust_phone,</if>
            <if test="gridName != null">grid_name,</if>
            <if test="userName != null">user_name,</if>
            <if test="custIsn != null">cust_isn,</if>
            <if test="deptName != null">dept_name,</if>
            <if test="tellerId != null">teller_id,</if>
            <if test="gridName2 != null">grid_name2,</if>
            <if test="idType != null">id_type,</if>
            <if test="outlets != null">outlets,</if>
            <if test="lpName != null">lp_name,</if>
            <if test="socialCreditCode != null">social_credit_code,</if>
            <if test="socialCreditCodeType != null">social_credit_code_type,</if>
            <if test="custType != null">cust_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="groupId != null">#{groupId},</if>
            <if test="custId != null">#{custId},</if>
            <if test="custName != null">#{custName},</if>
            <if test="custIdc != null">#{custIdc},</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="remark != null">#{remark},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="gridId != null">#{gridId},</if>
            <if test="pushStatus != null">#{pushStatus},</if>
            <if test="custPhone != null">#{custPhone},</if>
            <if test="gridName != null">#{gridName},</if>
            <if test="userName != null">#{userName},</if>
            <if test="custIsn != null">#{custIsn},</if>
            <if test="deptName != null">#{deptName},</if>
            <if test="tellerId != null">#{tellerId},</if>
            <if test="gridName2 != null">#{gridName2},</if>
            <if test="idType != null">#{idType},</if>
            <if test="outlets != null">#{outlets},</if>
            <if test="lpName != null">#{lpName},</if>
            <if test="socialCreditCode != null">#{socialCreditCode},</if>
            <if test="socialCreditCodeType != null">#{socialCreditCodeType},</if>
            <if test="custType != null">#{custType},</if>
         </trim>
    </insert>

    <update id="updateSysGroupCustomer" parameterType="SysGroupCustomer">
        update sys_group_customer
        <trim prefix="SET" suffixOverrides=",">
            <if test="groupId != null">group_id = #{groupId},</if>
            <if test="custId != null">cust_id = #{custId},</if>
            <if test="custName != null">cust_name = #{custName},</if>
            <if test="custIdc != null">cust_idc = #{custIdc},</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="remark != null">remark = #{remark},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="gridId != null">grid_id = #{gridId},</if>
            <if test="pushStatus != null">push_status = #{pushStatus},</if>
            <if test="custPhone != null">cust_phone = #{custPhone},</if>
            <if test="gridName != null">grid_name = #{gridName},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="custIsn != null">cust_isn = #{custIsn},</if>
            <if test="deptName != null">dept_name = #{deptName},</if>
            <if test="tellerId != null">teller_id = #{tellerId},</if>
            <if test="gridName2 != null">grid_name2 = #{gridName2},</if>
            <if test="idType != null">id_type = #{idType},</if>
            <if test="outlets != null">outlets = #{outlets},</if>
            <if test="lpName != null">lp_name = #{lpName},</if>
            <if test="socialCreditCode != null">social_credit_code = #{socialCreditCode},</if>
            <if test="socialCreditCodeType != null">social_credit_code_type = #{socialCreditCodeType},</if>
            <if test="custType != null">cust_type = #{custType},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysGroupCustomerById" parameterType="Long">
        delete from sys_group_customer where id = #{id}
    </delete>

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

    <select id="getCustNumByGroupId" parameterType="String">
        select count(*) from sys_group_customer where group_id = #{groupId};
    </select>

    <select id="getUserInfoList" parameterType="ListSelectForUser">
        select user_name from sys_group_customer where group_id = #{groupId} and cust_id = #{custId}
    </select>

    <select id="getGridInfoList" parameterType="ListSelectForUser">
        select grid_name from sys_group_customer where group_id = #{groupId} and cust_id = #{custId}
    </select>

    <update id="updateOutletsIdAndUserIdByUsername" parameterType="String">
        update  sys_group_customer sgc set sgc.user_id = (select u.user_id from sys_user u where u.user_name = sgc.teller_id ),sgc.outlets_id = (select u.dept_id from sys_user u inner join sys_dept sd on u.dept_id = sd.dept_id where u.user_name = sgc.teller_id and sd.dept_type = 'outlet' ),sgc.outlets = (select sd.dept_name from sys_user u inner join sys_dept sd on u.dept_id = sd.dept_id where u.user_name = sgc.teller_id and sd.dept_type = 'outlet' )  where group_id  = #{groupId}
    </update>

    <update id="updateCustIdAndCustIsn" parameterType="String">
        <if test="custType == 0">
        update  sys_group_customer sgc set sgc.cust_id = (select cust_id from cust_info_retail c where c.cust_idc = sgc.cust_idc limit 1 ),sgc.cust_isn = (select cust_isn from cust_info_retail c where c.cust_idc = sgc.cust_idc limit 1)  where group_id  = #{groupId} and cust_idc in
            <foreach item="id" collection="list" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="custType == 1">
            update  sys_group_customer sgc set sgc.cust_id = (select cust_id from cust_info_merchant c where c.cust_idc = sgc.cust_idc limit 1),sgc.cust_isn = (select cust_isn from cust_info_merchant c where c.cust_idc = sgc.cust_idc limit 1)  where group_id  = #{groupId} and cust_idc in
            <foreach item="id" collection="list" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="custType == 2">
            update  sys_group_customer sgc set sgc.cust_id = (select cust_id from cust_info_business c where c.cust_idc = sgc.cust_idc limit 1),sgc.cust_isn = (select cust_isn from cust_info_business c where c.cust_idc = sgc.cust_idc limit 1)  where group_id  = #{groupId} and cust_idc in
            <foreach item="id" collection="list" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>

    </update>


    <select id="selectCustIds" parameterType="String" resultType="String">
        <if test="custType == 0">
            select cust_id from cust_info_retail c where c.cust_id in
            <foreach item="id" collection="list" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="custType == 1">
            select cust_id from cust_info_merchant c where c.cust_id in
            <foreach item="id" collection="list" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="custType == 2">
            select cust_id from cust_info_business c where c.cust_id in
            <foreach item="id" collection="list" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>

    </select>

    <update id="updateCustIdAndCustIsnBySocialCreditCode" parameterType="String">
        <if test="custType == 1">
            update  sys_group_customer sgc set sgc.cust_id = (select cust_id from cust_info_merchant c where c.social_credit_code = sgc.social_credit_code limit 1),sgc.cust_isn = (select cust_isn from cust_info_merchant c where c.social_credit_code = sgc.social_credit_code limit 1)  where group_id  = #{groupId} and social_credit_code in
            <foreach item="id" collection="list" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="custType == 2">
            update  sys_group_customer sgc set sgc.cust_id = (select cust_id from cust_info_business c where c.social_credit_code = sgc.social_credit_code limit 1),sgc.cust_isn = (select cust_isn from cust_info_business c where c.social_credit_code = sgc.social_credit_code limit 1)  where group_id  = #{groupId} and social_credit_code in
            <foreach item="id" collection="list" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>

    </update>


    <insert id="insertSysGroupCustomerBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        insert into sys_group_customer
        (group_id,cust_id,cust_name,cust_idc,dept_id,user_id,create_by,create_time,update_by,
       update_time,remark,del_flag,grid_id,push_status,cust_phone,grid_name,user_name,cust_isn,
            dept_name,teller_id,grid_name2,id_type,outlets,lp_name,social_credit_code,social_credit_code_type,cust_type,cust_lb,outlets_id)
       values
        <foreach collection="list" item="item" index="index" separator=",">

           (#{item.groupId},#{item.custId},#{item.custName},#{item.custIdc},#{item.deptId},#{item.userId},#{item.createBy},#{item.createTime},
            #{item.updateBy},#{item.updateTime},#{item.remark},#{item.delFlag},#{item.gridId},#{item.pushStatus},#{item.custPhone},#{item.gridName},
            #{item.userName},#{item.custIsn},#{item.deptName},#{item.tellerId},#{item.gridName2},#{item.idType},#{item.outlets},#{item.lpName},
            #{item.socialCreditCode},#{item.socialCreditCodeType},#{item.custType},#{item.custLb},#{item.outletsId})

        </foreach>
    </insert>

    <update id="updateSysGroupCustomerBatch">
        <foreach collection="list" item="item" separator=";">
            update sys_group_customer
            <set>
                <if test="item.groupId != null">group_id = #{item.groupId},</if>
                <if test="item.custId != null">cust_id = #{item.custId},</if>
                <if test="item.custName != null">cust_name = #{item.custName},</if>
                <if test="item.custIdc != null">cust_idc = #{item.custIdc},</if>
                <if test="item.deptId != null">dept_id = #{item.deptId},</if>
                <if test="item.userId != null">user_id = #{item.userId},</if>
                <if test="item.createBy != null">create_by = #{item.createBy},</if>
                <if test="item.createTime != null">create_time = #{item.createTime},</if>
                <if test="item.updateBy != null">update_by = #{item.updateBy},</if>
                <if test="item.updateTime != null">update_time = #{item.updateTime},</if>
                <if test="item.remark != null">remark = #{item.remark},</if>
                <if test="item.delFlag != null">del_flag = #{item.delFlag},</if>
                <if test="item.gridId != null">grid_id = #{item.gridId},</if>
                <if test="item.pushStatus != null">push_status = #{item.pushStatus},</if>
                <if test="item.custPhone != null">cust_phone = #{item.custPhone},</if>
                <if test="item.gridName != null">grid_name = #{item.gridName},</if>
                <if test="item.userName != null">user_name = #{item.userName},</if>
                <if test="item.custIsn != null">cust_isn = #{item.custIsn},</if>
                <if test="item.deptName != null">dept_name = #{item.deptName},</if>
                <if test="item.tellerId != null">teller_id = #{item.tellerId},</if>
                <if test="item.gridName2 != null">grid_name2 = #{item.gridName2},</if>
                <if test="item.idType != null">id_type = #{item.idType},</if>
                <if test="item.outlets != null">outlets = #{item.outlets},</if>
                <if test="item.lpName != null">lp_name = #{item.lpName},</if>
                <if test="item.socialCreditCode != null">social_credit_code = #{item.socialCreditCode},</if>
                <if test="item.socialCreditCodeType != null">social_credit_code_type = #{item.socialCreditCodeType},</if>
                <if test="item.custType != null">cust_type = #{item.custType},</if>
            </set>

            <where>
                id = #{item.id}
            </where>

        </foreach>

    </update>

    <select id="selectCustIdcBySocialCreditCode" parameterType="String" resultType="java.lang.String">
        <if test="custType == 1">
            select cust_idc from cust_info_merchant c where c.social_credit_code = #{socialCreditCode} limit 1
        </if>
        <if test="custType == 2">
            select cust_idc from cust_info_business c where c.social_credit_code = #{socialCreditCode} limit 1
        </if>
    </select>

    <select id="selectCustIdcList" resultMap="SysGroupCustomerResult">
        <if test="custType == 0">
            select group_id,cust_id,cust_idc,push_status from sys_group_customer where group_id = #{groupId} and cust_id in
        </if>
        <if test="custType == 1 or custType == 2">
            select group_id,cust_id,social_credit_code,push_status from sys_group_customer where group_id = #{groupId} and cust_id in
        </if>
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <delete id="deleteSysGroupCustomerByCustId" parameterType="String">
        delete from sys_group_customer where group_id = #{groupId} and cust_id = #{custId} and push_status = "2"
    </delete>

</mapper>