<?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.sale.mapper.IcSaleoutHMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="IcSaleoutHExt" type="com.hzs.common.domain.scm.sale.ext.IcSaleoutHExt">
        <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_CREATOR" property="pkCreator" />
        <result column="PK_MODIFIED" property="pkModified" />
        <result column="PK_COUNTRY" property="pkCountry" />
        <result column="PK_DEPT" property="pkDept" />
        <result column="PK_WHSMANAGER" property="pkWhsmanager" />
        <result column="CUSTOMER_TYPE" property="customerType" />
        <result column="PK_CUSTOMER" property="pkCustomer" />
        <result column="PK_STOREHOUSE" property="pkStorehouse" />
        <result column="ORDER_DATE" property="orderDate" />
        <result column="ORDER_STATUS" property="orderStatus" />
        <result column="RETURN_STATUS" property="returnStatus" />
        <result column="PRINT_STATUS" property="printStatus" />
        <result column="PK_CORP" property="pkCorp" />
        <result column="TOTAL_NUM" property="totalNum" />
        <result column="ORDER_CODE" property="orderCode" />
        <result column="RETURN_REASON" property="returnReason" />
        <result column="REMARK" property="remark" />
        <result column="DEPT_NAME" property="deptName" />
        <result column="MANAGER_NAME" property="managerName" />
        <result column="CUSTOMER_NAME" property="customerName" />
        <result column="STOREHOUSE_NAME" property="storehouseName" />
        <result column="CORP_NAME" property="corpName" />
        <result column="PAY_TIME" property="payTime" />
        <result column="SOURCE_ORDER_CODE" property="sourceOrderCode" />
        <collection property="saleoutbExtList" ofType="com.hzs.common.domain.scm.sale.ext.IcSaleoutBExt">
            <result column="PK_ITEM_ID" property="pkId"/>
            <result column="PK_UNIT" property="pkUnit" />
            <result column="PK_SALEOUT" property="pkSaleout" />
            <result column="PK_CLOCATION" property="pkClocation" />
            <result column="PK_PRODUCT" property="pkProduct" />
            <result column="TRANS_TYPE" property="transType" />
            <result column="BIZ_DATE" property="bizDate" />
            <result column="PK_STOREHOUSE_ITEM" property="pkStorehouse" />
            <result column="PK_RATE" property="pkRate" />
            <result column="NNUM" property="nnum" />
            <result column="NORIGPRICE" property="norigprice" />
            <result column="NTAXRATE" property="ntaxrate" />
            <result column="NORIGTAXPRICE" property="norigtaxprice" />
            <result column="NORIGMNY" property="norigmny" />
            <result column="NTAX" property="ntax" />
            <result column="NORIGTAXMNY" property="norigtaxmny" />
            <result column="PK_BATCH_CODE" property="pkBatchCode" />
            <result column="batch_code" property="batchCode" />
            <result column="PRODUCT_CODE" property="productCode" />
            <result column="PRODUCT_NAME" property="productName" />
            <result column="SPECS_NAME" property="specsName" />
            <result column="IS_GIFT" property="isGift" />
            <result column="STO_NAME" property="storehouseName" />
            <result column="PK_SOURCE" property="pkSource" />
            <result column="PK_SOURCE_B" property="pkSourceB" />
            <result column="UNIT_NAME" property="unitName" />
        </collection>
    </resultMap>
    <insert id="insertSaleOutByList">
        insert ALL
        <foreach item="item" index="index" collection="saleOutExtList" separator=" " >
            into ic_saleout_h(pk_id,pk_dept,pk_whsmanager,customer_type,
            pk_customer,pk_storehouse,order_date,order_status,
            pk_corp,total_num,order_code,pk_creator,pk_country)
            values
            (
            #{item.pkId},#{item.pkDept},#{item.pkWhsmanager},#{item.customerType},
            #{item.pkCustomer},#{item.pkStorehouse},#{item.orderDate},#{item.orderStatus},
            #{item.pkCorp},#{item.totalNum},#{item.orderCode},#{item.pkCreator},#{item.pkCountry}
            )
        </foreach>
        SELECT 1 FROM dual
    </insert>
    <select id="querySaleOutSql" resultType="java.lang.Long">
        SELECT ic_saleout_h_seq.nextval
        FROM (select level from dual connect by #{size} >= level)
    </select>
    <select id="queryDisSaleOutExtCondition" resultMap="IcSaleoutHExt">
        select ih.*,so.order_code source_order_code,so.pay_time,sd.dept_name,su.nick_name manager_name,bc.member_name customer_name,sc.corp_name,
        ib.pk_id pk_item_id,ib.pk_unit,ib.pk_clocation,ib.pk_product,ib.biz_date,ib.pk_storehouse pk_storehouse_item,ib.pk_rate,ib.nnum,
        ib.norigprice,ib.ntaxrate,ib.norigtaxprice,ib.norigmny,ib.ntax,ib.norigtaxmny,bn.name unit_name,ic.batch_code,
        ib.pk_batch_code,ib.is_gift,bh.name sto_name,bp.product_code,bp.product_name,sp.specs_name from ic_saleout_h ih
        inner join ic_saleout_b ib
        on ih.pk_id=ib.pk_saleout
        left join sa_order so
        on so.pk_id=ib.pk_sa_order
        left join sys_dept sd
        on ih.pk_dept=sd.dept_id
        left join sys_user su
        on ih.pk_whsmanager=su.user_id
        left join cu_member bc
        on ih.pk_customer=bc.pk_id
        left join sys_corp sc
        on ih.pk_corp=sc.pk_id
        left join bd_unit bn
        on ib.pk_unit = bn.pk_id
        left join bd_storehouse bh
        on ib.pk_storehouse=bh.pk_id
        left join ic_batch_code ic
        on ib.pk_batch_code=ic.pk_id
        left join bd_product bp
        on ib.pk_product=bp.pk_id
        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=6
        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="saOrderCode != null">
            and so.order_code= #{saOrderCode}
        </if>
        <if test="pkCountry != null">
            and ih.pk_country=#{pkCountry}
        </if>
        <if test="returnStatus != null">
            and ih.return_status = #{returnStatus}
        </if>
        <if test="pkId != null">
            and ih.pk_id=#{pkId}
        </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>
    </select>
    <select id="querySaleOutExtId" resultMap="IcSaleoutHExt">
        select ih.*,ib.pk_id pk_item_id,ib.pk_unit,ib.pk_clocation,ib.pk_product,ib.biz_date,ib.pk_storehouse pk_storehouse_item,ib.pk_rate,ib.nnum,
               ib.norigprice,ib.ntaxrate,ib.norigtaxprice,ib.norigmny,ib.ntax,ib.norigtaxmny,ib.pk_source,ib.pk_source_b,
               ib.pk_batch_code,ib.is_gift from ic_saleout_h ih
        inner join ic_saleout_b ib
                   on ih.pk_id=ib.pk_saleout
        where ih.del_flag=0 and ib.del_flag=0
          and ih.pk_id=#{pkId}
          and ih.order_status = #{orderStatus}
    </select>
</mapper>
