<?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.ruoyi.nanjing.mapper.TRpProductchangeinfoMapper">

    <resultMap type="TRpProductchangeinfo" id="TRpProductchangeinfoResult">
        <result property="ID"    column="ID"    />
        <result property="ProductId"    column="ProductId"    />
        <result property="PersonInCharge"    column="PersonInCharge"    />
        <result property="BeginTime"    column="BeginTime"    />
        <result property="EndTime"    column="EndTime"    />
        <result property="Operator"    column="Operator"    />
        <result property="InsertTime"    column="InsertTime"    />
        <association property="tBdProductinfo" column="productName" javaType="com.ruoyi.nanjing.domain.TBdProductinfo" resultMap="prodName"/>
        <association property="user" column="userName" javaType="com.ruoyi.system.domain.SysUser" resultMap="User"/>
    </resultMap>

    <resultMap id="User" type="com.ruoyi.system.domain.SysUser">
        <result property="userName"     column="user_name"    />
    </resultMap>

    <resultMap id="prodName" type="com.ruoyi.nanjing.domain.TBdProductinfo">
        <result property="productName" column="ProductName"></result>
    </resultMap>
    <sql id="selectTRpProductchangeinfoVo">
        select t1.ID, t1.ProductId,t2.ProductName, t1.PersonInCharge, t1.BeginTime, t1.EndTime, t1.Operator, t1.InsertTime, t3.user_name from T_RP_ProductChangeInfo t1
        left join T_BD_ProductInfo t2 on t1.ProductId = t2.ProductID
        left join sys_user t3 on t1.Operator = Convert(varchar, t3.user_id)
    </sql>

    <select id="selectTRpProductchangeinfoList" parameterType="TRpProductchangeinfo" resultMap="TRpProductchangeinfoResult">
        <include refid="selectTRpProductchangeinfoVo"/>
        <where>
            <if test="ProductId != null "> and t1.ProductId = #{ProductId}</if>
            <if test="PersonInCharge != null  and PersonInCharge != ''"> and t1.PersonInCharge like ('%' + #{PersonInCharge} + '%')</if>
            and t1.BeginTime is null
            or t1.EndTime is null
        </where>
    </select>

    <select id="selectTRpProductchangeinfoById" parameterType="Long" resultMap="TRpProductchangeinfoResult">
        <include refid="selectTRpProductchangeinfoVo"/>
        where t1.ID = #{ID}
    </select>

    <select id="selectChangeInfo" resultMap="TRpProductchangeinfoResult">
        <include refid="selectTRpProductchangeinfoVo"/>
        <where>
            <if test="ProductId != null "> and t1.ProductId = #{ProductId}</if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                AND datediff(dd,#{params.beginTime},t1.BeginTime)>=0
            </if>
            <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                AND datediff(dd,t1.EndTime,#{params.endTime})>=0
            </if>
        </where>
    </select>

    <insert id="insertTRpProductchangeinfo" parameterType="TRpProductchangeinfo">
        insert into T_RP_ProductChangeInfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="ID != null">ID,</if>
            <if test="ProductId != null">ProductId,</if>
            <if test="PersonInCharge != null">PersonInCharge,</if>
            <if test="BeginTime != null">BeginTime,</if>
            <if test="EndTime != null">EndTime,</if>
            <if test="Operator != null">Operator,</if>
            <if test="InsertTime != null">InsertTime,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="ID != null">#{ID},</if>
            <if test="ProductId != null">#{ProductId},</if>
            <if test="PersonInCharge != null">#{PersonInCharge},</if>
            <if test="BeginTime != null">#{BeginTime},</if>
            <if test="EndTime != null">#{EndTime},</if>
            <if test="Operator != null">#{Operator},</if>
            <if test="InsertTime != null">#{InsertTime},</if>
         </trim>
    </insert>

    <update id="updateTRpProductchangeinfo" parameterType="TRpProductchangeinfo">
        update T_RP_ProductChangeInfo
        <trim prefix="SET" suffixOverrides=",">
            <if test="ProductId != null">ProductId = #{ProductId},</if>
            <if test="PersonInCharge != null">PersonInCharge = #{PersonInCharge},</if>
            <if test="BeginTime != null">BeginTime = #{BeginTime},</if>
            <if test="EndTime != null">EndTime = #{EndTime},</if>
            <if test="Operator != null">Operator = #{Operator},</if>
            <if test="InsertTime != null">InsertTime = #{InsertTime},</if>
        </trim>
        where ID = #{ID}
    </update>

    <delete id="deleteTRpProductchangeinfoById" parameterType="Long">
        delete from T_RP_ProductChangeInfo where ID = #{ID}
    </delete>

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

</mapper>
