<?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.io.mapper.IoOrderMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="IoOrderExt" type="com.hzs.common.domain.scm.io.ext.IoOrderExt">
        <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="PK_RATE" property="pkRate" />
        <result column="ORDER_STATUS" property="orderStatus" />
        <result column="PRINT_STATUS" property="printStatus" />
        <result column="PK_CUBASDOC" property="pkCubasdoc" />
        <result column="PK_CORP" property="pkCorp" />
        <result column="TOTAL_NUM" property="totalNum" />
        <result column="ORDER_CODE" property="orderCode" />
        <result column="ORDER_DATE" property="orderDate" />
        <result column="PK_PURPOSE" property="pkPurpose" />
        <result column="PURPOSE" property="purpose" />
        <result column="REMARK" property="remark" />
        <result column="REC_NAME" property="recName"/>
        <result column="REC_PHONE" property="recPhone"/>
        <result column="REC_PROVINCE" property="recProvince"/>
        <result column="REC_CITY" property="recCity"/>
        <result column="REC_COUNTY" property="recCounty"/>
        <result column="REC_PROVINCE_NAME" property="recProvinceName"/>
        <result column="REC_CITY_NAME" property="recCityName"/>
        <result column="REC_COUNTY_NAME" property="recCountyName"/>
        <result column="REC_ADDRESS" property="recAddress"/>
        <collection property="orderItemsExtList" ofType="com.hzs.common.domain.scm.io.ext.IoOrderItemsExt">
            <result column="PK_ITEMS" property="pkId" />
            <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_ORDER" property="pkOrder" />
            <result column="PK_PRODUCT_BAS" property="pkProductBas" />
            <result column="PK_STOREHOUSE" property="pkStorehouse" />
            <result column="PK_UNIT" property="pkUnit" />
            <result column="PK_RATE" property="pkRate" />
            <result column="NNUM" property="nnum" />
            <result column="PK_PRODUCT" property="pkProduct" />
            <result column="PK_SUPPLIER" property="pkSupplier" />
            <result column="STOREHOUSE_NAME" property="storehouseName" />
        </collection>
    </resultMap>
    <select id="queryIoOrderById" resultMap="IoOrderExt">
        select io.*,bps.purpose,it.pk_id pk_items,it.pk_product_bas,it.pk_unit,
        it.nnum,it.pk_creator item_pk_creator,bp.pk_id pk_product,bh.name storehouse_name from io_order io
        inner join io_order_items it
        on io.pk_id=it.pk_order
        left join bd_product bp
        on bp.pk_product_bas=it.pk_product_bas and bp.pk_country=io.pk_country and bp.del_flag=0
        left join bd_purpose bps
        on bps.pk_id=io.pk_purpose and bps.del_flag=0
        left join bd_storehouse bh
        on it.pk_storehouse=bh.pk_id and bh.del_flag=0
        where io.del_flag=0 and it.del_flag=0
        and io.pk_id=#{pkOrder}
        <if test="orderStatus != null">
            and io.order_status = #{orderStatus}
        </if>
    </select>
    <select id="queryIoOrderByCondition" resultMap="IoOrderExt">
        select po.*,bps.purpose,sd.dept_name,su.nick_name manager_name,bc.cust_name cubasdoc_name,sc.corp_name,
        pi.pk_id pk_items,pi.pk_unit,pi.pk_product_bas,pi.pk_storehouse,pi.nnum,bn.name unit_name,bh.name storehouse_name,
        pr.name rec_province_name,ct.name rec_city_name,co.name rec_county_name,
        bp.product_code,bp.product_name,sp.specs_name from io_order po
        inner join io_order_items pi
        on po.pk_id=pi.pk_order
        left join sys_dept sd
        on po.pk_dept=sd.dept_id
        left join sys_user su
        on po.pk_whsmanager=su.user_id
        left join bd_cubasdoc bc
        on po.pk_cubasdoc=bc.pk_id
        left join sys_corp sc
        on po.pk_corp=sc.pk_id
        left join bd_unit bn
        on pi.pk_unit = bn.pk_id
        left join bd_storehouse bh
        on pi.pk_storehouse=bh.pk_id
        left join bd_product_bas bp
        on pi.pk_product_bas=bp.pk_id
        left join bd_purpose bps
        on bps.pk_id=po.pk_purpose and bps.del_flag=0
        left join BD_AREA pr on pr.pk_id=po.REC_PROVINCE and pr.del_flag=0
        left join BD_AREA ct on ct.pk_id=po.REC_CITY and ct.del_flag=0
        left join BD_AREA co on co.pk_id=po.REC_COUNTY and co.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=11
        group by pk_order,pk_order_items) sp
        on pi.pk_id=sp.pk_order_items
        where po.del_flag=0 and pi.del_flag=0
        <if test="orderCode != null and orderCode != ''">
            and po.order_code = #{orderCode}
        </if>
        <if test="pkCountry != null">
            and po.pk_country=#{pkCountry}
        </if>
        <if test="pkId != null">
            and po.pk_id=#{pkId}
        </if>
        <if test="orderStatus != null">
            and po.order_status = #{orderStatus}
        </if>
        <if test="pkCubasdoc != null">
            and po.pk_cubasdoc = #{pkCubasdoc}
        </if>
        <if test="pkCorp != null">
            and po.pk_corp = #{pkCorp}
        </if>
        <if test="startDate != null">
            and po.order_date >= #{startDate, jdbcType=DATE}
        </if>
        <if test="endDate != null">
            and po.order_date &lt; #{endDate, jdbcType=DATE}
        </if>
        <if test="productName != null and productName != ''">
            and bp.product_name=#{productName}
        </if>
        order by po.creation_time desc
    </select>

</mapper>
