<?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.SysStockDetailMapper">

    <resultMap type="com.shkj.wms.vo.SysStockDetailVo" id="SysStockDetailResult">
        <result property="id" column="id"/>
        <result property="branchId" column="branch_id"/>
        <result property="storageId" column="storage_id"/>
        <result property="areaId" column="area_id"/>
        <result property="barcode" column="barcode"/>
        <result property="containerCode" column="container_code"/>
        <result property="qty" column="qty"/>
        <result property="locationId" column="location_id"/>
        <result property="inTime" column="in_time"/>
        <result property="outTime" column="out_time"/>
        <result property="containerName" column="container_name"/>
        <result property="locationName" column="location_name"/>
        <result property="moldName" column="mold_name"/>
        <result property="model" column="model"/>
        <result property="type" column="type"/>

    </resultMap>

    <resultMap type="com.shkj.wms.vo.SysStockDetailConVo" id="SysStockDetailResultNew">
        <result property="containerName" column="container_name"/>
        <result property="containerCode" column="container_code"/>
           <collection property="children" ofType="com.shkj.wms.vo.SysStockDetailVo">
                <result property="id" column="id"/>
                <result property="moldNo" column="mold_no"/>
                <result property="branchName" column="branch_name"/>
                <result property="areaName" column="areaName"/>
                <result property="locationCode" column="location_code"/>
                <result property="moldName" column="mold_name"/>
                <result property="inTime" column="in_time"/>
                <result property="onTime" column="ontime"/>
                <result property="qty" column="qty"/>
                <result property="containerCode" column="container_code"/>
                <result property="containerName" column="container_name"/>
                <result property="moldName" column="mold_name"/>
                <result property="model" column="model"/>
                <result property="type" column="type"/>
    </collection>


    </resultMap>

    <select id="StockInquiry"  parameterType="com.shkj.wms.bo.SysStockDetailQueryBo" resultMap="SysStockDetailResultNew">
        select
        ROW_NUMBER() over(order by d.id) as pId,
        d.*,
        b.branch_code,
        b.branch_name,
        DATEDIFF(curtime(),d.in_time) ontime,
        a.area_name areaName,
        s.storage_name storageName,
        location.location_name,
        location.location_code,
        cs.container_code,
        cs.container_name,
        g.mold_name,
        g.model,
        g.type
        FROM
        sys_stock_detail d
        JOIN base_mold g on g.id = d.mold_id
        JOIN base_storage s on s.id=d.storage_id
        JOIN base_area a on a.id=d.area_id
        JOIN base_container cs on cs.id=d.container_id
        JOIN base_branch b on b.id=d.branch_id
        JOIN base_location location on  d.location_id = location.id

        where
        d.container_id != 0 and qty !=0
            <if test="barcodeOrLocationCode !=null and barcodeOrLocationCode!=''">
                and (d.barcode  like concat('%', #{barcodeOrLocationCode}, '%')
                 or location.location_code like concat('%', #{barcodeOrLocationCode}, '%'))
            </if>
            <if test="barcode !=null and barcode!=''">
                and d.barcode  like concat('%', #{barcode}, '%')
            </if>
            <if test="locationCode !=null and locationCode!=''">
                and location.location_code like concat('%', #{locationCode}, '%')
            </if>

            <if test="moldNo !=null and moldNo!=''">
                and g.mold_no like concat('%', #{moldNo}, '%')
            </if>

            <if test="containerName !=null and containerName!=''">
                and cs.container_name like concat('%',#{containerName}, '%')
            </if>

    </select>
    <select id="StockInquiryPda"  parameterType="com.shkj.wms.bo.SysStockDetailQueryBo" resultMap="SysStockDetailResult">
        select
        ROW_NUMBER() over(order by d.id) as pId,
        d.*,
        b.branch_code,
        b.branch_name,
        DATEDIFF(curtime(),d.in_time) ontime,
        a.area_name areaName,
        s.storage_name storageName,
        location.location_name,
        location.location_code,
        c.container_code,
        c.container_name,
        g.mold_name,
        g.model,
        g.type
        FROM
        sys_stock_detail d
        JOIN base_mold g on g.id = d.mold_id
        JOIN base_storage s on s.id=d.storage_id
        JOIN base_area a on a.id=d.area_id
        JOIN base_container c on c.id=d.container_id
        JOIN base_branch b on b.id=d.branch_id
        JOIN base_location location on  d.location_id = location.id
        where
        d.container_id != 0 and qty !=0
        <if test="barcodeOrLocationCode !=null and barcodeOrLocationCode!=''">
            and (d.barcode  like concat('%', #{barcodeOrLocationCode}, '%')
            or location.location_code like concat('%', #{barcodeOrLocationCode}, '%'))
        </if>
        <if test="barcode !=null and barcode!=''">
            and d.barcode  like concat('%', #{barcode}, '%')
        </if>
        <if test="locationCode !=null and locationCode!=''">
            and location.location_code like concat('%', #{locationCode}, '%')
        </if>

    </select>

    <select id="getLocation" resultType="com.shkj.wms.vo.SysStockDetailVo"  parameterType="java.lang.String">
        select goods_no, location_id
        FROM sys_stock_detail
        where goods_no in
        <foreach collection="list" item="goodsno" open="(" separator="," close=")">
            #{goodsno}
        </foreach>
    </select>

    <select id="getInventoryInStockByGoods" resultType="com.shkj.wms.vo.SysStockDetailVo"  parameterType="java.lang.String">
        select detail.*,location.location_row,location.location_column,location.layer,location.tunnel
        from sys_stock_detail  detail
        inner join base_location location  on detail.location_id = location.id and detail.tunnel=location.tunnel
        where qty !=0 and goods_no =#{goodsNo}
    </select>




    <resultMap type="com.shkj.wms.vo.StockDetailLocationVo" id="StockDetailLocationVoMap">

        <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="locationRow" column="location_row"/>
        <result property="layer" column="layer"/>
        <result property="locationColumn" column="location_column"/>
        <result property="locationStatus" column="location_status"/>
        <result property="locationRow" column="location_row"/>
        <result property="locationColumn" column="location_column"/>
        <result property="length" column="length"/>
        <result property="width" column="width"/>
        <result property="height" column="height"/>
        <result property="volume" column="volume"/>
        <result property="route" column="route"/>
        <result property="branchId" column="branch_id"/>
        <result property="storageId" column="storage_id"/>

        <collection property="stockDetailList" resultMap="stockDetailListMap" ofType="com.shkj.wms.vo.StockDetailMoldVo"></collection>

    </resultMap>
    <resultMap id="stockDetailListMap" type="com.shkj.wms.vo.StockDetailMoldVo">
        <result property="id" column="s_id"/>
        <result property="branchId" column="branch_id"/>
        <result property="storageId" column="storage_id"/>
        <result property="areaId" column="area_id"/>
        <result property="goodsNo" column="goods_no"/>
        <result property="barcode" column="barcode"/>
        <result property="containerCode" column="container_code"/>
        <result property="qty" column="qty"/>
        <result property="inQty" column="in_qty"/>
        <result property="outQty" column="out_qty"/>
        <result property="containerId" column="container_id"/>
        <result property="locationId" column="location_id"/>
        <result property="inTime" column="in_time"/>
        <result property="outTime" column="out_time"/>
        <result property="length" column="mold_length"/>
        <result property="width" column="mold_width"/>
        <result property="height" column="mold_height"/>
        <result property="volume" column="mold_volume"/>

    </resultMap>

    <select id="findLocation" resultMap="StockDetailLocationVoMap">

        SELECT
            l.*, bs.id as storage_id,
            s.*,
			s.id as s_id,
            m.length as mold_length,
            m.width as mold_width,
            m.height as mold_height,
            m.volume as mold_volume
        FROM
            base_location l
        JOIN base_area ba ON l.area_id = ba.id
        JOIN base_storage bs ON ba.storage_id = bs.id
        LEFT JOIN sys_stock_detail s ON l.id = s.location_id
        LEFT JOIN base_mold m ON s.mold_id = m.id
        where l.is_del = 0
        and l.length is not null
        and l.width is not null
        and l.height is not null
        and l.location_column is not NULL
        and l.location_row is not null
        and l.layer is not null
        and l.branch_id = #{branchId}
        and not exists (select 1 from busin_stock_inv i WHERE i.location_id = l.id and i.inv_status != 'cancel' and i.inv_status != 'end')
        order by l.height, l.route

    </select>

    <select id="findLocationByModqty" resultType="com.shkj.wms.vo.StockDetailMoldVo" parameterType="com.shkj.wms.bo.BaseContainerCallBo">
        SELECT
            detail.*,location.*
        FROM
            base_container container
                INNER JOIN (
                SELECT sum(qty) modqty, container_id, container_code, location_id FROM sys_stock_detail
                -- where IFNULL(barcode,'')=''
                GROUP BY container_id, container_code, location_id
            ) AS detail ON detail.container_id = container.id
                inner join base_location location on detail.location_id=location.id
        <where>
            <if test="modlType != null  and modlType == '6'">
               and container_type in('6','3')
            </if>

            <if test="modlType != null  and modlType == '3'">
                and container_type ='3'
            </if>
            <if test="modlQty != null  and modlQty != '0'">
                and  (container.extend1-modqty)>=#{modlQty}
                and  location.location_status not in ('3','4')
            </if>

            <if test="containerCode != null and containerCode != ''">
                and container.container_code= #{containerCode}
            </if>

        </where>

        ORDER BY location.route
            limit 1
    </select>


    <select id="findLocationByContainer" resultMap="StockDetailLocationVoMap">

        SELECT
            l.*, bs.id as storage_id,
            s.*,
            s.id as s_id,
            m.length as mold_length,
            m.width as mold_width,
            m.height as mold_height,
            m.volume as mold_volume
        FROM
            base_location l
                JOIN base_area ba ON l.area_id = ba.id
                JOIN base_storage bs ON ba.storage_id = bs.id
                LEFT JOIN sys_stock_detail s ON l.id = s.location_id
                LEFT JOIN base_mold m ON s.mold_id = m.id
        where l.is_del = 0
          and l.length is not null
          and l.width is not null
          and l.height is not null
          and l.location_column is not NULL
          and l.location_row is not null
          and l.layer is not null
          and l.branch_id = #{branchId}
          and l.location_code =#{containerCode}
         --  and not exists (select 1 from busin_stock_inv i WHERE i.location_id = l.id and i.inv_status != 'cancel' and i.inv_status != 'end')
    </select>


    <select id="findOutLocation" resultType="com.shkj.wms.vo.SysStockDetailVo"
            parameterType="java.lang.String">
         select detail.*,
          location.location_row, location.layer,location.location_column
        from sys_stock_detail  detail
        join base_location location  on detail.location_id = location.id
        where qty - out_qty > 0 and detail.barcode =#{moldBarcode}
        and not exists (select 1 from busin_stock_inv i WHERE i.location_id = location.id and i.inv_status != 'cancel' and i.inv_status != 'end')
        order by location.height, location.route
    </select>
    <select id="getEmptyLocation" resultType="com.shkj.wms.domain.BaseLocation" parameterType="java.lang.Long">
        SELECT
            DISTINCT l.*
        FROM
            base_location l
        LEFT JOIN sys_stock_detail s ON l.id = s.location_id
        where l.is_del = 0
        and l.branch_id = #{branchId}
        and s.id is null
        and type='3'
        order by l.height, l.route
    </select>

    <select id="getPutInOutStockQty" resultType="com.shkj.wms.vo.SysStockAndInOutVo">
        SELECT
            ifnull( sum( qty ), 0 ) qty,
            'in' type
        FROM
            busin_out_in_plan
        WHERE
            create_time >= CURDATE()
          AND  plan_status = 2
          AND type = 'in'
            UNION
        SELECT
            ifnull( sum( qty ), 0 ) qty,
            'out' type
        FROM
            busin_out_in_plan
        WHERE
            create_time >= CURDATE()
          AND plan_status = 2
          AND type = 'out'
            UNION
        SELECT
            ifnull( sum( qty ), 0 ) AS qty,
            'stock' AS type
        FROM
            sys_stock_detail
        WHERE
            IFNULL( barcode, '' ) != ''
    </select>

    <select id="getInventoryManagement" resultType="com.shkj.wms.vo.SysStockByAreaVo">
        SELECT
            sum( qty ) qty,
            area_name
        FROM
            sys_stock_detail detail
                left join base_area area on detail.area_id=area.id
        GROUP BY area_name
    </select>

    <select id="getLocationUsageRate" resultType="com.shkj.wms.vo.SysStockByReteVo">
        SELECT
            ifnull( count(distinct container_code ), 0 ) AS qty,
            area_id
        FROM
            sys_stock_detail
        where IFNULL(container_code,'') !=''
        GROUP BY
            area_id
    </select>

    <select id="getLocationMoldQty" resultType="Integer">
        select max(extend1-qty) as resQty from (
        select count(*) qty,extend1 from sys_stock_detail detail
        inner join base_container container on detail.container_id =container.id
        where location_id=#{locationId}
        and qty!=0
        GROUP BY extend1
        )as groupResult
    </select>

    <select id="getLocationMoldByLocationCode" parameterType="String" resultType="com.shkj.wms.vo.BaseMoldVo">
        select mold.*,detail.in_time from sys_stock_detail detail
                               inner join base_mold mold on detail.mold_id=mold.id
                               inner join base_location location on detail.location_id=location.id
        where location_name = #{locationCode}
    </select>

    <select id="getInvWeightManagement" resultType="com.shkj.wms.vo.SysStockWeightByAreaVo">

        SELECT
            sum( IFNULL(m.weight,0) ) AS weight,
            a.area_name
        FROM
            sys_stock_detail s
                JOIN base_mold m on s.mold_id=m.id
                left join base_area a on s.area_id=a.id

        WHERE
            IFNULL( barcode, '' ) != ''
        group by area_name

    </select>

</mapper>
