<?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.yonge.cooleshow.biz.dal.mapper.PaymentDivMemberRecordMapper">

    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ AS id
        , t.tenant_id_ AS tenantId
        , t.app_id_ AS appId
        , t.member_id_ AS memberId
        , t.order_no_ AS orderNo
        , t.div_order_no_ AS divOrderNo
        , t.trans_no_ AS transNo
        , t.div_member_json_ AS divMemberJson
        , t.amount_ AS amount
        , t.tenant_enter_flag_ AS tenantEnterFlag
        , t.status_ AS status
        , t.fee_flag_ AS feeFlag
        , t.fee_amt_ AS feeAmt
        , t.create_time_ AS createTime
        , t.update_time_ AS updateTime
        </sql> 
    
    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.entity.PaymentDivMemberRecord">
		SELECT         
        	<include refid="baseColumns" />
		FROM payment_div_member_record t
	</select>
    <select id="queryIncome"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.PaymentDivMemberRecordWrapper$IndexIncomeQueryDto">
        select
            pdmr.order_no_ as orderNo,
            sut.name_ as name,
            su.avatar_ as avatar,
            su.phone_ as phone,
            pdmr.amount_ as amount,
            uo.pay_time_ as payTime,
            uo.order_type_ as orderType
        from
            payment_div_member_record pdmr
                left join
            user_order uo ON uo.order_no_ = pdmr.order_no_
                left join
            sys_user_tsign sut ON sut.user_id_ = pdmr.user_id_
                left join
            sys_user su ON su.id_ = sut.user_id_
        where
            pdmr.status_ = 'SUCCESS' AND pdmr.tenant_id_ = #{param.tenantId}
          <if test="param.startTime != null and param.startTime != ''">
              AND uo.pay_time_ BETWEEN #{param.startTime} AND #{param.endTime}
          </if>
          <if test="param.orderType != null">
              AND uo.order_type_ = #{param.orderType}
          </if>
          <if test="param.keyword != null and param.keyword != ''">
              and sut.name_ LIKE CONCAT('%',#{param.keyword},'%')
          </if>
           ORDER BY ${param.sortField} ${param.sortType}
    </select>
    <select id="getIncomeOrderDetail"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.PaymentDivMemberRecordWrapper$OrderDetailDto">
        select
            pdmr.order_no_ as orderNo,
            sut.name_ as name,
            su.avatar_ as avatar,
            su.phone_ as phone,
            pdmr.amount_ as amount,
            uo.pay_time_ as payTime,
            uo.order_type_ as orderType,
            uod.good_name_ as goodsName,
            uod.good_num_ as goodsNum,
            uod.good_type_ as goodsType,
            uod.good_url_ as goodsUrl
        from
            payment_div_member_record pdmr
                left join
            user_order uo ON uo.order_no_ = pdmr.order_no_
                left join
            sys_user_tsign sut ON sut.user_id_ = pdmr.user_id_
                left join
            sys_user su ON su.id_ = sut.user_id_
                left join
            user_order_detail uod ON uod.order_no_ = uo.order_no_
            WHERE pdmr.order_no_ = #{orderNo} AND pdmr.tenant_id_ = #{tenantId} LIMIT 1
    </select>
    <select id="incomeSummary"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.PaymentDivMemberRecordWrapper$IndexIncomeSummaryDetailDto">
        select uo.order_type_ as orderType,SUM(pdmr.amount_) amount
        from payment_div_member_record pdmr
        left join user_order uo ON uo.order_no_ = pdmr.order_no_
        where pdmr.status_ = 'SUCCESS' AND pdmr.tenant_id_ = #{query.tenantId}
        <if test="query.startTime != null and query.startTime != ''">
            AND uo.pay_time_ BETWEEN #{query.startTime} AND #{query.endTime}
        </if>
        group by uo.order_type_
    </select>
    <select id="expendSummary"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.PaymentDivMemberRecordWrapper$IndexIncomeSummaryDetailDto">
        select uo.order_type_ as orderType,SUM(uo.actual_price_) amount
        from user_order uo
        where uo.status_ = 'SUCCESS' AND uo.order_type_ = 'TENANT_ALBUM'
        AND uo.tenant_id_ = #{query.tenantId}
        <if test="query.startTime != null and query.startTime != ''">
            AND uo.pay_time_ BETWEEN #{query.startTime} AND #{query.endTime}
        </if>
        group by uo.order_type_
    </select>

</mapper>