<?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="cn.superdesk.uniorder.dao.mapper.operationCenter.accounting.UnifiedAccountingOrderGoodsMapperEx">

  <sql id="getOpCenterOderListConditions">

    <!--    客户过滤 ==关联运营商过滤 ==关联项目过滤  -->
    <if test="customerId !=null and customerId !='' and opCenterCustomerOperatorOrgList !=null and opCenterCustomerOperatorOrgList.size > 0">
      and
      <foreach collection="opCenterCustomerOperatorOrgList" index="index" item="operators" open="(" close=")" separator="or">
        ( t1.operator_id=#{operators.bizOperatorId} and t1.biz_platform_code=#{operators.bizPlatformCode}

        <if test="0==operators.isAllOrg  ">
          and t1.org_id=#{operators.bizOrgId}
        </if>

        )
      </foreach>
    </if>

    <!--    项目过滤    -->
    <if test="orgIds !=null and orgIds !=''">
      and   t1.org_id in (
      SELECT organization_id
      FROM
      sys_organization
      WHERE
      id in (${orgIds})
      )
    </if>
    <!--    企业过滤    -->
    <if test="enterpriseIds !=null and enterpriseIds != '' and sysPlatformVirList !=null and sysPlatformVirList.size>0">
      and (

      <foreach collection="sysPlatformVirList" index="index" item="enterprise" open="(" close=")" separator="or">
        t1.org_id=#{enterprise.bizOrgId} and t1.enterprise_id=#{enterprise.bizVirId}

      </foreach>

      )
    </if>


    <!--    业务平台过滤    -->
    <if test="bizPlatformCode != null and bizPlatformCode != ''">
      and t1.biz_platform_code in (${bizPlatformCode})
    </if>

<!--    <if test="bizPlatformCodeList != null and bizPlatformCodeList.size>0">-->
<!--      and t1.biz_platform_code in-->
<!--      <foreach collection="bizPlatformCodeList" index="index" item="bizPlatformCode" open="(" close=")" separator=",">-->
<!--        #{bizPlatformCode}-->
<!--      </foreach>-->
<!--    </if>-->




    <!--    订单类型过滤    -->
    <if test="orderTypeCode != null and orderTypeCode.size >0 ">
      and t1.order_type_code in
      <foreach collection="orderTypeCode" index="index" item="code" open="(" close=")" separator=",">
        #{code}
      </foreach>
    </if>

    <!--    业务类型过滤    -->
    <if test="orderBizTypeId != null and orderBizTypeId.size > 0">
      and t1.order_biz_type_id in
      <foreach collection="orderBizTypeId" index="index" item="typeId" open="(" close=")" separator=",">
        #{typeId}
      </foreach>
    </if>


    <!--  <if test="orderPayStatus != null and orderPayStatus.size > 0">-->
    <!--    and t1.order_pay_status in-->
    <!--    <foreach collection="orderPayStatus" index="index" item="payStatus" open="(" close=")" separator=",">-->
    <!--      #{payStatus}-->
    <!--    </foreach>-->
    <!--  </if>-->
    <!--  支付方式-->
    <if test="platformPayTypeIdList != null and platformPayTypeIdList.size > 0">
      and t1.platform_pay_type_id  in
      <foreach collection="platformPayTypeIdList" index="index" item="payTypeId" open="(" close=")" separator=",">
        #{payTypeId}
      </foreach>
    </if>

    <!--    交易状态过滤    -->
    <if test="transactionStatusId != null and transactionStatusId.size > 0">
      and t1.transaction_status_id in
      <foreach collection="transactionStatusId"  index="index" item="transactionId" open="(" close=")" separator=",">
        #{transactionId}
      </foreach>
    </if>


    <!--  <if test="platformAftersaleStatusId != null and platformAftersaleStatusId.size > 0">-->
    <!--    and t1.platform_aftersale_status_id in-->
    <!--    <foreach collection="platformAftersaleStatusId"  index="index" item="platformAftersaleStatusId" open="(" close=")" separator=",">-->
    <!--      #{platformAftersaleStatusId}-->
    <!--    </foreach>-->
    <!--  </if>-->


    <!--  <if test="platformAftersaleTypeId != null and platformAftersaleTypeId.size > 0">-->
    <!--    and t1.platform_aftersale_type_id in-->
    <!--    <foreach collection="platformAftersaleTypeId"  index="index" item="platformAftersaleTypeId" open="(" close=")" separator=",">-->
    <!--      #{platformAftersaleTypeId}-->
    <!--    </foreach>-->
    <!--  </if>-->



    <!--  <if test="expressTrackingNo != null and expressTrackingNo !=''">-->
    <!--    and t1.express_tracking_no=#{expressTrackingNo}-->
    <!--  </if>-->
    <!--  <if test="thirdPartyOrderSn != null and thirdPartyOrderSn !=''">-->
    <!--    and t1.third_party_order_sn=#{thirdPartyOrderSn}-->
    <!--  </if>-->
    <!--  <if test="refundOriginOrderSn != null and refundOriginOrderSn !=''">-->
    <!--    and t1.refund_origin_order_sn=#{refundOriginOrderSn}-->
    <!--  </if>-->
    <if test="orderSn != null and orderSn !=''">
      and ( t1.order_sn = #{orderSn} or t1.refund_origin_order_sn=#{orderSn}
      or  t1.order_sn in (
      SELECT
      refund_origin_order_sn
      FROM tb_operation_center_order WHERE order_sn = #{orderSn})
      )
    </if>

    <!--  订单时间 -->
    <if test="orderBeginTime != null and orderBeginTime !=''">
      and t1.order_create_time <![CDATA[ >= ]]> concat(#{orderBeginTime},' 00:00:00')
    </if>
    <if test="orderEndTime != null and orderEndTime !=''">
      and t1.order_create_time <![CDATA[ <= ]]> concat(#{orderEndTime},' 23:59:59')
    </if>


    <!--  支付时间 -->
    <if test="tradingBeginTime != null and tradingBeginTime !=''">
      and  t1.trading_time <![CDATA[ >= ]]> concat(#{tradingBeginTime},' 00:00:00')
    </if>
    <if test="tradingEndTime != null and tradingEndTime !=''">
      and  t1.trading_time <![CDATA[ <= ]]> concat(#{tradingEndTime},' 23:59:59')
    </if>


    <!--  发货时间 -->
    <if test="sendBeginTime != null and sendBeginTime !=''">
      and t1.send_time <![CDATA[ >= ]]> concat(#{sendBeginTime},' 00:00:00')
    </if>
    <if test="sendEndTime != null and sendEndTime !=''">
      and t1.send_time <![CDATA[ <= ]]> concat(#{sendEndTime},' 23:59:59')
    </if>
    <!--  完成时间 -->
    <if test="finishBeginTime != null and finishBeginTime !=''">
      and t1.finish_time  <![CDATA[ >= ]]> concat(#{finishBeginTime},' 00:00:00')
    </if>
    <if test="finishEndTime != null and finishEndTime !=''">
      and t1.finish_time <![CDATA[ <= ]]>  concat(#{finishEndTime},' 23:59:59')
    </if>

    <!--    对账状态过滤    -->
    <if test="accountingStatus !=null and accountingStatus!=''">
      and t.accounting_status in (${accountingStatus})
    </if>
    <!--    商品类型过滤    -->
    <if test="goodsTypeId !=null and goodsTypeId!=''">
      and t.goods_type_id= #{goodsTypeId}
    </if>
    <!--    下单人姓名过滤    -->
    <if test="buyerName != null and buyerName !=''">
      and t1.buyer_name=#{buyerName}
    </if>
    <!--    下单人手机号码过滤    -->
    <if test="buyerMobile != null and buyerMobile !=''">
      and t1.buyer_mobile=#{buyerMobile}
    </if>

    <!--    商品id过滤    -->
    <if test="goodsId != null and goodsId !=''">
      and t.goods_id=#{goodsId}
    </if>
    <!--    商品名称过滤    -->
    <if test="goodsName != null and goodsName !=''">
      and t.goods_name=#{goodsName}
    </if>
    <!--    退单原单号过滤    -->
    <if test="refundOriginOrderSn != null and refundOriginOrderSn !=''">
      and t1.refund_origin_order_sn=#{refundOriginOrderSn}
    </if>
    <!--    账单编号过滤    -->
    <if test="accountingOrderSnOperator != null and accountingOrderSnOperator !=''">
      and t.accounting_order_sn_operator=#{accountingOrderSnOperator}
    </if>


    <!--    是否参与结算过滤    -->
    <if test="isSettlement != null and isSettlement !=''">
      and t.is_settlement=#{isSettlement}
    </if>
    <!--    积分类型    -->
    <if test="integralTypeName != null and integralTypeName !=''">
      and t.integral_type_name=#{integralTypeName}
    </if>
    <!--    积分批次    -->
    <if test="batch != null and batch !=''">
      and t.batch=#{batch}
    </if>


  </sql>

  <sql id="OrderGoodsList_Column">
    t.id,
      t.order_sn orderSn ,
      t.biz_platform_name bizPlatformName,
      t.operator_name operatorName,
    t1.buyer_id buyerId,
      t1.buyer_name buyerName,
      t1.buyer_mobile buyerMobile,
      t.order_type_name orderTypeName,
      t.biz_type_id orderBizTypeId,
      t.order_create_time orderCreateTime,
      t1.trading_time tradingTime,
      t1.send_time sendTime,
      t1.finish_time finishTime,

      t.goods_type_id goodsTypeId,
         t.goods_id goodsId,
      t.goods_name goodsName,
      t.goods_specification goodsSpecification,
      t.cate_level_1_name cateLevel1Name,
      t.cate_level_2_name cateLevel2Name,
      t.cate_level_3_name cateLevel3Name,
      t.goods_sku goodsSku,
      t.quantity,
      t.unit,

      t.selling_unit_price sellingUnitPrice,
      t.selling_total_price sellingTotalPrice,
      t.discount_amount discountAmount,
      t.goods_order_amount goodsOrderAmount,
      t.settlement_amount settlementAmount,

      t1.transaction_status_id transactionStatusId,
      t1.biz_order_status_name bizOrderStatusName,
      t1.platform_aftersale_type platformAftersaleType,
      t1.platform_pay_type platformPayType,


      t1.org_name orgName,
      t1.enterprise_name enterpriseName,
      t1.refund_origin_order_sn refundOriginOrderSn,
      t.accounting_status accountingStatus,
      t.accounting_order_sn_operator accountingOrderSnOperator,
        t1.operator_id operatorId,
        t1.org_id orgId,
        t1.enterprise_id enterpriseId,
        t1.biz_platform_code bizPlatformCode,
        t1.order_type_code order_type_code,
        t1.platform_pay_type_id  platformPayTypeId,

      t.update_time updateTime
  </sql>
  <sql id="EXCEL_COLUMN">

    t1.consignee_address consigneeAddress,
        t1.express_name expressName,
        t1.express_tracking_no expressTrackingNo,
        t1.third_party_order_sn thirdPartyOrderSn,
        t.original_unit_price originalUnitPrice,
        t.original_total_price originalTotalPrice,
        t.negotiated_unit_price negotiatedUnitPrice,
        t.negotiated_total_price negotiatedTotalPrice,
        t.cost_unit_price costUnitPrice,
        t.cost_total_price costTotalPrice,
        t.supplier_name supplierName,
  </sql>

  <sql id="COUNT_ORDER_GOODS_COLUMN">
    count(1) countRows,
    SUM(IFNULL(t.goods_order_amount,0)) goodsOrderAmountCount,
    SUM(IFNULL(t.settlement_amount,0)) settlementAmountCount
  </sql>

  <select id="getOrderGoodsList"
          parameterType="cn.superdesk.uniorder.dto.param.operationCenter.order.OperationCenterOrderParam"
          resultType="cn.superdesk.uniorder.dto.vo.operationCenter.accounting.UnifiedAccountingOrderInfoVo">
    SELECT

    <choose>
      <!--  查询统计-->
      <when test="1 == flag">
        <include refid="COUNT_ORDER_GOODS_COLUMN"/>
      </when>
      <!--  查询excel-->
      <when test="2 == flag">
        <include refid="EXCEL_COLUMN"/>
        <include refid="OrderGoodsList_Column"/>
      </when>
      <!--  默认查询列表-->
      <otherwise>
        <include refid="OrderGoodsList_Column"/>
      </otherwise>

    </choose>

    FROM
    unified_accounting_order_goods t
    INNER JOIN
    unified_accounting_order t1
    ON
    t.order_sn =t1.order_sn
    where
    t1.deleted=0
    <!-- 对账状态  去掉 不对账 含未对账/对账中/已对账 -->
    and  t.accounting_status != 4
    <!-- 商品类型  商品和运费-->
    and   t.goods_type_id in (1,2)
    <include refid="getOpCenterOderListConditions"/>

    <choose>

      <!--  查询excel-->
      <when test="2 == flag">
        order by t.order_create_time
      </when>
      <!--  默认查询列表-->
      <otherwise>
        order by t.order_create_time desc
      </otherwise>

    </choose>


  </select>
</mapper>