<?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.hzs.scm.inventory.mapper.IcInventoryHMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="IcInventoryHExt" type="com.hzs.common.domain.scm.inventory.ext.IcInventoryHExt">
        <id column="PK_ID" property="pkId" />
        <result column="CREATION_TIME" property="creationTime" />
        <result column="MODIFIED_TIME" property="modifiedTime" />
        <result column="DEL_FLAG" property="delFlag" />
        <result column="PK_COUNTRY" property="pkCountry" />
        <result column="PK_CREATOR" property="pkCreator" />
        <result column="PK_MODIFIED" property="pkModified" />
        <result column="PK_DEPT" property="pkDept" />
        <result column="PK_CORP" property="pkCorp" />
        <result column="PK_WHSMANAGER" property="pkWhsmanager" />
        <result column="PK_ADJUSTOR" property="pkAdjustor" />
        <result column="PK_WAREHOUSE" property="pkWarehouse" />
        <result column="INV_TYPE" property="invType" />
        <result column="TRANS_TYPE" property="transType" />
        <result column="ORDER_DATE" property="orderDate" />
        <result column="ORDER_STATUS" property="orderStatus" />
        <result column="PRINT_STATUS" property="printStatus" />
        <result column="ORDER_CODE" property="orderCode" />
        <result column="REMARK" property="remark" />
        <result column="DEPT_NAME" property="deptName" />
        <result column="MANAGER_NAME" property="managerName" />
        <result column="STOREHOUSE_NAME" property="storehouseName" />
        <result column="CORP_NAME" property="corpName" />
        <result column="ADJUSTOR_NAME" property="adjustorName" />
        <collection property="inventoryBiExtList" ofType="com.hzs.common.domain.scm.inventory.ext.IcInventoryBExt">
            <result column="pk_item_id" property="pkId" />
            <result column="PK_UNIT" property="pkUnit" />
            <result column="PK_ONHANDNUM" property="pkOnhandnum"/>
            <result column="PK_INVENTORY" property="pkInventory" />
            <result column="PK_PRODUCT" property="pkProduct" />
            <result column="PK_CLOCATION" property="pkClocation" />
            <result column="PK_CUBASDOC" property="pkCubasdoc" />
            <result column="PRODUCT_DATE" property="productDate" />
            <result column="EXPIRE_DATE" property="expireDate" />
            <result column="NONHANDNUM" property="nonhandnum" />
            <result column="NADJUSTNUM" property="nadjustnum" />
            <result column="NDIFFASTNUM" property="ndiffastnum" />
            <result column="PK_BATCH_CODE" property="pkBatchCode" />
            <result column="UNIT_NAME" property="unitName" />
            <result column="CLOCATION_NAME" property="clocationName" />
            <result column="CUBASDOC_NAME" property="cubasdocName" />
            <result column="BATCH_CODE" property="batchCode" />
            <result column="PRODUCT_CODE" property="productCode" />
            <result column="PRODUCT_NAME" property="productName" />
            <result column="SPECS_NAME" property="specsName" />
        </collection>
    </resultMap>
    <select id="queryIcInventoryById" resultMap="IcInventoryHExt">
        select ih.*,ib.pk_id pk_item_id,ib.pk_unit,ib.pk_clocation,ib.pk_product,ib.pk_cubasdoc,ib.product_date,
               ib.expire_date,ib.nonhandnum,ib.nadjustnum,ib.ndiffastnum,ib.pk_batch_code from ic_inventory_h ih
       inner join ic_inventory_b ib
                  on ih.pk_id=ib.pk_inventory
        where ih.del_flag=0 and ib.del_flag=0
          and ih.pk_id = #{pkId}
          and ih.order_status= #{orderStatus}
    </select>
    <select id="queryDisIcInventoryExtByCondition" resultMap="IcInventoryHExt">
        select ih.*,sd.dept_name,su.nick_name manager_name,suu.nick_name adjustor_name,sc.corp_name,bc.cust_name cubasdoc_name,
        ib.pk_id pk_item_id,ib.pk_unit,ib.pk_onhandnum,ib.pk_clocation,ib.pk_product,ib.pk_cubasdoc,
        ib.nonhandnum,ib.nadjustnum,ib.ndiffastnum,ib.pk_batch_code,
        bn.name unit_name,bh.name storehouse_name,ic.batch_code,nvl(ic.product_date,ib.product_date) product_date,
        nvl(ic.expire_date,ib.expire_date) expire_date,
        bp.product_code,bp.product_name,sp.specs_name from ic_inventory_h ih
          inner join ic_inventory_b ib
                     on ih.pk_id=ib.pk_inventory and ib.del_flag=0
          left join sys_dept sd
                    on ih.pk_dept=sd.dept_id and sd.del_flag=0
          left join sys_user su
                    on ih.pk_whsmanager=su.user_id and su.del_flag=0
            left join sys_user suu
            on ih.pk_adjustor=suu.user_id and su.del_flag=0
          left join sys_corp sc
                    on ih.pk_corp=sc.pk_id and sc.del_flag=0
          left join bd_unit bn
                    on ib.pk_unit = bn.pk_id and bn.del_flag=0
          left join bd_storehouse bh
                    on ih.pk_warehouse=bh.pk_id and bh.del_flag=0
          left join ic_batch_code ic
                    on ib.pk_batch_code=ic.pk_id and ic.del_flag=0
          left join bd_product bp
                    on ib.pk_product=bp.pk_id and bp.del_flag=0
          left join bd_cubasdoc bc
          on bc.pk_id = ib.pk_cubasdoc and bc.del_flag=0
          left join (
            select pk_order,pk_order_items,wm_concat(specs_name) specs_name from scm_item_specs
            where del_flag=0 and order_type=10
            group by pk_order,pk_order_items) sp
         on ib.pk_id=sp.pk_order_items
        where ih.del_flag=0 and ib.del_flag=0
        <if test="pkCountry != null">
            and ih.pk_country=#{pkCountry}
        </if>
        <if test="pkId != null">
            and ih.pk_id=#{pkId}
        </if>
        <if test="pkWarehouse != null">
            and ih.pk_warehouse = #{pkWarehouse}
        </if>
        <if test="invType != null">
            and ih.inv_type = #{invType}
        </if>
        <if test="orderStatus != null">
            and ih.order_status = #{orderStatus}
        </if>
        <if test="pkCorp != null">
            and ih.pk_corp = #{pkCorp}
        </if>
        <if test="orderCode != null and orderCode != ''">
            and ih.order_code = #{orderCode}
        </if>
        <if test="startDate != null">
            and ih.order_date >= #{startDate, jdbcType=DATE}
        </if>
        <if test="endDate != null">
            and ih.order_date &lt; #{endDate, jdbcType=DATE}
        </if>
        <if test="productName != null and productName != ''">
            and bp.product_name=#{productName}
        </if>
        order by ih.order_date desc
    </select>


</mapper>
