<?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.bonus.canteen.core.supply.mapper.SupplyProductUnitMapper">
    <resultMap type="com.bonus.canteen.core.supply.domain.SupplyProductUnit" id="SupplyProductUnitResult">
        <result property="unitId"    column="unit_id"    />
        <result property="unitName"    column="unit_name"    />
        <result property="rate"    column="rate"    />
        <result property="weighType"    column="weigh_type"    />
        <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>

    <sql id="selectSupplyProductUnitVo">
        select unit_id, unit_name, rate, weigh_type, create_by, create_time, update_by, update_time
        from supply_product_unit
    </sql>

    <select id="selectSupplyProductUnitList" parameterType="com.bonus.canteen.core.supply.domain.SupplyProductUnit" resultMap="SupplyProductUnitResult">
        <include refid="selectSupplyProductUnitVo"/>
        <where>  
            <if test="unitName != null  and unitName != ''"> and unit_name like concat('%', #{unitName}, '%')</if>
            <if test="rate != null "> and rate = #{rate}</if>
            <if test="weighType != null "> and weigh_type = #{weighType}</if>
        </where>
    </select>
    
    <select id="selectSupplyProductUnitByUnitId" parameterType="Long" resultMap="SupplyProductUnitResult">
        <include refid="selectSupplyProductUnitVo"/>
        where unit_id = #{unitId}
    </select>
        
    <insert id="insertSupplyProductUnit" parameterType="com.bonus.canteen.core.supply.domain.SupplyProductUnit" useGeneratedKeys="true" keyProperty="unitId">
        insert into supply_product_unit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="unitName != null and unitName != ''">unit_name,</if>
            <if test="rate != null">rate,</if>
            <if test="weighType != null">weigh_type,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="unitName != null and unitName != ''">#{unitName},</if>
            <if test="rate != null">#{rate},</if>
            <if test="weighType != null">#{weighType},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateSupplyProductUnit" parameterType="com.bonus.canteen.core.supply.domain.SupplyProductUnit">
        update supply_product_unit
        <trim prefix="SET" suffixOverrides=",">
            <if test="unitName != null and unitName != ''">unit_name = #{unitName},</if>
            <if test="rate != null">rate = #{rate},</if>
            <if test="weighType != null">weigh_type = #{weighType},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where unit_id = #{unitId}
    </update>

    <delete id="deleteSupplyProductUnitByUnitId" parameterType="Long">
        delete from supply_product_unit where unit_id = #{unitId}
    </delete>

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