<?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.ximai.mes.md.mapper.MdProductFactoryMapper">

    <resultMap type="MdProductFactory" id="MdProductFactoryResult">
        <result property="productFactoryId"    column="product_factory_id"    />
        <result property="itemId"    column="item_id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="createBy"      column="create_by"     />
        <result property="createTime"    column="create_time"   />
        <result property="updateBy"      column="update_by"     />
        <result property="updateTime"    column="update_time"   />
    </resultMap>

    <resultMap type="com.ximai.mes.md.vo.MdProductFactoryVo" id="MdProductFactoryVoResult">
        <result property="productFactoryId"    column="product_factory_id"    />
        <result property="itemId"    column="item_id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="createBy"      column="create_by"     />
        <result property="createTime"    column="create_time"   />
        <result property="updateBy"      column="update_by"     />
        <result property="updateTime"    column="update_time"   />
        <result property="factoryName"    column="factory_name"   />
    </resultMap>

    <sql id="selectMdProductFactoryVo">
        select product_factory_id, item_id, factory_id, create_by, create_time, update_by, update_time from md_product_factory
    </sql>

    <sql id="Base_Column_List">
        pf.product_factory_id, pf.item_id, pf.factory_id, pf.create_by, pf.create_time, pf.update_by, pf.update_time, f.factory_name
    </sql>

    <select id="selectMdProductFactoryList" parameterType="MdProductFactory" resultMap="MdProductFactoryVoResult">
        select
        <include refid="Base_Column_List"/>
            from md_product_factory pf left join md_factory f on pf.factory_id = f.factory_id
        <where>
            <if test="itemId != null "> and pf.item_id = #{itemId}</if>
            <if test="factoryId != null"> and pf.factory_id = #{factoryId}</if>
        </where>
    </select>

    <select id="selectMdProductFactoryByProductFactoryId" parameterType="Long" resultMap="MdProductFactoryResult">
        <include refid="selectMdProductFactoryVo"/>
        where product_factory_id = #{productFactoryId}
    </select>

    <insert id="insertMdProductFactory" parameterType="MdProductFactory">
        insert into md_product_factory
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productFactoryId != null">product_factory_id,</if>
            <if test="itemId != null">item_id,</if>
            <if test="factoryId != null">factory_id,</if>
            <if test="createBy != null">create_by,</if>
            create_time
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="productFactoryId != null">#{productFactoryId},</if>
            <if test="itemId != null">#{itemId},</if>
            <if test="factoryId != null">#{factoryId},</if>
            <if test="createBy != null">#{createBy},</if>
            sysdate()
         </trim>
    </insert>

    <update id="updateMdProductFactory" parameterType="MdProductFactory">
        update md_product_factory
        <trim prefix="SET" suffixOverrides=",">
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="factoryId != null">factory_id = #{factoryId},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            update_time = sysdate()
        </trim>
        where product_factory_id = #{productFactoryId}
    </update>

    <delete id="deleteMdProductFactoryByProductFactoryId" parameterType="Long">
        delete from md_product_factory where product_factory_id = #{productFactoryId}
    </delete>

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