<?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.SupplyWarehouseInfoMapper">
    <resultMap type="com.bonus.canteen.core.supply.domain.SupplyWarehouseInfo" id="SupplyWarehouseInfoResult">
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="categoryId"    column="category_id"    />
        <result property="areaId"    column="area_id"    />
        <result property="canteenId"    column="canteen_id"    />
        <result property="warehouseCode"    column="warehouse_code"    />
        <result property="warehouseName"    column="warehouse_name"    />
        <result property="userId"    column="user_id"    />
        <result property="manager"    column="manager"    />
        <result property="fetchUserId"    column="fetch_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="ifUseWarehouseArea"    column="if_use_warehouse_area"    />
        <result property="ifUseWarehouseLocation"    column="if_use_warehouse_location"    />
        <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="selectSupplyWarehouseInfoVo">
        select warehouse_id, parent_id, category_id, area_id, canteen_id, warehouse_code,
               warehouse_name, user_id, manager, fetch_user_id, region_province, region_city,
               region_district, address, warehouse_type, if_use_warehouse_area,
               if_use_warehouse_location, create_by, create_time, update_by, update_time
        from supply_warehouse_info
    </sql>

    <select id="selectSupplyWarehouseInfoList" parameterType="com.bonus.canteen.core.supply.domain.SupplyWarehouseInfo" resultMap="SupplyWarehouseInfoResult">
        <include refid="selectSupplyWarehouseInfoVo"/>
        <where>  
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="categoryId != null "> and category_id = #{categoryId}</if>
            <if test="areaId != null "> and area_id = #{areaId}</if>
            <if test="canteenId != null "> and canteen_id = #{canteenId}</if>
            <if test="warehouseCode != null  and warehouseCode != ''"> and warehouse_code = #{warehouseCode}</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="manager != null "> and manager = #{manager}</if>
            <if test="fetchUserId != null "> and fetch_user_id = #{fetchUserId}</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="ifUseWarehouseArea != null "> and if_use_warehouse_area = #{ifUseWarehouseArea}</if>
            <if test="ifUseWarehouseLocation != null "> and if_use_warehouse_location = #{ifUseWarehouseLocation}</if>
        </where>
    </select>
    
    <select id="selectSupplyWarehouseInfoByWarehouseId" parameterType="Long" resultMap="SupplyWarehouseInfoResult">
        <include refid="selectSupplyWarehouseInfoVo"/>
        where warehouse_id = #{warehouseId}
    </select>

    <select id="getSupplyWarehouseCountByAreaIds" resultType="Integer">
        select count(1)
        from supply_warehouse_info
        where area_id in
        <foreach item="areaId" collection="array" open="(" separator="," close=")">
            #{areaId}
        </foreach>
    </select>
        
    <insert id="insertSupplyWarehouseInfo" parameterType="com.bonus.canteen.core.supply.domain.SupplyWarehouseInfo" useGeneratedKeys="true" keyProperty="warehouseId">
        insert into supply_warehouse_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="categoryId != null">category_id,</if>
            <if test="areaId != null">area_id,</if>
            <if test="canteenId != null">canteen_id,</if>
            <if test="warehouseCode != null">warehouse_code,</if>
            <if test="warehouseName != null and warehouseName != ''">warehouse_name,</if>
            <if test="userId != null">user_id,</if>
            <if test="manager != null">manager,</if>
            <if test="fetchUserId != null">fetch_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="ifUseWarehouseArea != null">if_use_warehouse_area,</if>
            <if test="ifUseWarehouseLocation != null">if_use_warehouse_location,</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="parentId != null">#{parentId},</if>
            <if test="categoryId != null">#{categoryId},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="canteenId != null">#{canteenId},</if>
            <if test="warehouseCode != null">#{warehouseCode},</if>
            <if test="warehouseName != null and warehouseName != ''">#{warehouseName},</if>
            <if test="userId != null">#{userId},</if>
            <if test="manager != null">#{manager},</if>
            <if test="fetchUserId != null">#{fetchUserId},</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="ifUseWarehouseArea != null">#{ifUseWarehouseArea},</if>
            <if test="ifUseWarehouseLocation != null">#{ifUseWarehouseLocation},</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="updateSupplyWarehouseInfo" parameterType="com.bonus.canteen.core.supply.domain.SupplyWarehouseInfo">
        update supply_warehouse_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="categoryId != null">category_id = #{categoryId},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="canteenId != null">canteen_id = #{canteenId},</if>
            <if test="warehouseCode != null">warehouse_code = #{warehouseCode},</if>
            <if test="warehouseName != null and warehouseName != ''">warehouse_name = #{warehouseName},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="manager != null">manager = #{manager},</if>
            <if test="fetchUserId != null">fetch_user_id = #{fetchUserId},</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="ifUseWarehouseArea != null">if_use_warehouse_area = #{ifUseWarehouseArea},</if>
            <if test="ifUseWarehouseLocation != null">if_use_warehouse_location = #{ifUseWarehouseLocation},</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 warehouse_id = #{warehouseId}
    </update>

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

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