<?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.clue.mapper.ExtCluePoolMapper">
    <select id="list" resultType="cn.cordys.crm.clue.dto.CluePoolDTO" parameterType="cn.cordys.common.dto.BasePageRequest">
        select * from clue_pool
        where organization_id = #{orgId}
        <include refid="filters">
            <property name="filters" value="request.filters"/>
        </include>
        <include refid="cn.cordys.common.mapper.CommonMapper.sort">
            <property name="sort" value="request.sort"/>
            <property name="prefix" value=""/>
            <property name="defaultSort" value="create_time desc"/>
        </include>
    </select>

    <update id="updatePickRule" parameterType="cn.cordys.crm.clue.domain.CluePoolPickRule">
        update clue_pool_pick_rule set limit_on_number = #{rule.limitOnNumber}, pick_number = #{rule.pickNumber}, limit_pre_owner = #{rule.limitPreOwner},
                                           pick_interval_days = #{rule.pickIntervalDays}, update_user = #{rule.updateUser}, update_time = #{rule.updateTime},
                                       limit_new = #{rule.limitNew}, new_pick_interval = #{rule.newPickInterval}
        where pool_id = #{rule.poolId}
    </update>

    <update id="updateRecycleRule" parameterType="cn.cordys.crm.clue.domain.CluePoolRecycleRule">
        update clue_pool_recycle_rule set operator = #{rule.operator}, `condition` = #{rule.condition}, update_user = #{rule.updateUser}, update_time = #{rule.updateTime}
        where pool_id = #{rule.poolId}
    </update>

    <select id="getAllPool" resultType="cn.cordys.crm.clue.domain.CluePool">
        select * from clue_pool where organization_id = #{orgId} and enable = true order by create_time desc
    </select>

    <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">
                    <if test="condition.name == 'auto'">
                        <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                            <property name="condition" value="condition"/>
                            <property name="column" value="clue_pool.auto"/>
                        </include>
                    </if>
                    <if test="condition.name == 'enable'">
                        <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                            <property name="condition" value="condition"/>
                            <property name="column" value="clue_pool.enable"/>
                        </include>
                    </if>
                </foreach>
            </trim>
        </if>
    </sql>
</mapper>