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

    <!-- 通用查询映射结果 -->
    <resultMap id="SaOrderSelfLogExt" type="com.hzs.common.domain.sale.ext.SaOrderSelfLogExt">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_ORDER" property="pkOrder"/>
        <result column="SELF_CODE" property="selfCode"/>
        <result column="PHONE" property="phone"/>
        <result column="REMARK" property="remark"/>
        <result column="APPROVE_STATUS" property="approveStatus"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="ORDER_CODE" property="orderCode"/>
        <result column="ORDER_TYPE" property="orderType"/>
        <result column="APPLY_NAME" property="applyName"/>
        <result column="CREATOR" property="creator"/>
        <result column="COVER" property="cover"/>
        <result column="PRODUCT_NAME" property="productName"/>
        <result column="SPECS_NAME" property="specsName"/>
        <result column="CLASSIFY_TYPE" property="classifyType"/>
        <result column="IS_GIFT" property="isGift"/>
        <result column="QUANTITY" property="quantity"/>
        <result column="ORDER_ITEMS_ID" property="orderItemsId"/>
        <result column="REJECT_MSG" property="rejectMsg"/>
        <result column="PRODUCT_ID" property="productId"/>
    </resultMap>

    <select id="querySaOrderSelfLog" resultMap="SaOrderSelfLogExt">
        select sl.pk_id,sl.self_code,sl.phone,sl.remark,sl.approve_status,sl.REJECT_MSG,cmb.member_name applyName,
        sl.creation_time,cm.member_code,cmr.member_name creator,
        cm.member_name,
        so.order_code,
        so.order_type,
        bp.cover,
        bp.product_name,
        soi.pk_id order_items_id,
        soi.is_gift,
        soi.QUANTITY,
        soi.specs_name,
        bc.name classify_type,
        soi.pk_product PRODUCT_ID
        from sa_order_self_log sl
        inner join sa_order so
        on sl.pk_order = so.pk_id
        inner join cu_member cm
        on cm.pk_id = so.pk_member
        left join cu_member cmb
        on sl.pk_creator = cmb.pk_id
        left join sa_order_items soi
        on soi.pk_order = so.pk_id
        and soi.del_flag = 0
        inner join BD_PRODUCT bp
        on bp.pk_id = soi.pk_product
        inner join bd_classify bc
        on bp.pk_classify = bc.pk_id
        left join cu_member cmr
        on sl.pk_creator = cmr.pk_id
        where so.del_flag = 0 and sl.del_flag=0 and cm.del_flag=0 and soi.del_flag=0
        and so.pk_country = #{pkSettleCountry}
        <if test="memberCode != null and memberCode != ''">
            and cm.member_code=#{memberCode}
        </if>
        <if test="pkMember != null">
            and so.pk_member=#{pkMember}
        </if>

        <if test="memberName != null and memberName != ''">
            and cm.member_name = #{memberName}
        </if>
        <if test="orderCode != null and orderCode != ''">
            and so.order_code = #{orderCode}
        </if>
        <if test="orderType != null and orderType != ''">
            and so.order_type = #{orderType}
        </if>
        <if test="phone != null and phone != ''">
            and sl.phone = #{phone}
        </if>
        <if test="selfCode != null and selfCode != ''">
            and sl.self_code = #{selfCode}
        </if>
        <if test="approveStatus != null">
            and sl.approve_status = #{approveStatus}
        </if>
        <if test="startDate != null">
            and sl.creation_time >=#{startDate}
        </if>
        <if test="endDate != null">
            and sl.creation_time &lt;= #{endDate}
        </if>
        order by so.order_code desc
    </select>

    <select id="queryMemberSaOrderSelfLog" resultMap="SaOrderSelfLogExt">
        select so.order_code,so.pay_time,sl.self_code,sl.approve_status,sl.creation_time,sl.remark,
        bp.cover,bp.product_name,
        soi.is_gift,
        soi.QUANTITY,
        soi.specs_name,
        soi.pk_product PRODUCT_ID
        from sa_order_self_log sl
        inner join sa_order so
        on sl.pk_order = so.pk_id
        left join sa_order_items soi
        on soi.pk_order = so.pk_id
        and soi.del_flag = 0
        inner join BD_PRODUCT bp
        on bp.pk_id = soi.pk_product
        where so.del_flag = 0 and sl.del_flag=0 and soi.del_flag=0
        and so.pk_country = #{pkSettleCountry}
        <if test="startDate != null">
            and sl.creation_time >=#{startDate}
        </if>
        <if test="endDate != null">
            and sl.creation_time &lt;= #{endDate}
        </if>
        <if test="selfCode != null and selfCode != ''">
            and sl.self_code = #{selfCode}
        </if>
        <if test="approveStatus != null">
            and sl.approve_status = #{approveStatus}
        </if>
    </select>

</mapper>
