<?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.hw.wms.mapper.WmsBaseLocationMapper">

    <resultMap type="WmsBaseLocation" id="WmsBaseLocationResult">
        <result property="locationId"    column="location_id"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="warehouseName"    column="warehouse_name"    />
        <result property="locationCode"    column="location_code"    />
        <result property="locRow"    column="loc_row"    />
        <result property="layerNum"    column="layer_num"    />
        <result property="locColumn"    column="loc_column"    />
        <result property="activeFlag"    column="active_flag"    />
        <result property="manualFlag"    column="manual_flag"    />
        <result property="qtyLimit"    column="qty_limit"    />
        <result property="instockFlag"    column="instock_flag"    />
        <result property="outstockFlag"    column="outstock_flag"    />
        <result property="locationStatus"    column="location_status"    />
        <result property="batchMix"    column="batch_mix"    />
        <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="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="shelfOrder"    column="shelf_order"    />
        <result property="checkOrder"    column="check_order"    />
        <result property="pickOrder"    column="pick_order"    />
        <result property="pickFlag"    column="pick_flag"    />
        <result property="isOpenKnFlag"    column="is_open_kn_flag"    />
        <result property="locationScrapType"    column="location_scrap_type"    />
        <result property="locationAttr"    column="location_attr"    />
        <result property="turnDemand"    column="turn_demand"    />
        <result property="checkCode"    column="check_code"    />
        <result property="workArea"    column="work_area"    />
        <result property="volumeLimit"    column="volume_limit"    />
        <result property="weightLimit"    column="weight_limit"    />
        <result property="boxLimit"    column="box_limit"    />
        <result property="palletLimit"    column="pallet_limit"    />
        <result property="length"    column="length"    />
        <result property="width"    column="width"    />
        <result property="height"    column="height"    />
    </resultMap>

    <sql id="selectWmsBaseLocationVo">
        select location_id, warehouse_id, location_code, loc_row, layer_num, loc_column, active_flag, manual_flag, qty_limit, instock_flag, outstock_flag, location_status, batch_mix, create_by, create_time, update_by, update_time, remark, del_flag, shelf_order, check_order, pick_order, pick_flag, is_open_kn_flag, location_scrap_type, location_attr, turn_demand, check_code, work_area, volume_limit, weight_limit, box_limit, pallet_limit, length, width, height from wms_base_location
    </sql>

    <select id="selectWmsBaseLocationList" parameterType="WmsBaseLocation" resultMap="WmsBaseLocationResult">
        <include refid="selectWmsBaseLocationVo"/>
        <where>
            <if test="warehouseId != null "> and warehouse_id = #{warehouseId}</if>
            <if test="locationCode != null  and locationCode != ''"> and location_code = #{locationCode}</if>
            <if test="locRow != null "> and loc_row = #{locRow}</if>
            <if test="layerNum != null "> and layer_num = #{layerNum}</if>
            <if test="locColumn != null "> and loc_column = #{locColumn}</if>
            <if test="activeFlag != null  and activeFlag != ''"> and active_flag = #{activeFlag}</if>
            <if test="manualFlag != null  and manualFlag != ''"> and manual_flag = #{manualFlag}</if>
            <if test="qtyLimit != null "> and qty_limit = #{qtyLimit}</if>
            <if test="instockFlag != null  and instockFlag != ''"> and instock_flag = #{instockFlag}</if>
            <if test="outstockFlag != null  and outstockFlag != ''"> and outstock_flag = #{outstockFlag}</if>
            <if test="locationStatus != null  and locationStatus != ''"> and location_status = #{locationStatus}</if>
            <if test="batchMix != null  and batchMix != ''"> and batch_mix = #{batchMix}</if>
            <if test="shelfOrder != null  and shelfOrder != ''"> and shelf_order = #{shelfOrder}</if>
            <if test="checkOrder != null  and checkOrder != ''"> and check_order = #{checkOrder}</if>
            <if test="pickOrder != null  and pickOrder != ''"> and pick_order = #{pickOrder}</if>
            <if test="pickFlag != null  and pickFlag != ''"> and pick_flag = #{pickFlag}</if>
            <if test="isOpenKnFlag != null  and isOpenKnFlag != ''"> and is_open_kn_flag = #{isOpenKnFlag}</if>
            <if test="locationScrapType != null  and locationScrapType != ''"> and location_scrap_type = #{locationScrapType}</if>
            <if test="locationAttr != null  and locationAttr != ''"> and location_attr = #{locationAttr}</if>
            <if test="turnDemand != null  and turnDemand != ''"> and turn_demand = #{turnDemand}</if>
            <if test="checkCode != null  and checkCode != ''"> and check_code = #{checkCode}</if>
            <if test="workArea != null  and workArea != ''"> and work_area = #{workArea}</if>
            <if test="volumeLimit != null "> and volume_limit = #{volumeLimit}</if>
            <if test="weightLimit != null "> and weight_limit = #{weightLimit}</if>
            <if test="boxLimit != null "> and box_limit = #{boxLimit}</if>
            <if test="palletLimit != null "> and pallet_limit = #{palletLimit}</if>
            <if test="length != null "> and length = #{length}</if>
            <if test="width != null "> and width = #{width}</if>
            <if test="height != null "> and height = #{height}</if>
        </where>
    </select>

    <select id="selectWmsBaseLocationByLocationId" parameterType="Long" resultMap="WmsBaseLocationResult">
        <include refid="selectWmsBaseLocationVo"/>
        where location_id = #{locationId}
    </select>

    <insert id="insertWmsBaseLocation" parameterType="WmsBaseLocation" useGeneratedKeys="true" keyProperty="locationId">
        insert into wms_base_location
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="locationCode != null and locationCode != ''">location_code,</if>
            <if test="locRow != null">loc_row,</if>
            <if test="layerNum != null">layer_num,</if>
            <if test="locColumn != null">loc_column,</if>
            <if test="activeFlag != null and activeFlag != ''">active_flag,</if>
            <if test="manualFlag != null">manual_flag,</if>
            <if test="qtyLimit != null">qty_limit,</if>
            <if test="instockFlag != null">instock_flag,</if>
            <if test="outstockFlag != null">outstock_flag,</if>
            <if test="locationStatus != null and locationStatus != ''">location_status,</if>
            <if test="batchMix != null">batch_mix,</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>
            <if test="remark != null">remark,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="shelfOrder != null">shelf_order,</if>
            <if test="checkOrder != null">check_order,</if>
            <if test="pickOrder != null">pick_order,</if>
            <if test="pickFlag != null">pick_flag,</if>
            <if test="isOpenKnFlag != null">is_open_kn_flag,</if>
            <if test="locationScrapType != null">location_scrap_type,</if>
            <if test="locationAttr != null">location_attr,</if>
            <if test="turnDemand != null">turn_demand,</if>
            <if test="checkCode != null">check_code,</if>
            <if test="workArea != null">work_area,</if>
            <if test="volumeLimit != null">volume_limit,</if>
            <if test="weightLimit != null">weight_limit,</if>
            <if test="boxLimit != null">box_limit,</if>
            <if test="palletLimit != null">pallet_limit,</if>
            <if test="length != null">length,</if>
            <if test="width != null">width,</if>
            <if test="height != null">height,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="locationCode != null and locationCode != ''">#{locationCode},</if>
            <if test="locRow != null">#{locRow},</if>
            <if test="layerNum != null">#{layerNum},</if>
            <if test="locColumn != null">#{locColumn},</if>
            <if test="activeFlag != null and activeFlag != ''">#{activeFlag},</if>
            <if test="manualFlag != null">#{manualFlag},</if>
            <if test="qtyLimit != null">#{qtyLimit},</if>
            <if test="instockFlag != null">#{instockFlag},</if>
            <if test="outstockFlag != null">#{outstockFlag},</if>
            <if test="locationStatus != null and locationStatus != ''">#{locationStatus},</if>
            <if test="batchMix != null">#{batchMix},</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>
            <if test="remark != null">#{remark},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="shelfOrder != null">#{shelfOrder},</if>
            <if test="checkOrder != null">#{checkOrder},</if>
            <if test="pickOrder != null">#{pickOrder},</if>
            <if test="pickFlag != null">#{pickFlag},</if>
            <if test="isOpenKnFlag != null">#{isOpenKnFlag},</if>
            <if test="locationScrapType != null">#{locationScrapType},</if>
            <if test="locationAttr != null">#{locationAttr},</if>
            <if test="turnDemand != null">#{turnDemand},</if>
            <if test="checkCode != null">#{checkCode},</if>
            <if test="workArea != null">#{workArea},</if>
            <if test="volumeLimit != null">#{volumeLimit},</if>
            <if test="weightLimit != null">#{weightLimit},</if>
            <if test="boxLimit != null">#{boxLimit},</if>
            <if test="palletLimit != null">#{palletLimit},</if>
            <if test="length != null">#{length},</if>
            <if test="width != null">#{width},</if>
            <if test="height != null">#{height},</if>
        </trim>
    </insert>

    <update id="updateWmsBaseLocation" parameterType="WmsBaseLocation">
        update wms_base_location
        <trim prefix="SET" suffixOverrides=",">
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="locationCode != null and locationCode != ''">location_code = #{locationCode},</if>
            <if test="locRow != null">loc_row = #{locRow},</if>
            <if test="layerNum != null">layer_num = #{layerNum},</if>
            <if test="locColumn != null">loc_column = #{locColumn},</if>
            <if test="activeFlag != null and activeFlag != ''">active_flag = #{activeFlag},</if>
            <if test="manualFlag != null">manual_flag = #{manualFlag},</if>
            <if test="qtyLimit != null">qty_limit = #{qtyLimit},</if>
            <if test="instockFlag != null">instock_flag = #{instockFlag},</if>
            <if test="outstockFlag != null">outstock_flag = #{outstockFlag},</if>
            <if test="locationStatus != null and locationStatus != ''">location_status = #{locationStatus},</if>
            <if test="batchMix != null">batch_mix = #{batchMix},</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>
            <if test="remark != null">remark = #{remark},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="shelfOrder != null">shelf_order = #{shelfOrder},</if>
            <if test="checkOrder != null">check_order = #{checkOrder},</if>
            <if test="pickOrder != null">pick_order = #{pickOrder},</if>
            <if test="pickFlag != null">pick_flag = #{pickFlag},</if>
            <if test="isOpenKnFlag != null">is_open_kn_flag = #{isOpenKnFlag},</if>
            <if test="locationScrapType != null">location_scrap_type = #{locationScrapType},</if>
            <if test="locationAttr != null">location_attr = #{locationAttr},</if>
            <if test="turnDemand != null">turn_demand = #{turnDemand},</if>
            <if test="checkCode != null">check_code = #{checkCode},</if>
            <if test="workArea != null">work_area = #{workArea},</if>
            <if test="volumeLimit != null">volume_limit = #{volumeLimit},</if>
            <if test="weightLimit != null">weight_limit = #{weightLimit},</if>
            <if test="boxLimit != null">box_limit = #{boxLimit},</if>
            <if test="palletLimit != null">pallet_limit = #{palletLimit},</if>
            <if test="length != null">length = #{length},</if>
            <if test="width != null">width = #{width},</if>
            <if test="height != null">height = #{height},</if>
        </trim>
        where location_id = #{locationId}
    </update>

    <delete id="deleteWmsBaseLocationByLocationId" parameterType="Long">
        delete from wms_base_location where location_id = #{locationId}
    </delete>

    <delete id="deleteWmsBaseLocationByLocationIds" parameterType="String">
        delete from wms_base_location where location_id in
        <foreach item="locationId" collection="array" open="(" separator="," close=")">
            #{locationId}
        </foreach>
    </delete>



    <select id="selectWmsBaseLocationByLocationCode" parameterType="String" resultMap="WmsBaseLocationResult">
        <include refid="selectWmsBaseLocationVo"/>
        where location_code = #{locationCode}
    </select>

    <select id="selectEmptyWmsBaseLocationList" parameterType="WmsBaseLocation" resultMap="WmsBaseLocationResult">
        select location_id, warehouse_id, location_code, loc_row, layer_num, loc_column, active_flag,
                qty_limit, instock_flag, outstock_flag, location_status,
               batch_mix from wms_base_location wbl

        <where>
            and not exists (select 1 from wms_raw_stock wrs where wrs.location_code = wbl.location_code and wrs.total_amount>0 )
            <if test="warehouseId != null "> and warehouse_id = #{warehouseId}</if>
            <if test="locationCode != null  and locationCode != ''"> and location_code = #{locationCode}</if>
            <if test="locRow != null "> and loc_row = #{locRow}</if>
            <if test="layerNum != null "> and layer_num = #{layerNum}</if>
            <if test="locColumn != null "> and loc_column = #{locColumn}</if>
            <if test="activeFlag != null  and activeFlag != ''"> and active_flag = #{activeFlag}</if>
            <if test="qtyLimit != null "> and qty_limit = #{qtyLimit}</if>
            <if test="instockFlag != null  and instockFlag != ''"> and instock_flag = #{instockFlag}</if>
            <if test="outstockFlag != null  and outstockFlag != ''"> and outstock_flag = #{outstockFlag}</if>
            <if test="locationStatus != null  and locationStatus != ''"> and location_status = #{locationStatus}</if>
            <if test="batchMix != null  and batchMix != ''"> and batch_mix = #{batchMix}</if>
            <if test="shelfOrder != null  and shelfOrder != ''"> and shelf_order = #{shelfOrder}</if>
            <if test="checkOrder != null  and checkOrder != ''"> and check_order = #{checkOrder}</if>
            <if test="pickOrder != null  and pickOrder != ''"> and pick_order = #{pickOrder}</if>
            <if test="pickFlag != null  and pickFlag != ''"> and pick_flag = #{pickFlag}</if>
            <if test="isOpenKnFlag != null  and isOpenKnFlag != ''"> and is_open_kn_flag = #{isOpenKnFlag}</if>
            <if test="locationScrapType != null  and locationScrapType != ''"> and location_scrap_type = #{locationScrapType}</if>
            <if test="locationAttr != null  and locationAttr != ''"> and location_attr = #{locationAttr}</if>
            <if test="turnDemand != null  and turnDemand != ''"> and turn_demand = #{turnDemand}</if>
            <if test="checkCode != null  and checkCode != ''"> and check_code = #{checkCode}</if>
            <if test="workArea != null  and workArea != ''"> and work_area = #{workArea}</if>
            <if test="volumeLimit != null "> and volume_limit = #{volumeLimit}</if>
            <if test="weightLimit != null "> and weight_limit = #{weightLimit}</if>
            <if test="boxLimit != null "> and box_limit = #{boxLimit}</if>
            <if test="palletLimit != null "> and pallet_limit = #{palletLimit}</if>
            <if test="length != null "> and length = #{length}</if>
            <if test="width != null "> and width = #{width}</if>
            <if test="height != null "> and height = #{height}</if>
        </where>
    </select>





    <select id="selectWmsBaseLocationJoinList" parameterType="WmsBaseLocation" resultMap="WmsBaseLocationResult">
        select wbl.location_id, wbl.warehouse_id, wbl.location_code, wbl.loc_row, wbl.layer_num, wbl.loc_column, wbl.active_flag, wbl.manual_flag,
        wbl.qty_limit, wbl.instock_flag, wbl.outstock_flag, wbl.location_status, wbl.remark,wbw.warehouse_name
        from wms_base_location wbl left join wms_base_warehouse wbw on wbl.warehouse_id = wbw.warehouse_id

        <where>
            <if test="warehouseId != null "> and wbl.warehouse_id = #{warehouseId}</if>
            <if test="locationCode != null  and locationCode != ''"> and wbl.location_code = #{locationCode}</if>
            <if test="locRow != null "> and wbl.loc_row = #{locRow}</if>
            <if test="layerNum != null "> and wbl.layer_num = #{layerNum}</if>
            <if test="locColumn != null "> and wbl.loc_column = #{locColumn}</if>
            <if test="activeFlag != null  and activeFlag != ''"> and wbl.active_flag = #{activeFlag}</if>
            <if test="manualFlag != null  and manualFlag != ''"> and wbl.manual_flag = #{manualFlag}</if>
            <if test="qtyLimit != null "> and wbl.qty_limit = #{qtyLimit}</if>
            <if test="instockFlag != null  and instockFlag != ''"> and wbl.instock_flag = #{instockFlag}</if>
            <if test="outstockFlag != null  and outstockFlag != ''"> and wbl.outstock_flag = #{outstockFlag}</if>
            <if test="locationStatus != null  and locationStatus != ''"> and wbl.location_status = #{locationStatus}</if>
        </where>
    </select>


</mapper>


