<?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="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="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
        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.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 ba.id = #{areaId}
        </if>
        <if test="extend1 != null">
            and bl.isuse = #{extend1}
        </if>
        <if test="storageClassify != null and storageClassify != ''">
            and bl.storage_classify = #{storageClassify}
        </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="queryLocationByTunnel" 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.tunnel = #{tunnel}
           and  bl.is_del = '0' and ba.is_del = '0'  and bs.is_del = '0' and bl.location_status ='0'
            and bl.area_id not in (16,17)
            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_logicarea_derive bld
        left join base_logic_area bla on bld.logicarea_id = bla.id
        left join base_location bl on bla.id = bl.logic_id
        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 bla.is_del = '0'

        <if test="vehicheModel != null">
            AND bld.derive_no = #{vehicheModel}
        </if>
        <if test="deriveId != null">
            and bld.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>
        <if test="route != null">
            and bl.route = #{route}
        </if>
        ORDER BY bl.route asc,is_deep desc
    </select>

    <select id="queryBaseLocationListByDerive1" 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
        WHERE
        bl.is_del = '0'
        AND ba.is_del = '0'
        AND bs.is_del = '0'
        <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>
        <if test="route != null">
            and bl.route = #{route}
        </if>
        ORDER BY bl.route asc,is_deep desc
    </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>
        ORDER BY
        bl.route ASC,bl.is_deep desc
    </select>

    <select id="getLocationByAreaIdTunel" parameterType="com.shkj.wms.bo.BaseLocationQueryBo" 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>

        <if test="tunnel != null">
            and bl.tunnel = #{tunnel}
        </if>
        ORDER BY
        bl.route ASC,bl.is_deep desc
    </select>



    <select id="getOutLocationByAreaId" 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 ='1'
        <if test="areaId != null">
            and ba.id = #{areaId}
        </if>
        ORDER BY
        bl.route ASC,bl.is_deep desc
    </select>

    <select id="gettunnel" resultType="com.shkj.wms.vo.BaseLocationVo">
        select distinct
            b.location_code,
            b.tunnel ,
            b.area_id,
            b.location_row,
            b.layer,
            b.location_column,
            b.length,
            b.width,
            b.height,
            b.is_deep,
            b.id,
           b.route,
           dev_direction,
           W.id as devId,s.batch
        from base_location b
        inner JOIN sys_stock_detail s on   s.location_id=b.id
        left JOIN wcs_device w on w.tunnel=b.tunnel
        <where>
             w.is_active='0' AND w.dev_direction='2'
            AND b.area_id in('15','18','19')
            AND b.area_id !='20'
            AND b.is_del != '1'
            and b.location_status='1'
            AND s.qty>0
            AND (1>s.out_qty or s.out_qty is null )
            AND s.goods_no in
            <foreach item="goodsNo" collection="list" open="(" separator="," close=")">
                #{goodsNo}
            </foreach>
            ORDER BY
            b.is_deep,s.batch,b.route
        </where>
    </select>

    <select id="getEmptyContainerFromLocation" resultType="com.shkj.wms.vo.BaseLocationVo">
        select
            b.location_code,
            b.tunnel ,
            b.area_id,
            b.location_row,
            b.layer,
            b.location_column,
            b.length,
            b.width,
            b.height,
            b.is_deep,
            b.id,
            b.route,
            dev_direction,
            W.id as devId
        from base_location b
        left JOIN wcs_device w on w.tunnel=b.tunnel
        <where>
            b.is_del = '0'
            and location_status=2
            and dev_direction = '1'
            <if test="areaId != null">
                and b.area_id = #{areaId}
            </if>
        </where>
            ORDER BY b.route
    </select>


    <select id="queryBaseLocationListByGoods" resultMap="BaseLocationResult"
            parameterType="com.shkj.wms.bo.BaseLocationQueryListBo">
        SELECT
        bl.* ,
        IFNULL( goodsStock.stock, 0 ) AS goodQty,
        IFNULL( goodsStock.tunnel,bl.tunnel )as stokctunnel,
        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 (SELECT count( goods_no ) stock, tunnel FROM sys_stock_detail stock WHERE stock.goods_no = #{goodsNo} GROUP BY tunnel)
        goodsStock on bl.tunnel=goodsStock.tunnel
        WHERE
        bl.is_del = '0'
        AND ba.is_del = '0'
        AND bs.is_del = '0' and bl.area_id not in(17,16)

        <if test="locationStatus != null">
            and ifnull(bl.location_status,'0') = #{locationStatus}
        </if>
        <if test="tunnel != null">
            and bl.tunnel = #{tunnel}
        </if>
        ORDER BY bl.route asc,is_deep desc
    </select>


    <select id="queryBaseLocationListByGoodsPunching" resultMap="BaseLocationResult"
            parameterType="com.shkj.wms.bo.BaseLocationQueryListBo">
        SELECT
        bl.* ,
        IFNULL( goodsStock.stock, 0 ) AS goodQty,
        IFNULL( goodsStock.tunnel,bl.tunnel )as stokctunnel,
        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 (SELECT count( goods_no ) stock, tunnel FROM sys_stock_detail stock WHERE stock.goods_no = #{goodsNo} GROUP BY tunnel)
        goodsStock on bl.tunnel=goodsStock.tunnel
        WHERE
        bl.is_del = '0'
        AND ba.is_del = '0'
        AND bs.is_del = '0'
        <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>
        ORDER BY bl.route asc,is_deep desc
    </select>


    <select id="getBaseLocationForMap" resultType="com.shkj.wms.vo.BaseLocationVo"
            parameterType="com.shkj.wms.bo.BaseLocationQueryPageBo">
        select
        distinct loc.*
        from base_location loc
        left join sys_stock_detail stock on loc.id =stock.location_id
        left join base_container con on stock.container_code=con.id
        where
        1=1
        <if test="tunnel != null and tunnel !='' ">
           and loc.tunnel = #{tunnel}
        </if>
        <if test="locationMsg != null and locationMsg != ''">
            and (loc.location_code like concat('%', #{locationMsg}, '%')
            or stock.qr_code like concat('%', #{locationMsg}, '%')
            or  con.container_code like concat('%', #{locationMsg}, '%')
                )
        </if>
    </select>


    <select id="queryTargetLocation" resultType="com.shkj.wms.vo.BaseLocationDetailVo"
            parameterType="com.shkj.wms.bo.BaseLocationUrgentBo">

        SELECT distinct
        l.id as targetLocationId,
        s.storage_code,
        s.storage_name,
        a.area_code,
        a.taskmode,
        l.id,
        l.location_name,
        l.location_code,
        l.tunnel
        FROM
        base_location l
        JOIN base_area a ON l.area_id = a.id
        JOIN base_storage s ON a.storage_id = s.id
        WHERE
        l.is_del = '0' AND a.is_del = '0'
        and l.location_status='0'
        <if test="locationName != null and locationName != ''">
            and l.location_name like concat('%', #{locationName},'%')
        </if>
        <if test="tunnel != null and tunnel != ''">
            and l.tunnel like concat('%', #{tunnel},'%')
        </if>



     </select>

</mapper>
