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

	<resultMap type="MarketingList" id="MarketingList">
		<id     property="listType"             column="list_type"           />
		<result property="groupName"           column="group_name"          />
		<result property="customerNum"            column="customer_num"       />
		<result property="campaignName"           column="campaign_name"      />
		<result property="createTime"          column="create_time"        />
		<result property="campaignDegree"  column="campaign_degree"   />
		<result property="startTime"          column="start_time"        />
		<result property="endTime"          column="end_time"        />
		<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="groupId"          column="group_id"        />
		<result property="campaignId"          column="campaign_id"        />
		<result property="claimType"          column="claim_type"        />
		<result property="orgClaimStatus"          column="org_claim_status"        />
		<result property="orgDistributeStatus"          column="org_distribute_status"        />
		<result property="custClaimStatus"          column="cust_claim_status"        />
		<result property="claimEndTime"          column="claim_end_time"        />
	</resultMap>

	<resultMap type="ListSelectByUser" id="ListSelectByUser">
		<id     property="groupId"             column="group_id"           />
		<result property="createRole"             column="create_role"           />
		<result property="groupType"           column="group_type"          />
		<result property="groupName"            column="group_name"       />
		<result property="campaignId"           column="campaign_id"      />
		<result property="campaignName"           column="campaign_name"      />
		<result property="startTime"          column="start_time"        />
		<result property="endTime"          column="end_time"        />
		<result property="campaignDegree"  column="campaign_degree"   />
		<result property="deptId"          column="dept_id"        />
		<result property="claimEndTime"          column="claim_end_time"        />
		<result property="claimType"          column="claim_type"        />
		<result property="orgClaimStatus"          column="org_claim_status"        />
		<result property="orgDistributeStatus"          column="org_distribute_status"        />
		<result property="customerNum"          column="customer_num"        />
		<result property="updateTime"          column="update_time"        />
		<result property="secondPushStatus"          column="second_push_status"        />
		<result property="executer"          column="executer"        />
		<result property="createTime"          column="create_time"        />
		<result property="belongDept"          column="dept_name"        />
		<result property="sendDept"          column="send_dept"        />
		<result property="checkTarget"          column="check_target"        />
	</resultMap>

	<resultMap type="ListSelectForUser" id="ListSelectForUser">
		<id     property="custId"             column="cust_id"           />
		<result property="createRole"             column="create_role"           />
		<result property="groupId"           column="group_id"          />
		<result property="groupName"            column="group_name"       />
		<result property="custPattern"             column="cust_type"           />
		<result property="campaignId"           column="campaign_id"      />
		<result property="campaignName"           column="campaign_name"      />
		<result property="custIdc"          column="cust_idc"        />
		<result property="socialCreditCode"          column="social_credit_code"        />
		<result property="custName"          column="cust_name"        />
		<result property="custPhone"          column="cust_phone"        />
		<result property="custIsn"          column="cust_isn"        />
		<result property="lpName"          column="lp_name"        />
		<result property="startTime"          column="start_time"        />
		<result property="endTime"          column="end_time"        />
		<result property="campaignDegree"  column="campaign_degree"   />
		<result property="deptId"          column="dept_id"        />
		<result property="claimType"          column="claim_type"        />
		<result property="orgClaimStatus"          column="org_claim_status"        />
		<result property="orgDistributeStatus"          column="org_distribute_status"        />
		<result property="custClaimStatus"          column="cust_claim_status"        />
		<result property="topGridName"          column="grid_name"        />
		<result property="secGridName"          column="grid_name2"        />
		<result property="outlets"          column="outlets"        />
		<result property="userName"          column="nick_name"        />
		<result property="belongDept"          column="dept_name"        />
		<result property="checkTarget"          column="check_target"        />
	</resultMap>



	<select id="selectMarketingList" parameterType="String" resultMap="MarketingList">
		select
			scgc.list_type,
		    scgc.group_id,
		    scgc.campaign_id,
			sg.group_name,
			sg.customer_num,
			sc.campaign_name,
			sc.create_time ,
			sc.campaign_degree,
			sc.start_time,
			sc.end_time,
		    sc.claim_end_time,
		    scgc.cust_id,
		    scgc.org_claim_status,
		    scgc.org_distribute_status,
		    scgc.cust_claim_status,
		    sc.claim_type

		from
			sys_campaign_group_customer scgc
				inner join sys_campaign sc on
				scgc.campaign_id = sc.campaign_id
				inner join sys_group sg on
				scgc.group_id = sg.group_id
		<where>
		<if test="keyword != null and keyword != ''">
			and sg.group_name like concat('%', #{keyword}, '%') or sc.campaign_name like concat('%', #{keyword}, '%')
	    </if>
			<if test="deptId != null ">
				and scgc.dept_id div 1000 = #{deptId}
			</if>
		</where>

	</select>


	<select id="selectMarketingList2" parameterType="String" resultMap="MarketingList">
		select
		scgc.list_type,
		scgc.group_id,
		scgc.campaign_id,
		sg.group_name,
		sg.customer_num,
		sc.campaign_name,
		sc.create_time ,
		sc.campaign_degree,
		sc.start_time,
		sc.end_time,
		sc.claim_end_time,
		scgc.cust_id,
		scgc.org_claim_status,
		scgc.org_distribute_status,
		scgc.cust_claim_status,
		sc.claim_type

		from
		sys_campaign_group_customer scgc
		inner join sys_campaign sc on
		scgc.campaign_id = sc.campaign_id
		inner join sys_group sg on
		scgc.group_id = sg.group_id
		<where>
			<if test="keyword != null and keyword != ''">
				and sg.group_name like concat('%', #{keyword}, '%') or sc.campaign_name like concat('%', #{keyword}, '%')
			</if>
			<if test="deptId != null ">
				and scgc.dept_id = #{deptId}
			</if>
			<if test="userId != null ">
				and scgc.user_id = #{userId}
			</if>
			<if test="createRole != null ">
				and scgc.create_role = #{createRole}
			</if>
		</where>

	</select>

	<select id="selectMarketingCustomerList" parameterType="String" resultMap="MarketingList">
		select
			scgc.list_type,
		    scgc.group_id,
		    scgc.campaign_id,
			sg.group_name,
			sg.customer_num,
			sc.campaign_name,
			sc.create_time ,
			sc.campaign_degree,
			sc.start_time,
			sc.end_time,
		    sc.claim_end_time,
			sgc.cust_name,
		    sgc.cust_phone,
		    sgc.cust_id,
		    sgc.cust_idc,
		    scgc.org_claim_status,
		    scgc.org_distribute_status,
		    scgc.cust_claim_status,
		    sc.claim_type
		from
			sys_campaign_group_customer scgc
				inner join sys_campaign sc on
				scgc.campaign_id = sc.campaign_id
				inner join sys_group sg on
				scgc.group_id = sg.group_id
				inner join sys_group_customer sgc on
				scgc.cust_id = sgc.cust_id and scgc.group_id = sgc.group_id
		<where>
		<if test="groupId != null and groupId != ''">
			 and scgc.group_id = #{groupId}
		</if>
		<if test="campaignId != null and campaignId != ''">
			 and scgc.campaign_id = #{campaignId}
		</if>
		<if test="keyword != null and keyword != ''">
			and (sgc.cust_idc like concat('%', #{keyword}, '%') or sgc.cust_name like concat('%', #{keyword}, '%') or sgc.cust_phone like concat('%', #{keyword}, '%'))
		</if>
			<if test="deptId != null ">
				and scgc.dept_id div 1000 = #{deptId}
			</if>
		</where>
	</select>


	<select id="selectMarketingCustomerList2" parameterType="String" resultMap="MarketingList">
		select
		scgc.list_type,
		scgc.group_id,
		scgc.campaign_id,
		sg.group_name,
		sg.customer_num,
		sc.campaign_name,
		sc.create_time ,
		sc.campaign_degree,
		sc.start_time,
		sc.end_time,
		sc.claim_end_time,
		sgc.cust_name,
		sgc.cust_phone,
		sgc.cust_id,
		sgc.cust_idc,
		scgc.org_claim_status,
		scgc.org_distribute_status,
		scgc.cust_claim_status,
		sc.claim_type
		from
		sys_campaign_group_customer scgc
		inner join sys_campaign sc on
		scgc.campaign_id = sc.campaign_id
		inner join sys_group sg on
		scgc.group_id = sg.group_id
		inner join sys_group_customer sgc on
		scgc.cust_id = sgc.cust_id and scgc.group_id = sgc.group_id
		<where>
			<if test="groupId != null and groupId != ''">
				and scgc.group_id = #{groupId}
			</if>
			<if test="campaignId != null and campaignId != ''">
				and scgc.campaign_id = #{campaignId}
			</if>
			<if test="keyword != null and keyword != ''">
				and (sgc.cust_idc like concat('%', #{keyword}, '%') or sgc.cust_name like concat('%', #{keyword}, '%') or sgc.cust_phone like concat('%', #{keyword}, '%'))
			</if>
			<if test="deptId != null ">
				and scgc.dept_id = #{deptId}
			</if>
			<if test="userId != null ">
				and scgc.user_id = #{userId}
			</if>
		</where>
	</select>


	<select id="selectMarketingListByBank" parameterType="ListSelectByUser" resultMap="ListSelectByUser">
		select distinct sg.group_id,
		any_value(sg.create_role) as create_role,
		any_value(sg.group_type) as group_type,
		any_value(sg.group_name) as group_name,
		any_value(sc.campaign_id) as campaign_id,
		any_value(sc.campaign_name) as campaign_name,
		any_value(sc.start_time) as start_time,
		any_value(sc.end_time) as end_time,
		any_value(sc.campaign_degree) as campaign_degree,
		any_value(scgc.dept_id) as dept_id,
		any_value(sc.claim_end_time) as claim_end_time,
		any_value(sc.claim_type) as claim_type,
		any_value(sc.check_target) as check_target,
		any_value(scgc.org_claim_status) as org_claim_status,
		any_value(scgc.org_distribute_status) as org_distribute_status,
		any_value(sg.update_time) as update_time,
		any_value(scgc.second_push_status) as second_push_status,
		any_value(sc.executer) as executer,
		any_value(sc.create_time) as create_time,
		any_value(sd.dept_name) as dept_name,
		any_value(sd2.dept_name) as send_dept,
		count(distinct scgc.cust_id) as customer_num
		from
		sys_campaign_group_customer scgc
		inner join (select distinct campaign_id,campaign_name,start_time,end_time,campaign_degree,claim_end_time,claim_type,check_target,executer,create_time,dept_id from sys_campaign) sc on
		scgc.campaign_id = sc.campaign_id
		inner join (select distinct group_id,cust_type,create_role,group_type,group_name,update_time from sys_group where cust_type = #{custPattern}) sg on
		scgc.group_id = sg.group_id
		inner join (select distinct dept_id,dept_name from sys_dept) sd on
		scgc.dept_id = sd.dept_id
		inner join (select distinct dept_id,dept_name from sys_dept) sd2 on
		sc.dept_id = sd2.dept_id
		where scgc.del_flag = '0' and scgc.org_claim_status in ('0','1') and scgc.push_status = '1'
		<if test="userDept != null and userDept != ''">
			and scgc.dept_id in
			(select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept},ancestors))
		</if>
		<if test="groupName != null and groupName != ''">
			and (sg.group_name like concat('%', #{groupName}, '%') or sc.campaign_name like concat('%', #{groupName}, '%'))
		</if>
		<if test="campaignName != null and campaignName != ''">
			and sc.campaign_name like concat('%', #{campaignName}, '%')
		</if>
	    <if test="orgClaimStatus != null and orgClaimStatus != ''">
	    	and scgc.org_claim_status = #{orgClaimStatus}
	    </if>
		<if test="orgDistributeStatus != null and orgDistributeStatus != ''">
			and scgc.org_distribute_status = #{orgDistributeStatus}
		</if>
		<if test="groupType != null and groupType != ''">
			and sg.create_role = #{groupType}
		</if>
		<if test="userId != null and userId != ''">
			and scgc.user_id = #{userId}
		</if>
		<if test="startTime != null ">
			and DATE_FORMAT(sc.start_time,'%Y-%m-%d') = DATE_FORMAT(#{startTime},'%Y-%m-%d')
		</if>
		<if test="endTime != null ">
			and DATE_FORMAT(sc.end_time,'%Y-%m-%d') = DATE_FORMAT(#{endTime},'%Y-%m-%d')
		</if>
		<if test="campaignDegree != null and campaignDegree != ''">
			and sc.campaign_degree = #{campaignDegree}
		</if>
		<if test="belongDept != null and belongDept != ''">
			and scgc.dept_id = #{belongDept}
		</if>
		<if test="claimEndTime != null ">
			and DATE_FORMAT(sc.claim_end_time,'%Y-%m-%d') = DATE_FORMAT(#{claimEndTime},'%Y-%m-%d')
		</if>
		<if test="claimType != null and claimType != ''">
			and sc.claim_type = #{claimType}
		</if>
		<if test="orgClaimStatus != null and orgClaimStatus != ''">
			and scgc.org_claim_status = #{orgClaimStatus}
		</if>
		<if test="orgDistributeStatus != null and orgDistributeStatus != ''">
			and scgc.org_distribute_status = #{orgDistributeStatus}
		</if>
		<if test="actionStatus != null and actionStatus != ''">
			and case when #{actionStatus} = '待开始' then sc.start_time &gt; #{nowTime}
			when #{actionStatus} = '进行中' then sc.end_time &gt;= #{nowTime} and #{nowTime} &gt;= sc.start_time
			when #{actionStatus} = '已结束' then #{nowTime} &gt; sc.end_time
			else 1=1 end
		</if>
		group by scgc.campaign_id,left(scgc.dept_id,5),sg.group_id
		union all
		select distinct
		sg.group_id,
		any_value(sg.create_role) as create_role,
		any_value(sg.group_type) as group_type,
		any_value(sg.group_name) as group_name,
		any_value(sc.campaign_id) as campaign_id,
		any_value(sc.campaign_name) as campaign_name,
		any_value(sc.start_time) as start_time,
		any_value(sc.end_time) as end_time,
		any_value(sc.campaign_degree) as campaign_degree,
		null as dept_id,
		any_value(sc.claim_end_time) as claim_end_time,
		any_value(sc.claim_type) as claim_type,
		any_value(sc.check_target) as check_target,
		any_value(scgc.org_claim_status) as org_claim_status,
		any_value(scgc.org_distribute_status) as org_distribute_status,
		any_value(sg.update_time) as update_time,
		any_value(scgc.second_push_status) as second_push_status,
		any_value(sc.executer) as executer,
		any_value(sc.create_time) as create_time,
		null as dept_name,
		any_value(sd2.dept_name) as send_dept,
		count(distinct scgc.cust_id) as customer_num
		from
		sys_campaign_group_customer scgc
		inner join (select distinct campaign_id,campaign_name,start_time,end_time,campaign_degree,claim_end_time,claim_type,check_target,executer,create_time,dept_id from sys_campaign) sc on
		scgc.campaign_id = sc.campaign_id
		inner join (select distinct group_id,cust_type,create_role,group_type,group_name,update_time from sys_group where cust_type = #{custPattern} ) sg on
		scgc.group_id = sg.group_id
		inner join (select distinct dept_id,dept_name from sys_dept) sd on
		scgc.dept_id = sd.dept_id
		inner join (select distinct dept_id,dept_name from sys_dept) sd2 on
		sc.dept_id = sd2.dept_id
		where scgc.del_flag = '0' and scgc.org_claim_status not in ('0','1') and scgc.push_status = '1'
		<if test="userDept != null and userDept != ''">
			and scgc.dept_id in
			(select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept},ancestors))
		</if>
		<if test="groupName != null and groupName != ''">
			and (sg.group_name like concat('%', #{groupName}, '%') or sc.campaign_name like concat('%', #{groupName}, '%'))
		</if>
		<if test="campaignName != null and campaignName != ''">
			and sc.campaign_name like concat('%', #{campaignName}, '%')
		</if>
		<if test="orgClaimStatus != null and orgClaimStatus != ''">
			and scgc.org_claim_status = #{orgClaimStatus}
		</if>
		<if test="orgDistributeStatus != null and orgDistributeStatus != ''">
			and scgc.org_distribute_status = #{orgDistributeStatus}
		</if>
		<if test="groupType != null and groupType != ''">
			and sg.create_role = #{groupType}
		</if>
		<if test="userId != null and userId != ''">
			and scgc.user_id = #{userId}
		</if>
		<if test="startTime != null ">
			and DATE_FORMAT(sc.start_time,'%Y-%m-%d') = DATE_FORMAT(#{startTime},'%Y-%m-%d')
		</if>
		<if test="endTime != null ">
			and DATE_FORMAT(sc.end_time,'%Y-%m-%d') = DATE_FORMAT(#{endTime},'%Y-%m-%d')
		</if>
		<if test="campaignDegree != null and campaignDegree != ''">
			and sc.campaign_degree = #{campaignDegree}
		</if>
		<if test="belongDept != null and belongDept != ''">
			and scgc.dept_id = #{belongDept}
		</if>
		<if test="claimEndTime != null ">
			and DATE_FORMAT(sc.claim_end_time,'%Y-%m-%d') = DATE_FORMAT(#{claimEndTime},'%Y-%m-%d')
		</if>
		<if test="claimType != null and claimType != ''">
			and sc.claim_type = #{claimType}
		</if>
		<if test="orgClaimStatus != null and orgClaimStatus != ''">
			and scgc.org_claim_status = #{orgClaimStatus}
		</if>
		<if test="orgDistributeStatus != null and orgDistributeStatus != ''">
			and scgc.org_distribute_status = #{orgDistributeStatus}
		</if>
		<if test="actionStatus != null and actionStatus != ''">
			and case when #{actionStatus} = '待开始' then sc.start_time &gt; #{nowTime}
			when #{actionStatus} = '进行中' then sc.end_time &gt; #{nowTime} and #{nowTime} &gt; sc.start_time
			when #{actionStatus} = '已结束' then #{nowTime} &gt; sc.end_time
			else 1=1 end
		</if>
		group by scgc.campaign_id,sg.group_id
		order by create_time desc
	</select>


	<select id="selectMarketingListByOutlet" parameterType="ListSelectByUser" resultMap="ListSelectByUser">
		select distinct sg.group_id,
		any_value(sg.create_role) as create_role,
		any_value(sg.group_type) as group_type,
		any_value(sg.group_name) as group_name,
		any_value(sc.campaign_id) as campaign_id,
		any_value(sc.campaign_name) as campaign_name,
		any_value(sc.start_time) as start_time,
		any_value(sc.end_time) as end_time,
		any_value(sc.campaign_degree) as campaign_degree,
		any_value(scgc.dept_id) as dept_id,
		any_value(sc.claim_end_time) as claim_end_time,
		any_value(sc.claim_type) as claim_type,
		any_value(sc.check_target) as check_target,
		any_value(scgc.org_claim_status) as org_claim_status,
		any_value(scgc.org_distribute_status) as org_distribute_status,
		any_value(sg.update_time) as update_time,
		any_value(scgc.second_push_status) as second_push_status,
		any_value(sc.executer) as executer,
		any_value(sc.create_time) as create_time,
		any_value(sd.dept_name) as dept_name,
		any_value(sd2.dept_name) as send_dept,
		count(distinct scgc.cust_id) as customer_num
		from
		sys_campaign_group_customer scgc
		inner join (select distinct campaign_id,campaign_name,start_time,end_time,campaign_degree,claim_end_time,claim_type,check_target,executer,create_time,dept_id from sys_campaign) sc on
		scgc.campaign_id = sc.campaign_id
		inner join (select distinct group_id,cust_type,create_role,group_type,group_name,update_time from sys_group where cust_type = #{custPattern}) sg on
		scgc.group_id = sg.group_id
		inner join (select distinct dept_id,dept_name from sys_dept) sd on
		scgc.dept_id = sd.dept_id
		inner join (select distinct dept_id,dept_name from sys_dept) sd2 on
		sc.dept_id = sd2.dept_id
		where scgc.del_flag = '0'  and scgc.push_status = '1'
		<if test="userDept != null and userDept != ''">
			and scgc.dept_id in
			(select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept},ancestors))
		</if>
		<if test="groupName != null and groupName != ''">
			and (sg.group_name like concat('%', #{groupName}, '%') or sc.campaign_name like concat('%', #{groupName}, '%'))
		</if>
		<if test="campaignName != null and campaignName != ''">
			and sc.campaign_name like concat('%', #{campaignName}, '%')
		</if>
		<if test="orgClaimStatus != null and orgClaimStatus != ''">
			and scgc.org_claim_status = #{orgClaimStatus}
		</if>
		<if test="orgDistributeStatus != null and orgDistributeStatus != ''">
			and scgc.org_distribute_status = #{orgDistributeStatus}
		</if>
		<if test="groupType != null and groupType != ''">
			and sg.create_role = #{groupType}
		</if>
		<if test="userId != null and userId != ''">
			and scgc.user_id = #{userId}
		</if>
		<if test="startTime != null ">
			and DATE_FORMAT(sc.start_time,'%Y-%m-%d') = DATE_FORMAT(#{startTime},'%Y-%m-%d')
		</if>
		<if test="endTime != null ">
			and DATE_FORMAT(sc.end_time,'%Y-%m-%d') = DATE_FORMAT(#{endTime},'%Y-%m-%d')
		</if>
		<if test="campaignDegree != null and campaignDegree != ''">
			and sc.campaign_degree = #{campaignDegree}
		</if>
		<if test="belongDept != null and belongDept != ''">
			and scgc.dept_id = #{belongDept}
		</if>
		<if test="belongOutlet != null and belongOutlet != ''">
			and scgc.outlets_id = #{belongOutlet}
		</if>
		<if test="claimEndTime != null ">
			and DATE_FORMAT(sc.claim_end_time,'%Y-%m-%d') = DATE_FORMAT(#{claimEndTime},'%Y-%m-%d')
		</if>
		<if test="claimType != null and claimType != ''">
			and sc.claim_type = #{claimType}
		</if>
		<if test="orgClaimStatus != null and orgClaimStatus != ''">
			and scgc.org_claim_status = #{orgClaimStatus}
		</if>
		<if test="orgDistributeStatus != null and orgDistributeStatus != ''">
			and scgc.org_distribute_status = #{orgDistributeStatus}
		</if>
		<if test="actionStatus != null and actionStatus != ''">
			and case when #{actionStatus} = '待开始' then sc.start_time &gt; #{nowTime}
			when #{actionStatus} = '进行中' then sc.end_time &gt;= #{nowTime} and #{nowTime} &gt;= sc.start_time
			when #{actionStatus} = '已结束' then #{nowTime} &gt; sc.end_time
			else 1=1 end
		</if>
		group by scgc.campaign_id,left(scgc.dept_id,5),sg.group_id
		order by create_time desc
	</select>

	<select id="selectCustListByBankRetail" parameterType="ListSelectForUser" resultMap="ListSelectForUser">
		select
		sgc.cust_id,
		sg.create_role,
		sg.group_id,
		sg.group_name,
		sc.campaign_id,
		sc.campaign_name,
		scgc.cust_idc,
		sgc.cust_name,
		scgc.cust_phone,
		sgc.cust_isn,
		sc.start_time,
		sc.end_time,
		sc.campaign_degree,
		sc.check_target,
		scgc.dept_id,
		sc.claim_type,
		scgc.org_claim_status,
		scgc.org_distribute_status,
		scgc.cust_claim_status,
		scgc.grid_name,
		scgc.grid_name2,
		scgc.outlets,
		su.nick_name as nick_name,
		sd.dept_name as dept_name
		from
		sys_campaign_group_customer scgc
		inner join sys_campaign sc on
		scgc.campaign_id = sc.campaign_id
		inner join sys_group sg on
		scgc.group_id = sg.group_id
		inner join cust_info_retail sgc on
		scgc.cust_id = sgc.cust_id
		left join sys_user su on
		scgc.user_id = su.user_id
		left join sys_dept sd on
		scgc.dept_id = sd.dept_id
		where sg.cust_type = #{custPattern} and scgc.del_flag = '0' and scgc.push_status = '1'
		<if test="isOutlet == 0">
		and case when #{deptId} is null then scgc.org_claim_status not in ('0','1')
		else (scgc.dept_id in (select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId},ancestors)) and scgc.org_claim_status in ('0','1')) end
		</if>
		<if test="isOutlet == 1">
		and scgc.outlets_id = #{userDept}
		</if>
		<if test="userDept != null and userDept != '' and isOutlet == 0">
			and scgc.dept_id in
			(select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept},ancestors))
		</if>
		<if test="groupId != null and groupId != ''">
			and sg.group_id = #{groupId}
		</if>
		<if test="campaignId != null and campaignId != ''">
			and scgc.campaign_id = #{campaignId}
		</if>
		<if test="custName != null and custName != ''">
			and (sgc.cust_name like concat('%', #{custName}, '%') or sgc.cust_id like concat('%', #{custName}, '%') or scgc.cust_idc like concat('%', #{custName}, '%') or sgc.cust_phone like concat('%', #{custName}, '%') )
		</if>
		<if test="custClaimStatus != null and custClaimStatus != ''">
			and scgc.cust_claim_status = #{custClaimStatus}
		</if>
		<if test="userId != null and userId != ''">
			and scgc.user_id = #{userId}
		</if>
		<if test="custClaimStatus != null and custClaimStatus != ''">
			and scgc.cust_claim_status = #{custClaimStatus}
		</if>
		<if test="topGridName!= null and topGridName != ''">
			and scgc.grid_name like concat('%', #{topGridName}, '%')
		</if>
		<if test="secGridName != null and secGridName != ''">
			and scgc.grid_name2 like concat('%', #{secGridName}, '%')
		</if>
		<if test="belongDept != null and belongDept != ''">
			and sd.dept_name like concat('%', #{belongDept}, '%') and sd.dept_type ='branch'
		</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>
	</select>

	<select id="selectCustListByBankMerchant" parameterType="ListSelectForUser" resultMap="ListSelectForUser">
		select
		sgc.cust_id,
		sg.create_role,
		sg.group_id,
		sg.group_name,
		sc.campaign_id,
		sc.campaign_name,
		scgc.cust_idc,
		sgc.cust_name,
		scgc.cust_phone,
		sgc.cust_isn,
		sgc.social_credit_code,
		sgc.lp_name,
		sc.start_time,
		sc.end_time,
		sc.campaign_degree,
		sc.check_target,
		scgc.dept_id,
		sc.claim_type,
		scgc.org_claim_status,
		scgc.org_distribute_status,
		scgc.cust_claim_status,
		scgc.grid_name,
		scgc.grid_name2,
		scgc.outlets,
		su.nick_name as nick_name,
		sd.dept_name as dept_name
		from
		sys_campaign_group_customer scgc
		inner join sys_campaign sc on
		scgc.campaign_id = sc.campaign_id
		inner join sys_group sg on
		scgc.group_id = sg.group_id
		inner join cust_info_merchant sgc on
		scgc.cust_id = sgc.cust_id
		left join sys_user su on
		scgc.user_id = su.user_id
		left join sys_dept sd on
		scgc.dept_id = sd.dept_id
		where sg.cust_type = #{custPattern} and scgc.del_flag = '0' and scgc.push_status = '1'
		<if test="isOutlet == 0">
			and case when #{deptId} is null then scgc.org_claim_status not in ('0','1')
			else (scgc.dept_id in (select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId},ancestors)) and scgc.org_claim_status in ('0','1')) end
		</if>
		<if test="isOutlet == 1">
			and scgc.outlets_id = #{userDept}
		</if>
		<if test="userDept != null and userDept != '' and isOutlet == 0">
			and scgc.dept_id in
			(select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept},ancestors))
		</if>
		<if test="groupId != null and groupId != ''">
			and sg.group_id = #{groupId}
		</if>
		<if test="campaignId != null and campaignId != ''">
			and scgc.campaign_id = #{campaignId}
		</if>
		<if test="custName != null and custName != ''">
			and (sgc.cust_name like concat('%', #{custName}, '%') or sgc.cust_id like concat('%', #{custName}, '%') or scgc.cust_idc like concat('%', #{custName}, '%') or sgc.cust_phone like concat('%', #{custName}, '%'))
		</if>
		<if test="custClaimStatus != null and custClaimStatus != ''">
			and scgc.cust_claim_status = #{custClaimStatus}
		</if>
		<if test="userId != null and userId != ''">
			and scgc.user_id = #{userId}
		</if>
		<if test="custClaimStatus != null and custClaimStatus != ''">
			and scgc.cust_claim_status = #{custClaimStatus}
		</if>
		<if test="topGridName!= null and topGridName != ''">
			and scgc.grid_name like concat('%', #{topGridName}, '%')
		</if>
		<if test="secGridName != null and secGridName != ''">
			and scgc.grid_name2 like concat('%', #{secGridName}, '%')
		</if>
		<if test="belongDept != null and belongDept != ''">
			and sd.dept_name like concat('%', #{belongDept}, '%') and sd.dept_type ='branch'
		</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>
	</select>


	<select id="selectCustListByBankBusiness" parameterType="ListSelectForUser" resultMap="ListSelectForUser">
		select
		sgc.cust_id,
		sg.create_role,
		sg.group_id,
		sg.group_name,
		sc.campaign_id,
		sc.campaign_name,
		scgc.cust_idc,
		sgc.cust_name,
		scgc.cust_phone,
		sgc.cust_isn,
		sgc.social_credit_code,
		sgc.lp_name,
		sc.start_time,
		sc.end_time,
		sc.campaign_degree,
		sc.check_target,
		scgc.dept_id,
		sc.claim_type,
		scgc.org_claim_status,
		scgc.org_distribute_status,
		scgc.cust_claim_status,
		scgc.grid_name,
		scgc.grid_name2,
		scgc.outlets,
		su.nick_name as nick_name,
		sd.dept_name as dept_name
		from
		sys_campaign_group_customer scgc
		inner join sys_campaign sc on
		scgc.campaign_id = sc.campaign_id
		inner join sys_group sg on
		scgc.group_id = sg.group_id
		inner join cust_info_business sgc on
		scgc.cust_id = sgc.cust_id
		left join sys_user su on
		scgc.user_id = su.user_id
		left join sys_dept sd on
		scgc.dept_id = sd.dept_id
		where sg.cust_type = #{custPattern} and scgc.del_flag = '0' and scgc.push_status = '1'
		<if test="isOutlet == 0">
			and case when #{deptId} is null then scgc.org_claim_status not in ('0','1')
			else (scgc.dept_id in (select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId},ancestors)) and scgc.org_claim_status in ('0','1')) end
		</if>
		<if test="isOutlet == 1">
			and scgc.outlets_id = #{userDept}
		</if>
		<if test="userDept != null and userDept != '' and isOutlet == 0">
			and scgc.dept_id in
			(select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept},ancestors))
		</if>
		<if test="groupId != null and groupId != ''">
			and sg.group_id = #{groupId}
		</if>
		<if test="campaignId != null and campaignId != ''">
			and scgc.campaign_id = #{campaignId}
		</if>
		<if test="custName != null and custName != ''">
			and (sgc.cust_name like concat('%', #{custName}, '%') or sgc.cust_id like concat('%', #{custName}, '%') or scgc.cust_idc like concat('%', #{custName}, '%') or sgc.cust_phone like concat('%', #{custName}, '%') )
		</if>
		<if test="custClaimStatus != null and custClaimStatus != ''">
			and scgc.cust_claim_status = #{custClaimStatus}
		</if>
		<if test="userId != null and userId != ''">
			and scgc.user_id = #{userId}
		</if>
		<if test="custClaimStatus != null and custClaimStatus != ''">
			and scgc.cust_claim_status = #{custClaimStatus}
		</if>
		<if test="topGridName!= null and topGridName != ''">
			and scgc.grid_name like concat('%', #{topGridName}, '%')
		</if>
		<if test="secGridName != null and secGridName != ''">
			and scgc.grid_name2 like concat('%', #{secGridName}, '%')
		</if>
		<if test="belongDept != null and belongDept != ''">
			and sd.dept_name like concat('%', #{belongDept}, '%') and sd.dept_type ='branch'
		</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>
	</select>


	<select id="selectCustListForExcel"  resultMap="ListSelectForUser">
		select
		sgc.cust_id,
		sg.create_role,
		sg.group_id,
		sg.group_name,
		sg.cust_type,
		sc.campaign_id,
		sc.campaign_name ,
		sgc.cust_idc,
		sgc.cust_name,
		sgc.cust_phone,
		sgc.cust_isn,
		sgc.social_credit_code,
		sc.start_time,
		sc.end_time,
		sc.campaign_degree,
		scgc.dept_id,
		sc.claim_type,
		scgc.org_claim_status,
		scgc.org_distribute_status,
		scgc.cust_claim_status,
		scgc.grid_name,
		scgc.grid_name2,
		scgc.outlets,
		su.nick_name as nick_name,
		sd.dept_name as dept_name
		from
		sys_campaign_group_customer scgc
		inner join sys_campaign sc on
		scgc.campaign_id = sc.campaign_id
		inner join sys_group sg on
		scgc.group_id = sg.group_id
		inner join sys_group_customer sgc on
		scgc.cust_id = sgc.cust_id and scgc.group_id = sgc.group_id
		left join sys_user su on
		scgc.user_id = su.user_id
		left join sys_dept sd on
		scgc.dept_id = sd.dept_id
		<where>
			case when #{deptId} is null then scgc.org_claim_status not in ('0','1')
			else (scgc.dept_id in
			(select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId},ancestors)) and scgc.org_claim_status in ('0','1')) end
			<if test="groupId != null  and groupId != ''"> and sgc.group_id = #{groupId}</if>
			and sgc.cust_id in <foreach collection="custIds" index="index" item="item" open="(" separator="," close=")">#{item}</foreach>
		</where>
	</select>


</mapper> 