<?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.domain.SysStockDetail" id="SysStockDetailResult">
        <result property="id" column="id"/>
        <result property="consignorId" column="consignor_id"/>
        <result property="branchId" column="branch_id"/>
        <result property="storageId" column="storage_id"/>
        <result property="areaId" column="area_id"/>
        <result property="tunnel" column="tunnel"/>
        <result property="goodsNo" column="goods_no"/>
        <result property="qrCode" column="qr_code"/>
        <result property="containerCode" column="container_code"/>
        <result property="qty" column="qty"/>
        <result property="locationId" column="location_id"/>
        <result property="batch" column="batch"/>
        <result property="isPuch" column="is_puch"/>
        <result property="inTime" column="in_time"/>
        <result property="outTime" column="out_time"/>
    </resultMap>

    <select id="StockInquiry" resultType="com.shkj.wms.vo.SysStockDetailVo"
            parameterType="com.shkj.wms.bo.SysStockDetailQueryBo">
        select
        d.container_code containerCode,
        br.branch_name branchName,
        d.branch_id branchId,
        a.area_name areaName,
        d.area_id areaId,
        s.storage_name storageName,
        d.storage_id storageId,
        location.location_name locationName,
        d.location_id locationId,
        g.vehiche_model vehicheModel,
        g.color color,
        g.location location,
        g.location_code locationCode,
        g.is_split isSplit,
        d.is_puch isPuch,
        d.in_time inTime,
        DATEDIFF(curtime(),d.in_time) ontime
        FROM
        sys_stock_detail d

        JOIN base_goods_data g on g.goods_no = d.goods_no
        JOIN base_storage s on s.id=d.storage_id
        JOIN base_area a on a.id=d.area_id
        JOIN base_branch br on br.id=d.branch_id
        JOIN base_location location on  d.location_id = location.id
        <where>
            d.qty>0
            <if test="storageId !=null and storageId!=''">
                and s.id=#{storageId}
            </if>
            <if test="containerCode !=null and containerCode!=''">
                and d.container_code=#{containerCode}
            </if>
            <if test="isPuch !=null and isPuch!=''">
                and d.is_puch=#{isPuch}
            </if>
            <if test="vehicheModel !=null and vehicheModel!=''">
                and g.vehiche_model=#{vehicheModel}
            </if>
            <if test="color !=null and color!=''">
                and b.color=#{color}
            </if>
            <if test="ontime !=null and ontime!=''">
                and DATEDIFF(curtime(),d.in_time)=#{ontime}
            </if>
            <if test="isSplit !=null and isSplit!=''">
                and g.is_split=#{isSplit}
            </if>
            <if test="locationCode !=null and locationCode!=''">
                and g.location_code=#{locationCode}
            </if>
        </where>

    </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="map"  parameterType="java.lang.String">
        SELECT
        count( detail.container_code ) as countContainer,
        GROUP_CONCAT( DISTINCT detail.branch_id ) as branchId,
        GROUP_CONCAT( DISTINCT detail.goods_no ) as goodsNo,
        GROUP_CONCAT( DISTINCT detail.qr_code ) as qrCode,
        GROUP_CONCAT( DISTINCT detail.container_code ) as containerCode,
        GROUP_CONCAT( DISTINCT location.location_row ) as locationRow,
        GROUP_CONCAT( DISTINCT location.location_column ) as locationColumn,
        GROUP_CONCAT( DISTINCT location.layer ) as layer,
        GROUP_CONCAT( DISTINCT detail.location_id ) as locationId,
        GROUP_CONCAT( DISTINCT location.tunnel ) as tunnel
        FROM
        sys_stock_detail detail
        INNER JOIN base_location location ON detail.location_id = location.id
        AND detail.tunnel = location.tunnel
        WHERE
        detail.qty != 0
        AND detail.area_id = 18
        AND detail.out_qty IS NULL
        and detail.goods_no in
        <foreach collection="arg0" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="arg1 != null and arg1 !='' ">
            and container_code != (select container_code from sys_stock_detail where qty != 0 AND area_id = 18 AND out_qty IS NULL and goods_no = #{arg1})
        </if>
        GROUP BY
            detail.location_id
        ORDER BY
            count( detail.container_code ) desc
        limit 1
    </select>


    <update id="updateSysStockDetail" parameterType="com.shkj.wms.bo.SysStockReduceBo">

        update  sys_stock_detail set qty =qty -1
        <where>
            <if test="branchId != null and branchId !='' ">branch_id = #{branchId},</if>
            <if test="storageId != null and storageId !='' ">storage_id = #{storageId},</if>
            <if test="areaId != null and areaId !='' ">area_id = #{areaId},</if>
            <if test="locationId != null and locationId !='' ">location_id = #{locationId},</if>
        </where>
    </update>
    <select id="getStockDetailByLocationId" resultType="com.shkj.wms.domain.SysStockDetail" parameterType="String">
        select *
        from sys_stock_detail s
        where
            qty > 0
        <if test="id!=null">
            and s.location_id=#{id}
        </if>
    </select>

    <select id="selectsystock" resultType="com.shkj.wms.domain.SysStockDetail" >
    select *
    from sys_stock_detail s
    <where>
        s.qty = '1'
        AND s.location_id = #{locationid}
       <!-- <if test="GoodsNos !=null and  GoodsNos !='' " >
            AND  s.qr_code in
            <foreach collection="GoodsNos" item="good" open="(" separator="," close=")">
                #{good}
            </foreach>
        </if>-->
    </where>

    </select>

    <delete id="deleteBySysStockByLocationId" parameterType="java.lang.Long">
        delete from sys_stock_detail where location_id = #{locationId}
    </delete>


</mapper>
