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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.sale.order.SaOrderChargeLog">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_ORDER" property="pkOrder"/>
        <result column="CHARGE_TYPE" property="chargeType"/>
        <result column="BEFORE_DATA" property="beforeData"/>
        <result column="AFTER_DATA" property="afterData"/>
        <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="APPROVAL_STATUS" property="approvalStatus"/>
        <result column="BUSINESS_CODE" property="businessCode"/>
        <result column="REMARK" property="remark"/>
        <result column="SOURCE" property="source"/>

    </resultMap>

    <!-- 查询订单以及修改次数 -->
    <select id="queryOrderAndLogCount" resultType="com.hzs.common.domain.sale.ext.SaOrderChargeLogExt">
        select pk_order, count(pk_order) log_count
        from SA_ORDER_CHARGE_LOG socl
        where socl.del_flag = 0
        <if test="pkCountry != null">
            and socl.pk_country = #{pkCountry}
        </if>
        and socl.pk_order in
        <foreach collection="pkOrderList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and socl.approval_status = 2
        group by pk_order
    </select>

    <!-- 根据类型和编号查询数据 -->
    <select id="queryDataByTypeAndCode" resultType="com.hzs.common.domain.sale.ext.SaOrderChargeLogExt">
        select socl.*,
        so.order_code,
        cm.member_code creator_member_code,
        cm.member_name creator_member_name,
        cmm.member_code,
        cmm.member_name
        from sa_order_charge_log socl
        inner join sa_order so
        on so.pk_id = socl.pk_order
        inner join cu_member cm
        on cm.pk_id = so.pk_creator
        inner join cu_member cmm
        on cmm.pk_id = so.pk_member
        where socl.del_flag = 0
        <if test="pkCountry != null">
            and socl.pk_country = #{pkCountry}
        </if>
        and socl.charge_type = #{chargeType}
        and socl.business_code = #{businessCode}
    </select>

    <sql id="checkOrderApproval">
        select count(socl.pk_id)
        from SA_ORDER_CHARGE_LOG socl
        where socl.del_flag = 0
        and socl.pk_country = #{pkCountry}
        and socl.charge_type in
        <foreach collection="typeList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and socl.approval_status = 1
        and socl.pk_order in
        (select pk_id
        from sa_order so
        where so.del_flag = 0
        and so.pk_country = #{pkCountry}
        and so.order_code in
        <foreach collection="orderCodeList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        )
    </sql>
    <!-- 订单审批是否进行中 -->
    <select id="checkOrderApprovalExist" resultType="int">
        <include refid="checkOrderApproval"></include>
    </select>
    <select id="checkOrderApprovalExistRemoveBusinessCode" resultType="java.lang.Integer">
        <include refid="checkOrderApproval"></include>
        and business_code != #{businessCode}
    </select>

    <select id="selectByOrderCharge" resultType="com.hzs.common.domain.sale.ext.SaOrderChargeLogExt">

        select
        ocl.CREATION_TIME cancelOrderTime,ocl.CHARGE_TYPE chargeType,ocl.PK_ORDER pkOrder,so.ORDER_CODE orderCode,so.ORDER_TYPE orderType,cm.MEMBER_NAME memberName ,cm.MEMBER_CODE memberCode,
        cm.CUSTOMER_TYPE customerType,cm.PHONE phone,cm.PK_PARENT pkParent,cm.PK_PLACE_PARENT pkPlaceParent,cm.PK_CENTER_CODE pkCenterCode,
        so.ORDER_AMOUNT orderAmount,so.ORDER_ACHIEVE orderAchieve,so.PK_RATE pkRate,so.PK_VERTEX pkVertex,cm.PK_TEAM_CODE pkTeamCode,
        so.REC_NAME recName,so.REC_PHONE recPhone,so.REC_PROVINCE recProvince ,so.REC_CITY recCity,so.REC_COUNTY recCounty,so.REC_ADDRESS recAddress,
        so.POSTAGE postage,so.ORDER_STATUS orderStatus,so.CREATION_TIME creationTime,so.PAY_TIME payTime,so.DELIVERY_WAY deliveryWay,so.TRAN_TYPE tranType,
        so.PAY_TYPE payType,ocl.PK_CREATOR pkCreator,ocl.APPROVAL_STATUS approvalStatus,
        (select ab.APPROVAL_CODE from SYS_APPROVAL_BUSINESS ab where ab.BUSINESS_CODE = ocl.BUSINESS_CODE and ab.APPROVAL_BUSINESS = ocl.CHARGE_TYPE) approvalCode,
        su.USER_NAME lastInstanceApprover,ocl.MODIFIED_TIME lastInstanceDate ,ocl.REMARK lastInstanceRemark,bc.IN_EXCHANGE_RATE inExchangeRate,cm.PLACE_DEPT placeDept,
        s.USER_NAME cancelTheOrderName
        from SA_ORDER_CHARGE_LOG ocl
        left join SA_ORDER so on so.PK_ID = ocl.PK_ORDER

        left join CU_MEMBER cm on cm.PK_ID = so.PK_MEMBER
        left join SYS_USER su on su.USER_ID = ocl.PK_MODIFIED
        left join BD_CURRENCY bc on bc.PK_ID = so.PK_RATE
        left join sys_user s on s.USER_ID = ocl.PK_CREATOR
        where 1 = 1 and ocl.DEL_FLAG = 0 and (ocl.CHARGE_TYPE = 233 or ocl.CHARGE_TYPE = 234)
        <if test="approvalCode != null and approvalCode != ''">
            and (select count(ab.PK_ID) from SYS_APPROVAL_BUSINESS ab where ab.APPROVAL_BUSINESS = ocl.CHARGE_TYPE and ab.BUSINESS_CODE = ocl.BUSINESS_CODE and ab.APPROVAL_CODE = #{approvalCode}) > 0
        </if>
        <if test="orderCode != null and orderCode != ''">
            and so.ORDER_CODE = #{orderCode}
        </if>
        <if test="memberName != null and memberName != ''">
            and cm.MEMBER_NAME = #{memberName}
        </if>
        <if test="memberCode != null and memberCode != ''">
            and cm.MEMBER_CODE = #{memberCode}
        </if>
        <if test="phone != null and phone != ''">
            and cm.PHONE = #{phone}
        </if>
        <if test="tranType != null">
            and so.TRAN_TYPE = #{tranType}
        </if>
        <if test="payType != null">
            and so.PAY_TYPE = #{payType}
        </if>
        <if test="pkVertex != null">
            and so.PK_VERTEX = #{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            and cm.PK_TEAM_CODE = #{pkTeamCode}
        </if>

        <if test="creatorName != null and creatorName != ''">
            and (select count(su.USER_ID) from SYS_USER su where ocl.PK_CREATOR = su.USER_ID and su.NICK_NAME = #{creatorName}) > 0
        </if>
        <if test="parentName != null and parentName != ''">
            and (select count(me.PK_ID) from CU_MEMBER me where me.PK_PARENT = cm.PK_PARENT and me.MEMBER_NAME = #{parentName}) > 0
        </if>
        <if test="parentCode != null and parentCode != ''">
            and (select count(me.PK_ID) from CU_MEMBER me where me.PK_PARENT = cm.PK_PARENT and me.MEMBER_CODE = #{parentCode}) > 0
        </if>
        <if test="placeParentName != null and placeParentName != ''">
            and (select count(me.PK_ID) from CU_MEMBER me where me.PK_PLACE_PARENT = cm.PK_PLACE_PARENT and me.MEMBER_NAME = #{placeParentName}) > 0
        </if>
        <if test="placeParentCode != null and placeParentCode != ''">
            and (select count(me.PK_ID) from CU_MEMBER me where me.PK_PARENT = cm.PK_PARENT and me.MEMBER_CODE = #{placeParentCode}) > 0
        </if>
        <if test="pkCenterCode != null and pkCenterCode != ''">
            and (select count(me.PK_ID) from CU_MEMBER me where me.pkCenterCode = cm.pkCenterCode and me.MEMBER_CODE = #{pkCenterCode}) > 0
        </if>
        <if test="pkCenterName != null and pkCenterName != ''">
            and (select count(me.PK_ID) from CU_MEMBER me where me.pkCenterCode = cm.pkCenterCode and me.MEMBER_NAME = #{pkCenterName}) > 0
        </if>
        <if test="pkCountry != null">
            and so.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="orderType != null">
            and so.ORDER_TYPE = #{orderType}
        </if>
        <if test="deliveryWay != null">
            and so.DELIVERY_WAY = #{deliveryWay}
        </if>
        <if test="orderStatus != null">
            and so.ORDER_STATUS = #{orderStatus}
        </if>

        <if test="operateScope != null">
            and (
            select
            count(oi.PK_ID)
            from sa_order_items oi
            left join BD_WARES_EXTEND bw on bw.PK_WARES = oi.PK_WARES
            where oi.PK_ORDER = ocl.PK_ORDER and bw.OPERATE_SCOPE = #{operateScope}
            ) > 0
        </if>

        <if test="preSaleStatus != null">
            and (
            select
            count(oi.PK_ID)
            from sa_order_items oi
            left join BD_WARES_EXTEND bw on bw.PK_WARES = oi.PK_WARES
            where oi.PK_ORDER = ocl.PK_ORDER and bw.PRE_SALE_STATUS = #{preSaleStatus}
            ) > 0
        </if>
        <if test="productName != null and productName != ''">
            and (
            select
            count(oi.PK_ID)
            from sa_order_items oi
            left join BD_PRODUCT bp on bp.PK_ID = oi.PK_PRODUCT
            where oi.PK_ORDER = ocl.PK_ORDER and bp.PRODUCT_NAME = #{productName}
            ) > 0
        </if>
        <if test="specsName != null and specsName != ''">
            and (
            select
            count(oi.PK_ID)
            from sa_order_items oi
            where oi.PK_ORDER = ocl.PK_ORDER and oi.SPECS_NAME = #{specsName}
            ) > 0
        </if>
        <if test="logisticsCompany != null and logisticsCompany != ''">
            and (
            select
            count(oi.PK_ID)
            from sa_order_items oi
            left join BD_PRODUCT bp on bp.PK_ID = oi.PK_PRODUCT
            left join SA_DELIVER_ITEMS di on di.PK_ORDER_ITEMS = oi.PK_ID
            left join SA_DELIVER sd on sd.PK_ID = di.PK_DELIVER
            where oi.PK_ORDER = ocl.PK_ORDER and sd.LOGISTICS_COMPANY = #{logisticsCompany}
            ) > 0
        </if>
        <if test="logisticsCode != null and logisticsCode != ''">
            and (
            select
            count(oi.PK_ID)
            from sa_order_items oi
            left join BD_PRODUCT bp on bp.PK_ID = oi.PK_PRODUCT
            left join SA_DELIVER_ITEMS di on di.PK_ORDER_ITEMS = oi.PK_ID
            left join SA_DELIVER sd on sd.PK_ID = di.PK_DELIVER
            where oi.PK_ORDER = ocl.PK_ORDER and sd.LOGISTICS_CODE = #{logisticsCode}
            ) > 0
        </if>
        <if test="recName != null and recName !=''">
            and so.REC_NAME = #{recName}
        </if>
        <if test="recPhone != null and recPhone !=''">
            and so.REC_PHONE = #{recPhone}
        </if>
        <if test="recProvince != null">
            and so.REC_PROVINCE = #{recProvince}
        </if>
        <if test="recCity != null">
            and so.REC_CITY = #{recCity}
        </if>
        <if test="recCounty != null">
            and so.REC_COUNTY = #{recCounty}
        </if>
        <if test="recAddress != null and recAddress != ''">
            and so.REC_ADDRESS = #{recAddress}
        </if>
        <if test="startCreationTime != null">
            and ocl.CREATION_TIME &gt;= #{startCreationTime}
        </if>
        <if test="endCreationTime != null">
            and ocl.CREATION_TIME &lt;= #{endCreationTime}
        </if>
        <if test="startPayTime != null">
            and so.PAY_TIME &gt;= #{startPayTime}
        </if>
        <if test="endPayTime != null">
            and so.PAY_TIME &lt;= #{endPayTime}
        </if>
        <if test="startLastInstanceDate != null">
            and ocl.MODIFIED_TIME &gt;= #{startLastInstanceDate}
        </if>
        <if test="endLastInstanceDate != null">
            and ocl.MODIFIED_TIME &lt;= #{endLastInstanceDate}
        </if>
        <if test="lastInstanceApprover != null and lastInstanceApprover != ''">
            and (select count(t.USER_ID) from SYS_USER t where ocl.PK_MODIFIED = t.USER_ID and t.USER_NAME like #{lastInstanceApprover}||'%') &gt; 0
        </if>
        <if test="memberIdList != null and memberIdList.size() >0">
            and so.PK_MEMBER in
            <foreach collection="memberIdList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ORDER BY ocl.CREATION_TIME desc


    </select>


    <select id="cancellationList" resultType="com.hzs.common.domain.sale.ext.SaOrderChargeLogExt">
        select a.BUSINESS_CODE approvalCode, so.ORDER_CODE orderCode,so.ORDER_TYPE orderType,
        a.CHARGE_TYPE chargeType,cm.CUSTOMER_TYPE customerType,cb.MEMBER_CODE memberCode,cb.MEMBER_NAME memberName,
        cb.PHONE phone,parent.MEMBER_CODE parentCode,parent.MEMBER_NAME parentName,cm.PK_PARENT pkParent,
        pm.MEMBER_CODE placeParentCode,pm.MEMBER_NAME placeParentName ,cm.PK_PLACE_PARENT pkPlaceParent,
        cb.PLACE_DEPT placeDept,cm.MEMBER_CODE pkCenterCode,cm.MEMBER_NAME pkCenterName,so.ORDER_AMOUNT orderAmount,
        so.ORDER_ACHIEVE orderAchieve,bc.IN_EXCHANGE_RATE inExchangeRate,cm.PK_VERTEX pkVertex,cm.PK_TEAM_CODE pkTeamCode,
        so.REC_NAME recName,so.REC_PHONE recPhone,so.REC_PROVINCE recProvince,so.REC_CITY recCity ,so.REC_COUNTY recCounty,
        so.REC_ADDRESS recAddress,soi.PK_PRODUCT,bp.PRODUCT_NAME productName,bcl.NAME classifyName,soi.QUANTITY quantity,
        soi.PRICE price,so.POSTAGE postage,soi.ORDER_STATUS orderStatus,soi.LOGISTICS_CODE logisticsCode,soi.LOGISTICS_COMPANY logisticsCompany,
        soi.CREATION_TIME creationTime,so.PAY_TIME payTime,soi.MODIFIED_TIME deliveryTime,so.DELIVERY_WAY deliveryWay,
        we.OPERATE_SCOPE operateScope,so.PAY_TYPE payType,we.IS_LAND_TRANS isLandTrans,we.IS_AIR_TRANS isAirTrans,we.IS_OCEAN_TRANS isOceanTrans,
        we.PRE_SALE_STATUS preSaleStatus,a.APPROVAL_STATUS approvalStatus,
        COALESCE(a.CREATION_TIME,soi.MODIFIED_TIME) cancelOrderTime,
        COALESCE(a.USER_NAME,mm.USER_NAME) cancelTheOrderName,
        a.source orderChargeLogSource,
        cb.ORDER_SOURCE orderSource,
        a.MODIFIED_TIME lastInstanceDate
        from sa_order so
        left join sa_order_items soi
        on soi.pk_order = so.pk_id
        left join cu_member cm on cm.pk_id = so.PK_CREATOR
        left join cu_member ct on ct.pk_id = cm.PK_CENTER_CODE
        left join cu_member cb on cb.pk_id = so.PK_MEMBER
        left join cu_member parent on parent.pk_id = cb.PK_PARENT
        left join cu_member pm on pm.pk_id = cb.PK_PLACE_PARENT
        left join sys_user mm on mm.USER_ID = soi.PK_MODIFIED
        left join BD_CURRENCY bc on bc.PK_ID = so.PK_RATE
        left join bd_product bp on bp.PK_ID = soi.PK_PRODUCT
        left join bd_classify bcl on bcl.pk_id = bp.PK_CLASSIFY
        left join BD_WARES_EXTEND we on we.PK_WARES = soi.PK_WARES
        left join (
        select ocl.PK_ORDER ,ocl.BUSINESS_CODE,ocl.CREATION_TIME,su.USER_NAME,ocl.APPROVAL_STATUS,ocl.SOURCE source,
        ocl.CHARGE_TYPE,ocl.MODIFIED_TIME
        from SA_ORDER_CHARGE_LOG ocl
        left join sys_user su on su.USER_ID = ocl.PK_CREATOR
        where ocl.CHARGE_TYPE in (233, 234, 235)
        and ocl.DEL_FLAG = 0
        and ocl.APPROVAL_STATUS = 2
        and ocl.pk_country = #{pkCountry}
        ) a on a.PK_ORDER = so.pk_id
        where so.pk_country = #{pkCountry}
        and so.order_status = 1
        and soi.del_flag = 1
        and (
        soi.exchange_goods = 1
        or (soi.exchange_goods = 0 and exists (
        select * from SA_ORDER_CHARGE_LOG ocl
        where ocl.del_flag = 0 and ocl.pk_country = #{pkCountry} and ocl.charge_type in (233, 234, 235) and ocl.APPROVAL_STATUS = 2 and ocl.pk_order = so.pk_id
        ))
        )
        and soi.modified_time is not null
        <if test="orderChargeLogSource != null">
            and a.source = #{orderChargeLogSource}
        </if>
        <if test="chargeType != null">
            and a.CHARGE_TYPE = #{chargeType}
        </if>
        <if test="customerType != null">
            and cm.CUSTOMER_TYPE = #{customerType}
        </if>
        <if test="orderSource != null">
            and cb.ORDER_SOURCE = #{orderSource}
        </if>

        <if test="orderCode != null and orderCode != ''">
            and so.ORDER_CODE = #{orderCode}
        </if>
        <if test="memberName != null and memberName != ''">
            and cb.MEMBER_NAME = #{memberName}
        </if>
        <if test="memberCode != null and memberCode != ''">
            and cb.MEMBER_CODE like #{memberCode} || '%'
        </if>
        <if test="memberIdList != null and memberIdList.size() >0">
            and so.PK_MEMBER in
            <foreach collection="memberIdList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="phone != null and phone != ''">
            and cb.PHONE = #{phone}
        </if>
        <if test="creatorName != null and creatorName != ''">
            and a.USER_NAME = #{creatorName}
        </if>
        <if test="approvalCode != null and approvalCode != ''">
            and a.BUSINESS_CODE = #{approvalCode}
        </if>
        <if test="pkCenterCode != null and pkCenterCode != ''">
            and cm.MEMBER_CODE like #{pkCenterCode} || '%'
        </if>
        <if test="pkCenterName != null and pkCenterName != ''">
            and cm.MEMBER_NAME like #{pkCenterName} || '%'
        </if>
        <if test="orderType != null">
            and so.ORDER_TYPE = #{orderType}
        </if>
        <if test="orderStatus != null">
            and so.ORDER_STATUS = #{orderStatus}
        </if>
        <if test="pkVertex != null">
            and so.PK_VERTEX = #{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            and cm.PK_TEAM_CODE = #{pkTeamCode}
        </if>
        <if test="startCancelOrderTime != null">
            and COALESCE(a.CREATION_TIME,soi.MODIFIED_TIME) &gt;= #{startCancelOrderTime}
        </if>
        <if test="endCancelOrderTime != null">
            and COALESCE(a.CREATION_TIME,soi.MODIFIED_TIME) &lt;= #{endCancelOrderTime}
        </if>
        <if test="startCreationTime != null">
            and soi.CREATION_TIME &gt;= #{startCreationTime}
        </if>
        <if test="endCreationTime != null">
            and soi.CREATION_TIME &lt;= #{endCreationTime}
        </if>
        <if test="startPayTime != null">
            and so.PAY_TIME &gt;= #{startPayTime}
        </if>
        <if test="endPayTime != null">
            and so.PAY_TIME &lt;= #{endPayTime}
        </if>
        <if test="startLastInstanceDate != null">
            and a.MODIFIED_TIME &gt;= #{startLastInstanceDate}
        </if>
        <if test="endLastInstanceDate != null">
            and a.MODIFIED_TIME &lt;= #{endLastInstanceDate}
        </if>

        <if test="memberIdList != null and memberIdList.size() >0">
            and so.PK_MEMBER in
            <foreach collection="memberIdList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        order by soi.modified_time desc, so.ORDER_CODE
    </select>

</mapper>
