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


    <resultMap type="CustInfoRetail" id="CustInfoRetailResult">
        <result property="id"    column="id"    />
        <result property="custId"    column="cust_id"    />
        <result property="custIsn"    column="cust_isn"    />
        <result property="custIdc"    column="cust_idc"    />
        <result property="custName"    column="cust_name"    />
        <result property="custPhone"    column="cust_phone"    />
        <result property="openAccTime"    column="open_acc_time"    />
        <result property="belongBranchId"    column="belong_branch_id"    />
        <result property="belongBranchName"    column="belong_branch_name"    />
        <result property="custGender"    column="cust_gender"    />
        <result property="isMarried"    column="is_married"    />
        <result property="activeBranchId"    column="active_branch_id"    />
        <result property="activeBranchName"    column="active_branch_name"    />
        <result property="custAge"    column="cust_age"    />
        <result property="belongBusi"    column="belong_busi"    />
        <result property="custEdu"    column="cust_edu"    />
        <result property="custSalary"    column="cust_salary"    />
        <result property="custLocation"    column="cust_location"    />
        <result property="custTag"    column="cust_tag"    />
        <result property="updateDept"    column="update_dept"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="belongUserId"    column="belong_user_id"    />
        <result property="belongUserName"    column="belong_user_name"    />
        <result property="belongUserNameList"    column="belong_user_name_list"    />
        <result property="status"    column="status"    />
        <result property="loanTag"    column="loan_tag"    />
        <result property="custType"    column="cust_type"    />
        <result property="belongOutletId"    column="belong_outlet_id"    />
        <result property="belongOutletName"    column="belong_outlet_name"    />
        <result property="regionCode"    column="region_code"    />
        <result property="birthday"    column="birthday"    />
        <result property="risk"    column="risk"    />
        <result property="identification"    column="identification"    />
        <result property="label"    column="label"    />
        <result property="registerLocation"    column="register_location"    />
        <result property="jobLocation"    column="job_location"    />
        <result property="recordStatus"    column="record_status"    />
        <result property="asset"    column="asset"    />
        <result property="credit"    column="credit"    />
    </resultMap>

    <resultMap type="CustInfoRetail" id="CustInfoRetailResult2">
        <result property="id"    column="id"    />
        <result property="custId"    column="cust_id"    />
        <result property="custIsn"    column="cust_isn"    />
        <result property="custIdc"    column="cust_idc"    />
        <result property="custName"    column="cust_name"    />
        <result property="custPhone"    column="cust_phone"    />
        <result property="openAccTime"    column="open_acc_time"    />
        <result property="belongBranchId"    column="belong_branch_id"    />
        <result property="belongBranchName"    column="dept_name"    />
        <result property="custGender"    column="cust_gender"    />
        <result property="isMarried"    column="is_married"    />
        <result property="activeBranchId"    column="active_branch_id"    />
        <result property="activeBranchName"    column="active_branch_name"    />
        <result property="custAge"    column="cust_age"    />
        <result property="belongBusi"    column="belong_busi"    />
        <result property="custEdu"    column="cust_edu"    />
        <result property="custSalary"    column="cust_salary"    />
        <result property="custLocation"    column="cust_location"    />
        <result property="custTag"    column="cust_tag"    />
        <result property="updateDept"    column="update_dept"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="belongUserId"    column="belong_user_id"    />
        <result property="belongUserName"    column="belong_user_name"    />
        <result property="belongUserNameList"    column="belong_user_name_list"    />
        <result property="status"    column="status"    />
        <result property="loanTag"    column="loan_tag"    />
        <result property="custType"    column="cust_type"    />
        <result property="belongOutletId"    column="belong_outlet_id"    />
        <result property="belongOutletName"    column="outlets"    />
        <result property="regionCode"    column="region_code"    />
        <result property="birthday"    column="birthday"    />
        <result property="risk"    column="risk"    />
        <result property="identification"    column="identification"    />
        <result property="label"    column="label"    />
        <result property="registerLocation"    column="register_location"    />
        <result property="jobLocation"    column="job_location"    />
    </resultMap>

    <resultMap type="CustManageInfo" id="CustManageInfoResult">
        <result property="custId"    column="cust_id"    />
        <result property="custName"    column="cust_name"    />
        <result property="custNo"    column="cust_idc"    />
        <result property="belongUserId"    column="user_name"    />
        <result property="belongUserName"    column="nick_name"    />
        <result property="custPattern"    column="cust_type"    />
        <result property="regionCode"    column="region_code"    />
    </resultMap>

    <resultMap type="com.ruoyi.ibs.list.domain.GridInfo" id="GridInfoResult">
        <result property="gridName"    column="grid_name"    />
        <result property="gridName2"    column="grid_name2"    />
        <result property="userName"    column="user_name"    />
        <result property="opsDept"    column="ops_dept"    />
    </resultMap>

    <sql id="selectCustInfoRetailVo">
        select id, cust_id, cust_isn, cust_idc, cust_name, cust_phone, open_acc_time, belong_branch_id, belong_branch_name, cust_gender, is_married, active_branch_id, active_branch_name, cust_age, belong_busi, cust_edu, cust_salary, cust_location, cust_tag, update_dept, update_by, update_time, belong_user_id, belong_user_name, status, loan_tag, cust_type, belong_outlet_id, belong_outlet_name,
               region_code, birthday, risk, identification,label,register_location,job_location,record_status, cust_level from cust_info_retail
    </sql>

    <select id="selectCustInfoRetailList" parameterType="CustInfoRetail" resultMap="CustInfoRetailResult">
        <include refid="selectCustInfoRetailVo"/>
        <where>
            <if test="custId != null  and custId != ''"> and cust_id = #{custId}</if>
            <if test="custIsn != null  and custIsn != ''"> and cust_isn = #{custIsn}</if>
            <if test="custIdc != null  and custIdc != ''"> and cust_idc = #{custIdc}</if>
            <if test="custName != null  and custName != ''"> and cust_name like concat('%', #{custName}, '%')</if>
            <if test="custPhone != null  and custPhone != ''"> and cust_phone = #{custPhone}</if>
            <if test="openAccTime != null "> and open_acc_time = #{openAccTime}</if>
            <if test="belongBranchId != null  and belongBranchId != ''"> and belong_branch_id = #{belongBranchId}</if>
            <if test="belongBranchName != null  and belongBranchName != ''"> and belong_branch_name like concat('%', #{belongBranchName}, '%')</if>
            <if test="custGender != null  and custGender != ''"> and cust_gender = #{custGender}</if>
            <if test="isMarried != null  and isMarried != ''"> and is_married = #{isMarried}</if>
            <if test="activeBranchId != null  and activeBranchId != ''"> and active_branch_id = #{activeBranchId}</if>
            <if test="activeBranchName != null  and activeBranchName != ''"> and active_branch_name like concat('%', #{activeBranchName}, '%')</if>
            <if test="custAge != null  and custAge != ''"> and cust_age = #{custAge}</if>
            <if test="belongBusi != null  and belongBusi != ''"> and belong_busi = #{belongBusi}</if>
            <if test="custEdu != null  and custEdu != ''"> and cust_edu = #{custEdu}</if>
            <if test="custSalary != null  and custSalary != ''"> and cust_salary = #{custSalary}</if>
            <if test="custLocation != null  and custLocation != ''"> and cust_location = #{custLocation}</if>
            <if test="custTag != null  and custTag != ''"> and cust_tag = #{custTag}</if>
            <if test="updateDept != null  and updateDept != ''"> and update_dept = #{updateDept}</if>
            <if test="belongUserId != null  and belongUserId != ''"> and belong_user_id = #{belongUserId}</if>
            <if test="belongUserName != null  and belongUserName != ''"> and belong_user_name like concat('%', #{belongUserName}, '%')</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="loanTag != null  and loanTag != ''"> and loan_tag = #{loanTag}</if>
            <if test="custType != null  and custType != ''"> and cust_type = #{custType}</if>
            <if test="belongOutletId != null  and belongOutletId != ''"> and belong_outlet_id = #{belongOutletId}</if>
            <if test="belongOutletName != null  and belongOutletName != ''"> and belong_outlet_name like concat('%', #{belongOutletName}, '%')</if>
            <if test="birthday != null  and birthday != ''"> and birthday = #{birthday}</if>
            <if test="risk != null  and risk != ''"> and risk = #{risk}</if>
            <if test="identification != null  and identification != ''"> and identification = #{identification}</if>
            <if test="label != null  and label != ''"> and label = #{label}</if>
            <if test="registerLocation != null  and registerLocation != ''"> and register_location = #{registerLocation}</if>
            <if test="jobLocation != null  and jobLocation != ''"> and job_location = #{jobLocation}</if>
            <if test="recordStatus != null  and recordStatus != ''"> and record_status = #{recordStatus}</if>
        </where>
    </select>

    <select id="selectCustInfoRetailListByKeyword" parameterType="String" resultMap="CustInfoRetailResult">

        <include refid="selectCustInfoRetailVo"/>
        <where>
            <if test="keyword != null  and keyword != ''"> and ( cust_idc like concat('%', #{keyword}, '%') or cust_name like concat('%', #{keyword}, '%')
                or cust_phone like concat('%', #{keyword}, '%'))
            </if>
        </where>
        limit #{start},#{size}
    </select>

    <select id="selectCustInfoRetailListByKeywordBranchAndManager" parameterType="String" resultMap="CustInfoRetailResult">
        select  distinct a.id, a.cust_id, a.cust_idc, a.cust_name, a.cust_phone,a.update_time from cust_info_retail a
        inner join
        (select user_id,dept_id,outlets_id,cust_id  from cust_dept_user_grid
        <where>
            <if test="deptId != null  and deptId != ''">
                and dept_id in (select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId},ancestors))
            </if>
            <if test="outletsId != null  and outletsId != ''">
                and outlets_id in (select dept_id from sys_dept where dept_id = #{outletsId} or find_in_set(#{outletsId},ancestors))
            </if>
            <if test="userId != null  and userId != ''">
                and user_id = #{userId}
            </if>
            <if test="opsDept != null  and opsDept != ''">
                and ops_dept = #{opsDept}
            </if>
        </where>
        union all
        (select t2.user_id,t2.dept_id,t2.outlets_id,t2.cust_id  from cust_dept_user_cmpm t2
        <where>
            and t2.cust_type = '0'
            <if test="deptId != null  and deptId != ''">
                and t2.dept_id in (select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId},ancestors))
            </if>
            <if test="outletsId != null  and outletsId != ''">
                and t2.outlets_id in (select dept_id from sys_dept where dept_id = #{outletsId} or find_in_set(#{outletsId},ancestors))
            </if>
            <if test="userId != null  and userId != ''">
                and t2.user_id = #{userId}
            </if>
        </where>
        )) b
        on a.cust_id = b.cust_id
        <where>
            <if test="keyword != null  and keyword != ''"> and ( a.cust_idc like concat('%', #{keyword}, '%') or a.cust_name like concat('%', #{keyword}, '%')
            or a.cust_phone like concat('%', #{keyword}, '%'))
            </if>
        </where>
        limit #{start},#{size}
    </select>

    <select id="selectCustInfoRetailById" parameterType="Long" resultMap="CustInfoRetailResult">
        <include refid="selectCustInfoRetailVo"/>
        where id = #{id}
    </select>

    <select id="selectCustInfoRetailBycustIdc" parameterType="String" resultMap="CustInfoRetailResult">
        <include refid="selectCustInfoRetailVo"/>
        where cust_idc = #{custIdc}
    </select>

    <insert id="insertCustInfoRetail" parameterType="CustInfoRetail" useGeneratedKeys="true" keyProperty="id">
        insert into cust_info_retail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="custId != null">cust_id,</if>
            <if test="custIsn != null">cust_isn,</if>
            <if test="custIdc != null">cust_idc,</if>
            <if test="custName != null">cust_name,</if>
            <if test="custPhone != null">cust_phone,</if>
            <if test="openAccTime != null">open_acc_time,</if>
            <if test="belongBranchId != null">belong_branch_id,</if>
            <if test="belongBranchName != null">belong_branch_name,</if>
            <if test="custGender != null">cust_gender,</if>
            <if test="isMarried != null">is_married,</if>
            <if test="activeBranchId != null">active_branch_id,</if>
            <if test="activeBranchName != null">active_branch_name,</if>
            <if test="custAge != null">cust_age,</if>
            <if test="belongBusi != null">belong_busi,</if>
            <if test="custEdu != null">cust_edu,</if>
            <if test="custSalary != null">cust_salary,</if>
            <if test="custLocation != null">cust_location,</if>
            <if test="custTag != null">cust_tag,</if>
            <if test="updateDept != null">update_dept,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="belongUserId != null">belong_user_id,</if>
            <if test="belongUserName != null">belong_user_name,</if>
            <if test="status != null">status,</if>
            <if test="loanTag != null">loan_tag,</if>
            <if test="custType != null">cust_type,</if>
            <if test="belongOutletId != null">belong_outlet_id,</if>
            <if test="belongOutletName != null">belong_outlet_name,</if>
            <if test="birthday != null">birthday,</if>
            <if test="risk != null">risk,</if>
            <if test="identification != null">identification,</if>
            <if test="label != null">label,</if>
            <if test="registerLocation != null "> register_location ,</if>
            <if test="jobLocation != null"> job_location,</if>
            <if test="recordStatus != null ">  record_status ,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="custId != null">#{custId},</if>
            <if test="custIsn != null">#{custIsn},</if>
            <if test="custIdc != null">#{custIdc},</if>
            <if test="custName != null">#{custName},</if>
            <if test="custPhone != null">#{custPhone},</if>
            <if test="openAccTime != null">#{openAccTime},</if>
            <if test="belongBranchId != null">#{belongBranchId},</if>
            <if test="belongBranchName != null">#{belongBranchName},</if>
            <if test="custGender != null">#{custGender},</if>
            <if test="isMarried != null">#{isMarried},</if>
            <if test="activeBranchId != null">#{activeBranchId},</if>
            <if test="activeBranchName != null">#{activeBranchName},</if>
            <if test="custAge != null">#{custAge},</if>
            <if test="belongBusi != null">#{belongBusi},</if>
            <if test="custEdu != null">#{custEdu},</if>
            <if test="custSalary != null">#{custSalary},</if>
            <if test="custLocation != null">#{custLocation},</if>
            <if test="custTag != null">#{custTag},</if>
            <if test="updateDept != null">#{updateDept},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="belongUserId != null">#{belongUserId},</if>
            <if test="belongUserName != null">#{belongUserName},</if>
            <if test="status != null">#{status},</if>
            <if test="loanTag != null">#{loanTag},</if>
            <if test="custType != null">#{custType},</if>
            <if test="belongOutletId != null">#{belongOutletId},</if>
            <if test="belongOutletName != null">#{belongOutletName},</if>
            <if test="birthday != null">#{birthday},</if>
            <if test="risk != null">#{risk},</if>
            <if test="identification != null">#{identification},</if>
            <if test="label != null">#{label},</if>
            <if test="registerLocation != null ">#{registerLocation},</if>
            <if test="jobLocation != null">#{jobLocation},</if>
            <if test="recordStatus != null">  #{recordStatus}</if>
         </trim>
    </insert>

    <update id="updateCustInfoRetail" parameterType="CustInfoRetail">
        update cust_info_retail
        <trim prefix="SET" suffixOverrides=",">
            <if test="custId != null">cust_id = #{custId},</if>
            <if test="custIsn != null">cust_isn = #{custIsn},</if>
            <if test="custIdc != null">cust_idc = #{custIdc},</if>
            <if test="custName != null">cust_name = #{custName},</if>
            <if test="custPhone != null">cust_phone = #{custPhone},</if>
            <if test="openAccTime != null">open_acc_time = #{openAccTime},</if>
            <if test="belongBranchId != null">belong_branch_id = #{belongBranchId},</if>
            <if test="belongBranchName != null">belong_branch_name = #{belongBranchName},</if>
            <if test="custGender != null">cust_gender = #{custGender},</if>
            <if test="isMarried != null">is_married = #{isMarried},</if>
            <if test="activeBranchId != null">active_branch_id = #{activeBranchId},</if>
            <if test="activeBranchName != null">active_branch_name = #{activeBranchName},</if>
            <if test="custAge != null">cust_age = #{custAge},</if>
            <if test="belongBusi != null">belong_busi = #{belongBusi},</if>
            <if test="custEdu != null">cust_edu = #{custEdu},</if>
            <if test="custSalary != null">cust_salary = #{custSalary},</if>
            <if test="custLocation != null">cust_location = #{custLocation},</if>
            <if test="custTag != null">cust_tag = #{custTag},</if>
            <if test="updateDept != null">update_dept = #{updateDept},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="belongUserId != null">belong_user_id = #{belongUserId},</if>
            <if test="belongUserName != null">belong_user_name = #{belongUserName},</if>
            <if test="status != null">status = #{status},</if>
            <if test="loanTag != null">loan_tag = #{loanTag},</if>
            <if test="custType != null">cust_type = #{custType},</if>
            <if test="belongOutletId != null">belong_outlet_id = #{belongOutletId},</if>
            <if test="belongOutletName != null">belong_outlet_name = #{belongOutletName},</if>
            <if test="birthday != null">birthday = #{birthday},</if>
            <if test="risk != null">risk = #{risk},</if>
            <if test="identification != null">identification = #{identification},</if>
            <if test="label != null">label = #{label},</if>
            <if test="registerLocation != null "> register_location = #{registerLocation},</if>
            <if test="jobLocation != null">  job_location = #{jobLocation},</if>
            <if test="recordStatus != null  and recordStatus != ''"> record_status = #{recordStatus},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateCustInfoRetailLabel" parameterType="CustInfoRetail">
        update cust_info_retail
        <trim prefix="SET" suffixOverrides=",">
            <if test="custId != null and custId != ''">cust_id = #{custId},</if>
            <if test="custIsn != null and custIsn != ''">cust_isn = #{custIsn},</if>
            <if test="custIdc != null and custIdc != ''">cust_idc = #{custIdc},</if>
            <if test="custName != null and custName != ''">cust_name = #{custName},</if>
            <if test="custPhone != null and custPhone != ''">cust_phone = #{custPhone},</if>
            <if test="openAccTime != null and openAccTime != ''">open_acc_time = #{openAccTime},</if>
            <if test="belongBranchId != null and belongBranchId != ''">belong_branch_id = #{belongBranchId},</if>
            <if test="belongBranchName != null and belongBranchName != ''">belong_branch_name = #{belongBranchName},</if>
            <if test="custGender != null and custGender != ''">cust_gender = #{custGender},</if>
            <if test="isMarried != null and isMarried != ''">is_married = #{isMarried},</if>
            <if test="activeBranchId != null and activeBranchId != ''">active_branch_id = #{activeBranchId},</if>
            <if test="activeBranchName != null and activeBranchName != ''">active_branch_name = #{activeBranchName},</if>
            <if test="custAge != null and custAge != ''">cust_age = #{custAge},</if>
            <if test="belongBusi != null and belongBusi != ''">belong_busi = #{belongBusi},</if>
            <if test="custEdu != null and custEdu != ''">cust_edu = #{custEdu},</if>
            <if test="custSalary != null and custSalary != ''">cust_salary = #{custSalary},</if>
            <if test="custLocation != null and custLocation != ''">cust_location = #{custLocation},</if>
            <if test="custTag != null and custTag != ''">cust_tag = #{custTag},</if>
            <if test="updateDept != null and updateDept != ''">update_dept = #{updateDept},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="belongUserId != null and belongUserId != ''">belong_user_id = #{belongUserId},</if>
            <if test="belongUserName != null and belongUserName != ''">belong_user_name = #{belongUserName},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="loanTag != null and loanTag != ''">loan_tag = #{loanTag},</if>
            <if test="custType != null and custType != ''">cust_type = #{custType},</if>
            <if test="belongOutletId != null and belongOutletId != ''">belong_outlet_id = #{belongOutletId},</if>
            <if test="belongOutletName != null and belongOutletName != ''">belong_outlet_name = #{belongOutletName},</if>
            <if test="birthday != null and birthday != ''">birthday = #{birthday},</if>
            <if test="risk != null and risk != ''">risk = #{risk},</if>
            <if test="identification != null and identification != ''">identification = #{identification},</if>
            <if test="label != null and label != ''">label = #{label},</if>
            <if test="registerLocation != null and registerLocation != ''"> register_location = #{registerLocation},</if>
            <if test="jobLocation != null and jobLocation != ''">  job_location = #{jobLocation},</if>
            <if test="birthday != null ">birthday = #{birthday},</if>
            <if test="recordStatus != null  and recordStatus != ''"> record_status = #{recordStatus},</if>
        </trim>
        where cust_id = #{custId}
    </update>

    <delete id="deleteCustInfoRetailById" parameterType="Long">
        delete from cust_info_retail where id = #{id}
    </delete>

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

    <select id="selectCustInfoRetailByCustId" parameterType="String" resultMap="CustInfoRetailResult">
        <include refid="selectCustInfoRetailVo"/>
        where cust_id = #{custId}
    </select>

    <select id="selectCustInfoRetailByCustIdc" parameterType="String" resultMap="CustInfoRetailResult">
        <include refid="selectCustInfoRetailVo"/>
        where cust_idc = #{custIdc}
    </select>


    <select id="selectMyCustInfoRetailList" parameterType="CustInfoRetail" resultMap="CustInfoRetailResult2" >
select  distinct
        aaa.id,
        aaa.cust_id,
        aaa.cust_isn,
        aaa.cust_idc,
        aaa.cust_name,
        aaa.cust_phone,
        aaa.cust_tag,
        aaa.cust_type,
        b.belong_user_name_list,
        b.dept_name,
        b.outlets
from
        (SELECT a.id,
        a.cust_id,
        a.cust_isn,
        a.cust_idc,
        a.cust_name,
        a.cust_phone,
        a.cust_tag,
        a.cust_type
        from cust_info_retail a
        <where>
            <if test="custTag != null  and custTag != ''"> and
                ((CASE WHEN substr(#{custTag}, 1, 1) = '1' THEN substr(a.cust_tag, 1, 1) = '1' end)
                OR (CASE WHEN substr(#{custTag}, 2, 1) = '1' THEN substr(a.cust_tag, 2, 1) = '1' end)
                OR (CASE WHEN substr(#{custTag}, 3, 1) = '1' THEN substr(a.cust_tag, 3, 1) = '1' end))
            </if>
            <if test="custName != null  and custName != ''"> and a.cust_name like concat('%', #{custName}, '%') </if>
            <if test="custType != null  and custType != ''"> and a.cust_type = #{custType}</if>
        </where>
        ) aaa
        <if test="opsDept != null  and opsDept != ''">
            inner </if>
        <if test="opsDept == null  or opsDept == ''">
            left </if>
        join  (select GROUP_CONCAT(user_id SEPARATOR ',') as user_id,GROUP_CONCAT(dept_id SEPARATOR ',') dept_id,cust_id,GROUP_CONCAT(dept_name SEPARATOR ',') as dept_name,GROUP_CONCAT(outlets SEPARATOR ',') as outlets ,GROUP_CONCAT(user_name SEPARATOR ',') as belong_user_name_list
        from (select user_id,dept_id,cust_id,dept_name,outlets,concat_ws('-',user_name,teller_id) as user_name from cust_dept_user_grid
        <where>
            <if test="opsDept != null  and opsDept != ''">
                ops_dept = #{opsDept}</if>
        </where>
              union
              (select t2.user_id,t2.dept_id,t2.cust_id,'','','' from cust_dept_user_cmpm t2
              left join (select cust_id from cust_dept_user_grid
        <where>
            <if test="opsDept != null  and opsDept != ''">
                ops_dept = #{opsDept}</if>
        </where>
        group by cust_id)t1 on t2.cust_id = t1.cust_id
               where (t1.cust_id is null or t1.cust_id = '') and t2.cust_type = '0' and t2.dept_id is not null
        ))f
        where 1=1
            <if test = "isHead == false">
            ${params.dataScope}
            </if>
        group by
cust_id
        ) b on aaa.cust_id = b.cust_id
        where
        1=1
        <if test = "isHead == false">
            and b.user_id >0
        </if>
        limit #{start},#{size}
    </select>


    <select id="selectMyCustInfoRetailListOps" parameterType="CustInfoRetail" resultType="com.ruoyi.ibs.customerselect.domain.CustBaseInfo" >

        SELECT distinct a.id,
        a.cust_id,
        a.cust_isn,
        a.cust_idc,
        a.cust_name,
        a.cust_phone,
        a.cust_tag,
        a.cust_type,a.cur_bal_d, a.cur_bal_t, a.bal_loan, a.cur_bal_5_bad, a.cur_d_ave,a.asset,a.credit,
        a.cur_t_ave, a.loan_ave, is_ph, a.is_sx, a.is_yxht, a.is_xyk, a.fshl, a.is_sd, a.etc, a.dian, a.is_black, a.is_bad,
        a.is_365zf,a.is_180zf,a.is_90zf,a.is_30zf, a.cust_level
        from cust_info_retail a
        <if test="isSelectGrid == false">
        inner join (select user_id,dept_id,cust_id from cust_dept_user_grid
            <where>

                <if test="opsDept != null  and opsDept != ''">
                    ops_dept = #{opsDept}</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="outletsId != null ">
                    and outlets_id in (select dept_id from sys_dept where dept_id = #{outletsId} or find_in_set(#{outletsId},ancestors))
                </if>
                <if test="userId != null  ">
                    and user_id = #{userId}</if>
            </where>
            union all
            (select t2.user_id,t2.dept_id,t2.cust_id from cust_dept_user_cmpm t2 where t2.cust_type = '0' and t2.dept_id is not null
            <if test="deptId != null ">
                and t2.dept_id in (select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId},ancestors))
            </if>
            <if test="outletsId != null ">
                and t2.outlets_id in (select dept_id from sys_dept where dept_id = #{outletsId} or find_in_set(#{outletsId},ancestors))
            </if>
            <if test="userId != null  ">
                and t2.user_id = #{userId}</if>

            ) ) b on a.cust_id = b.cust_id</if>
        <where>
            <if test="regionTopGridIds != null and regionTopGridIds.size() > 0 ">
                AND a.cust_id IN (
                SELECT cust_id
                FROM grid_region_cust_user grcu
                <where>
                    <if test="regionTopGridIds != null and regionTopGridIds.size()>0">
                        <foreach collection="regionTopGridIds" item="param" separator="or">
                            grcu.top_grid_id = #{param.value}
                        </foreach>
                    </if>
                    )
                </where>
            </if>
            <if test="regionSecGridIds != null and regionSecGridIds.size()>0">
                AND a.cust_id IN (
                SELECT cust_id
                FROM grid_region_cust_user grcu
                <where>
                    <foreach collection="regionSecGridIds" item="param" separator="or">
                        grcu.sec_grid_id = #{param.value}
                    </foreach>
                </where>
                )
            </if>
            <if test="virtualGridIds != null and virtualGridIds.size() > 0 ">
                AND a.cust_id IN (
                SELECT cust_id
                FROM grid_virtual_cust_user gvcu
                <where>
                    <foreach collection="virtualGridIds" item="param" separator="or">
                        gvcu.grid_id = #{param.value}
                    </foreach>
                </where>
                )
            </if>
            <if test="drawGridIds != null and drawGridIds.size() > 0 ">
                AND a.cust_id IN (
                SELECT dsc.cust_id
                FROM grid_draw_user_relate gdur
                INNER JOIN grid_draw_shape_relate gdsr  ON gdur.grid_id = gdsr.grid_id
                INNER JOIN draw_shape_cust dsc ON gdsr.shape_id = dsc.shape_id
                <where>
                    <foreach collection="drawGridIds" item="param" separator="or">
                        gdur.grid_id = #{param.value}
                    </foreach>
                </where>
                )
            </if>
            <if test="continuousParams != null and continuousParams.size() > 0 or discreteParams != null and discreteParams.size() > 0">
                    <if test="continuousParams != null and continuousParams.size() > 0">
                    and
                        <foreach collection="continuousParams" item="param" separator="and">
                            <choose>
                                <when test="param.value[0] != null and param.value[0] != '' and param.value[1] != null and param.value[1] != ''">
                                    CAST(${param.key} AS DECIMAL(10,2)) BETWEEN CAST(#{param.value[0]} AS DECIMAL(10,2)) AND CAST(#{param.value[1]} AS DECIMAL(10,2))
                                </when>
                                <when test="param.value[0] != null and param.value[0] != ''">
                                    CAST(${param.key} AS DECIMAL(10,2)) &gt;=  CAST(#{param.value[0]} AS DECIMAL(10,2))
                                </when>
                                <when test="param.value[1] != null and param.value[1] != ''">
                                    CAST(${param.key} AS DECIMAL(10,2)) &lt;=  CAST(#{param.value[1]} AS DECIMAL(10,2))
                                </when>
                            </choose>
                        </foreach>
                    </if>
                    <if test="discreteParams != null and discreteParams.size() > 0">
                        and
                        <foreach collection="discreteParams" item="param" separator="and">
                            ${param.key} = #{param.value}
                        </foreach>
                    </if>
            </if>
            <if test="custTag != null  and custTag != ''"> and
                ((CASE WHEN substr(#{custTag}, 1, 1) = '1' THEN substr(a.cust_tag, 1, 1) = '1' end)
                OR (CASE WHEN substr(#{custTag}, 2, 1) = '1' THEN substr(a.cust_tag, 2, 1) = '1' end)
                OR (CASE WHEN substr(#{custTag}, 3, 1) = '1' THEN substr(a.cust_tag, 3, 1) = '1' end))
            </if>
            <if test="custName != null  and custName != ''"> and a.cust_name like concat('%', #{custName}, '%') </if>
            <if test="custId != null  and custId != ''"> and a.cust_id like concat('%', #{custId}, '%') </if>
            <if test="custType != null  and custType != ''"> and a.cust_type = #{custType}</if>
        </where>
        limit #{start},#{size}
    </select>

    <select id="selectMyCustInfoRetailListHead" parameterType="CustInfoRetail" resultType="com.ruoyi.ibs.customerselect.domain.CustBaseInfo" >
SELECT distinct a.id,
        a.cust_id,
        a.cust_isn,
        a.cust_idc,
        a.cust_name,
        a.cust_phone,
        a.cust_tag,
        a.cust_type,
        a.cust_type,a.cur_bal_d, a.cur_bal_t, a.bal_loan, a.cur_bal_5_bad, a.cur_d_ave,a.asset,a.credit,
        a.cur_t_ave, a.loan_ave, is_ph, a.is_sx, a.is_yxht, a.is_xyk, a.fshl, a.is_sd, a.etc, a.dian, a.is_black, a.is_bad,
        a.is_365zf,a.is_180zf,a.is_90zf,a.is_30zf, a.cust_level
        from cust_info_retail a
        <where>
            <if test="regionTopGridIds != null and regionTopGridIds.size() > 0 ">
                AND a.cust_id IN (
                SELECT cust_id
                FROM grid_region_cust_user grcu
                <where>
                    <if test="regionTopGridIds != null and regionTopGridIds.size()>0">
                        <foreach collection="regionTopGridIds" item="param" separator="or">
                            grcu.top_grid_id = #{param.value}
                        </foreach>
                    </if>
                </where>
                )
            </if>
            <if test="regionSecGridIds != null and regionSecGridIds.size()>0">
                AND a.cust_id IN (
                SELECT cust_id
                FROM grid_region_cust_user grcu
                <where>
                    <foreach collection="regionSecGridIds" item="param" separator="or">
                        grcu.sec_grid_id = #{param.value}
                    </foreach>
                </where>
                )
            </if>
            <if test="virtualGridIds != null and virtualGridIds.size() > 0 ">
                AND a.cust_id IN (
                SELECT cust_id
                FROM grid_virtual_cust_user gvcu
                <where>
                    <foreach collection="virtualGridIds" item="param" separator="or">
                        gvcu.grid_id = #{param.value}
                    </foreach>
                </where>
                )
            </if>
            <if test="drawGridIds != null and drawGridIds.size() > 0 ">
                AND a.cust_id IN (
                SELECT dsc.cust_id
                FROM grid_draw_user_relate gdur
                INNER JOIN grid_draw_shape_relate gdsr  ON gdur.grid_id = gdsr.grid_id
                INNER JOIN draw_shape_cust dsc ON gdsr.shape_id = dsc.shape_id
                <where>
                    <foreach collection="drawGridIds" item="param" separator="or">
                        gdur.grid_id = #{param.value}
                    </foreach>
                </where>
                )
            </if>
            <if test="continuousParams != null and continuousParams.size() > 0 or discreteParams != null and discreteParams.size() > 0">
                    <if test="continuousParams != null and continuousParams.size() > 0">
                    and
                        <foreach collection="continuousParams" item="param" separator="and">
                            <choose>
                                <when test="param.value[0] != null and param.value[0] != '' and param.value[1] != null and param.value[1] != ''">
                                    CAST(${param.key} AS DECIMAL(10,2)) BETWEEN CAST(#{param.value[0]} AS DECIMAL(10,2)) AND CAST(#{param.value[1]} AS DECIMAL(10,2))
                                </when>
                                <when test="param.value[0] != null and param.value[0] != ''">
                                    CAST(${param.key} AS DECIMAL(10,2)) &gt;=  CAST(#{param.value[0]} AS DECIMAL(10,2))
                                </when>
                                <when test="param.value[1] != null and param.value[1] != ''">
                                    CAST(${param.key} AS DECIMAL(10,2)) &lt;=  CAST(#{param.value[1]} AS DECIMAL(10,2))
                                </when>
                            </choose>
                        </foreach>
                    </if>
                    <if test="discreteParams != null and discreteParams.size() > 0">
                        and
                        <foreach collection="discreteParams" item="param" separator="and">
                            ${param.key} = #{param.value}
                        </foreach>
                    </if>
            </if>
            <if test="custTag != null  and custTag != ''"> and
                ((CASE WHEN substr(#{custTag}, 1, 1) = '1' THEN substr(a.cust_tag, 1, 1) = '1' end)
                OR (CASE WHEN substr(#{custTag}, 2, 1) = '1' THEN substr(a.cust_tag, 2, 1) = '1' end)
                OR (CASE WHEN substr(#{custTag}, 3, 1) = '1' THEN substr(a.cust_tag, 3, 1) = '1' end))
            </if>
            <if test="custName != null  and custName != ''"> and a.cust_name like concat('%', #{custName}, '%') </if>
            <if test="custId != null  and custId != ''"> and a.cust_id like concat('%', #{custId}, '%') </if>
            <if test="custType != null  and custType != ''"> and a.cust_type = #{custType}</if>
        </where>
        limit #{start},#{size}
    </select>

    <select id="selectMyCustInfoRetailList_COUNT" resultType="Long">
        select -1
    </select>

    <select id="selectCustInfoRetailByAdminList" >
        select a.* from cust_info_retail a inner join grid_region_admin_division_relate b on b.code like concat('',a.region_code , '%')
        where b.grid_id = #{gridId}
        <if test="custName != null  and custName != ''"> and a.cust_name like concat('%', #{custName}, '%') </if>
        <if test="custId != null  and custId != ''"> and a.cust_id like concat('%', #{custId}, '%') </if>
    </select>

    <select id="selectunDisRetailByAdminList" >
        select a.* from cust_info_retail a inner join grid_region_admin_division_relate b on b.code like concat('',a.region_code , '%')
        where b.grid_id = #{gridId} and a.belong_user_name = ''
        <if test="custName != null  and custName != ''"> and cust_name like concat('%', #{custName}, '%') </if>
        <if test="custId != null  and custId != ''"> and cust_id like concat('%', #{custId}, '%') </if>
        group by a.cust_id
    </select>

    <select id="selectCustInfoRetailByVirtualList" >
        select a.* from cust_info_retail a inner join grid_virtual_customer_relate b on b.cust_id =a.cust_id
        where b.grid_id = #{gridId}
        <if test="custName != null  and custName != ''"> and a.cust_name like concat('%', #{custName}, '%') </if>
        <if test="custId != null  and custId != ''"> and a.cust_id like concat('%', #{custId}, '%') </if>
        group by a.cust_id
    </select>

    <select id="selectunDisRetailByVirtualList" >
        select a.* from cust_info_retail a inner join grid_virtual_customer_relate b on b.cust_id =a.cust_id
        where b.grid_id = #{gridId} and a.belong_user_name = ''
        <if test="custName != null  and custName != ''"> and a.cust_name like concat('%', #{custName}, '%') </if>
        <if test="custId != null  and custId != ''"> and a.cust_id like concat('%', #{custId}, '%') </if>
        group by a.cust_id
    </select>

    <select id="selectCustNumByAdmin" parameterType="Long" resultType="java.lang.Long">
        select count(distinct a.cust_id) from (select cust_id,region_code from cust_info_retail) a
        inner join (select distinct code,grid_id from grid_region_admin_division_relate) b on b.code = a.region_code
        inner join (select distinct grid_id from grid_region_user_relate where delete_flag ='0'
        and (relate_dept_id in (select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept},ancestors)) or user_name = #{userDept})) c on b.grid_id = c.grid_id
        where b.grid_id = #{gridId}
    </select>

    <select id="getRetailNumByDeptId" parameterType="Long" resultType="java.lang.Long">
        select count(distinct a.cust_id) from cust_info_retail a
                                                  inner join (select distinct code, grid_id
                                                              from grid_region_admin_division_relate
                                                              where delete_flag = '0') b
        on b.code = a.region_code
        inner join (select distinct grid_id from grid_region_user_relate where delete_flag ='0' and parent_grid_id is null
        and relate_dept_id in (select dept_id from sys_dept where dept_id = #{headDeptId} or find_in_set(#{headDeptId},ancestors))) c
        on b.grid_id =c.grid_id
    </select>

    <select id="getRetailNumByUserId" parameterType="String" resultType="java.lang.Long">
        select count(distinct a.cust_id) from cust_info_retail a
                                                  inner join (select distinct code, grid_id
                                                              from grid_region_admin_division_relate
                                                              where delete_flag = '0'
                                                                and ops_dept != '1') b
        on b.code = a.region_code
                                                  inner join (select distinct grid_id, user_name, delete_flag
                                                              from grid_region_user_relate
                                                              where delete_flag = '0'
                                                                and parent_grid_id is not null) c
        on b.grid_id = c.grid_id
        where  c.user_name = #{UserId} and c.delete_flag ='0'
    </select>

    <select id="selectCustInfoByAdminList" >
        select a.cust_id,a.cust_name,a.cust_idc,a.belong_branch_id,a.belong_branch_name,a.belong_outlet_name,a.belong_user_id,a.belong_user_name
        from cust_info_retail a inner join grid_region_admin_division_relate b on b.code like concat('',a.region_code , '%')
        where b.grid_id = #{gridId}
        <if test="custName != null  and custName != ''"> and a.cust_name like concat('%', #{custName}, '%') </if>
        <if test="custId != null  and custId != ''"> and a.cust_id like concat('%', #{custId}, '%') </if>
        group by a.cust_id
        union all
        select a.cust_id,a.cust_name,a.social_credit_code,a.belong_branch_id,a.belong_branch_name,a.belong_outlet_name,a.belong_user_id,a.belong_user_name
        from cust_info_business a inner join grid_region_admin_division_relate b on b.code like concat('',a.region_code , '%')
        where b.grid_id = #{gridId}
        <if test="custName != null  and custName != ''"> and a.cust_name like concat('%', #{custName}, '%') </if>
        <if test="custId != null  and custId != ''"> and a.cust_id like concat('%', #{custId}, '%') </if>
        group by a.cust_id
    </select>

    <select id="selectunDisByAdminList" >
        select a.cust_id,a.cust_name,a.cust_idc,a.belong_branch_id,a.belong_branch_name,a.belong_outlet_name,a.belong_user_id,a.belong_user_name
        from cust_info_retail a inner join grid_region_admin_division_relate b on b.code like concat('',a.region_code , '%')
        where b.grid_id = #{gridId} and a.belong_user_name = ''
        <if test="custName != null  and custName != ''"> and a.cust_name like concat('%', #{custName}, '%') </if>
        <if test="custId != null  and custId != ''"> and a.cust_id like concat('%', #{custId}, '%') </if>
        group by a.cust_id
        union all
        select a.cust_id,a.cust_name,a.social_credit_code,a.belong_branch_id,a.belong_branch_name,a.belong_outlet_name,a.belong_user_id,a.belong_user_name
        from cust_info_business a inner join grid_region_admin_division_relate b on b.code like concat('',a.region_code , '%')
        where b.grid_id = #{gridId} and a.belong_user_name = ''
        <if test="custName != null  and custName != ''"> and a.cust_name like concat('%', #{custName}, '%') </if>
        <if test="custId != null  and custId != ''"> and a.cust_id like concat('%', #{custId}, '%') </if>
        group by a.cust_id
    </select>

    <select id="getRetailNumByLegalId" parameterType="String" resultType="java.lang.Long">
        select count(*) from cust_info_retail
        where cust_idc = #{LegalId}
    </select>

    <update id="updateRetailByAnchor" parameterType="CustInfoUpdateFromAnchor">
        update cust_info_retail
        <trim prefix="SET" suffixOverrides=",">
            <if test="anchorName != null">cust_name = #{anchorName},</if>
            <if test="belongBusiness != null">belong_busi = #{belongBusiness},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateOrg != null">update_dept = #{updateOrg},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="address != null">${addressName} = #{address},</if>
            <if test="regionCode != null">region_code = #{regionCode},</if>
        </trim>
        where cust_idc = #{legalId}
    </update>

    <insert id="insertRetailByAnchor" parameterType="CustInfoUpdateFromAnchor" >
        insert into cust_info_retail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="custId != null">cust_id,</if>
            <if test="custIsn != null">cust_isn,</if>
            <if test="legalId != null">cust_idc,</if>
            <if test="anchorName != null">cust_name,</if>
            <if test="belongBusiness != null">belong_busi,</if>
            <if test="updateOrg != null">update_dept,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="address != null">${addressName},</if>
            <if test="regionCode != null">region_code,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="custId != null">#{custId},</if>
            <if test="custIsn != null">#{custIsn},</if>
            <if test="legalId != null">#{legalId},</if>
            <if test="anchorName != null">#{anchorName},</if>
            <if test="belongBusiness != null">#{belongBusiness},</if>
            <if test="updateOrg != null">#{updateOrg},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="address != null">#{address},</if>
            <if test="regionCode != null">#{regionCode},</if>
        </trim>
    </insert>

    <delete id="deleteRetailByAnchor" parameterType="CustInfoDeleteFromAnchor">
        update cust_info_retail set region_code ='' where cust_idc = #{legalId}
    </delete>

    <select id="getRetailIdcByCustId" parameterType="String">
        select distinct cust_idc from cust_info_retail
        where cust_id = #{custId}
    </select>

    <select id="selectRetailInfoByManage" parameterType="CustManageInfo"  resultMap="CustManageInfoResult">
        select distinct a.cust_id,a.cust_name,a.cust_idc,a.region_code,c.user_name,c.nick_name,'0' as cust_type from cust_info_retail a
        inner join (select distinct code,grid_id from grid_region_admin_division_relate where delete_flag = '0' ) b on
        b.code =a.region_code
        inner join (select distinct grid_id,relate_dept_id,user_name,nick_name from grid_region_user_relate where delete_flag ='0'
        and (relate_dept_id in (select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept},ancestors)) or user_name =#{userDept})) c
        on b.grid_id = c.grid_id
        where  c.grid_id = #{gridId}
        <if test="custName != null  and custName != ''"> and a.cust_name like concat('%', #{custName}, '%') </if>
        <if test="custId != null  and custId != ''"> and a.cust_id like concat('%', #{custId}, '%') </if>
        <if test="custNo != null  and custNo != ''"> and a.social_credit_code like concat('%', #{custNo}, '%') </if>
        <if test="distributeTag != null  and distributeTag != ''">
            and case when #{distributeTag} = '0' then exists (select * from sys_grid_user_cust d where d.grid_id = #{gridId} and a.cust_id =d.cust_id and c.user_name = d.user_name)
            when #{distributeTag} = '1' then not exists (select * from sys_grid_user_cust d where d.grid_id = #{gridId} and a.cust_id =d.cust_id and c.user_name = d.user_name)
            else 1=1 end
        </if>
    </select>

    <select id="selectRetailInfoByManage_COUNT" resultType="Long">
        select count(distinct a.cust_id) from cust_info_retail a
        inner join (select distinct code,grid_id from grid_region_admin_division_relate ) b on b.code =a.region_code
        inner join (select distinct grid_id,relate_dept_id,user_name,nick_name from grid_region_user_relate where delete_flag ='0'
        and (relate_dept_id in (select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept},ancestors)) or user_name =#{userDept})) c
        on b.grid_id = c.grid_id
        where  c.grid_id = #{gridId}
        <if test="custName != null  and custName != ''">and a.cust_name like concat('%', #{custName}, '%')</if>
        <if test="custId != null  and custId != ''">and a.cust_id like concat('%', #{custId}, '%')</if>
        <if test="custNo != null  and custNo != ''">and a.social_credit_code like concat('%', #{custNo}, '%')</if>
        <if test="distributeTag != null  and distributeTag != ''">
            and case when #{distributeTag} = '0' then exists (select * from sys_grid_user_cust d where d.grid_id =
            #{gridId} and a.cust_id =d.cust_id and c.user_name = d.user_name)
            when #{distributeTag} = '1' then not exists (select * from sys_grid_user_cust d where d.grid_id = #{gridId}
            and a.cust_id =d.cust_id and c.user_name = d.user_name)
            else 1=1 end
        </if>
    </select>

    <select id="selectNickName" parameterType="String" resultType="String">
        select user_name from cust_dept_user_grid
        <where>
            user_id is not null and user_name is not null
            <if test="deptId != null  and deptId != ''"> and dept_id in (select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId},ancestors))
                </if>
            <if test="custId != null  and custId != ''"> and cust_id = #{custId}</if>
            <if test="userName != null  and userName != ''"> and teller_id = #{userName}</if>
        </where>
    </select>

    <select id="selectGridInfo" parameterType="String" resultMap="GridInfoResult">
        select grid_name,grid_name2,concat_ws('-',user_name,teller_id) as user_name,ops_dept from cust_dept_user_grid
        <where>
            user_id is not null and user_name is not null
            <if test="deptId != null  and deptId != ''"> and dept_id in (select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId},ancestors))
            </if>
            <if test="custId != null  and custId != ''"> and cust_id = #{custId}</if>
            <if test="userName != null  and userName != ''"> and teller_id = #{userName}</if>
        </where>
    </select>

    <select id="selectCmpmNickName" parameterType="String" resultType="String">
        select CONCAT(nick_name, '-', user_name) as nick_name from cust_dept_user_cmpm
        <where>
            user_id is not null and user_name is not null
            <if test="deptId != null  and deptId != ''"> and dept_id in (select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId},ancestors))
            </if>
            <if test="custId != null  and custId != ''"> and cust_id = #{custId}</if>
            <if test="userName != null  and userName != ''"> and user_name = #{userName}</if>
        </where>
    </select>

    <select id="selectCmpmNickName2" parameterType="String" resultType="com.ruoyi.ibs.list.domain.CmpmUserList">
        select concat(code_value_level2,'客户经理') as cmpm_type,GROUP_CONCAT(CONCAT(nick_name, '-', user_name) SEPARATOR ',') as cmpm_user_list,code_no_level1,code_no_level2  from cust_dept_user_cmpm
        <where>
            user_id is not null and user_name is not null
            <if test="deptId != null  and deptId != ''"> and dept_id in (select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId},ancestors))</if>
            <if test="custId != null  and custId != ''"> and cust_id = #{custId}</if>
            <if test="userName != null  and userName != ''"> and user_name = #{userName}</if>
        </where>
        group by code_value_level2,code_no_level1,code_no_level2
    </select>

    <select id="countByCode" resultType="Long">
        select count(distinct cust_id)
        from cust_info_retail_${deptCode}
        where region_code like concat(#{code}, '%')
    </select>

    <select id="selectCommunityBycustId" resultType="String">
        select b.community from ibs_anchor a left join ibs_anchor_address b on a.address_id = b.id
        where cust_id = #{custId}
    </select>


    <insert id="insertCustomersToRetailByCustId" parameterType="java.util.List">
        INSERT INTO cust_info_retail
        (cust_id,cust_name, cust_idc, update_by, update_time, cust_phone, belong_busi, asset, credit)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.custId},#{item.custName}, #{item.custIdc}, #{item.createBy}, #{item.createTime}, #{item.custPhone},#{item.industry},#{item.asset},#{item.credit})
        </foreach>
    </insert>

    <select id="selectCustInfoRetailBycustIdcList" resultType="java.lang.String">
        select cust_idc from cust_info_retail where cust_idc in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="selectRecord" resultType="com.ruoyi.ibs.list.domain.CustInfoRetail">
        select id,cust_name from cust_info_retail where cust_idc = #{custIdc}
    </select>


    <delete id="deleteCustTagByCustId" >
        delete from sys_cust_tag where cust_id = #{custId}
    </delete>

    <delete id="deleteCustManualTagByCustId">
        delete from sys_cust_tag_manual where cust_id = #{custId}
    </delete>

    <insert id="insertCustTag" parameterType="CustTagDTO">
        insert into sys_cust_tag
        (cust_id, cust_tag_name, tag_id, create_by, create_time)
        VALUES
        <foreach collection="custTagList" item="item" separator=",">
            (#{custId},#{item.custTagName}, #{item.id}, #{createBy}, sysdate())
        </foreach>
    </insert>

    <insert id="insertCustManualTag" parameterType="CustManualTagDTO">
        insert into sys_cust_tag_manual
        (cust_id, cust_tag_name, tag_id, create_by, create_time)
        VALUES
        <foreach collection="custTagLight" item="item" separator=",">
            (#{custId}, #{item.custTagName}, #{item.id}, #{createBy}, sysdate())
        </foreach>
    </insert>

    <insert id="insertCustTagNemu" parameterType="map">
        insert into sys_cust_tag_nemu
        (cust_tag_name, dept_id, cust_tag_level1, cust_tag_level2, cust_tag_type, tag_type)
        VALUES
            (#{custTagName}, #{deptId}, #{custTagLevel1}, #{custTagLevel2}, #{custTagType}, '1')
    </insert>

<!--    <insert id="insertManualTag" parameterType="List">-->
<!--        insert into sys_tag_manual-->
<!--        (parent_id, cust_tag_name, cust_tag_type, dept_id, create_by, create_time)-->
<!--        values-->
<!--        <foreach collection="list" item="item" separator=",">-->
<!--            (#{item.parentId}, #{item.custTagName}, #{item.custTagType}, #{item.deptId}, #{item.createBy}, sysdate())-->
<!--        </foreach>-->
<!--    </insert>-->

    <insert id="insertManualTag" parameterType="CustManualTag">
        insert into sys_tag_manual
        (parent_id, cust_tag_name, cust_tag_type, dept_id, create_by, create_time)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.parentId}, #{item.custTagName}, #{item.custTagType}, #{item.deptId}, #{item.createBy}, sysdate())
        </foreach>
    </insert>

    <update id="updateManualTag" parameterType="List">
        <foreach collection="list" item="item" separator=";">
            update sys_tag_manual
            <set>
                <if test="item.custTagName != null and item.custTagName != ''">
                    cust_tag_name = #{item.custTagName},
                </if>
                <if test="item.parentId != null and item.parentId != ''">
                    parent_id = #{item.parentId},
                </if>
                <if test="item.updateBy != null and item.updateBy != ''">
                    update_by = #{item.updateBy},
                </if>
                update_time = #{item.updateTime}
            </set>
            where id = #{item.id}
        </foreach>
    </update>

    <select id="countByCustTagName" resultType="int">
        select count(1) from sys_cust_tag_nemu
        <where>
            <if test="custTagName != null and custTagName != ''">
                and cust_tag_name = #{custTagName}
            </if>
            <if test="deptId != null and deptId != ''">
                and dept_id = #{deptId}
            </if>
            <if test="custTagLevel1 != null and custTagLevel1 != ''">
                and cust_tag_level1 = #{custTagLevel1}
            </if>
            <if test="custTagLevel2 != null and custTagLevel2 != ''">
                and cust_tag_level2 = #{custTagLevel2}
            </if>
            <if test="custTagType != null and custTagType != ''">
                and cust_tag_type = #{custTagType}
            </if>
        </where>
    </select>

    <select id="selectManualTagByParentIdAndName" resultType="java.lang.Integer">
        select count(1)
        from sys_tag_manual
        where parent_id = #{parentId} AND cust_tag_name = #{custTagName}
    </select>

</mapper>