<?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.supermarket.mapper.SupermarketUnitMapper">
    <resultMap type="com.bonus.canteen.core.supermarket.domain.SupermarketUnit" id="SupermarketUnitResult">
        <result property="unitId"    column="unit_id"    />
        <result property="areaId"    column="area_id"    />
        <result property="unitName"    column="unit_name"    />
        <result property="rate"    column="rate"    />
        <result property="weighType"    column="weigh_type"    />
        <result property="revision"    column="revision"    />
        <result property="delFlag"    column="del_flag"    />
        <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="selectSupermarketUnitVo">
        select unit_id, area_id, unit_name, rate, weigh_type, revision, del_flag, create_by, create_time, update_by, update_time from supermarket_unit
    </sql>

    <select id="selectSupermarketUnitList" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketUnit" resultMap="SupermarketUnitResult">
        <include refid="selectSupermarketUnitVo"/>
        <where>  
            <if test="areaId != null "> and area_id = #{areaId}</if>
            <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>
            <if test="revision != null "> and revision = #{revision}</if>
        </where>
    </select>
    
    <select id="selectSupermarketUnitByUnitId" parameterType="Long" resultMap="SupermarketUnitResult">
        <include refid="selectSupermarketUnitVo"/>
        where unit_id = #{unitId}
    </select>
        
    <insert id="insertSupermarketUnit" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketUnit" useGeneratedKeys="true" keyProperty="unitId">
        insert into supermarket_unit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaId != null">area_id,</if>
            <if test="unitName != null and unitName != ''">unit_name,</if>
            <if test="rate != null">rate,</if>
            <if test="weighType != null">weigh_type,</if>
            <if test="revision != null">revision,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaId != null">#{areaId},</if>
            <if test="unitName != null and unitName != ''">#{unitName},</if>
            <if test="rate != null">#{rate},</if>
            <if test="weighType != null">#{weighType},</if>
            <if test="revision != null">#{revision},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateSupermarketUnit" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketUnit">
        update supermarket_unit
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaId != null">area_id = #{areaId},</if>
            <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="revision != null">revision = #{revision},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where unit_id = #{unitId}
    </update>

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

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