<?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.shinsoft.sto.mapper.ware.WareStoreMapper">

    <resultMap id="WareStoreResultMap" type="com.shinsoft.sto.model.ware.WareStoreQueryVO">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="ware_id" property="wareId" jdbcType="VARCHAR"/>
        <result column="stack_id" property="stackId" jdbcType="VARCHAR"/>
        <result column="store_id" property="storeId" jdbcType="VARCHAR"/>
        <result column="ware_name" property="wareName" jdbcType="VARCHAR"/>
        <result column="stack_name" property="stackName" jdbcType="VARCHAR"/>
        <result column="layer_no" property="layerNo" jdbcType="VARCHAR"/>
        <result column="material_name" property="materialName" jdbcType="VARCHAR"/>
        <result column="st_grade" property="stGrade" jdbcType="VARCHAR"/>
        <result column="in_dtm" property="inDtm" jdbcType="TIMESTAMP"/>
        <result column="owner_company" property="ownerCompany" jdbcType="VARCHAR"/>
        <result column="receiving_company" property="receivingCompany" jdbcType="VARCHAR"/>
        <result column="customer_company" property="customerCompany" jdbcType="VARCHAR"/>
        <result column="prod_no" property="prodNo" jdbcType="VARCHAR"/>
        <result column="in_truck_no" property="inTruckNo" jdbcType="VARCHAR"/>
        <result column="order_no" property="orderNo" jdbcType="VARCHAR"/>
        <result column="contract_no" property="contractNo" jdbcType="VARCHAR"/>
        <result column="weight" property="weight" jdbcType="DECIMAL"/>
        <result column="quantity" property="quantity" jdbcType="DECIMAL"/>
        <result column="out_flag" property="outFlag" jdbcType="VARCHAR"/>
        <result column="delivery_address" property="deliveryAddress" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="QueryWhere">
        where 1=1
        <if test="parms.wareId != null and parms.wareId != ''">
            and ws.ware_id = #{parms.wareId}
        </if>
        <if test="parms.stackId != null and parms.stackId != ''">
            and ws.stack_id = #{parms.stackId}
        </if>
        <if test="parms.storeId != null and parms.storeId != ''">
            and ws.store_id = #{parms.storeId}
        </if>
        <if test="parms.layerNo != null and parms.layerNo != ''">
            and ws.layer_no = #{parms.layerNo}
        </if>
        <if test="parms.prodNo != null and parms.prodNo != ''">
            and pm.prod_no like '%' || #{parms.prodNo} || '%'
        </if>
        <if test="parms.outFlag != null and parms.outFlag != ''">
            and pm.out_flag = #{parms.outFlag}
        </if>
        <if test="parms.ownerCompany != null and parms.ownerCompany != ''">
            and pm.owner_company = #{parms.ownerCompany}
        </if>
        <if test="parms.receivingCompany != null and parms.receivingCompany != ''">
            and pm.receiving_company = #{parms.receivingCompany}
        </if>
        <if test="parms.customerCompany != null and parms.customerCompany != ''">
            and pm.customer_company = #{parms.customerCompany}
        </if>
        <if test="parms.wareName != null and parms.wareName != ''">
            and w.ware_nm like '%' || #{parms.wareName} || '%'
        </if>
        <if test="parms.stackName != null and parms.stackName != ''">
            and s.name like '%' || #{parms.stackName} || '%'
        </if>
        <if test="parms.materialName != null and parms.materialName != ''">
            and m.material_name like '%' || #{parms.materialName} || '%'
        </if>
    </sql>

    <select id="selectWareStorePage" resultType="com.shinsoft.sto.model.ware.WareStoreQueryVO" parameterType="map">
        select
            ws.id,
            ws.ware_id,
            ws.stack_id,
            ws.store_id,
            ws.layer_no,
            wa.ware_nm as ware_name,
            s.name as stack_name,
            m.material_name,
            pm.st_grade,
            pm.in_dtm,
            w.CUSTOMER_NM owner_company,
            w1.CUSTOMER_NM receiving_company,
            w2.CUSTOMER_NM customer_company,
            pm.prod_no,
            pm.in_truck_no,
            pm.attrib_01 attrib_01,
            pm.attrib_02 attrib_02,
            pm.attrib_03,
            pm.attrib_04,
            pm.attrib_05,
            pm.remark_1,
            pm.weight,
            pm.quantity,
            pm.out_flag,
            pm.delivery_address
        from T_WARE_STORE ws
        left join T_MAIN_WARE wa on wa.id = ws.ware_id
        left join T_MAIN_STACK s on ws.stack_id = s.id
        left join T_PROD_MASTER pm on ws.store_id = pm.id
        left join T_MAIN_MATERIAL m on pm.material_id = m.id
        left join T_MAIN_CUSTOMER w on w.id = pm.OWNER_COMPANY
        left join T_MAIN_CUSTOMER w1 on w1.id = pm.RECEIVING_COMPANY
        left join T_MAIN_CUSTOMER w2 on w2.id = pm.CUSTOMER_COMPANY
        <include refid="QueryWhere"/>
        order by pm.in_dtm desc
    </select>

    <select id="selectWareStoreStat" resultType="map" parameterType="map">
        select
            nvl(sum(pm.weight), 0) as totalWeight,
            nvl(sum(pm.quantity), 0) as totalQuantity
        from T_WARE_STORE ws
        left join T_PROD_MASTER pm on ws.store_id = pm.id
        <include refid="QueryWhere"/>
    </select>
</mapper>

