<?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.shkj.wms.mapper.BaseLocationMapper">

    <resultMap type="com.shkj.wms.vo.BaseLocationVo" id="BaseLocationResult">
        <result property="id" column="id"/>
        <result property="locationCode" column="location_code"/>
        <result property="locationName" column="location_name"/>
        <result property="areaId" column="area_id"/>
        <result property="type" column="type"/>
        <result property="tunnel" column="tunnel"/>
        <result property="layer" column="layer"/>
        <result property="locationStatus" column="location_status"/>
        <result property="locationRow" column="location_row"/>
        <result property="locationColumn" column="location_column"/>
        <result property="isuse" column="isuse"/>
        <result property="length" column="length"/>
        <result property="width" column="width"/>
        <result property="height" column="height"/>
        <result property="volume" column="volume"/>
        <result property="weight" column="weight"/>
        <result property="capacity" column="capacity"/>
        <result property="skunum" column="skunum"/>
        <result property="logicArea" column="logic_area"/>
        <result property="abcClassify" column="abc_classify"/>
        <result property="floor" column="floor"/>
        <result property="storageConditions" column="storage_conditions"/>
        <result property="route" column="route"/>
        <result property="storageClassify" column="storage_classify"/>
        <result property="extend1" column="extend1"/>
        <result property="extend2" column="extend2"/>
        <result property="extend3" column="extend3"/>
        <result property="extend4" column="extend4"/>
        <result property="extend5" column="extend5"/>
        <result property="remark" column="remark"/>
        <result property="branchId" column="branch_id"/>
        <result property="isDel" column="is_del"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>

        <result property="storageName" column="bs_storage_name"/>
        <result property="areaCode" column="ba_area_code"/>
        <result property="taskmode" column="taskmode"/>
        <result property="storageId" column="storage_id"/>
        <result property="storageCode" column="storage_code"/>
    </resultMap>

    <select id="queryLocations" resultMap="BaseLocationResult"
            parameterType="com.shkj.wms.bo.BaseLocationQueryBo">
        select
        bl.*,
        bs.storage_name as bs_storage_name,
        ba.area_code as ba_area_code,
        ba.taskmode
        from base_location bl
        left join base_area ba
        on bl.area_id = ba.id
        left join base_storage bs
        on ba.storage_id = bs.id
        <where>
            <if test="locationCode != null and locationCode != ''">
                and location_code like concat('%', #{locationCode}, '%')
            </if>
        </where>
    </select>

    <select id="queryBaseLocationList" resultMap="BaseLocationResult"
            parameterType="com.shkj.wms.bo.BaseLocationQueryPageBo">
        select
        bl.*,
        ba.storage_id as storage_id,
        bs.storage_name as bs_storage_name,
        bs.storage_code as storage_code,
        ba.area_code as ba_area_code,
        bb.branch_code,
        bb.branch_name,
        ba.area_name
        from base_location bl
        join base_area ba
        on bl.area_id = ba.id
        join base_storage bs
        on ba.storage_id = bs.id
        join base_branch bb
        on bl.branch_id = bb.id
        where bl.is_del = '0'
        and ba.is_del = '0'
        and bs.is_del = '0'
        <if test="locationMsg != null and locationMsg != ''">
            and (bl.location_code like concat('%', #{locationMsg}, '%')
            or bl.location_name like concat('%', #{locationMsg}, '%'))
        </if>
        <if test="storageId != null">
            and ba.storage_id = #{storageId}
        </if>
        <if test="areaId != null">
            and bl.id = #{areaId}
        </if>
        <if test="branchId != null">
            and bl.branch_id = #{branchId}
        </if>
        <if test="isActivity != null">
            and bl.is_activity = #{isActivity}
        </if>
        ${branchParams.dataScopeBranch}
    </select>

    <resultMap type="com.shkj.wms.vo.BaseLocationRecommendVo" id="BaseLocationRecommendResult">
        <result property="id" column="id"/>
        <result property="locationCode" column="location_code"/>
        <result property="locationName" column="location_name"/>
        <result property="areaId" column="area_id"/>
        <result property="type" column="type"/>
        <result property="tunnel" column="tunnel"/>
        <result property="row" column="row"/>
        <result property="layer" column="layer"/>
        <result property="column" column="column"/>
        <result property="isuse" column="isuse"/>
        <result property="length" column="length"/>
        <result property="width" column="width"/>
        <result property="height" column="height"/>
        <result property="volume" column="volume"/>
        <result property="weight" column="weight"/>
        <result property="capacity" column="capacity"/>
        <result property="skunum" column="skunum"/>
        <result property="group" column="group"/>
        <result property="logicArea" column="logic_area"/>
        <result property="abcClassify" column="abc_classify"/>
        <result property="floor" column="floor"/>
        <result property="storageConditions" column="storage_conditions"/>
        <result property="route" column="route"/>
        <result property="storageClassify" column="storage_classify"/>
        <result property="extend1" column="extend1"/>
        <result property="extend2" column="extend2"/>
        <result property="extend3" column="extend3"/>
        <result property="extend4" column="extend4"/>
        <result property="extend5" column="extend5"/>
        <result property="remark" column="remark"/>
        <result property="branchId" column="branch_id"/>
        <result property="isDel" column="is_del"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>

        <result property="storageName" column="storage_name"/>
        <association property="baseArea" resultMap="BaseAreaResult"></association>
    </resultMap>

    <resultMap type="com.shkj.wms.domain.BaseArea" id="BaseAreaResult">
        <result property="id" column="area_id"/>
        <result property="storageId" column="storage_id"/>
        <result property="areaCode" column="area_code"/>
        <result property="areaName" column="area_name"/>
        <result property="pickmode" column="pickmode"/>
        <result property="taskmode" column="taskmode"/>
        <result property="pickgroup" column="pickgroup"/>
        <result property="isDouboper" column="is_douboper"/>
    </resultMap>
    <select id="getLocationByAreaIds" resultMap="BaseLocationRecommendResult">
        select
        bl.*
        from base_location bl join base_area ba on bl.area_id = ba.id
        left join base_storage bs on ba.storage_id = bs.id
        LEFT JOIN sys_stock_lot s ON s.location_id = bl.id
        where
        (s.id is null or (s.quantity = 0 and s.onlock_in_quantity = 0) )
        <if test="locationName != null and locationName != ''">
            and bl.location_name like concat('%', #{locationName}, '%')
        </if>
        <if test="abcClassify != null and abcClassify != ''">
            and bl.abc_classify = #{abcClassify}
        </if>
        and bl.logic_id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>

    </select>
    <select id="getNearestEmptyLocationById" resultType="com.shkj.wms.domain.BaseLocation">
        SELECT
        bl.*
        FROM
        base_location bl
        join base_area ba on bl.area_id = ba.id
        join base_storage bs on ba.storage_id = bs.id
        LEFT JOIN sys_stock_lot s ON s.location_id = bl.id
        WHERE
        (s.id is null or (s.quantity = 0 and s.onlock_in_quantity = 0) )
        and ba.storage_id = #{storageId}
        and bl.area_id = #{areaId}
        and bl.id !=#{locationId}
        <!--        <if test="abcClassify != null and abcClassify != ''">-->
        <!--            and bl.abc_classify = #{abcClassify}-->
        <!--        </if>-->
    </select>

    <select id="queryLocationsByIds" resultMap="BaseLocationResult">
        select
        bl.*,
        ba.storage_id,
        bs.storage_name as bs_storage_name,
        ba.area_code as ba_area_code,
        ba.taskmode
        from base_location bl
        left join base_area ba
        on bl.area_id = ba.id
        left join base_storage bs
        on ba.storage_id = bs.id
        <where>
            bl.id in
            <foreach collection="ids" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </where>
    </select>
    <select id="queryLocationsPackageByIds" resultType="com.shkj.wms.vo.BaseLocationVo">
        select
        bl.*,
        ba.area_code as ba_area_code,
        ba.taskmode,
        bp.package_qty
        from base_location bl
        left join base_area ba on bl.area_id = ba.id
        join sys_stock_lot s on s.location_id = bl.id
        join sys_batch sba on s.batch_id = sba.id
        join base_package bp on bp.id = sba.package_id
        <where>
            s.good_id = #{goodId}
            and s.is_lock = '0'
            and bl.id in
            <foreach collection="ids" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </where>
    </select>
    <select id="queryLocationById" resultType="com.shkj.wms.vo.BaseLocationVo">
        select
        bl.*,
        ba.storage_id,
        bs.storage_name as bs_storage_name,
        ba.area_code as ba_area_code,
        ba.taskmode
        from base_location bl
         join base_area ba
        on bl.area_id = ba.id
         join base_storage bs
        on ba.storage_id = bs.id
        where bl.id = #{locationId}
    </select>

    <select id="queryLocationByCode" resultType="com.shkj.wms.vo.BaseLocationVo">
        select
        bl.*,
        ba.storage_id,
        bs.storage_name as bs_storage_name,
        ba.area_code as ba_area_code,
        ba.taskmode
        from base_location bl
         join base_area ba
        on bl.area_id = ba.id
         join base_storage bs
        on ba.storage_id = bs.id
        where bl.location_code = #{locationCode}
        and bl.branch_id = #{branchId}
    </select>
    <select id="queryLocationByName" resultType="com.shkj.wms.vo.BaseLocationVo">
        select
        bl.*,
        ba.storage_id,
        bs.storage_name as bs_storage_name,
        ba.area_code as ba_area_code,
        ba.taskmode
        from base_location bl
         join base_area ba
        on bl.area_id = ba.id
         join base_storage bs
        on ba.storage_id = bs.id
        where bl.location_name = #{locationName}
        and bl.branch_id = #{branchId}
        limit 1
    </select>
    <select id="getLocationByGoodsIdStorageId" resultType="com.shkj.wms.vo.BaseLocationRecommendVo">
        select
        bl.*
        from base_location bl
        join base_area ba on bl.area_id = ba.id
        join base_storage bs on ba.storage_id = bs.id
        left JOIN sys_stock_lot s ON s.location_id = bl.id
        where
        (s.id is null or (s.quantity = 0 and s.onlock_in_quantity = 0) )
        and  ba.storage_id = #{storageId}
        and  bl.branch_id = #{branchId}
    </select>


    <select id="queryBaseLocationListByDerive" resultMap="BaseLocationResult"
            parameterType="com.shkj.wms.bo.BaseLocationQueryListBo">
        select
        bl.*,
        ba.storage_id as storage_id,
        bs.storage_name as bs_storage_name,
        bs.storage_code as storage_code
        from base_location bl
        left join base_area ba
        on bl.area_id = ba.id
        left join base_storage bs
        on ba.storage_id = bs.id
        left join base_logic_area bla
        on bl.logic_id = bla.id
        left join base_logicarea_derive ble
        on ble.logicarea_id= bla.id
        left join base_derive bd
        on ble.derive_no = bd.id
        where bl.is_del = '0'
        and ba.is_del = '0'
        and bs.is_del = '0'
        and bla.is_del = '0'
        <if test="deriveId != null">
            and ble.derive_id = #{deriveId}
        </if>
        <if test="areaId != null">
            and bl.area_id = #{areaId}
        </if>
        <if test="locationStatus != null">
            and ifnull(bl.location_status,'0') = #{locationStatus}
        </if>
        <if test="tunnel != null">
            and bl.tunnel = #{tunnel}
        </if>
        <if test="locationRow != null">
            and bl.location_row = #{locationRow}
        </if>
        <if test="layer != null">
            and bl.layer = #{layer}
        </if>
        <if test="locationColumn != null">
            and bl.location_column = #{locationColumn}
        </if>
        <if test="isDeep != null">
            and bl.is_deep = #{isDeep}
        </if>
        ORDER BY bl.route asc
    </select>

    <select id="getLocationByAreaId" parameterType="Long" resultType="com.shkj.wms.vo.BaseLocationVo">
        select
        bl.*,
        ba.storage_id as storage_id,
        bs.storage_name as bs_storage_name,
        bs.storage_code as storage_code
        from base_location bl
        left join base_area ba on bl.area_id = ba.id
        left join base_storage bs on ba.storage_id = bs.id
        where bl.is_del = '0' and ba.is_del = '0'  and bs.is_del = '0' and bl.location_status ='0'
        <if test="areaId != null">
            and ba.id = #{areaId}
        </if>
    </select>
    <select id="gettunnel" resultType="com.shkj.wms.vo.BaseLocationVo">
        select s.goods_no,
        b.location_code,
        b.tunnel ,
        b.location_row,
        b.layer,
        b.location_column,
        b.length,
        b.width,
        b.height,
        b.is_deep,
        b.id,
        w.tunnel,b.tunnel,dev_direction,W.id as devId
        from base_location b
        inner JOIN sys_stock_detail s on b.branch_id=s.branch_id and b.area_id=s.area_id   and  s.location_id=b.id
        left JOIN wcs_device w on w.tunnel=b.tunnel
        <where>
            AND w.is_active='0' AND w.dev_direction='2'
            <if test="goodsNo!=null">
                AND s.goods_no=#{goodsNo}
            </if>
        </where>
    </select>

    <select id="getBaseLocationForMap" resultType="com.shkj.wms.vo.BaseLocationMapListVo"
            parameterType="com.shkj.wms.bo.BaseLocationQueryPageBo">
        select
        1 as w,
        1 as h,
        loc.location_column as x,
        loc.layer as y,
        loc.location_code as i,
        loc.tunnel as warehouse,
        loc.*
        from base_location loc
        left join base_area area on loc.area_id=area.id
        left join base_storage stor on stor.id =area.storage_id
        <where>

            <if test="layer != null and layer !='' ">
                and loc.layer = #{layer}
            </if>
            <if test="locationMsg != null and locationMsg != ''">
                and (loc.location_code like concat('%', #{locationMsg}, '%'))
            </if>
        </where>

    </select>

</mapper>
