<?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.CustInfoMerchantMapper">
    
    <resultMap type="CustInfoMerchant" id="CustInfoMerchantResult">
        <result property="id"    column="id"    />
        <result property="custType"    column="cust_type"    />
        <result property="custTag"    column="cust_tag"    />
        <result property="custScale"    column="cust_scale"    />
        <result property="custName"    column="cust_name"    />
        <result property="lpName"    column="lp_name"    />
        <result property="custPhone"    column="cust_phone"    />
        <result property="custId"    column="cust_id"    />
        <result property="custIsn"    column="cust_isn"    />
        <result property="custCapital"    column="cust_capital"    />
        <result property="custLocation"    column="cust_location"    />
        <result property="loanTag"    column="loan_tag"    />
        <result property="status"    column="status"    />
        <result property="belongBranchId"    column="belong_branch_id"    />
        <result property="belongBranchName"    column="belong_branch_name"    />
        <result property="belongOutletId"    column="belong_outlet_id"    />
        <result property="belongOutletName"    column="belong_outlet_name"    />
        <result property="belongUserId"    column="belong_user_id"    />
        <result property="belongUserName"    column="belong_user_name"    />
        <result property="belongUserNameList"    column="belong_user_name_list"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="regionCode"    column="region_code"    />
        <result property="socialCreditCode"    column="social_credit_code"    />
        <result property="tel"    column="tel"    />
        <result property="registerLocation"    column="register_location"    />
        <result property="businessScope"    column="business_scope"    />
        <result property="taxpayerIdentificationNumber"    column="taxpayer_identification_number"    />
        <result property="taxpayerQualification"    column="taxpayer_qualification"    />
        <result property="label"    column="label"    />
        <result property="industry"    column="industry"    />
        <result property="custIdc"    column="cust_idc"    />
        <result property="birthday"    column="birthday"    />
        <result property="custAge"    column="cust_age"    />
        <result property="custGender"    column="cust_gender"    />
        <result property="recordStatus"    column="record_status"    />
    </resultMap>

    <resultMap type="CustInfoMerchant" id="CustInfoMerchantResult2">
        <result property="id"    column="id"    />
        <result property="custType"    column="cust_type"    />
        <result property="custTag"    column="cust_tag"    />
        <result property="custScale"    column="cust_scale"    />
        <result property="custName"    column="cust_name"    />
        <result property="lpName"    column="lp_name"    />
        <result property="custPhone"    column="cust_phone"    />
        <result property="custId"    column="cust_id"    />
        <result property="custIsn"    column="cust_isn"    />
        <result property="custCapital"    column="cust_capital"    />
        <result property="custLocation"    column="cust_location"    />
        <result property="loanTag"    column="loan_tag"    />
        <result property="status"    column="status"    />
        <result property="belongBranchId"    column="belong_branch_id"    />
        <result property="belongBranchName"    column="dept_name"    />
        <result property="belongOutletId"    column="belong_outlet_id"    />
        <result property="belongOutletName"    column="outlets"    />
        <result property="belongUserId"    column="belong_user_id"    />
        <result property="belongUserName"    column="belong_user_name"    />
        <result property="belongUserNameList"    column="belong_user_name_list"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="regionCode"    column="region_code"    />
        <result property="socialCreditCode"    column="social_credit_code"    />
        <result property="tel"    column="tel"    />
        <result property="registerLocation"    column="register_location"    />
        <result property="businessScope"    column="business_scope"    />
        <result property="taxpayerIdentificationNumber"    column="taxpayer_identification_number"    />
        <result property="taxpayerQualification"    column="taxpayer_qualification"    />
        <result property="label"    column="label"    />
        <result property="industry"    column="industry"    />
        <result property="custIdc"    column="cust_idc"    />
        <result property="birthday"    column="birthday"    />
        <result property="custAge"    column="cust_age"    />
        <result property="custGender"    column="cust_gender"    />
        <result property="actualController"    column="actual_controller"    />

    </resultMap>

    <resultMap type="CustManageInfo" id="CustManageInfoResult">
        <result property="custId"    column="cust_id"    />
        <result property="custName"    column="cust_name"    />
        <result property="custNo"    column="social_credit_code"    />
        <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>

    <sql id="selectCustInfoMerchantVo">
        select id, cust_type, cust_tag, cust_scale, cust_name, lp_name, cust_phone, cust_id, cust_isn, cust_capital, cust_location, loan_tag, status, belong_branch_id, belong_branch_name, belong_outlet_id, belong_outlet_name, belong_user_id, belong_user_name, create_by, create_time, update_by, update_time, region_code, social_credit_code, tel, register_location, business_scope, taxpayer_identification_number, taxpayer_qualification,
               label, industry, cust_idc, birthday,cust_age,cust_gender,record_status from cust_info_merchant
    </sql>

    <select id="selectCustInfoMerchantList" parameterType="CustInfoMerchant" resultMap="CustInfoMerchantResult">
        <include refid="selectCustInfoMerchantVo"/>
        <where>  
            <if test="custType != null  and custType != ''"> and cust_type = #{custType}</if>
            <if test="custTag != null  and custTag != ''"> and cust_tag = #{custTag}</if>
            <if test="custScale != null  and custScale != ''"> and cust_scale = #{custScale}</if>
            <if test="custName != null  and custName != ''"> and cust_name like concat('%', #{custName}, '%')</if>
            <if test="lpName != null  and lpName != ''"> and lp_name like concat('%', #{lpName}, '%')</if>
            <if test="custPhone != null  and custPhone != ''"> and cust_phone = #{custPhone}</if>
            <if test="custId != null  and custId != ''"> and cust_id = #{custId}</if>
            <if test="custIsn != null  and custIsn != ''"> and cust_isn = #{custIsn}</if>
            <if test="custCapital != null  and custCapital != ''"> and cust_capital = #{custCapital}</if>
            <if test="custLocation != null  and custLocation != ''"> and cust_location = #{custLocation}</if>
            <if test="loanTag != null  and loanTag != ''"> and loan_tag = #{loanTag}</if>
            <if test="status != null  and status != ''"> and status = #{status}</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="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="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="regionCode != null  and regionCode != ''"> and region_code = #{regionCode}</if>
            <if test="socialCreditCode != null  and socialCreditCode != ''"> and social_credit_code = #{socialCreditCode}</if>
            <if test="tel != null  and tel != ''"> and tel = #{tel}</if>
            <if test="registerLocation != null  and registerLocation != ''"> and register_location = #{registerLocation}</if>
            <if test="businessScope != null  and businessScope != ''"> and business_scope = #{businessScope}</if>
            <if test="taxpayerIdentificationNumber != null  and taxpayerIdentificationNumber != ''"> and taxpayer_identification_number = #{taxpayerIdentificationNumber}</if>
            <if test="taxpayerQualification != null  and taxpayerQualification != ''"> and taxpayer_qualification = #{taxpayerQualification}</if>
            <if test="label != null  and label != ''"> and label = #{label}</if>
            <if test="industry != null  and industry != ''"> and industry = #{industry}</if>
            <if test="custIdc != null  and custIdc != ''"> and cust_idc = #{custIdc}</if>
            <if test="birthday != null "> and birthday = #{birthday}</if>
            <if test="custAge != null  and custAge != ''"> and cust_age = #{custAge}</if>
            <if test="custGender != null  and custGender != ''"> and cust_gender = #{custGender}</if>
        </where>
    </select>

    <select id="selectCustInfoMerchantListByKeyword" parameterType="String" resultMap="CustInfoMerchantResult">
        <include refid="selectCustInfoMerchantVo"/>
        <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}, '%') or lp_name like concat('%', #{keyword}, '%') )
            </if>
        </where>
    </select>

    <select id="selectCustInfoMerchantListByKeywordBranchAndManager" parameterType="String" resultMap="CustInfoMerchantResult">
        select distinct a.id, a.cust_name, a.lp_name, a.cust_id,  a.social_credit_code,a.cust_phone,a.tel ,a.cust_idc
        from cust_info_merchant 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 = '1'
            <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}, '%') or a.lp_name like concat('%', #{keyword}, '%'))
            </if>
        </where>
    </select>

    
    <select id="selectCustInfoMerchantById" parameterType="Long" resultMap="CustInfoMerchantResult">
        <include refid="selectCustInfoMerchantVo"/>
        where id = #{id}
    </select>

    <insert id="insertCustInfoMerchant" parameterType="CustInfoMerchant" useGeneratedKeys="true" keyProperty="id">
        insert into cust_info_merchant
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="custType != null">cust_type,</if>
            <if test="custTag != null">cust_tag,</if>
            <if test="custScale != null">cust_scale,</if>
            <if test="custName != null">cust_name,</if>
            <if test="lpName != null">lp_name,</if>
            <if test="custPhone != null">cust_phone,</if>
            <if test="custId != null">cust_id,</if>
            <if test="custIsn != null">cust_isn,</if>
            <if test="custCapital != null">cust_capital,</if>
            <if test="custLocation != null">cust_location,</if>
            <if test="loanTag != null">loan_tag,</if>
            <if test="status != null">status,</if>
            <if test="belongBranchId != null">belong_branch_id,</if>
            <if test="belongBranchName != null">belong_branch_name,</if>
            <if test="belongOutletId != null">belong_outlet_id,</if>
            <if test="belongOutletName != null">belong_outlet_name,</if>
            <if test="belongUserId != null">belong_user_id,</if>
            <if test="belongUserName != null">belong_user_name,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="regionCode != null">region_code,</if>
            <if test="socialCreditCode != null">social_credit_code,</if>
            <if test="tel != null">tel,</if>
            <if test="registerLocation != null">register_location,</if>
            <if test="businessScope != null">business_scope,</if>
            <if test="taxpayerIdentificationNumber != null">taxpayer_identification_number,</if>
            <if test="taxpayerQualification != null">taxpayer_qualification,</if>
            <if test="label != null">label,</if>
            <if test="industry != null">industry,</if>
            <if test="custIdc != null">cust_idc,</if>
            <if test="birthday != null">birthday,</if>
            <if test="custAge != null">cust_age,</if>
            <if test="custGender != null">cust_gender,</if>
            <if test="recordStatus != null ">  record_status ,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="custType != null">#{custType},</if>
            <if test="custTag != null">#{custTag},</if>
            <if test="custScale != null">#{custScale},</if>
            <if test="custName != null">#{custName},</if>
            <if test="lpName != null">#{lpName},</if>
            <if test="custPhone != null">#{custPhone},</if>
            <if test="custId != null">#{custId},</if>
            <if test="custIsn != null">#{custIsn},</if>
            <if test="custCapital != null">#{custCapital},</if>
            <if test="custLocation != null">#{custLocation},</if>
            <if test="loanTag != null">#{loanTag},</if>
            <if test="status != null">#{status},</if>
            <if test="belongBranchId != null">#{belongBranchId},</if>
            <if test="belongBranchName != null">#{belongBranchName},</if>
            <if test="belongOutletId != null">#{belongOutletId},</if>
            <if test="belongOutletName != null">#{belongOutletName},</if>
            <if test="belongUserId != null">#{belongUserId},</if>
            <if test="belongUserName != null">#{belongUserName},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="regionCode != null">#{regionCode},</if>
            <if test="socialCreditCode != null">#{socialCreditCode},</if>
            <if test="tel != null">#{tel},</if>
            <if test="registerLocation != null">#{registerLocation},</if>
            <if test="businessScope != null">#{businessScope},</if>
            <if test="taxpayerIdentificationNumber != null">#{taxpayerIdentificationNumber},</if>
            <if test="taxpayerQualification != null">#{taxpayerQualification},</if>
            <if test="label != null">#{label},</if>
            <if test="industry != null">#{industry},</if>
            <if test="custIdc != null">#{custIdc},</if>
            <if test="birthday != null">#{birthday},</if>
            <if test="custAge != null">#{custAge},</if>
            <if test="custGender != null">#{custGender},</if>
            <if test="recordStatus != null">  #{recordStatus}</if>
         </trim>
    </insert>

    <update id="updateCustInfoMerchant" parameterType="CustInfoMerchant">
        update cust_info_merchant
        <trim prefix="SET" suffixOverrides=",">
            <if test="custType != null">cust_type = #{custType},</if>
            <if test="custTag != null">cust_tag = #{custTag},</if>
            <if test="custScale != null">cust_scale = #{custScale},</if>
            <if test="custName != null">cust_name = #{custName},</if>
            <if test="lpName != null">lp_name = #{lpName},</if>
            <if test="custPhone != null">cust_phone = #{custPhone},</if>
            <if test="custId != null">cust_id = #{custId},</if>
            <if test="custIsn != null">cust_isn = #{custIsn},</if>
            <if test="custCapital != null">cust_capital = #{custCapital},</if>
            <if test="custLocation != null">cust_location = #{custLocation},</if>
            <if test="loanTag != null">loan_tag = #{loanTag},</if>
            <if test="status != null">status = #{status},</if>
            <if test="belongBranchId != null">belong_branch_id = #{belongBranchId},</if>
            <if test="belongBranchName != null">belong_branch_name = #{belongBranchName},</if>
            <if test="belongOutletId != null">belong_outlet_id = #{belongOutletId},</if>
            <if test="belongOutletName != null">belong_outlet_name = #{belongOutletName},</if>
            <if test="belongUserId != null">belong_user_id = #{belongUserId},</if>
            <if test="belongUserName != null">belong_user_name = #{belongUserName},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="regionCode != null">region_code = #{regionCode},</if>
            <if test="socialCreditCode != null">social_credit_code = #{socialCreditCode},</if>
            <if test="tel != null">tel = #{tel},</if>
            <if test="registerLocation != null">register_location = #{registerLocation},</if>
            <if test="businessScope != null">business_scope = #{businessScope},</if>
            <if test="taxpayerIdentificationNumber != null">taxpayer_identification_number = #{taxpayerIdentificationNumber},</if>
            <if test="taxpayerQualification != null">taxpayer_qualification = #{taxpayerQualification},</if>
            <if test="label != null">label = #{label},</if>
            <if test="industry != null">industry = #{industry},</if>
            <if test="custIdc != null">cust_idc = #{custIdc},</if>
            <if test="birthday != null">birthday = #{birthday},</if>
            <if test="custAge != null">cust_age = #{custAge},</if>
            <if test="custGender != null">cust_gender = #{custGender},</if>
            <if test="recordStatus != null  and recordStatus != ''"> record_status = #{recordStatus},</if>
            <if test="actualController != null">actual_controller = #{actualController},</if>
        </trim>
        where id = #{id}
    </update>


    <update id="updateCustInfoMerchantLabel" parameterType="CustInfoMerchant">
        update cust_info_merchant
        <trim prefix="SET" suffixOverrides=",">
            <if test="custType != null and custType != ''">cust_type = #{custType},</if>
            <if test="custTag != null and custTag != ''">cust_tag = #{custTag},</if>
            <if test="custScale != null and custScale != ''">cust_scale = #{custScale},</if>
            <if test="custName != null and custName != ''">cust_name = #{custName},</if>
            <if test="lpName != null and lpName != ''">lp_name = #{lpName},</if>
            <if test="custPhone != null and custPhone != ''">cust_phone = #{custPhone},</if>
            <if test="custId != null and custId != ''">cust_id = #{custId},</if>
            <if test="custIsn != null and custIsn != ''">cust_isn = #{custIsn},</if>
            <if test="custCapital != null and custCapital != ''">cust_capital = #{custCapital},</if>
            <if test="custLocation != null and custLocation != ''">cust_location = #{custLocation},</if>
            <if test="loanTag != null and loanTag != ''">loan_tag = #{loanTag},</if>
            <if test="status != null and status != ''">status = #{status},</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="belongOutletId != null and belongOutletId != ''">belong_outlet_id = #{belongOutletId},</if>
            <if test="belongOutletName != null and belongOutletName != ''">belong_outlet_name = #{belongOutletName},</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="createBy != null and createBy != ''">create_by = #{createBy},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="regionCode != null and regionCode != ''">region_code = #{regionCode},</if>
            <if test="socialCreditCode != null and socialCreditCode != ''">social_credit_code = #{socialCreditCode},</if>
            <if test="tel != null and tel != ''">tel = #{tel},</if>
            <if test="registerLocation != null and registerLocation != ''">register_location = #{registerLocation},</if>
            <if test="businessScope != null and businessScope != ''">business_scope = #{businessScope},</if>
            <if test="taxpayerIdentificationNumber != null and taxpayerIdentificationNumber != ''">taxpayer_identification_number = #{taxpayerIdentificationNumber},</if>
            <if test="taxpayerQualification != null and taxpayerQualification != ''">taxpayer_qualification = #{taxpayerQualification},</if>
            <if test="label != null and label != ''">label = #{label},</if>
            <if test="industry != null and industry != ''">industry = #{industry},</if>
            <if test="custIdc != null and custIdc != ''">cust_idc = #{custIdc},</if>
            <if test="custAge != null and custAge != ''">cust_age = #{custAge},</if>
            <if test="custGender != null and custGender != ''">cust_gender = #{custGender},</if>
            <if test="recordStatus != null  and recordStatus != ''"> record_status = #{recordStatus},</if>
            <if test="birthday != null ">birthday = #{birthday},</if>
            <if test="actualController != null  and actualController != ''">actual_controller = #{actualController},</if>
        </trim>
        where cust_id = #{custId}
    </update>

    <delete id="deleteCustInfoMerchantById" parameterType="Long">
        delete from cust_info_merchant where id = #{id}
    </delete>

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

    <select id="selectCustInfoMerchantByCustId" parameterType="String" resultMap="CustInfoMerchantResult">
        select * from cust_info_merchant
        where cust_id = #{custId}
    </select>

    <select id="selectCustInfoMerchantBySocialCreditCode" parameterType="String" resultMap="CustInfoMerchantResult">
        select * from cust_info_merchant
        where social_credit_code = #{socialCreditCode}
    </select>



    <select id="selectMyCustInfoMerchantList" parameterType="CustInfoBusiness" resultMap="CustInfoMerchantResult2">
        SELECT a.id, a.cust_type, a.cust_tag, a.cust_scale, a.cust_name, a.lp_name,
        a.cust_phone, a.cust_id, a.cust_isn,
        GROUP_CONCAT(b.dept_name SEPARATOR ',') as dept_name,
        GROUP_CONCAT(b.outlets SEPARATOR ',') as outlets,
        a.cust_idc,
        GROUP_CONCAT(b.user_name SEPARATOR ',') as belong_user_name_list
        from cust_info_merchant a
        <if test="opsDept != null  and opsDept != ''">
            inner </if>
        <if test="opsDept == null  or opsDept == ''">
            left </if> join
        (select user_name,user_id,dept_id,cust_id,dept_name,outlets from
        (select concat_ws('-',user_name,teller_id) as user_name,user_id,dept_id,cust_id,dept_name,outlets 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 = '1' and t2.dept_id is not null)) f
        where 1=1
        <if test = "isHead == false">
            ${params.dataScope}
        </if>
        ) b on a.cust_id = b.cust_id
        <where>
            <if test = "isHead == false">
                b.user_id>0
            </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="custType != null  and custType != ''"> and a.cust_type = #{custType}</if>
        </where>
        group by a.cust_id,a.id
        limit #{start},#{size}
    </select>

    <select id="selectMyCustInfoMerchantListHead" parameterType="CustInfoBusiness" resultType="com.ruoyi.ibs.customerselect.domain.CustBaseInfo">
        SELECT a.id, a.cust_type, a.cust_tag, a.cust_scale, a.cust_name, a.lp_name,
        a.cust_phone, a.cust_id, a.cust_isn,
        a.cust_idc,
        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.cust_gender, a.social_credit_code, a.cust_location, a.status, a.cust_level
        from cust_info_merchant 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>
            <if test="searchStr != null  and searchStr != ''"> and (a.cust_id like concat('%', #{searchStr}, '%') or a.cust_name like concat('%', #{searchStr}, '%')) </if>
        </where>
        group by a.cust_id,a.id
        limit #{start},#{size}
    </select>


    <select id="selectMyCustInfoMerchantListOps" parameterType="CustInfoBusiness" resultType="com.ruoyi.ibs.customerselect.domain.CustBaseInfo">
        SELECT distinct a.id, a.cust_type, a.cust_tag, a.cust_scale, a.cust_name, a.lp_name,
        a.cust_phone, a.cust_id, a.cust_isn,
        a.cust_idc,
        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.cust_gender, a.social_credit_code, a.cust_location, a.status, a.cust_level
        from cust_info_merchant a
        <if test="isSelectGrid == false">
            inner  join
            (select user_id,dept_id,cust_id from
            (select user_id,dept_id,cust_id,outlets_id from cust_dept_user_grid
            <where>
                <if test="opsDept != null  and opsDept != ''">
                    ops_dept = #{opsDept}</if>
            </where>
            union all
            select t2.user_id,t2.dept_id,t2.cust_id,t2.outlets_id from cust_dept_user_cmpm t2 where 1=1 and t2.cust_type = '1'
             )f
            where 1=1
            <if test="deptId != null ">
                and f.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 f.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 f.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>
                    <if test="userName != null  ">
                        and  gvcu.user_name = #{userName}
                    </if>
                </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>
            <if test="searchStr != null  and searchStr != ''"> and (a.cust_id like concat('%', #{searchStr}, '%') or a.cust_name like concat('%', #{searchStr}, '%')) </if>
        </where>
        group by a.cust_id,a.id
        limit #{start},#{size}
    </select>
    <select id="selectMyCustInfoMerchantList_COUNT" resultType="Long">
        select -1
    </select>
    <select id="getMerchantNumByDeptId" parameterType="Long" resultType="java.lang.Long">
        select count(distinct a.cust_id) from cust_info_merchant 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="getMerchantNumByUserId" parameterType="String" resultType="java.lang.Long">
        select count(distinct a.cust_id) from cust_info_merchant 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="getMerchantNumByLegalId" parameterType="String" resultType="java.lang.Long">
        select count(*) from cust_info_merchant
        where social_credit_code = #{LegalId}
    </select>

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

    <insert id="insertMerchantByAnchor" parameterType="CustInfoUpdateFromAnchor" >
        insert into cust_info_merchant
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="custId != null">cust_id,</if>
            <if test="custIsn != null">cust_isn,</if>
            <if test="legalId != null">social_credit_code,</if>
            <if test="anchorName != null">cust_name,</if>
            <if test="belongBusiness != null">industry,</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="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="deleteMerchantByAnchor" parameterType="CustInfoDeleteFromAnchor">
        update cust_info_merchant set region_code ='' where social_credit_code = #{legalId}
    </delete>

    <select id="selectPosNumByAdmin" parameterType="Long" resultType="java.lang.Long">
        select count(distinct a.cust_id) from (select cust_id,region_code from cust_info_merchant) 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="getLpNameByCustId" >
        select distinct lp_name from cust_info_merchant
        where cust_id = #{custId}
    </select>

    <select id="selectCustInfoMerchatByCustNameAndCustIdc" parameterType="String" resultMap="CustInfoMerchantResult">
        <include refid="selectCustInfoMerchantVo"/>
        <where>
            <if test="custName != null  and custName != ''"> and cust_name = #{custName}</if>
            <if test="custIdc != null  and custIdc != ''"> and cust_idc = #{custIdc}</if>
        </where>
    </select>

    <select id="selectCustInfoMerchatByCustId" parameterType="String" resultMap="CustInfoMerchantResult">
        <include refid="selectCustInfoMerchantVo"/>
        <where>
            <if test="custId != null "> and cust_id = #{custId}</if>
        </where>
    </select>

    <select id="getMerchantIdcByCustId" parameterType="String">
        select distinct social_credit_code from cust_info_merchant
        where cust_id = #{custId}
    </select>

    <select id="selectMerchantInfoByManage" parameterType="CustManageInfo"  resultMap="CustManageInfoResult">
        select distinct a.cust_id,a.cust_name,a.social_credit_code,a.region_code,c.user_name,c.nick_name,'1' as cust_type from cust_info_merchant 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.cust_idc 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="countByCode" resultType="Long">
        select count(distinct cust_id)
        from cust_info_merchant_${deptCode}
        where region_code like concat(#{code}, '%')
    </select>

    <insert id="insertCustomersToMmerchantByScCode" parameterType="java.util.List">
        INSERT INTO cust_info_merchant
        (cust_id,cust_name, social_credit_code, update_by, update_time, cust_phone)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.custId},#{item.custName}, #{item.socialCreditCode}, #{item.createBy}, #{item.createTime}, #{item.custPhone})
        </foreach>
    </insert>

    <select id="selectCustInfoByScCodeList" resultType="java.lang.String">
        select social_credit_code from
            <if test="custType == 1">cust_info_merchant</if>
            <if test="custType == 2">cust_info_business</if>
        where social_credit_code in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
	<select id="selectRecord" resultType="com.ruoyi.ibs.list.domain.CustInfoMerchant">
        select id,cust_name from cust_info_merchant where social_credit_code = #{socialCreditCode}
    </select>

</mapper>