<?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.opportunity.mapper.ExtOpportunityRuleMapper">
    <select id="list" parameterType="cn.cordys.common.dto.BasePageRequest" resultType="cn.cordys.crm.opportunity.dto.OpportunityRuleDTO">
        select * from opportunity_rule
        where organization_id = #{orgId}
        <if test="request.keyword != null and request.keyword != ''">
            and name like concat('%', #{request.keyword},'%')
        </if>
        <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>

    <select id="getAllRule" resultType="cn.cordys.crm.opportunity.domain.OpportunityRule">
        select * from opportunity_rule where organization_id = #{orgId} and enable = 1 order by create_time desc
    </select>

    <sql id="sort">
        <choose>
            <when test="${sort} != null and ${sort}.valid()">
                <bind name="sortType" value="${sort}.type" />
                order by
                <choose>
                    <when test="${sort}.name == 'name'">
                        `name` ${sortType}
                    </when>
                    <when test="${sort}.name == 'enable'">
                        `enable` ${sortType}
                    </when>
                    <when test="${sort}.name == 'members'">
                        `scope_id` ${sortType}
                    </when>
                </choose>
            </when>
            <otherwise>
                order by create_time desc
            </otherwise>
        </choose>
    </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">
                    <if test="condition.name == 'auto'">
                        <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                            <property name="condition" value="condition"/>
                            <property name="column" value="opportunity_rule.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="opportunity_rule.enable"/>
                        </include>
                    </if>
                </foreach>
            </trim>
        </if>
    </sql>
</mapper>