<?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.yunfeiyun.agmp.fms.mapper.FmsPolicyMapper">
    
    <resultMap type="FmsPolicy" id="FmsPolicyResult">
        <result property="decisionId"    column="decisionId"    />
        <result property="decisionType"    column="decisionType"    />
        <result property="decisionTitle"    column="decisionTitle"    />
        <result property="decisionContent"    column="decisionContent"    />
        <result property="decisionCreator"    column="decisionCreator"    />
        <result property="decisionCreateddate"    column="decisionCreateddate"    />
    </resultMap>

    <sql id="selectFmsPolicyVo">
        select decisionId, decisionType, decisionTitle, decisionContent, decisionCreator, decisionCreateddate from FmsPolicy
    </sql>

    <select id="selectFmsPolicyList" parameterType="FmsPolicy" resultType="FmsPolicyListResVo">
        select decisionId, decisionType, decisionTitle, u.userName as decisionCreator, decisionCreateddate
        from FmsPolicy
            LEFT JOIN SysRes AS r ON r.resBusId = decisionId
            LEFT JOIN SysUser AS u ON decisionCreator = u.userId
        <where>  
            <if test="decisionType != null  and decisionType != ''"> and decisionType = #{decisionType}</if>
            <if test="decisionTitle != null  and decisionTitle != ''"> and decisionTitle like concat('%', #{decisionTitle}, '%')</if>
            <if test="startDate != null  and startDate != ''">
                and decisionCreateddate >=  #{startDate}
            </if>
            <if test="endDate != null  and endDate != ''">
                and decisionCreateddate <![CDATA[ < ]]>  #{endDate}
            </if>
        </where>
        ORDER BY decisionCreateddate DESC
    </select>
    
    <select id="selectFmsPolicyByDecisionId" parameterType="String" resultType="FmsPolicyVo">
        select decisionId, decisionType, decisionTitle, decisionContent, u.userName as decisionCreator, decisionCreateddate, r.resUrl
        from FmsPolicy
            LEFT JOIN SysRes AS r ON r.resBusId = decisionId
            LEFT JOIN SysUser AS u ON decisionCreator = u.userId
        where decisionId = #{decisionId}
    </select>
        
    <insert id="insertFmsPolicy" parameterType="FmsPolicy">
        insert into FmsPolicy
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="decisionId != null">decisionId,</if>
            <if test="decisionType != null">decisionType,</if>
            <if test="decisionTitle != null">decisionTitle,</if>
            <if test="decisionContent != null">decisionContent,</if>
            <if test="decisionCreator != null">decisionCreator,</if>
            <if test="decisionCreateddate != null">decisionCreateddate,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="decisionId != null">#{decisionId},</if>
            <if test="decisionType != null">#{decisionType},</if>
            <if test="decisionTitle != null">#{decisionTitle},</if>
            <if test="decisionContent != null">#{decisionContent},</if>
            <if test="decisionCreator != null">#{decisionCreator},</if>
            <if test="decisionCreateddate != null">#{decisionCreateddate},</if>
         </trim>
    </insert>

    <update id="updateFmsPolicy" parameterType="FmsPolicy">
        update FmsPolicy
        <trim prefix="SET" suffixOverrides=",">
            <if test="decisionType != null">decisionType = #{decisionType},</if>
            <if test="decisionTitle != null">decisionTitle = #{decisionTitle},</if>
            <if test="decisionContent != null">decisionContent = #{decisionContent},</if>
            <if test="decisionCreator != null">decisionCreator = #{decisionCreator},</if>
            <if test="decisionCreateddate != null">decisionCreateddate = #{decisionCreateddate},</if>
        </trim>
        where decisionId = #{decisionId}
    </update>

    <delete id="deleteFmsPolicyByDecisionId" parameterType="String">
        delete from FmsPolicy where decisionId = #{decisionId}
    </delete>

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