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

    <resultMap type="com.ximai.mes.md.domain.MdSaleUnit" id="MdSaleUnitResult">
        <result property="id"    column="id"    />
        <result property="itemId"    column="item_id"    />
        <result property="unitId"    column="unit_id"    />
        <result property="saleConversionValue"    column="sale_conversion_value"    />
        <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.MdSaleUnitVo" id="MdSaleUnitVoResult">
        <result property="id"    column="id"    />
        <result property="itemId"    column="item_id"    />
        <result property="unitId"    column="unit_id"    />
        <result property="saleConversionValue"    column="sale_conversion_value"    />
        <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="unitCode"    column="unit_code"    />
        <result property="unitName"    column="unit_name"    />
    </resultMap>

    <sql id="selectMdSaleUnitVo">
        select id, item_id, unit_id, sale_conversion_value, create_by, create_time, update_by, update_time from md_sale_unit
    </sql>

    <sql id="Base_Column_List">
        su.id, su.item_id, su.unit_id,um.measure_code as unit_code,um.measure_name as unit_name, su.sale_conversion_value, su.create_by,su.create_time, su.update_by, su.update_time
    </sql>

    <select id="selectMdSaleUnitList" parameterType="MdSaleUnit" resultMap="MdSaleUnitVoResult">
        select
        <include refid="Base_Column_List"/>
            from md_sale_unit su left join md_unit_measure um on su.unit_id = measure_id
        <where>
            <if test="itemId != null "> and su.item_id = #{itemId}</if>
            <if test="unitId != null  and unitId != ''"> and su.unit_id = #{unitId}</if>
            <if test="saleConversionValue != null "> and su.sale_conversion_value = #{saleConversionValue}</if>
        </where>
    </select>

    <select id="selectMdSaleUnitById" parameterType="Long" resultMap="MdSaleUnitVoResult">
        select
        <include refid="Base_Column_List"/>
        from md_sale_unit su left join md_unit_measure um on su.unit_id = measure_id
        where su.id = #{id}
    </select>

    <insert id="insertMdSaleUnit" parameterType="MdSaleUnit" useGeneratedKeys="true" keyProperty="id">
        insert into md_sale_unit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="itemId != null">item_id,</if>
            <if test="unitId != null">unit_id,</if>
            <if test="saleConversionValue != null">sale_conversion_value,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="itemId != null">#{itemId},</if>
            <if test="unitId != null">#{unitId},</if>
            <if test="saleConversionValue != null">#{saleConversionValue},</if>
            <if test="createBy != null">#{createBy},</if>
            sysdate()
         </trim>
    </insert>

    <update id="updateMdSaleUnit" parameterType="MdSaleUnit">
        update md_sale_unit
        <trim prefix="SET" suffixOverrides=",">
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="unitId != null">unit_id = #{unitId},</if>
            <if test="saleConversionValue != null">sale_conversion_value = #{saleConversionValue},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            update_time = sysdate()
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMdSaleUnitById" parameterType="Long">
        delete from md_sale_unit where id = #{id}
    </delete>

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