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

    <resultMap type="com.shkj.wms.domain.SysStock" id="SysStockResult">
            <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="qty" column="qty"/>
            <result property="inQty" column="in_qty"/>
            <result property="outQty" column="out_qty"/>
            <result property="stockStatus" column="stock_status"/>
            <result property="createTime" column="create_time"/>
            <result property="updateTime" column="update_time"/>
            <result property="createBy" column="create_by"/>
            <result property="updateBy" column="update_by"/>
    </resultMap>

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

                update  sys_stock set qty =qty -1
                <where>
                        <if test="branchId != null and branchId !='' ">
                        branch_id = #{branchId}
                                      </if>
                        <if test="storageId != null and storageId !='' ">and storage_id = #{storageId}</if>
                        <if test="areaId != null and areaId !='' ">and area_id = #{areaId}</if>
                </where>
        </update>

        <select id="queryStockList" resultType="com.shkj.wms.vo.SysStockNumVo" parameterType="com.shkj.wms.bo.SysReportStockQueryBo">
                select ptPart,ptPartName, ptBreakCat,inNum,outNum,  IFNULL(invqty,0)+IFNULL(inNum,0)-IFNULL(outNum,0) as invQty,stockNum
                from (
                SELECT
                a.ptPart,
                a.ptPartName,
                a.ptBreakCat,
                sum( CASE WHEN a.temp = 'in' THEN a.num END ) AS inNum,
                sum( CASE WHEN a.temp = 'out' THEN a.num END ) AS outNum,
                sum( CASE WHEN a.temp = 'stock' THEN a.num END ) AS stockNum
                FROM
                (
                SELECT
                'in' AS temp,
                bid.pt_part AS ptPart,
                bid.pt_name AS ptPartName,
                bid.pt_break_cat as ptBreakCat,
                ifnull( bii.tt_qty_rct, '' ) AS num,
                bii.in_date AS date
                FROM
                base_item_data bid
                INNER JOIN busin_item_in bii ON bid.pt_part = bii.tt_part
                where bii.in_status = 3 and bii.in_type in(1,0)

                <if test="invTimeStart != null  and invTimeStart != ''">
                        and  bii.create_time   &gt; #{invTimeStart}
                </if>

                <if test="startTime != null  and startTime != ''">
                        and  bii.create_time   &lt; #{startTime}
                </if>

                UNION ALL

                SELECT
                'out' AS temp,
                bid.pt_part AS ptPart,
                bid.pt_name AS ptPartName,
                bid.pt_break_cat as ptBreakCat,
                ifnull( bio.tt_qty_iss, '' ) AS num,
                DATE_FORMAT( bio.create_time, '%Y-%m-%d' ) AS date
                FROM
                base_item_data bid
                INNER JOIN busin_item_out bio ON bid.pt_part = bio.tt_part
                where bio.out_type in(1,0)
                and bio.out_status = 3
                <if test="invTimeStart != null  and invTimeStart != ''">
                        and  bio.create_time   &gt; #{invTimeStart}
                </if>

                <if test="startTime != null  and startTime != ''">
                        and  bio.create_time   &lt; #{startTime}
                </if>

                UNION ALL

                SELECT
                'stock' AS temp,
                bid.pt_part AS ptPart,
                bid.pt_name AS ptPartName,
                bid.pt_break_cat as ptBreakCat,
                ifnull(ssd.qty,0) AS num,
                batch AS date
                FROM
                base_item_data bid
                INNER JOIN sys_stock_detail ssd ON bid.pt_part = ssd.pt_part
                where ifnull(ssd.qty,0) > 0
                ) a

                <where>
                        <if test="ptPart != null  and ptPart != ''">
                                and a.ptPart like UPPER(concat('%', #{ptPart}, '%'))
                        </if>

                        <if test="ptBreakCat != null  and ptBreakCat != ''">
                                and a.ptBreakCat like UPPER(concat('%', #{ptBreakCat}, '%'))
                        </if>
                </where>
                GROUP BY a.ptPart,a.ptPartName,a.ptBreakCat
                order by ptBreakCat
                )
                as result
                LEFT JOIN (
                SELECT sum( tt_qty_rct ) AS invqty, tt_part FROM busin_item_in itemin WHERE in_status = '99'   GROUP BY tt_part
                ) invStock ON result.ptPart = invStock.tt_part
        </select>

        <select id="vehicheColorList" resultType="com.shkj.wms.vo.SysStockVehicheColorVo" parameterType="com.shkj.wms.bo.SysReportStockQueryBo">
                select sum(qty) qty,g.color, dict.dict_value as colorId, g.vehiche_model
                from  sys_dict_data dict
                              INNER JOIN base_goods_data g on dict.dict_label=g.color
                              left join sys_stock_detail d on g.goods_no=d.goods_no
                where     IFNULL(qty,0)>0
                GROUP BY g.color,  g.vehiche_model,dict.dict_value;
        </select>

        <select id="vehicheColorLocationList" resultType="com.shkj.wms.vo.SysStockVCLocationVo" parameterType="com.shkj.wms.bo.SysReportStockQueryBo">
                SELECT
                        g.color,
                        IFNULL(g.vehiche_model,'empty') as vehiche_model,
                        loc.tunnel,
                        sum(case when location_status = 0  then 1 else 0 end ) as emptyLocQty,
                        sum(case when location_status = 2  then 1 else 0 end ) as emptyConQty,
                        count(distinct  stock.location_id) stockqty
                FROM
                        base_location loc
                                left join sys_stock_detail stock on loc.id =stock.location_id and IFNULL(goods_no,'')!=''
	                   LEFT JOIN base_goods_data g ON g.goods_no = stock.goods_no
                where loc.is_del=0
                GROUP BY
                        g.color,
                        g.vehiche_model,
                        loc.tunnel
        </select>

        <select id="getVehicheStockList" resultType="com.shkj.wms.vo.SysStockVehicheVo">
                SELECT
                        count( location_id ) as qty,
                        color,
                        vehiche_model,
                        location,
                        is_punching,
                        vehiche
                FROM
                        (
                                SELECT
                                        max( qty ) qty,
                                        color,
                                        vehiche_model,
                                        location,
                                        is_punching,
                                        LEFT ( qr_code, 4 ) AS vehiche,
                                        location_id
                                FROM
                                        sys_stock_detail stock
                                        INNER JOIN base_goods_data goods ON stock.goods_no = goods.goods_no
                                GROUP BY
                                        color,
                                        vehiche_model,
                                        location,
                                        is_punching,
                                        vehiche,
                                        location_id
                        ) AS res
                GROUP BY
                        color,
                        vehiche_model,
                        location,
                        is_punching,
                        vehiche
        </select>


        <select id="queryStockDetailList" resultType="com.shkj.wms.vo.SysStockNumDetailVo" parameterType="com.shkj.wms.bo.SysStockNumQueryBo">
                SELECT
                stock.location_id,
                item.color,
                item.vehiche_model,
                group_concat( stock.qr_code) as qr_code,
                stock.qty,
                DATEDIFF( curtime(), stock.batch ) AS storageAge,
                loc.tunnel,
                loc.location_code,
                con.container_code,
                stock.batch,
                loc.is_del
                FROM
                base_location loc
                LEFT JOIN  sys_stock_detail stock ON loc.id = stock.location_id
                LEFT JOIN base_goods_data item ON stock.goods_no = item.goods_no
                LEFT JOIN base_container con ON stock.container_code = con.id
                <where>
                        <if test="layer != null  and layer != ''">
                                and  loc.layer= #{layer}
                        </if>

                        <if test="location != null  and location != ''">
                                and  loc.location_code= #{location}
                        </if>

                        <if test="locationCode != null  and locationCode != ''">
                                and  loc.location_name= #{locationCode}
                        </if>

                        <if test="containerCode != null  and containerCode != ''">
                                and  con.container_code= #{containerCode}
                        </if>
                </where>
                GROUP BY
                item.color,
                item.vehiche_model,
                stock.qty,
                storageAge,
                loc.tunnel,
                loc.location_code,
                con.container_code,
                stock.batch

        </select>

</mapper>
