<?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.SysCampaignGroupCustomerMapper">

    <resultMap type="SysCampaignGroupCustomer" id="SysCampaignGroupCustomerResult">
        <result property="id" column="id"/>
        <result property="custId" column="cust_id"/>
        <result property="campaignId" column="campaign_id"/>
        <result property="groupId" column="group_id"/>
        <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="listType" column="list_type"/>
        <result property="pushStatus" column="push_status"/>
        <result property="orgClaimStatus" column="org_claim_status"/>
        <result property="orgDistributeStatus" column="org_distribute_status"/>
        <result property="custClaimStatus" column="cust_claim_status"/>
        <result property="createRole" column="create_role"/>
        <result property="gridName" column="grid_name"/>
        <result property="gridName2" column="grid_name2"/>
        <result property="outlets" column="outlets"/>
        <result property="custIdc" column="cust_idc"/>
        <result property="secondPushStatus" column="second_push_status"/>
        <result property="socialCreditCode"          column="social_credit_code"        />
        <result property="custPhone"          column="cust_phone"        />
        <result property="outletsId" column="outlets_id"/>

    </resultMap>

    <sql id="selectSysCampaignGroupCustomerVo">
        select id,
               cust_id,
               campaign_id,
               group_id,
               dept_id,
               user_id,
               create_by,
               create_time,
               update_by,
               update_time,
               remark,
               del_flag,
               grid_id,
               list_type,
               push_status,
               org_claim_status,
               org_distribute_status,
               cust_claim_status,
               create_role,
               grid_name,
               grid_name2,
               outlets,cust_idc,second_push_status,social_credit_code,cust_phone,outlets_id
        from sys_campaign_group_customer
    </sql>

    <select id="selectSysCampaignGroupCustomerList" parameterType="SysCampaignGroupCustomer"
            resultMap="SysCampaignGroupCustomerResult">
        <include refid="selectSysCampaignGroupCustomerVo"/>
        <where>
            <if test="custId != null  and custId != ''">and cust_id = #{custId}</if>
            <if test="campaignId != null  and campaignId != ''">and campaign_id = #{campaignId}</if>
            <if test="groupId != null  and groupId != ''">and group_id = #{groupId}</if>
            <if test="deptId != null ">and dept_id = #{deptId}</if>
            <if test="outletsId != null ">and outlets_id = #{outletsId}</if>
            <if test="userId != null ">and user_id = #{userId}</if>
            <if test="deptFlag != null ">and user_id is null </if>
            <if test="gridId != null ">and grid_id = #{gridId}</if>
            <if test="listType != null  and listType != ''">and list_type = #{listType}</if>
            <if test="pushStatus != null  and pushStatus != ''">and push_status = #{pushStatus}</if>
            <if test="secondPushStatus != null and secondPushStatus != ''">and second_push_status = #{secondPushStatus}</if>
            <if test="createRole != null  and createRole != ''">and create_role = #{createRole}</if>
        </where>
    </select>

    <select id="selectSysCampaignGroupCustomerList2" parameterType="SysCampaignGroupCustomer"
            resultMap="SysCampaignGroupCustomerResult">
        <include refid="selectSysCampaignGroupCustomerVo"/>
        <where>
            <if test="custId != null  and custId != ''">and cust_id = #{custId}</if>
            <if test="campaignId != null  and campaignId != ''">and campaign_id = #{campaignId}</if>
            <if test="groupId != null  and groupId != ''">and group_id = #{groupId}</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="listType != null  and listType != ''">and list_type = #{listType}</if>
            <if test="pushStatus != null  and pushStatus != ''">and push_status = #{pushStatus}</if>
            <if test="createRole != null  and createRole != ''">and create_role = #{createRole}</if>
        </where>
    </select>

    <select id="selectSysCampaignGroupCustomerById" parameterType="Long" resultMap="SysCampaignGroupCustomerResult">
        <include refid="selectSysCampaignGroupCustomerVo"/>
        where id = #{id}
    </select>

    <insert id="insertSysCampaignGroupCustomer" parameterType="SysCampaignGroupCustomer" useGeneratedKeys="true"
            keyProperty="id">
        insert into sys_campaign_group_customer
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="custId != null">cust_id,</if>
            <if test="campaignId != null">campaign_id,</if>
            <if test="groupId != null">group_id,</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="listType != null">list_type,</if>
            <if test="pushStatus != null">push_status,</if>
            <if test="orgClaimStatus != null">org_claim_status,</if>
            <if test="orgDistributeStatus != null">org_distribute_status,</if>
            <if test="custClaimStatus != null">cust_claim_status,</if>
            <if test="createRole != null">create_role,</if>
            <if test="gridName!= null">grid_name,</if>
            <if test="gridName2 != null">grid_name2,</if>
            <if test="outlets != null">outlets,</if>
            <if test="custIdc != null">cust_idc,</if>
            <if test="secondPushStatus != null">second_push_status,</if>
            <if test="socialCreditCode != null">social_credit_code,</if>
            <if test="custPhone != null">cust_phone,</if>
            <if test="outletsId != null">outlets_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="custId != null">#{custId},</if>
            <if test="campaignId != null">#{campaignId},</if>
            <if test="groupId != null">#{groupId},</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="listType != null">#{listType},</if>
            <if test="pushStatus != null">#{pushStatus},</if>
            <if test="orgClaimStatus != null">#{orgClaimStatus},</if>
            <if test="orgDistributeStatus != null">#{orgDistributeStatus},</if>
            <if test="custClaimStatus != null">#{custClaimStatus},</if>
            <if test="createRole != null">#{createRole},</if>
            <if test="gridName != null">#{gridName},</if>
            <if test="gridName2 != null">#{gridName2},</if>
            <if test="outlets != null">#{outlets},</if>
            <if test="custIdc != null">#{custIdc},</if>
            <if test="secondPushStatus != null">#{secondPushStatus},</if>
            <if test="socialCreditCode != null">#{socialCreditCode},</if>
            <if test="custPhone != null">#{custPhone},</if>
            <if test="outletsId != null">#{outletsId},</if>
        </trim>
    </insert>

    <insert id="insertSysCampaignGroupCustomerBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        insert into sys_campaign_group_customer
        (cust_id,campaign_id,group_id,dept_id,user_id,create_by,create_time,update_by,
        update_time,remark,del_flag,grid_id,list_type,push_status,org_claim_status,org_distribute_status,cust_claim_status,create_role,grid_name,
        grid_name2,outlets,cust_idc,second_push_status,social_credit_code,cust_phone,outlets_id,push_user_level)
        values
        <foreach collection="list" item="item" index="index" separator=",">

            (#{item.custId},#{item.campaignId},#{item.groupId},#{item.deptId},#{item.userId},#{item.createBy},sysdate(),
            #{item.updateBy},#{item.updateTime},#{item.remark},#{item.delFlag},#{item.gridId},#{item.listType},#{item.pushStatus},
            #{item.orgClaimStatus},#{item.orgDistributeStatus},#{item.custClaimStatus},#{item.createRole},#{item.gridName},
            #{item.gridName2},#{item.outlets},#{item.custIdc},#{item.secondPushStatus},#{item.socialCreditCode},#{item.custPhone},#{item.outletsId},#{item.pushUserLevel})

        </foreach>
    </insert>

    <insert id="insertHeadHandledMission" >
        insert into head_handled_mission (campaign_id, create_time, create_by)
        values (#{campaignId},sysdate(),#{userName})
    </insert>




    <update id="updateSysCampaignGroupCustomer" parameterType="SysCampaignGroupCustomer">
        update sys_campaign_group_customer
        <trim prefix="SET" suffixOverrides=",">
            <if test="custId != null">cust_id = #{custId},</if>
            <if test="campaignId != null">campaign_id = #{campaignId},</if>
            <if test="groupId != null">group_id = #{groupId},</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="listType != null">list_type = #{listType},</if>
            <if test="pushStatus != null">push_status = #{pushStatus},</if>
            <if test="orgClaimStatus != null">org_claim_status = #{orgClaimStatus},</if>
            <if test="orgDistributeStatus != null">org_distribute_status = #{orgDistributeStatus},</if>
            <if test="custClaimStatus != null">cust_claim_status = #{custClaimStatus},</if>
            <if test="createRole != null">create_role = #{createRole},</if>
            <if test="gridName != null">grid_name = #{gridName},</if>
            <if test="gridName2 != null">grid_name2 = #{gridName2},</if>
            <if test="outlets != null">outlets = #{outlets},</if>
            <if test="custIdc != null">cust_idc = #{custIdc},</if>
            <if test="secondPushStatus != null">second_push_status = #{secondPushStatus},</if>
            <if test="socialCreditCode != null">social_credit_code = #{socialCreditCode},</if>
            <if test="custPhone != null">cust_phone = #{custPhone},</if>
            <if test="outletsId != null">outlets_id = #{outletsId},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateSysCampaignGroupCustomerBatch">
        <foreach collection="list" item="item" separator=";">
            update sys_campaign_group_customer
            <set>
                <if test="item.custId != null">cust_id = #{item.custId},</if>
                <if test="item.campaignId != null">campaign_id = #{item.campaignId},</if>
                <if test="item.groupId != null">group_id = #{item.groupId},</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.listType != null">list_type = #{item.listType},</if>
                <if test="item.pushStatus != null">push_status = #{item.pushStatus},</if>
                <if test="item.orgClaimStatus != null">org_claim_status = #{item.orgClaimStatus},</if>
                <if test="item.orgDistributeStatus != null">org_distribute_status = #{item.orgDistributeStatus},</if>
                <if test="item.custClaimStatus != null">cust_claim_status = #{item.custClaimStatus},</if>
                <if test="item.createRole != null">create_role = #{item.createRole},</if>
                <if test="item.gridName != null">grid_name = #{item.gridName},</if>
                <if test="item.gridName2 != null">grid_name2 = #{item.gridName2},</if>
                <if test="item.outlets != null">outlets = #{item.outlets},</if>
                <if test="item.custIdc != null">cust_idc = #{item.custIdc},</if>
                <if test="item.secondPushStatus != null">second_push_status = #{item.secondPushStatus},</if>
                <if test="item.socialCreditCode != null">social_credit_code = #{item.socialCreditCode},</if>
                <if test="item.custPhone != null">cust_phone = #{item.custPhone},</if>
                <if test="item.outletsId != null">outlets_id = #{item.outletsId},</if>
                <if test="item.pushUserLevel != null">push_user_level = #{item.pushUserLevel},</if>
            </set>

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

        </foreach>

    </update>


    <delete id="deleteSysCampaignGroupCustomerById" parameterType="Long">
        delete
        from sys_campaign_group_customer
        where id = #{id}
    </delete>

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

    <select id="getDeptIdByUserId" parameterType="Long" resultType="java.lang.Long">
        select dept_id
        from sys_user
        where user_Id = #{userId}
    </select>

    <select id="getReceiveUserNum" resultType="java.lang.String">
        select distinct user_id from sys_campaign_group_customer where group_id = #{groupId} and campaign_id = #{campaignId} and cust_claim_status = '1'
        and case when #{deptId} is null then org_claim_status not in ('0','1')
        and dept_id in (select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept},ancestors))
        when #{deptId} is not null then dept_id = #{deptId} and org_claim_status in ('0','1')
        else 1=1 end
    </select>

    <select id="getAllUserNum" resultType="java.lang.String">
        select distinct user_id from sys_campaign_group_customer where group_id = #{groupId} and campaign_id = #{campaignId}
        and case when #{deptId} is null then org_claim_status not in ('0','1')
        and dept_id in (select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept},ancestors))
        when #{deptId} is not null then dept_id = #{deptId} and org_claim_status in ('0','1')
        else 1=1 end
    </select>

    <select id="getReceiveUserNumOutlet" resultType="java.lang.String">
        select distinct user_id from sys_campaign_group_customer where group_id = #{groupId} and campaign_id = #{campaignId} and cust_claim_status = '1'
        and outlets_id =#{userDept}
    </select>

    <select id="getAllUserNumOutlet" resultType="java.lang.String">
        select distinct user_id from sys_campaign_group_customer where group_id = #{groupId} and campaign_id = #{campaignId}
        and outlets_id =#{userDept}
    </select>

    <select id="getUserInfoList" parameterType="ListSelectForUser">
        select distinct b.nick_name from sys_campaign_group_customer a ,sys_user b where a.group_id = #{groupId} and a.cust_id = #{custId} and a.user_id=b.user_id
    </select>

    <select id="getGroupCustIdByGroupId">
        select distinct cust_id from sys_campaign_group_customer where group_id = #{groupId}
        and case when #{deptId} is null then user_id is not null else (dept_id = #{deptId} and user_id is null) end
    </select>

    <select id="selectSysCampaignGroupCustomerListUnsuccess" parameterType="SysCampaignGroupCustomer"
            resultMap="SysCampaignGroupCustomerResult">
        <include refid="selectSysCampaignGroupCustomerVo"/>
        <where>
            user_id is  null
            <if test="custId != null  and custId != ''">and cust_id = #{custId}</if>
            <if test="campaignId != null  and campaignId != ''">and campaign_id = #{campaignId}</if>
            <if test="groupId != null  and groupId != ''">and group_id = #{groupId}</if>
            <if test="deptId != null ">and dept_id = #{deptId}</if>
            <if test="outletsId != null ">and outlets_id = #{outletsId}</if>
            <if test="userId != null ">and user_id = #{userId}</if>
            <if test="createRole != null  and createRole != ''">and create_role = #{createRole}</if>
        </where>
    </select>

    <update id="updateUserIdByCampaignId" >
        update sys_campaign_group_customer_coop
        <set>
            user_id = #{userId} , outlets_id = #{deptId}, cust_claim_status = '1'
        </set>
        where campaign_id = #{campaignId}  and cust_id = #{custId}
    </update>
</mapper>