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

    <resultMap id="InrecordResultMap" type="com.shinsoft.sto.model.record.Inrecord">
        <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="in_ware_name" property="inWareName" jdbcType="VARCHAR"/>
        <result column="in_stack_name" property="inStackName" jdbcType="VARCHAR"/>
        <result column="in_layer_no" property="inLayerNo" 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="in_weight" property="inWeight" jdbcType="DECIMAL"/>
        <result column="in_quantity" property="inQuantity" jdbcType="DECIMAL"/>
        <result column="in_flag" property="inFlag" 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="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="StatisticsWhere">
        where 1=1
<!--        <if test="parms.belongId != null and parms.belongId != ''">-->
<!--            and ir.belong_id = #{parms.belongId}-->
<!--        </if>-->
        <if test="parms.wareName != null and parms.wareName != ''">
            and ir.IN_WARE_NAME like   '%' || #{parms.wareName} || '%'
        </if>
        <if test="parms.stackName != null and parms.stackName != ''">
            and ir.IN_STACK_NAME like   '%' || #{parms.stackName} || '%'
        </if>
        <if test="parms.inTruckNo != null and parms.inTruckNo != ''">
            and ir.in_truck_no like '%' || #{parms.inTruckNo} || '%'
        </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 owner.id =#{parms.receivingCompany}
        </if>
        <if test="parms.customerCompany != null and parms.customerCompany != ''">
            and owner.id =#{parms.customerCompany}
        </if>
        <if test="parms.startDtm != null and parms.startDtm != ''">
            and ir.IN_DTM &gt;= TO_DATE(#{parms.startDtm}, 'yyyy-MM-dd hh24:mi:ss')
        </if>
        <if test="parms.endDtm != null and parms.endDtm != ''">
            and ir.IN_DTM &lt;= TO_DATE(#{parms.endDtm}, 'yyyy-MM-dd hh24:mi:ss')
        </if>
<!--        <if test="parms.prodNo != null and parms.prodNo != ''">-->
<!--            and ir.PROD_NO like '%' || #{parms.prodNo} || '%'-->
<!--        </if>-->
        <if test="parms.stGrade != null and parms.stGrade.size() > 0 and parms.stGrade!=''">
            and ir.st_grade in
            <foreach collection="parms.stGrade" item="item" open="(" separator="," close=")">
                #{item,jdbcType=VARCHAR}
            </foreach>
        </if>
        <if test="parms.attrib_01 != null and parms.attrib_01.size() > 0">
            and PROD.attrib_01 in
            <foreach collection="parms.attrib_01" item="item" open="(" separator="," close=")">
                #{item,jdbcType=VARCHAR}
            </foreach>
        </if>
        <if test="parms.attrib_02 != null and parms.attrib_02.size() > 0 ">
            and PROD.attrib_02 in
            <foreach collection="parms.attrib_02" item="item" open="(" separator="," close=")">
                #{item,jdbcType=VARCHAR}
            </foreach>
        </if>
        <if test="parms.attrib_04 != null and parms.attrib_04.size() > 0 ">
            and PROD.attrib_04 in
            <foreach collection="parms.attrib_04" item="item" open="(" separator="," close=")">
                #{item,jdbcType=VARCHAR}
            </foreach>
        </if>
        <if test="parms.inTruckNo != null and parms.inTruckNo.size() > 0 ">
            and PROD.IN_TRUCK_NO in
            <foreach collection="parms.inTruckNo" item="item" open="(" separator="," close=")">
                #{item,jdbcType=VARCHAR}
            </foreach>
        </if>
        <if test="parms.prodNo != null and parms.prodNo.size() > 0 ">
            and ir.prod_no in
            <foreach collection="parms.prodNo" item="item" open="(" separator="," close=")">
                #{item,jdbcType=VARCHAR}
            </foreach>
        </if>
<!--        <if test="parms.orderNo != null and parms.orderNo != ''">-->
<!--            and ir.order_NO like '%' || #{parms.orderNo} || '%'-->
<!--        </if>-->

<!--        <if test="parms.contractNo != null and parms.contractNo != ''">-->
<!--            and ir.contract_no like '%' || #{parms.contractNo} || '%'-->
<!--        </if>-->
    </sql>

    <select id="selectInrecordPage" resultMap="InrecordResultMap" parameterType="map">
        select
        ir.id,
        ir.belong_id,
        ir.in_truck_no,
        ir.tally_people,
        ir.in_dtm,
        ir.in_ware_name,
        ir.in_stack_name,
        ir.in_layer_no,
        ir.prod_no,
        ir.owner_company,
        ir.receiving_company,
        ir.customer_company,
        ir.material_id,
        ir.st_grade,
        ir.in_weight,
        ir.in_quantity,
        ir.in_flag,
        nvl(ir.delivery_address, owner.default_address) as delivery_address,
        ir.attrib_01,
        ir.attrib_02,
        ir.attrib_03,
        ir.attrib_04,
        ir.attrib_05,
        ir.attrib_06,
        ir.attrib_07,
        ir.attrib_08,
        ir.attrib_09,
        ir.attrib_10,
        ir.attrib_11,
        ir.attrib_12,
        ir.attrib_13,
        ir.attrib_14,
        ir.attrib_15,
        ir.attrib_16,
        ir.attrib_17,
        ir.attrib_18,
        ir.attrib_19,
        ir.attrib_20,
        ir.attrib_21,
        ir.attrib_22,
        ir.attrib_23,
        ir.attrib_24,
        ir.attrib_25,
        ir.remark_1,
        ir.remark_2,
        ir.remark_3,
        ir.remark_4,
        ir.remark_5,
        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,
        prod.ATTRIB_01 as orderNo,
        PROD.ATTRIB_02 as contractNo,
        PROD.IN_TRUCK_NO as carNo,
        PROD.ATTRIB_03 as specification,
        PROD.ATTRIB_05 as placeOfOrigin,
        PROD.IN_USER_NAME,
        PROD.ATTRIB_04 as bundleNumber,
        PROD.id as prodId
        from T_IN_RECORD ir
        left join T_MAIN_CUSTOMER owner on owner.id = ir.owner_company
        left join T_MAIN_CUSTOMER receiving on receiving.id = ir.receiving_company
        left join T_MAIN_CUSTOMER customer on customer.id = ir.customer_company
        left join T_MAIN_MATERIAL material on material.id = ir.material_id
        left JOIN T_PROD_MASTER PROD ON PROD.PROD_No = ir.prod_no
        <include refid="StatisticsWhere"/>
        order by ir.in_dtm desc
    </select>

    <select id="selectStatistics" resultType="map" parameterType="map">
        select
            nvl(sum(ir.in_quantity), 0) as totalQuantity,
            nvl(sum(ir.in_weight), 0) as totalWeight
        from T_IN_RECORD ir
                 left join T_MAIN_CUSTOMER owner on owner.id = ir.owner_company
                 left join T_MAIN_CUSTOMER receiving on receiving.id = ir.receiving_company
                 left join T_MAIN_CUSTOMER customer on customer.id = ir.customer_company
                 left join T_MAIN_MATERIAL material on material.id = ir.material_id
        <include refid="StatisticsWhere"/>
    </select>
</mapper>

