<?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.warehouse.mapper.WarehouseInfoMapper">
    <resultMap type="com.bonus.canteen.core.warehouse.domain.WarehouseInfo" id="WarehouseInfoResult">
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="warehouseNum"    column="warehouse_num"    />
        <result property="warehouseName"    column="warehouse_name"    />
        <result property="userId"    column="user_id"    />
        <result property="regionProvince"    column="region_province"    />
        <result property="regionCity"    column="region_city"    />
        <result property="regionDistrict"    column="region_district"    />
        <result property="address"    column="address"    />
        <result property="warehouseType"    column="warehouse_type"    />
        <result property="status"    column="status"    />
        <result property="categoryId"    column="category_id"    />
        <result property="areaId"    column="area_id"    />
        <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"    />
        <result property="ifUseWarehouseArea"    column="if_use_warehouse_area"    />
        <result property="ifUseWarehouseLocation"    column="if_use_warehouse_location"    />
        <result property="parentId"    column="parent_id"    />
        <result property="canteenId"    column="canteen_id"    />
        <result property="fetchUserId"    column="fetch_user_id"    />
    </resultMap>

    <sql id="selectWarehouseInfoVo">
        select warehouse_id, warehouse_num, warehouse_name, user_id, region_province, region_city, region_district, address, warehouse_type, status, category_id, area_id, revision, del_flag, create_by, create_time, update_by, update_time, if_use_warehouse_area, if_use_warehouse_location, parent_id, canteen_id, fetch_user_id from warehouse_info
    </sql>

    <select id="selectWarehouseInfoList" parameterType="com.bonus.canteen.core.warehouse.domain.WarehouseInfo" resultMap="WarehouseInfoResult">
        <include refid="selectWarehouseInfoVo"/>
        <where>  
            <if test="warehouseNum != null  and warehouseNum != ''"> and warehouse_num = #{warehouseNum}</if>
            <if test="warehouseName != null  and warehouseName != ''"> and warehouse_name like concat('%', #{warehouseName}, '%')</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="regionProvince != null  and regionProvince != ''"> and region_province = #{regionProvince}</if>
            <if test="regionCity != null  and regionCity != ''"> and region_city = #{regionCity}</if>
            <if test="regionDistrict != null  and regionDistrict != ''"> and region_district = #{regionDistrict}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="warehouseType != null "> and warehouse_type = #{warehouseType}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="categoryId != null "> and category_id = #{categoryId}</if>
            <if test="areaId != null "> and area_id = #{areaId}</if>
            <if test="revision != null "> and revision = #{revision}</if>
            <if test="ifUseWarehouseArea != null "> and if_use_warehouse_area = #{ifUseWarehouseArea}</if>
            <if test="ifUseWarehouseLocation != null "> and if_use_warehouse_location = #{ifUseWarehouseLocation}</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="canteenId != null "> and canteen_id = #{canteenId}</if>
            <if test="fetchUserId != null "> and fetch_user_id = #{fetchUserId}</if>
        </where>
    </select>
    
    <select id="selectWarehouseInfoByWarehouseId" parameterType="Long" resultMap="WarehouseInfoResult">
        <include refid="selectWarehouseInfoVo"/>
        where warehouse_id = #{warehouseId}
    </select>
        
    <insert id="insertWarehouseInfo" parameterType="com.bonus.canteen.core.warehouse.domain.WarehouseInfo" useGeneratedKeys="true" keyProperty="warehouseId">
        insert into warehouse_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="warehouseNum != null">warehouse_num,</if>
            <if test="warehouseName != null and warehouseName != ''">warehouse_name,</if>
            <if test="userId != null">user_id,</if>
            <if test="regionProvince != null">region_province,</if>
            <if test="regionCity != null">region_city,</if>
            <if test="regionDistrict != null">region_district,</if>
            <if test="address != null">address,</if>
            <if test="warehouseType != null">warehouse_type,</if>
            <if test="status != null">status,</if>
            <if test="categoryId != null">category_id,</if>
            <if test="areaId != null">area_id,</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>
            <if test="ifUseWarehouseArea != null">if_use_warehouse_area,</if>
            <if test="ifUseWarehouseLocation != null">if_use_warehouse_location,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="canteenId != null">canteen_id,</if>
            <if test="fetchUserId != null">fetch_user_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="warehouseNum != null">#{warehouseNum},</if>
            <if test="warehouseName != null and warehouseName != ''">#{warehouseName},</if>
            <if test="userId != null">#{userId},</if>
            <if test="regionProvince != null">#{regionProvince},</if>
            <if test="regionCity != null">#{regionCity},</if>
            <if test="regionDistrict != null">#{regionDistrict},</if>
            <if test="address != null">#{address},</if>
            <if test="warehouseType != null">#{warehouseType},</if>
            <if test="status != null">#{status},</if>
            <if test="categoryId != null">#{categoryId},</if>
            <if test="areaId != null">#{areaId},</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>
            <if test="ifUseWarehouseArea != null">#{ifUseWarehouseArea},</if>
            <if test="ifUseWarehouseLocation != null">#{ifUseWarehouseLocation},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="canteenId != null">#{canteenId},</if>
            <if test="fetchUserId != null">#{fetchUserId},</if>
         </trim>
    </insert>

    <update id="updateWarehouseInfo" parameterType="com.bonus.canteen.core.warehouse.domain.WarehouseInfo">
        update warehouse_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="warehouseNum != null">warehouse_num = #{warehouseNum},</if>
            <if test="warehouseName != null and warehouseName != ''">warehouse_name = #{warehouseName},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="regionProvince != null">region_province = #{regionProvince},</if>
            <if test="regionCity != null">region_city = #{regionCity},</if>
            <if test="regionDistrict != null">region_district = #{regionDistrict},</if>
            <if test="address != null">address = #{address},</if>
            <if test="warehouseType != null">warehouse_type = #{warehouseType},</if>
            <if test="status != null">status = #{status},</if>
            <if test="categoryId != null">category_id = #{categoryId},</if>
            <if test="areaId != null">area_id = #{areaId},</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>
            <if test="ifUseWarehouseArea != null">if_use_warehouse_area = #{ifUseWarehouseArea},</if>
            <if test="ifUseWarehouseLocation != null">if_use_warehouse_location = #{ifUseWarehouseLocation},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="canteenId != null">canteen_id = #{canteenId},</if>
            <if test="fetchUserId != null">fetch_user_id = #{fetchUserId},</if>
        </trim>
        where warehouse_id = #{warehouseId}
    </update>

    <delete id="deleteWarehouseInfoByWarehouseId" parameterType="Long">
        delete from warehouse_info where warehouse_id = #{warehouseId}
    </delete>

    <delete id="deleteWarehouseInfoByWarehouseIds" parameterType="String">
        delete from warehouse_info where warehouse_id in 
        <foreach item="warehouseId" collection="array" open="(" separator="," close=")">
            #{warehouseId}
        </foreach>
    </delete>

    <select id="getWarehouseCountByAreaIds" resultType="Integer">
        select count(1)
        from warehouse_info
        where del_flag = 0 and area_id in
        <foreach item="areaId" collection="array" open="(" separator="," close=")">
            #{areaId}
        </foreach>
    </select>
</mapper>