<?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.record.OutrecordMapper">

    <resultMap id="OutrecordResultMap" type="com.shinsoft.sto.model.record.Outrecord">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="belong_id" property="belongId" jdbcType="VARCHAR"/>
        <result column="in_truck_no" property="inTruckNo" jdbcType="VARCHAR"/>
        <result column="tally_people" property="tallyPeople" jdbcType="VARCHAR"/>
        <result column="in_dtm" property="inDtm" jdbcType="TIMESTAMP"/>
        <result column="out_ware_name" property="outWareName" jdbcType="VARCHAR"/>
        <result column="out_stack_name" property="outStackName" jdbcType="VARCHAR"/>
        <result column="out_layer_no" property="outLayerNo" jdbcType="VARCHAR"/>
        <result column="prod_no" property="prodNo" jdbcType="VARCHAR"/>
        <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="material_id" property="materialId" jdbcType="VARCHAR"/>
        <result column="st_grade" property="stGrade" jdbcType="VARCHAR"/>
        <result column="out_weight" property="outWeight" jdbcType="DECIMAL"/>
        <result column="out_quantity" property="outQuantity" jdbcType="DECIMAL"/>
        <result column="out_flag" property="outFlag" jdbcType="VARCHAR"/>
        <result column="delivery_address" property="deliveryAddress" jdbcType="VARCHAR"/>
        <result column="attrib_01" property="attrib01" jdbcType="VARCHAR"/>
        <result column="attrib_02" property="attrib02" jdbcType="VARCHAR"/>
        <result column="attrib_03" property="attrib03" jdbcType="VARCHAR"/>
        <result column="attrib_04" property="attrib04" jdbcType="VARCHAR"/>
        <result column="attrib_05" property="attrib05" jdbcType="VARCHAR"/>
        <result column="attrib_06" property="attrib06" jdbcType="VARCHAR"/>
        <result column="attrib_07" property="attrib07" jdbcType="VARCHAR"/>
        <result column="attrib_08" property="attrib08" jdbcType="VARCHAR"/>
        <result column="attrib_09" property="attrib09" jdbcType="VARCHAR"/>
        <result column="attrib_10" property="attrib10" jdbcType="VARCHAR"/>
        <result column="attrib_11" property="attrib11" jdbcType="VARCHAR"/>
        <result column="attrib_12" property="attrib12" jdbcType="VARCHAR"/>
        <result column="attrib_13" property="attrib13" jdbcType="VARCHAR"/>
        <result column="attrib_14" property="attrib14" jdbcType="VARCHAR"/>
        <result column="attrib_15" property="attrib15" jdbcType="VARCHAR"/>
        <result column="attrib_16" property="attrib16" jdbcType="VARCHAR"/>
        <result column="attrib_17" property="attrib17" jdbcType="VARCHAR"/>
        <result column="attrib_18" property="attrib18" jdbcType="VARCHAR"/>
        <result column="attrib_19" property="attrib19" jdbcType="VARCHAR"/>
        <result column="attrib_20" property="attrib20" jdbcType="VARCHAR"/>
        <result column="attrib_21" property="attrib21" jdbcType="VARCHAR"/>
        <result column="attrib_22" property="attrib22" jdbcType="VARCHAR"/>
        <result column="attrib_23" property="attrib23" jdbcType="VARCHAR"/>
        <result column="attrib_24" property="attrib24" jdbcType="VARCHAR"/>
        <result column="attrib_25" property="attrib25" jdbcType="VARCHAR"/>
        <result column="remark_1" property="remark1" jdbcType="VARCHAR"/>
        <result column="remark_2" property="remark2" jdbcType="VARCHAR"/>
        <result column="remark_3" property="remark3" jdbcType="VARCHAR"/>
        <result column="remark_4" property="remark4" jdbcType="VARCHAR"/>
        <result column="remark_5" property="remark5" jdbcType="VARCHAR"/>
        <result column="out_truck_no" property="outTruckNo" jdbcType="VARCHAR"/>
        <result column="out_dtm" property="outDtm" jdbcType="TIMESTAMP"/>
        <result column="distrib_id" property="distribId" jdbcType="VARCHAR"/>
        <result column="out_user_id" property="outUserId" jdbcType="VARCHAR"/>
        <result column="owner_company_name" property="ownerCompanyName" jdbcType="VARCHAR"/>
        <result column="receiving_company_name" property="receivingCompanyName" jdbcType="VARCHAR"/>
        <result column="customer_company_name" property="customerCompanyName" jdbcType="VARCHAR"/>
        <result column="material_name" property="materialName" jdbcType="VARCHAR"/>
        <result column="material_code" property="materialCode" jdbcType="VARCHAR"/>
        <result column="material_standard" property="materialStandard" jdbcType="VARCHAR"/>
        <result column="material_model" property="materialModel" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="OutrecordWhere">
        where 1=1
        <if test="parms.wareName != null and parms.wareName != ''">
            and tor.OUT_WARE_NAME like '%' || #{parms.wareName} || '%'
        </if>
        <if test="parms.outWareName != null and parms.outWareName != ''">
            and tor.OUT_WARE_NAME like '%' || #{parms.outWareName} || '%'
        </if>
        <if test="parms.stackName != null and parms.stackName != ''">
            and tor.OUT_STACK_NAME like '%' || #{parms.stackName} || '%'
        </if>
        <if test="parms.outStackName != null and parms.outStackName != ''">
            and tor.OUT_STACK_NAME like '%' || #{parms.outStackName} || '%'
        </if>
        <if test="parms.outLayerNo != null and parms.outLayerNo != ''">
            and tor.OUT_LAYER_NO = #{parms.outLayerNo}
        </if>
        <if test="parms.inTruckNo != null and parms.inTruckNo != ''">
            and tor.IN_TRUCK_NO like '%' || #{parms.inTruckNo} || '%'
        </if>
        <if test="parms.outTruckNo != null and parms.outTruckNo != ''">
            and tor.OUT_TRUCK_NO like '%' || #{parms.outTruckNo} || '%'
        </if>
        <if test="parms.materialName != null and parms.materialName != ''">
            and material.MATERIAL_NAME like '%' || #{parms.materialName} || '%'
        </if>
        <if test="parms.ownerCompany != null and parms.ownerCompany != ''">
            and owner.id = #{parms.ownerCompany}
        </if>
        <if test="parms.receivingCompany != null and parms.receivingCompany != ''">
            and receiving.id = #{parms.receivingCompany}
        </if>
        <if test="parms.customerCompany != null and parms.customerCompany != ''">
            and customer.id = #{parms.customerCompany}
        </if>
        <if test="parms.startDtm != null and parms.startDtm != ''">
            and tor.OUT_DTM &gt;= TO_DATE(#{parms.startDtm}, 'yyyy-MM-dd hh24:mi:ss')
        </if>
        <if test="parms.endDtm != null and parms.endDtm != ''">
            and tor.OUT_DTM &lt;= TO_DATE(#{parms.endDtm}, 'yyyy-MM-dd hh24:mi:ss')
        </if>
        <if test="parms.startOutDtm != null and parms.startOutDtm != ''">
            and tor.OUT_DTM &gt;= TO_DATE(#{parms.startOutDtm}, 'yyyy-MM-dd hh24:mi:ss')
        </if>
        <if test="parms.endOutDtm != null and parms.endOutDtm != ''">
            and tor.OUT_DTM &lt;= TO_DATE(#{parms.endOutDtm}, 'yyyy-MM-dd hh24:mi:ss')
        </if>
        <if test="parms.startInDtm != null and parms.startInDtm != ''">
            and tor.IN_DTM &gt;= TO_DATE(#{parms.startInDtm}, 'yyyy-MM-dd hh24:mi:ss')
        </if>
        <if test="parms.endInDtm != null and parms.endInDtm != ''">
            and tor.IN_DTM &lt;= TO_DATE(#{parms.endInDtm}, 'yyyy-MM-dd hh24:mi:ss')
        </if>
        <if test="parms.prodNo != null and parms.prodNo != ''">
            and tor.PROD_NO like '%' || #{parms.prodNo} || '%'
        </if>
        <if test="parms.materialId != null and parms.materialId != ''">
            and tor.MATERIAL_ID = #{parms.materialId}
        </if>
        <if test="parms.outFlag != null and parms.outFlag != ''">
            and tor.OUT_FLAG = #{parms.outFlag}
        </if>
        <if test="parms.distribId != null and parms.distribId != ''">
            and tor.DISTRIB_ID = #{parms.distribId}
        </if>
        <if test="parms.outUserId != null and parms.outUserId != ''">
            and tor.OUT_USER_ID = #{parms.outUserId}
        </if>
    </sql>

    <select id="selectOutrecordPage" resultMap="OutrecordResultMap" parameterType="map">
        select
            tor.id,
            tor.belong_id,
            tor.in_truck_no,
            tor.tally_people,
            tor.in_dtm,
            tor.out_ware_name,
            tor.out_stack_name,
            tor.out_layer_no,
            tor.prod_no,
            tor.owner_company,
            tor.receiving_company,
            tor.customer_company,
            tor.material_id,
            tor.st_grade,
            tor.out_weight,
            tor.out_quantity,
            tor.out_flag,
            nvl(tor.delivery_address, owner.default_address) as delivery_address,
            tor.attrib_01,
            tor.attrib_02,
            tor.attrib_03,
            tor.attrib_04,
            tor.attrib_05,
            tor.attrib_06,
            tor.attrib_07,
            tor.attrib_08,
            tor.attrib_09,
            tor.attrib_10,
            tor.attrib_11,
            tor.attrib_12,
            tor.attrib_13,
            tor.attrib_14,
            tor.attrib_15,
            tor.attrib_16,
            tor.attrib_17,
            tor.attrib_18,
            tor.attrib_19,
            tor.attrib_20,
            tor.attrib_21,
            tor.attrib_22,
            tor.attrib_23,
            tor.attrib_24,
            tor.attrib_25,
            tor.remark_1,
            tor.remark_2,
            tor.remark_3,
            tor.remark_4,
            tor.remark_5,
            tor.out_truck_no,
            tor.out_dtm,
            tor.distrib_id,
            tor.out_user_id,
            owner.customer_nm as owner_company_name,
            receiving.customer_nm as receiving_company_name,
            customer.customer_nm as customer_company_name,
            material.material_name as material_name,
            material.material_code as material_code,
            material.material_standard as material_standard,
            material.material_model as material_model
        from T_OUT_RECORD tor
                 left join T_MAIN_CUSTOMER owner on owner.id = tor.owner_company
                 left join T_MAIN_CUSTOMER receiving on receiving.id = tor.receiving_company
                 left join T_MAIN_CUSTOMER customer on customer.id = tor.customer_company
                 left join T_MAIN_MATERIAL material on material.id = tor.material_id
        <include refid="OutrecordWhere"/>
        order by tor.out_dtm desc
    </select>

    <select id="selectStatistics" resultType="map" parameterType="map">
        select
            nvl(sum(tor.out_quantity), 0) as totalQuantity,
            nvl(sum(tor.out_weight), 0) as totalWeight
        from T_OUT_RECORD tor
                 left join T_MAIN_CUSTOMER owner on owner.id = tor.owner_company
                 left join T_MAIN_CUSTOMER receiving on receiving.id = tor.receiving_company
                 left join T_MAIN_CUSTOMER customer on customer.id = tor.customer_company
                 left join T_MAIN_MATERIAL material on material.id = tor.material_id
        <include refid="OutrecordWhere"/>
    </select>
</mapper>

