<?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="cn.cordys.crm.customer.mapper.ExtCustomerMapper">
    <update id="batchTransfer">
        update customer
        set owner = #{request.owner},
            customer.collection_time = ${@java.lang.System@currentTimeMillis()},
            customer.update_time = ${@java.lang.System@currentTimeMillis()}, customer.update_user = #{userId}
        where owner != #{request.owner} and id in
        <foreach collection="request.ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

    <select id="list" resultType="cn.cordys.crm.customer.dto.response.CustomerListResponse">
        select c.id, c.`name`, if(c.in_shared_pool, null, c.owner) as owner, c.create_time, c.update_time, c.create_user, c.update_user, c.collection_time, c.pool_id,
               c.follower, c.follow_time, c.reason_id
                <if test="dataPermission != null and dataPermission.visible">
                    ,cc.collaboration_type
                </if>
        from customer c

        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.filters"/>
            <property name="tablePrefix" value="filter"/>
        </include>
        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.combineSearch.conditions"/>
            <property name="tablePrefix" value="combine"/>
        </include>
        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.viewCombineSearch.conditions"/>
            <property name="tablePrefix" value="viewCombine"/>
        </include>
        <include refid="fieldSortJoin">
            <property name="sort" value="request.sort"/>
        </include>

        <if test="dataPermission != null and dataPermission.deptIds.size() > 0">
            join sys_organization_user sou on c.owner = sou.user_id
            and (
                sou.department_id in
                <foreach collection="dataPermission.deptIds" item="deptId" open="(" close=")" separator=",">
                    #{deptId}
                </foreach>
                <if test="dataPermission.viewId == 'ALL'">
                    or sou.user_id = #{userId}
                </if>
            )
        </if>

        <if test="dataPermission != null and dataPermission.visible">
            join customer_collaboration cc on c.id = cc.customer_id
            and cc.user_id = #{userId}
        </if>

        <if test="request.transition and !request.transitionDataPermission.all">
            join (
                select c.id from customer c
                left join customer_collaboration cc on c.id = cc.customer_id
                left join sys_organization_user sou on c.owner = sou.user_id
                where cc.user_id = #{userId}
                <if test="request.transitionDataPermission != null and request.transitionDataPermission.deptIds.size() > 0">
                    or sou.department_id in
                    <foreach collection="request.transitionDataPermission.deptIds" item="deptId" open="(" close=")" separator=",">
                        #{deptId}
                    </foreach>
                </if>
                <if test="request.transitionDataPermission != null and request.transitionDataPermission.self">
                    or c.owner = #{userId}
                </if>
                <if test="request.transitionPoolIds != null and request.transitionPoolIds.size()> 0">
                    or (
                    c.in_shared_pool is true and c.pool_id in
                    <foreach collection="request.transitionPoolIds" item="poolId" open="(" close=")" separator=",">
                        #{poolId}
                    </foreach>
                    )
                </if>
                group by c.id
            ) t on c.id = t.id
        </if>

        where c.organization_id = #{orgId}

        <if test="request.poolId == null and !request.transition">
            and c.in_shared_pool is false
        </if>

        <if test="request.poolId != null and !request.transition">
            and c.in_shared_pool is true and c.pool_id = #{request.poolId}
        </if>

        <if test="dataPermission != null and dataPermission.self">
            and c.owner = #{userId}
        </if>

        <if test="request.keyword != null and request.keyword != ''">
            and c.name like concat('%', #{request.keyword},'%')
        </if>

        <include refid="filters">
            <property name="filters" value="request.filters"/>
        </include>

        <include refid="combine">
            <property name="combineSearch" value="request.combineSearch"/>
        </include>
        <include refid="viewCombine">
            <property name="combineSearch" value="request.viewCombineSearch"/>
        </include>

        <include refid="sort">
            <property name="sort" value="request.sort"/>
        </include>
    </select>

    <select id="chart" resultType="cn.cordys.common.dto.chart.ChartResult">
        select
        <include refid="cn.cordys.common.mapper.CommonMapper.chartSelect">
            <property name="mainTable" value="c"/>
        </include>

        from customer c

        <if test="dataPermission != null and dataPermission.deptIds.size() > 0">
            join sys_organization_user sou on c.owner = sou.user_id
            and (
            sou.department_id in
            <foreach collection="dataPermission.deptIds" item="deptId" open="(" close=")" separator=",">
                #{deptId}
            </foreach>
            or sou.user_id = #{userId}
            )
        </if>

        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.filterCondition.conditions"/>
            <property name="tablePrefix" value="combine"/>
        </include>
        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.viewFilterCondition.conditions"/>
            <property name="tablePrefix" value="viewCombine"/>
        </include>

        <include refid="cn.cordys.common.mapper.CommonMapper.chartAxisJoin">
            <property name="axisParam" value="request.categoryAxisParam"/>
            <property name="tablePrefix" value="categoryAxis"/>
            <property name="mainTable" value="customer"/>
            <property name="mainTablePrefix" value="c"/>
        </include>
        <include refid="cn.cordys.common.mapper.CommonMapper.chartAxisJoin">
            <property name="axisParam" value="request.valueAxisParam"/>
            <property name="tablePrefix" value="valueAxis"/>
            <property name="mainTable" value="customer"/>
            <property name="mainTablePrefix" value="c"/>
        </include>
        <if test="request.subCategoryAxisParam != null">
            <include refid="cn.cordys.common.mapper.CommonMapper.chartAxisJoin">
                <property name="axisParam" value="request.subCategoryAxisParam"/>
                <property name="tablePrefix" value="subCategoryAxis"/>
                <property name="mainTable" value="customer"/>
                <property name="mainTablePrefix" value="c"/>
            </include>
        </if>

        <if test="dataPermission != null and dataPermission.visible">
            join customer_collaboration cc on c.id = cc.customer_id
            and cc.user_id = #{userId}
        </if>

        where c.organization_id = #{orgId}

        <if test="request.poolId == null or request.poolId == ''">
            and c.in_shared_pool is false
        </if>

        <if test="request.poolId != null and request.poolId != ''">
            and c.in_shared_pool is true and c.pool_id = #{request.poolId}
        </if>

        <if test="dataPermission != null and dataPermission.self">
            and c.owner = #{userId}
        </if>

        <include refid="combine">
            <property name="combineSearch" value="request.filterCondition"/>
        </include>
        <include refid="viewCombine">
            <property name="combineSearch" value="request.viewFilterCondition"/>
        </include>

        <include refid="cn.cordys.common.mapper.CommonMapper.chartGroupBy">
            <property name="mainTable" value="c"/>
        </include>
    </select>

    <select id="sourceList" resultType="cn.cordys.crm.customer.dto.response.CustomerListResponse">
        select distinct c.id, c.`name`, if(c.in_shared_pool, null, c.owner) as owner, c.create_time, c.update_time, c.create_user, c.update_user, c.collection_time, c.pool_id,
        c.follower, c.follow_time
        from customer c left join customer_collaboration cc on c.id = cc.customer_id and cc.user_id = #{userId}

        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.filters"/>
            <property name="tablePrefix" value="filter"/>
        </include>
        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.combineSearch.conditions"/>
            <property name="tablePrefix" value="combine"/>
        </include>
        <include refid="fieldSortJoin">
            <property name="sort" value="request.sort"/>
        </include>

        <if test="dataPermission != null and dataPermission.deptIds.size() > 0">
            join sys_organization_user sou on c.owner = sou.user_id
            and ( sou.department_id in
                <foreach collection="dataPermission.deptIds" item="deptId" open="(" close=")" separator=",">
                    #{deptId}
                </foreach>
                <if test="dataPermission.viewId == 'ALL'">
                    or sou.user_id = #{userId}
                </if>
                or cc.id is not null
            )
        </if>

        where c.organization_id = #{orgId} and c.in_shared_pool is false

        <if test="dataPermission != null and dataPermission.self">
            and (c.owner = #{userId} or cc.id is not null)
        </if>

        <if test="request.keyword != null and request.keyword != ''">
            and c.name like concat('%', #{request.keyword},'%')
        </if>

        <include refid="filters">
            <property name="filters" value="request.filters"/>
        </include>

        <include refid="combine">
            <property name="combineSearch" value="request.combineSearch"/>
        </include>

        <include refid="sort">
            <property name="sort" value="request.sort"/>
        </include>
    </select>

    <sql id="sort">
        <choose>
            <when test="${sort} != null and ${sort}.valid()">
                <bind name="sortType" value="${sort}.type" />
                <bind name="sortName" value="${sort}.name" />
                order by
                <choose>
                    <when test="${sort}.name in
                        {'name', 'owner', 'create_time', 'update_time', 'follower', 'follow_time', 'create_user', 'update_user', 'collection_time', 'reason_id'}">
                        c.${sortName} ${sortType}, c.id ASC
                    </when>
                    <when test="${sort}.name == 'department_id'">
                        sou_sort.${sortName} ${sortType}, c.id ASC
                    </when>
                    <otherwise>
                        CASE
                        WHEN module_table.type = 'INPUT_NUMBER'
                        THEN sort_table.field_value+0
                        END ${sortType},
                        sort_table.field_value ${sortType},
                        c.id ASC
                    </otherwise>
                </choose>
            </when>
            <otherwise>
                order by c.create_time desc, c.id asc
            </otherwise>
        </choose>
    </sql>

    <sql id="fieldConditionJoin">
        <if test="${conditions} != null and ${conditions}.size() > 0">
            <foreach collection="${conditions}" item="condition" index="i">
                <if test="condition.name not in
                {'name', 'owner', 'createTime', 'updateTime', 'follower', 'followTime', 'createUser', 'updateUser', 'departmentId'}">
                    <if test="condition.multipleValue">
                        left join customer_field_blob ${tablePrefix}_${i}
                        on c.id = ${tablePrefix}_${i}.resource_id and ${tablePrefix}_${i}.field_id = #{condition.name}
                    </if>
                    <if test="!condition.multipleValue">
                        left join customer_field ${tablePrefix}_${i}
                        on c.id = ${tablePrefix}_${i}.resource_id and ${tablePrefix}_${i}.field_id = #{condition.name}
                    </if>
                </if>
                <if test="condition.name == 'departmentId'">
                    left join sys_organization_user ${tablePrefix}_${i}
                    on ${tablePrefix}_${i}.user_id = c.owner
                </if>
            </foreach>
        </if>
    </sql>

    <sql id="fieldSortJoin">
        <if test="${sort} != null and ${sort}.valid()">
            <bind name="sortName" value="${sort}.name"/>
            <choose>
                <when test="${sort}.name == 'department_id'">
                    left join sys_organization_user sou_sort on c.owner = sou_sort.user_id
                </when>
                <when test="${sort}.name not in
                    {'name', 'owner', 'create_time', 'update_time', 'follower', 'follow_time', 'create_user', 'update_user', 'collection_time', 'reason_id'}">
                    left join customer_field sort_table
                    on c.id = sort_table.resource_id and sort_table.field_id = #{sortName}
                    left join sys_module_field module_table on sort_table.field_id = module_table.id
                </when>
            </choose>
        </if>
    </sql>

    <sql id="filters">
        <if test="${filters} != null and ${filters}.size() > 0">
            <trim prefix="and">
                <foreach collection="${filters}" item="condition" open="(" close=")" index="i" separator="and">
                    <include refid="condition">
                        <property name="condition" value="condition"/>
                        <property name="fieldTable" value="filter_${i}.field_value"/>
                    </include>
                </foreach>
            </trim>
        </if>
    </sql>

    <sql id="combine">
        <if test="${combineSearch} != null and ${combineSearch}.conditions != null and ${combineSearch}.conditions.size() > 0">
            <trim prefix="and">
                <trim prefix="(" suffix=")" suffixOverrides="and|or">
                    <foreach collection="${combineSearch}.conditions" index="i" item="condition">
                        <include refid="condition">
                            <property name="condition" value="condition"/>
                            <property name="fieldTable" value="combine_${i}.field_value"/>
                            <property name="table" value="combine_${i}"/>
                        </include>
                        <include refid="cn.cordys.common.mapper.CommonMapper.searchMode">
                            <property name="searchMode" value="${combineSearch}.searchMode"/>
                        </include>
                    </foreach>
                </trim>
            </trim>
        </if>
    </sql>

    <sql id="viewCombine">
        <if test="${combineSearch} != null and ${combineSearch}.conditions != null and ${combineSearch}.conditions.size() > 0">
            <trim prefix="and">
                <trim prefix="(" suffix=")" suffixOverrides="and|or">
                    <foreach collection="${combineSearch}.conditions" index="i" item="condition">
                        <include refid="condition">
                            <property name="condition" value="condition"/>
                            <property name="fieldTable" value="viewCombine_${i}.field_value"/>
                            <property name="table" value="viewCombine_${i}"/>
                        </include>
                        <include refid="cn.cordys.common.mapper.CommonMapper.searchMode">
                            <property name="searchMode" value="${combineSearch}.searchMode"/>
                        </include>
                    </foreach>
                </trim>
            </trim>
        </if>
    </sql>

    <sql id="condition">
        <choose>
            <when test="condition.name == 'name'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="c.name"/>
                </include>
            </when>
            <when test="condition.name == 'owner'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="c.owner"/>
                </include>
            </when>
            <when test="condition.name == 'follower'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="c.follower"/>
                </include>
            </when>
            <when test="condition.name == 'followTime'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="c.follow_time"/>
                </include>
            </when>
            <when test="condition.name == 'createTime'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="c.create_time"/>
                </include>
            </when>
            <when test="condition.name == 'updateTime'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="c.update_time"/>
                </include>
            </when>
            <when test="condition.name == 'createUser'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="c.create_user"/>
                </include>
            </when>
            <when test="condition.name == 'updateUser'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="c.update_user"/>
                </include>
            </when>
            <when test="condition.name == 'departmentId'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="${table}.department_id"/>
                </include>
            </when>
            <when test="condition.name == 'reasonId'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="c.reason_id"/>
                </include>
            </when>
            <otherwise>
                <include refid="cn.cordys.common.mapper.CommonMapper.moduleFieldCondition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="${fieldTable}"/>
                </include>
            </otherwise>
        </choose>
    </sql>

    <select id="selectOptionByIds" resultType="cn.cordys.common.dto.OptionDTO">
        select id, name
        from customer
        where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="checkRepeatCustomer" resultType="cn.cordys.crm.search.response.advanced.AdvancedCustomerResponse">
        select c.id, c.`name`, if(c.in_shared_pool, null, c.owner) as owner, c.create_time, c.update_time, c.create_user, c.update_user, c.collection_time, c.pool_id,
        c.follower, c.follow_time, c.reason_id
        from customer c

        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.filters"/>
            <property name="tablePrefix" value="filter"/>
        </include>
        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.combineSearch.conditions"/>
            <property name="tablePrefix" value="combine"/>
        </include>
        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.viewCombineSearch.conditions"/>
            <property name="tablePrefix" value="viewCombine"/>
        </include>
        <include refid="fieldSortJoin">
            <property name="sort" value="request.sort"/>
        </include>

        where c.organization_id = #{orgId}

        and c.in_shared_pool is false

        <if test="request.keyword != null and request.keyword != ''">
            and c.name like concat('%', #{request.keyword},'%')
        </if>

        <include refid="filters">
            <property name="filters" value="request.filters"/>
        </include>

        <include refid="combine">
            <property name="combineSearch" value="request.combineSearch"/>
        </include>
        <include refid="viewCombine">
            <property name="combineSearch" value="request.viewCombineSearch"/>
        </include>

        <include refid="sort">
            <property name="sort" value="request.sort"/>
        </include>
    </select>


    <select id="countByOwner" resultType="java.lang.Integer">
        select count(id) from customer
        where owner = #{owner}
    </select>

    <select id="getCustomerOptions" resultType="cn.cordys.common.dto.OptionDTO">
        select id, name
        from customer
        where
        organization_id = #{orgId}
        <if test="keyword != null and keyword != ''">
            and name like concat('%', #{keyword},'%')
        </if>
    </select>
    <select id="getCustomerOptionsByIds" resultType="cn.cordys.common.dto.OptionDTO">
        select id, name
        from customer
        where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <update id="moveToPool" parameterType="cn.cordys.crm.customer.domain.Customer">
        update customer set pool_id = #{customer.poolId}, in_shared_pool = #{customer.inSharedPool}, owner = #{customer.owner}, reason_id = #{customer.reasonId},
                            collection_time = #{customer.collectionTime}, update_time = #{customer.updateTime}, update_user = #{customer.updateUser}
        where id = #{customer.id}
    </update>

    <select id="hasRefOpportunity" resultType="java.lang.Boolean">
        select count(1) from opportunity where customer_id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="hasRefContact" resultType="java.lang.Boolean">
        select count(1) from customer_contact where customer_id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="filterOwnerCount" resultType="java.lang.Long">
        select count(distinct c.id) from customer c
        <if test="filters != null and filters.size > 0">
            join opportunity o on c.id = o.customer_id and
            <foreach collection="filters" item="filter" open="(" separator=" and " close=")">
                <if test="filter.column == 'stage'">
                    <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                        <property name="condition" value="filter"/>
                        <property name="column" value="o.stage"/>
                    </include>
                </if>
            </foreach>
        </if>
        where c.owner = #{ownerId}
    </select>

    <select id="getListByIds" resultType="cn.cordys.crm.customer.dto.response.CustomerListResponse">
        select c.id, c.`name`, c.owner, c.create_time, c.update_time, c.create_user, c.update_user, c.collection_time, c.pool_id,
               c.follower, c.follow_time, c.in_shared_pool, c.organization_id, c.reason_id
        from customer c
        where c.id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        order by c.create_time desc
    </select>
    <select id="selectCustomerCount" resultType="java.lang.Long">
        select count(*) from customer c

        <if test="request.dataPermission != null and request.dataPermission.deptIds.size() > 0">
            join sys_organization_user sou on c.owner = sou.user_id
            and (
            sou.department_id in
                <foreach collection="request.dataPermission.deptIds" item="deptId" open="(" close=")" separator=",">
                    #{deptId}
                </foreach>
                <if test="request.dataPermission.viewId == 'ALL'">
                    or sou.user_id = #{request.userId}
                </if>
            )
        </if>

        <if test="unfollowed">
            LEFT JOIN follow_up_record fur on c.id = fur.customer_id
        </if>

        where c.organization_id = #{request.orgId} and c.in_shared_pool is false
        <if test="unfollowed">
            and fur.id is null
        </if>
        <if test="request.startTime != null and request.endTime != null">
            and c.create_time &gt;= #{request.startTime} and c.create_time &lt;= #{request.endTime}
        </if>

        <if test="request.dataPermission != null and request.dataPermission.self">
            and c.owner = #{request.userId}
        </if>
    </select>


    <select id="customerPoolList" resultType="cn.cordys.crm.search.response.advanced.AdvancedCustomerPoolResponse">
        select c.id, c.`name`, if(c.in_shared_pool, null, c.owner) as owner, c.create_time, c.update_time, c.create_user, c.update_user, c.collection_time, c.pool_id,
        c.follower, c.follow_time, c.reason_id
        from customer c

        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.filters"/>
            <property name="tablePrefix" value="filter"/>
        </include>
        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.combineSearch.conditions"/>
            <property name="tablePrefix" value="combine"/>
        </include>
        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.viewCombineSearch.conditions"/>
            <property name="tablePrefix" value="viewCombine"/>
        </include>
        <include refid="fieldSortJoin">
            <property name="sort" value="request.sort"/>
        </include>

        where c.organization_id = #{orgId}
        and c.in_shared_pool is true

        <if test="request.keyword != null and request.keyword != ''">
            and c.name like concat('%', #{request.keyword},'%')
        </if>

        <include refid="filters">
            <property name="filters" value="request.filters"/>
        </include>

        <include refid="combine">
            <property name="combineSearch" value="request.combineSearch"/>
        </include>
        <include refid="viewCombine">
            <property name="combineSearch" value="request.viewCombineSearch"/>
        </include>

        <include refid="sort">
            <property name="sort" value="request.sort"/>
        </include>
    </select>
    <select id="globalPoolSearchList" resultType="cn.cordys.crm.search.response.global.GlobalCustomerPoolResponse">
        select c.id, c.`name`, c.pool_id, cp.name as poolName, c.follow_time
        from customer c inner join customer_pool cp on c.pool_id = cp.id
        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.combineSearch.conditions"/>
            <property name="tablePrefix" value="combine"/>
        </include>
        where c.organization_id = #{orgId}
        and c.in_shared_pool is true and c.pool_id is not null
        <include refid="combine">
            <property name="combineSearch" value="request.combineSearch"/>
        </include>
        <include refid="sort">
            <property name="sort" value="request.sort"/>
        </include>
    </select>

    <select id="globalPoolSearchListCount" resultType="java.lang.Long">
        select count(1)
        from customer c inner join customer_pool cp on c.pool_id = cp.id
        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.combineSearch.conditions"/>
            <property name="tablePrefix" value="combine"/>
        </include>
        where c.organization_id = #{orgId}
        and c.in_shared_pool is true and c.pool_id is not null
        <include refid="combine">
            <property name="combineSearch" value="request.combineSearch"/>
        </include>
    </select>

    <select id="globalSearchList" resultType="cn.cordys.crm.search.response.global.GlobalCustomerResponse">
        select c.id, c.`name`, if(c.in_shared_pool, null, c.owner) as owner, c.create_time, c.follow_time
        from customer c
        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.combineSearch.conditions"/>
            <property name="tablePrefix" value="combine"/>
        </include>
        where c.organization_id = #{orgId}
        and c.in_shared_pool is false
        <include refid="combine">
            <property name="combineSearch" value="request.combineSearch"/>
        </include>
        <include refid="sort">
            <property name="sort" value="request.sort"/>
        </include>
    </select>
    <select id="globalSearchListCount" resultType="java.lang.Long">
        select count(1)
        from customer c
        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.combineSearch.conditions"/>
            <property name="tablePrefix" value="combine"/>
        </include>
        where c.organization_id = #{orgId}
        and c.in_shared_pool is false
        <include refid="combine">
            <property name="combineSearch" value="request.combineSearch"/>
        </include>
    </select>

    <update id="updateIncludeNullById" parameterType="cn.cordys.crm.customer.domain.Customer">
        update customer set pool_id = #{customer.poolId}, in_shared_pool = #{customer.inSharedPool}, owner = #{customer.owner},
                collection_time = #{customer.collectionTime}, update_time = #{customer.updateTime}
        where id = #{customer.id}
    </update>

    <update id="batchUpdate">
        UPDATE customer
        <set>
            <if test="request.fieldName == 'owner'">
                owner = #{request.fieldValue},
            </if>
            <if test="request.fieldName == 'name'">
                name = #{request.fieldValue},
            </if>
            update_time = #{request.updateTime},
            update_user = #{request.updateUser}
        </set>
        WHERE id IN
        <foreach collection="request.ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>


    <select id="getCustomerPoolId" resultType="cn.cordys.common.dto.OptionDTO">
        select id, IFNULL(NULLIF(pool_id, ''), '') AS name
        from customer
        where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>
</mapper>