<?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.fibo.ddp.common.dao.strategyx.knowledge.RuleMapper">
    <cache></cache>
    <resultMap type="com.fibo.ddp.common.model.strategyx.knowledge.Rule" id="ruleMap">
	     <id column="id" property="id"/>    
	     <result column="name" property="name"/> 
	     <result column="code" property="code"/>
	     <result column="description" property="description"/>  
	     <result column="priority" property="priority"/>  
	     <result column="parent_id" property="parentId"/>    
	     <result column="user_id" property="userId"/>
	     <result column="author" property="author"/>
	     <result column="nick_name" property="authorName"/>
	     <result column="organ_id" property="organId"/>
	     <result column="engine_id" property="engineId"/>
	     <result column="status" property="status"/>
	     <result column="type" property="type"/>
	     <result column="is_non" property="isNon"/>
	     <result column="created" property="created"/>
	     <result column="updated" property="updated"/>
	     <result column="rule_type" property="ruleType"/>
	     <result column="rule_audit" property="ruleAudit"/>
	     <result column="score" property="score"/>
	     <result column="last_logical" property="lastLogical"/>
	     <result column="engineName" property="engineName"/>
    </resultMap>

    <sql id="Base_Column">
        r.id,
        r.name,
        r.code,
        r.description,
        r.priority,
	    r.parent_id,
	    r.user_id,
	    r.author,
	    u.nick_name as nick_name,
	    r.organ_id,
	    r.engine_id,
	    r.status,
	    r.rule_audit,
	    r.type,
	    r.score,
	    r.last_logical,
	    r.is_non,
	    r.created,
	    r.updated,
	    r.rule_type,
	    r.difficulty
    </sql>

    <sql id ="Base_Column_1">
        r.id,
        r.name,   
        r.code,
        r.description,
        r.priority,
	    r.parent_id,
	    r.user_id,
	    r.author,
	    u.nick_name,
	    r.organ_id,
	    r.engine_id,
		(CASE r.STATUS
		 WHEN r.id IN (
			SELECT
				rule_id
			FROM
				t_engine_rule_rel td
			WHERE
				engine_id =#{engineId}
		) THEN
			1
		ELSE
			0
		END) AS status,
	    r.rule_audit,
	    r.type,
	    r.score,
	    r.last_logical,
	    r.is_non,
	    r.created,
	    r.updated,
	    r.rule_type     
    </sql>
    
    <sql id ="Base_Column_2">
        r.id,
        r.name,   
        r.code,
        r.description,
        r.engine_id,
        r.priority,
	    r.parent_id,    
	    r.user_id,
	    r.author,
	    r.organ_id,
	    r.engine_id,
	    r.status,
	    r.type,
	    r.is_non,
	    r.rule_audit,
	    r.score,
	    r.last_logical,
	    r.created,
	    r.updated,
	    r.rule_type     
    </sql>

    <select id="getRuleList" parameterType="Map" resultMap="ruleMap">
        <if test="type == 2">
            select * from (
        </if>
        select
        <include refid="Base_Column"/>
        from t_rule r left join t_user u on r.author = u.user_id
        where 1=1
        <if test="parentIds != null and parentIds.size() > 0">
            and r.parent_id in
            <foreach collection="parentIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="status != null">
            and r.status in
            <foreach collection="status" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="key!=null and value!=null">
            <choose>
                <when test="key=='ruleName'">
                    and r.name like CONCAT('%',#{value},'%' )
                </when>
            </choose>
        </if>
        <if test="type == 0">
            and r.type = 0
        </if>
        <if test="type == 1">
            and r.organ_id = #{organId}
            and r.type = 1
            order by updated desc
        </if>
        <if test="type == 2">
            and r.engine_id = #{engineId}
            and r.type = 2
            union
            select
            <choose>
                <when test="engineId!='' and engineId!=null">
                    <include refid="Base_Column_1"/>
                </when>
                <otherwise>
                    <include refid="Base_Column"/>
                </otherwise>
            </choose>
            from
            t_rule r left join t_user u on r.author = u.user_id
            where 1=1
            <if test="parentIds != null and parentIds!=''">
                and r.parent_id in
                <foreach collection="parentIds" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="status != null">
                and r.status in
                <foreach collection="status" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            and r.organ_id = #{organId}
            <if test="key!=null and value!=null">
                <choose>
                    <when test="key=='ruleName'">
                        and r.name like CONCAT('%',#{value},'%' )
                    </when>
                </choose>
            </if>
            and r.type = 1
            and r.id in (select rule_id from t_engine_rule_rel td where engine_id = #{engineId})
            order by updated desc
            ) y
        </if>
    </select>

    <select id = "checkByField"  parameterType = "map" resultMap = "ruleMap">
        select r.id,r.name,r.engine_id,e.name as engineName
          from (
                select id,name,engine_id 
                  from t_rule 
                 where organ_id = ( select organ_id from t_user where user_id = #{userId} )
                       <if test="engineId != null">
                       and engine_id = #{engineId}
                       </if>
                       and status = 1
                union all
                select r.id,r.name,er.engine_id 
                  from t_engine_rule_rel er left join t_rule r on er.rule_id = r.id 
                 where r.organ_id = ( select organ_id from t_user where user_id = #{userId} )
                       <if test="engineId != null"> 
                       and er.engine_id = #{engineId}
                       </if>
                       and r.status = 1
               )r left join t_engine e on r.engine_id = e.id
         where (exists (select 1 from (select rule_id, left(field_id,LOCATE('|',field_id)-1) as f from t_rule_field) rf 
                                where r.id = rf.rule_id 
                                      and rf.f in 
                                           <foreach collection="fieldIds" item="item" index="index" open="(" separator="," close=")">  
              	  						   #{item}
              							   </foreach>
              		    ) 
                or exists (select 1 from (select rule_id, left(field_id,LOCATE('|',field_id)-1) as f from t_rule_content) rc 
                                   where r.id = rc.rule_id 
                                         and rc.f in 
                                           <foreach collection="fieldIds" item="item" index="index" open="(" separator="," close=")">  
              	                           #{item}
              	                           </foreach> 
              	           ) 
               )
       
    </select>

    <select id="getAllCodeNameParentId" resultType="com.fibo.ddp.common.model.strategyx.knowledge.Rule">
        select code,name,parent_id from t_rule where status !=-1
    </select>

</mapper>