<?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.MdProductLossMapper">

    <resultMap type="MdProductLoss" id="MdProductLossResult">
        <result property="productLossId"    column="product_loss_id"    />
        <result property="itemId"    column="item_id"    />
        <result property="customerId"    column="customer_id"    />
        <result property="segmentCount"    column="segment_count"    />
        <result property="maxLossCount"    column="max_loss_count"    />
        <result property="lossRate"    column="loss_rate"    />
        <result property="lossRedundance"    column="loss_redundance"    />
        <result property="remark"    column="remark"    />
        <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.MdProductLossVo" id="MdProductLossVoResult">
        <result property="productLossId"    column="product_loss_id"    />
        <result property="itemId"    column="item_id"    />
        <result property="customerId"    column="customer_id"    />
        <result property="segmentCount"    column="segment_count"    />
        <result property="maxLossCount"    column="max_loss_count"    />
        <result property="lossRate"    column="loss_rate"    />
        <result property="lossRedundance"    column="loss_redundance"    />
        <result property="remark"    column="remark"    />
        <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="customerName"    column="customer_name"    />
    </resultMap>

    <sql id="selectMdProductLossVo">
        select product_loss_id, item_id, customer_id, segment_count, max_loss_count, loss_rate, loss_redundance,remark,create_by, create_time, update_by, update_time from md_product_loss
    </sql>

    <select id="selectMdProductLossList" parameterType="MdProductLoss" resultMap="MdProductLossVoResult">
        select
        pl.product_loss_id, pl.item_id, pl.customer_id, pl.segment_count, pl.max_loss_count, pl.loss_rate,
        pl.loss_redundance,pl.remark,pl.create_by, pl.create_time, pl.update_by, pl.update_time,
        c.client_name as customer_name
        from md_product_loss pl left join md_client c on pl.customer_id = c.client_id
        <where>
            <if test="itemId != null ">and pl.item_id = #{itemId}</if>
        </where>
    </select>

    <select id="selectMdProductLossByProductLossId" parameterType="Long" resultMap="MdProductLossVoResult">
        select
            pl.product_loss_id, pl.item_id, pl.customer_id, pl.segment_count, pl.max_loss_count, pl.loss_rate,
            pl.loss_redundance,pl.remark,pl.create_by, pl.create_time, pl.update_by, pl.update_time,
            c.client_name as customer_name
        from md_product_loss pl left join md_client c on pl.customer_id = c.client_id
        where pl.product_loss_id = #{productLossId}
    </select>

    <insert id="insertMdProductLoss" parameterType="MdProductLoss" useGeneratedKeys="true" keyProperty="productLossId">
        insert into md_product_loss
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="itemId != null">item_id,</if>
            <if test="customerId != null">customer_id,</if>
            <if test="segmentCount != null">segment_count,</if>
            <if test="maxLossCount != null">max_loss_count,</if>
            <if test="lossRate != null">loss_rate,</if>
            <if test="lossRedundance != null">loss_redundance,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            create_time
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="itemId != null">#{itemId},</if>
            <if test="customerId != null">#{customerId},</if>
            <if test="segmentCount != null">#{segmentCount},</if>
            <if test="maxLossCount != null">#{maxLossCount},</if>
            <if test="lossRate != null">#{lossRate},</if>
            <if test="lossRedundance != null">#{lossRedundance},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            sysdate()
         </trim>
    </insert>

    <update id="updateMdProductLoss" parameterType="MdProductLoss">
        update md_product_loss
        <trim prefix="SET" suffixOverrides=",">
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="customerId != null">customer_id = #{customerId},</if>
            <if test="segmentCount != null">segment_count = #{segmentCount},</if>
            <if test="maxLossCount != null">max_loss_count = #{maxLossCount},</if>
            <if test="lossRate != null">loss_rate = #{lossRate},</if>
            <if test="lossRedundance != null">loss_redundance = #{lossRedundance},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            update_time = sysdate()
        </trim>
        where product_loss_id = #{productLossId}
    </update>

    <delete id="deleteMdProductLossByProductLossId" parameterType="Long">
        delete from md_product_loss where product_loss_id = #{productLossId}
    </delete>

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