<?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.dao.UserOrderDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.UserOrder">
        <result column="id_" property="id"/>
        <result column="order_no_" property="orderNo"/>
        <result column="user_id_" property="userId"/>
        <result column="recom_user_id_" property="recomUserId"/>
        <result column="activity_id_" property="activityId"/>
        <result column="reward_id_" property="rewardId"/>
        <result column="order_name_" property="orderName"/>
        <result column="order_client_" property="orderClient"/>
        <result column="order_type_" property="orderType"/>
        <result column="order_desc_" property="orderDesc"/>
        <result column="status_" property="status"/>
        <result column="original_price_" property="originalPrice"/>
        <result column="expect_price_" property="expectPrice"/>
        <result column="actual_price_" property="actualPrice"/>
        <result column="coupon_amount_" property="couponAmount"/>
        <result column="plantform_fee_" property="plantformFee"/>
        <result column="user_note_" property="userNote"/>
        <result column="create_time_" property="createTime"/>
        <result column="pay_time_" property="payTime"/>
        <result column="reason_" property="reason"/>
        <result column="update_time_" property="updateTime"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ as id
        , t.order_no_ as orderNo
         ,t.payment_version_ as paymentVersion
         ,t.payment_vendor_ as paymentVendor
        , t.user_id_ as userId
        , t.tenant_id_ as tenantId
        , t.recom_user_id_ as recomUserId
        , t.activity_id_ as activityId
        , t.reward_id_ as rewardId
        , t.order_name_ as orderName
        , t.order_client_ as orderClient
        , t.order_type_ as orderType
        , t.order_desc_ as orderDesc
        , t.status_ as status
        , t.original_price_ as originalPrice
        , t.expect_price_ as expectPrice
        , t.actual_price_ as actualPrice
        , t.coupon_amount_ as couponAmount
        , t.plantform_fee_ as plantformFee
        , t.user_note_ as userNote
        , t.create_time_ as createTime
        , t.pay_time_ as payTime
        , t.reason_ as reason
        , t.update_time_ as updateTime
        </sql>

    <select id="detailById" resultType="com.yonge.cooleshow.biz.dal.vo.UserOrderVo">
        SELECT
            <include refid="baseColumns"/>,
            p.open_type_ as openType,
            p.payment_client_ as paymentClient,
            p.pay_channel_ as payChannel,
            p.trans_no_ as transNo,
            p.payment_no_ as paymentNo,
            p.fee_amt_ as feeAmt,
            u.username_ as username,
            u.phone_ as phone
        FROM user_order t
        left join user_order_payment p on t.order_no_ = p.order_no_ and (p.status_ = 'pending' or p.status_ = 'succeeded')
        left join sys_user u on t.user_id_ = u.id_
        where u.del_flag_ = 0 and t.id_ = #{id}
    </select>
    <select id="detailByOrderNo" resultType="com.yonge.cooleshow.biz.dal.vo.UserOrderVo">
        SELECT
            <include refid="baseColumns"/>,
            p.open_type_ as openType,
            p.payment_client_ as paymentClient,
            p.pay_channel_ as payChannel,
            p.trans_no_ as transNo,
            p.payment_no_ as paymentNo,
            p.fee_amt_ as feeAmt,
            u.username_ as username,
            u.phone_ as phone
        FROM user_order t
        left join user_order_payment p on t.order_no_ = p.order_no_ and (p.status_ = 'pending' or p.status_ = 'succeeded')
        left join sys_user u on t.user_id_ = u.id_
        where u.del_flag_ = 0 and t.order_no_ = #{param.orderNo}
        <if test="param.userId != null">
            and t.user_id_ = #{param.userId}
        </if>
    </select>

    <select id="detailApp" resultType="com.yonge.cooleshow.biz.dal.vo.UserOrderVo">
        SELECT
            <include refid="baseColumns"/>,
            p.open_type_ as openType,
            p.payment_client_ as paymentClient,
            p.pay_channel_ as payChannel,
            p.trans_no_ as transNo,
            p.payment_no_ as paymentNo
        FROM user_order t
        left join user_order_payment p on t.order_no_ = p.order_no_ and (p.status_ = 'pending' or p.status_ = 'succeeded')
        <where>
            <if test="param.userId != null">
                and t.user_id_ = #{param.userId}
            </if>
            <if test="param.id != null">
                and t.id_ = #{param.id}
            </if>
            <if test="param.orderNo != null and param.orderNo != ''">
                and t.order_no_ = #{param.orderNo}
            </if>
        </where>

    </select>

    <sql id="selectSql">
        SELECT
            <include refid="baseColumns"/>,
            p.open_type_ as openType,
            p.payment_client_ as paymentClient,
            p.pay_channel_ as payChannel,
            p.trans_no_ as transNo,
            p.payment_no_ as paymentNo,
            p.fee_amt_ as feeAmt,
            u.username_ as username,
            u.phone_ as phone
        FROM user_order t
        left join user_order_payment p on t.order_no_ = p.order_no_ and (p.status_ = 'pending' or p.status_ = 'succeeded')
        left join sys_user u on t.user_id_ = u.id_

        <if test="null != param.search and '' != param.search">
            left join tenant_info ti on t.tenant_id_ = ti.id_
        </if>
        <where>
            u.del_flag_ = 0
            <if test="null != param.orderClient and '' != param.orderClient">
                AND t.order_client_ = #{param.orderClient}
            </if>
            <if test="param.tenantId != null">
                and t.tenant_id_ = #{param.tenantId}
            </if>
            <if test="param.keyword != null and param.keyword != ''">
                and t.order_name_ like CONCAT('%', #{param.keyword}, '%')
            </if>
            <if test="param.paymentVersion != null">
                and t.payment_version_ = #{param.paymentVersion}
            </if>
            <if test="null != param.search and '' != param.search">
                AND (
                t.user_id_ LIKE CONCAT('%', #{param.search}, '%') or
                u.username_ LIKE CONCAT('%', #{param.search}, '%') or
                u.phone_ LIKE CONCAT('%', #{param.search}, '%') or
                ti.name_ LIKE CONCAT('%', #{param.search}, '%')
                )
            </if>
            <if test="null != param.searchNo and '' != param.searchNo">
                AND (
                t.order_no_ LIKE CONCAT('%', #{param.searchNo}, '%') or
                p.trans_no_ LIKE CONCAT('%', #{param.searchNo}, '%')
                )
            </if>
            <if test="null != param.orderType and '' != param.orderType">
                AND FIND_IN_SET(t.order_type_,#{param.orderType})
            </if>
            <if test="null != param.status and '' != param.status">
                AND FIND_IN_SET(t.status_,#{param.status})
            </if>
            <if test="param.startTime !=null">
                <![CDATA[AND t.create_time_ >= #{param.startTime} ]]>
            </if>
            <if test="param.endTime !=null">
                <![CDATA[AND t.create_time_ < #{param.endTime} ]]>
            </if>
            <if test="param.userId !=null">
                AND t.user_id_ = #{param.userId}
            </if>
            <if test="param.merchId !=null or (param.goodType !=null and param.goodType !='') or param.bizId !=null">
                AND exists(
                    select 1 from user_order_detail d where t.order_no_ = d.order_no_
                    <if test="param.merchId !=null ">
                        and d.merch_id_ = #{param.merchId}
                    </if>
                    <if test="param.goodType !=null and param.goodType !=''">
                        and d.good_type_ = #{param.goodType}
                    </if>
                    <if test="param.bizId !=null">
                        and d.biz_id_ = #{param.bizId}
                    </if>
                )
            </if>
        </where>
        order by field(t.status_,'WAIT_PAY','PAYING') desc, t.create_time_ desc
    </sql>

    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.UserOrderVo">
        <include refid="selectSql"/>
    </select>
    <select id="selectAllList" resultType="com.yonge.cooleshow.biz.dal.vo.UserOrderVo">
        <include refid="selectSql"/>
    </select>

    <select id="selectPendingList" resultType="com.yonge.cooleshow.biz.dal.vo.UserOrderVo">
        SELECT
            <include refid="baseColumns"/>
        FROM user_order t
        left join user_order_payment p on t.order_no_ = p.order_no_ and (p.status_ = 'pending' or p.status_ = 'succeeded')
        <where>
            <if test="null != param.status and '' != param.status">
                AND t.status_ = #{param.status}
            </if>
            <if test="param.paymentVersion != null">
                and t.payment_version_ = #{param.paymentVersion}
            </if>
            <if test="param.startTime != null">
                AND (
                    (p.id_ is null and t.create_time_ &gt;= #{param.startTime}) or (p.id_ is not null and p.create_time_ &gt;= #{param.startTime})
                )
            </if>
            <if test="param.endTime != null">
                AND (
                    (p.id_ is null and t.create_time_ &lt; #{param.endTime}) or (p.id_ is not null and p.create_time_ &lt; #{param.endTime})
                )
            </if>
        </where>
        order by t.create_time_
    </select>
    <select id="getPendingOrder" resultType="com.yonge.cooleshow.biz.dal.vo.UserOrderVo">
        SELECT
            <include refid="baseColumns"/>,
            p.open_type_ as openType,
            p.payment_client_ as paymentClient,
            p.pay_channel_ as payChannel,
            p.trans_no_ as transNo,
            p.payment_no_ as paymentNo
        FROM user_order t
        left join user_order_payment p on t.order_no_ = p.order_no_ and (p.status_ = 'pending' or p.status_ = 'succeeded')
        where t.status_ in ('WAIT_PAY','PAYING')
        and t.user_id_ = #{param.userId}
        <if test="null != param.orderClient and '' != param.orderClient">
            AND t.order_client_ = #{param.orderClient}
        </if>
        <if test="param.orderType != null">
            and find_in_set(t.order_type_ ,#{param.goodType})
        </if>
        and exists (
            select 1 from user_order_detail d where t.order_no_ = d.order_no_
            and find_in_set(d.good_type_ ,#{param.goodType})
            <choose>
                <when test="param.goodType != null and param.goodType != 'PRACTICE' and param.goodType != 'VIP_COURSE'">
                    <if test="param.bizId != null">
                        and d.biz_id_ = #{param.bizId}
                    </if>
                </when>
            </choose>
        )
        order by t.create_time_ desc limit 1
    </select>

    <select id="selectUnRecordTimeOrder" resultType="com.yonge.cooleshow.biz.dal.entity.UserOrder">
        SELECT
            <include refid="baseColumns"/>
        FROM user_order t
        left join student_time st on t.user_id_ = st.user_id_
        where t.status_ = 'PAID'
        and t.order_client_ = #{param.orderClient}
        and exists (
            select 1 from user_order_detail od where t.order_no_ = od.order_no_
            and (
                (st.first_vip_time_ is null and od.good_type_ = 'VIP') or
                (st.first_practice_time_ is null and od.good_type_ = 'PRACTICE') or
                (st.first_video_time_ is null and od.good_type_ = 'VIDEO') or
                (st.first_live_time_ is null and od.good_type_ = 'LIVE') or
                (st.first_activity_time_ is null and od.good_type_ = 'ACTI_REGIST') or
                (st.first_music_time_ is null and od.good_type_ = 'MUSIC')  or
                (st.first_pay_time_ is null and t.actual_price_ &gt; 0)
            )
        )
        <if test="param.startTime !=null">
            <![CDATA[AND t.create_time_ >= #{param.startTime} ]]>
        </if>
        <if test="param.endTime !=null">
            <![CDATA[AND t.create_time_ < #{param.endTime} ]]>
        </if>
    </select>
    <select id="getUserOrderByPaymentNoOrTransNo" resultType="com.yonge.cooleshow.biz.dal.vo.UserOrderVo">
        SELECT
            <include refid="baseColumns"/>,
            p.open_type_ as openType,
            p.payment_client_ as paymentClient,
            p.pay_channel_ as payChannel,
            p.trans_no_ as transNo,
            p.payment_no_ as paymentNo
        FROM user_order t
        left join user_order_payment p on t.order_no_ = p.order_no_ and (p.status_ = 'pending' or p.status_ = 'succeeded')
        <where>
            <choose>
                <when test="paymentNo != null and paymentNo != ''">
                    and (
                        exists (select 1 from user_order_payment p1 where t.order_no_ = p1.order_no_ and  p1.payment_no_ = #{paymentNo}) or
                        exists (
                                select 1 from user_order_refund b1
                                left join user_order_refund_bill b2 on b1.id_ = b2.refund_id_
                                where t.order_no_ = b1.order_no_ and b2.bill_no_ = #{paymentNo}
                        )
                    )
                </when>
                <otherwise>
                    and (
                        exists (select 1 from user_order_payment p1 where p1.trans_no_ = #{transNo}) or
                        exists (
                                select 1 from user_order_refund b1
                                left join user_order_refund_bill b2 on b1.id_ = b2.refund_id_
                                where t.order_no_ = b1.order_no_ and b2.trans_no_ = #{transNo}
                        )
                    )
                </otherwise>
            </choose>
        </where>
    </select>

    <update id="updateStatusByOrderNo">
        update user_order set status_ = #{orderStatus},update_time_ = now() where order_no_ = #{orderNo}
    </update>

    <select id="selectPaymentOrderPage" resultMap="BaseResultMap">
        select  t.*
        from user_order t
            <where>
                t.payment_version_ = 'V2'
                <if test="query.paymentStatus != null and query.paymentStatus.size() != 0">
                    and t.status_ in
                    <foreach collection="query.paymentStatus" item="item" separator="," open="(" close=")">
                        #{item}
                    </foreach>
                </if>
                <if test="query.endTime != null">
                    AND t.create_time_ &lt; #{query.endTime}
                </if>
            </where>
    </select>
    <select id="queryExpend"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.PaymentDivMemberRecordWrapper$IndexIncomeQueryDto">
        select
        uo.order_no_ as orderNo,
        sut.name_ as name,
        su.avatar_ as avatar,
        su.phone_ as phone,
        uo.actual_price_ as amount,
        uo.pay_time_ as payTime,
        uo.order_type_ as orderType
        from user_order uo
        left join
        sys_user_tsign sut ON sut.user_id_ = uo.user_id_
        left join
        sys_user su ON su.id_ = sut.user_id_
        where uo.status_ = 'SUCCESS' AND uo.tenant_id_ = #{param.tenantId} AND uo.order_type_ = 'TENANT_ALBUM'
        <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="getExpendOrderDetail"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.PaymentDivMemberRecordWrapper$OrderDetailDto">
        select
            uo.order_no_ as orderNo,
            sut.name_ as name,
            su.avatar_ as avatar,
            su.phone_ as phone,
            uo.actual_price_ 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 user_order uo
         left join
        sys_user_tsign sut ON sut.user_id_ = uo.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} LIMIT 1
    </select>

    <select id="getStudentTenantAlbumNum" resultType="java.lang.Long">
        select d.tenant_group_album_id_
        from user_order t
        left join user_order_detail d on t.order_no_ = d.order_no_
        where t.status_ in ('WAIT_PAY','PAYING','PAID') and t.order_type_ = 'TENANT_ALBUM' and d.good_type_ = 'TENANT_ALBUM'
        and t.user_id_ = #{userId}
        and d.tenant_group_album_id_ in
        <foreach collection="tenantGroupAlbumIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
</mapper>