<?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.microservices.pms.product.mapper.PmsProductReqOperationRecordMapper">

    <resultMap type="PmsProductReqOperationRecord" id="PmsProductReqOperationRecordResult">
        <result property="id" column="id"/>
        <result property="operator" column="operator"/>
        <result property="type" column="type"/>
        <result property="operation" column="operation"/>
        <result property="operatingTime" column="operating_time"/>
        <result property="productRequirementId" column="product_requirement_id"/>
        <result property="content" column="content"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="delFlag" column="del_flag"/>
        <result property="reservedField1" column="reserved_field_1"/>
        <result property="reservedField2" column="reserved_field_2"/>
        <result property="reservedField3" column="reserved_field_3"/>
        <result property="reservedField4" column="reserved_field_4"/>
    </resultMap>

    <sql id="selectPmsProductReqOperationRecordVo">
        select id,
               operator,
               type,
               operation,
               operating_time,
               product_requirement_id,
               content,
               update_time,
               update_by,
               create_time,
               create_by,
               del_flag,
               reserved_field_1,
               reserved_field_2,
               reserved_field_3,
               reserved_field_4
        from pms_product_req_operation_record
    </sql>

    <select id="selectPmsProductReqOperationRecordList" parameterType="PmsProductReqOperationRecord"
            resultMap="PmsProductReqOperationRecordResult">
        <include refid="selectPmsProductReqOperationRecordVo"/>
        <where>
            <if test="operator != null  and operator != ''">and operator = #{operator}</if>
            <if test="type != null  and type != ''">and type = #{type}</if>
            <if test="operation != null  and operation != ''">and operation = #{operation}</if>
            <if test="operatingTime != null ">and operating_time = #{operatingTime}</if>
            <if test="productRequirementId != null ">and product_requirement_id = #{productRequirementId}</if>
            <if test="content != null  and content != ''">and content = #{content}</if>
            <if test="reservedField1 != null  and reservedField1 != ''">and reserved_field_1 = #{reservedField1}</if>
            <if test="reservedField2 != null  and reservedField2 != ''">and reserved_field_2 = #{reservedField2}</if>
            <if test="reservedField3 != null  and reservedField3 != ''">and reserved_field_3 = #{reservedField3}</if>
            <if test="reservedField4 != null  and reservedField4 != ''">and reserved_field_4 = #{reservedField4}</if>
            order by create_time desc
        </where>
    </select>

    <select id="selectPmsProductReqOperationRecordById" parameterType="Long"
            resultMap="PmsProductReqOperationRecordResult">
        <include refid="selectPmsProductReqOperationRecordVo"/>
        where id = #{id}
    </select>
    <select id="selectUserReqLatestReviewRecord" parameterType="Long"
            resultMap="PmsProductReqOperationRecordResult">
        <include refid="selectPmsProductReqOperationRecordVo"/>
        where type = 'reviewUserReq' and operation = 'pass'
        and product_requirement_id=#{reqId}
        order by create_time desc
        limit 1
    </select>
    <select id="selectReqSpecsLatestChangeRecord"
            resultMap="PmsProductReqOperationRecordResult">
        <include refid="selectPmsProductReqOperationRecordVo"/>
        where type = 'contentModify'
        and operation = 'changingReq'
        and product_requirement_id=#{reqId}
        order by create_time desc
        limit 1
    </select>
    <select id="selectReqSpecsLatestReviewRecord" parameterType="Long"
            resultMap="PmsProductReqOperationRecordResult">
        <include refid="selectPmsProductReqOperationRecordVo"/>
        where type = 'reviewReqSpecs' and operation = 'pass'
        and product_requirement_id=#{reqId}
        order by create_time desc
        limit 1
    </select>

    <insert id="insertPmsProductReqOperationRecord" parameterType="PmsProductReqOperationRecord" useGeneratedKeys="true"
            keyProperty="id">
        insert into pms_product_req_operation_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="operator != null and operator != ''">operator,</if>
            <if test="type != null and type != ''">type,</if>
            <if test="operation != null and operation != ''">operation,</if>
            <if test="operatingTime != null">operating_time,</if>
            <if test="productRequirementId != null">product_requirement_id,</if>
            <if test="content != null">content,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="reservedField1 != null">reserved_field_1,</if>
            <if test="reservedField2 != null">reserved_field_2,</if>
            <if test="reservedField3 != null">reserved_field_3,</if>
            <if test="reservedField4 != null">reserved_field_4,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="operator != null and operator != ''">#{operator},</if>
            <if test="type != null and type != ''">#{type},</if>
            <if test="operation != null and operation != ''">#{operation},</if>
            <if test="operatingTime != null">#{operatingTime},</if>
            <if test="productRequirementId != null">#{productRequirementId},</if>
            <if test="content != null">#{content},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="reservedField1 != null">#{reservedField1},</if>
            <if test="reservedField2 != null">#{reservedField2},</if>
            <if test="reservedField3 != null">#{reservedField3},</if>
            <if test="reservedField4 != null">#{reservedField4},</if>
        </trim>
    </insert>

    <update id="updatePmsProductReqOperationRecord" parameterType="PmsProductReqOperationRecord">
        update pms_product_req_operation_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="operator != null and operator != ''">operator = #{operator},</if>
            <if test="type != null and type != ''">type = #{type},</if>
            <if test="operation != null and operation != ''">operation = #{operation},</if>
            <if test="operatingTime != null">operating_time = #{operatingTime},</if>
            <if test="productRequirementId != null">product_requirement_id = #{productRequirementId},</if>
            <if test="content != null">content = #{content},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="reservedField1 != null">reserved_field_1 = #{reservedField1},</if>
            <if test="reservedField2 != null">reserved_field_2 = #{reservedField2},</if>
            <if test="reservedField3 != null">reserved_field_3 = #{reservedField3},</if>
            <if test="reservedField4 != null">reserved_field_4 = #{reservedField4},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePmsProductReqOperationRecordById" parameterType="Long">
        delete
        from pms_product_req_operation_record
        where id = #{id}
    </delete>

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