<?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="id"              column="id"                />
        <result property="areaId"          column="area_id"           />
        <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="delFlag"         column="del_flag"          />
        <result property="revision"        column="revision"          />
        <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 area_id,unit_id,unit_name,rate,weigh_type,del_flag,revision,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">
        select su.unit_id as unitid,su.area_id as areaid,aa.area_name as areaname,su.unit_name as unitname,su.rate,su.weigh_type as weightype,su.del_flag as delflag,
        su.revision,su.create_by as createby,su.create_time as createtime,su.update_by as updateby,su.update_time as updatetime
        from supermarket_unit su
        left join alloc_area aa on su.area_id=aa.area_id
        <where>  
            <if test="areaId != null  and areaId != ''"> and su.area_id = #{areaId}</if>
            <if test="unitName != null  and unitName != ''"> and aa.unit_name like concat('%', #{unitName}, '%')</if>
        </where>
    </select>
        
    <insert id="insertSupermarketUnit" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketUnit">
        insert into supermarket_unit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaId    !=null"  >area_id    , </if>
            <if test="unitName  !=null"  >unit_name  , </if>
            <if test="rate      !=null"  >rate       , </if>
            <if test="weighType !=null"  >weigh_type , </if>
            <if test="delFlag   !=null"  >del_flag   , </if>
            <if test="revision  !=null"  >revision   , </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="areaId    !=null"  >#{areaId    },</if>
            <if test="unitName  !=null"  >#{unitName  },</if>
            <if test="rate      !=null"  >#{rate      },</if>
            <if test="weighType !=null"  >#{weighType },</if>
            <if test="delFlag   !=null"  >#{delFlag   },</if>
            <if test="revision  !=null"  >#{revision  },</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="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"  >unit_name     =#{unitName  },</if>
            <if test="rate        !=null"  >rate          =#{rate      },</if>
            <if test="weighType   !=null"  >weigh_type    =#{weighType },</if>
            <if test="delFlag     !=null"  >del_flag      =#{delFlag   },</if>
            <if test="revision    !=null"  >revision      =#{revision  },</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"  >updateTime    =#{updateTime},</if>
        </trim>
        where unit_id = #{unitId}
    </update>

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

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