<?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.thinkgem.jeesite.modules.pay.dao.PayApiDao">

    <insert id="saveOrderInfo">
        INSERT INTO youka_orders(
            code,
            user_code,
            pay_no,
            pay_code,
            pay_method,
            pay_channel,
            pay_status,
            amount,
            subject,
            trade_no,
            pay_type,
            card_code,
            create_by,
            update_by
        ) VALUES (#{code}, #{userCode}, #{payNo}, #{payCode},#{payMethod}, #{payChannel}, #{payStatus}, #{amount}, #{subject}, #{tradeNo}, #{payType}, #{cardCode}, #{createdUser}, #{modifiedUser})
    </insert>
    <update id="updateOrderStatus">
        UPDATE youka_orders
        SET pay_status = #{payStatus}
        <if test="tradeNo != null and tradeNo != ''">
            ,trade_no = #{tradeNo}
        </if>
        WHERE pay_code = #{payCode}
    </update>

    <update id="updateOrderTradeNo">
        UPDATE youka_orders
        SET trade_no = #{tradeNo}
        WHERE pay_code = #{payCode}
    </update>


    <select id="getCDEUsers" resultType="com.thinkgem.jeesite.modules.pay.entity.UserRateEntity">
        SELECT
            yu.code AS userCode,
            yu.level AS userLevel,
            yr.rate AS rate
        FROM
            youka_users yu
        LEFT JOIN youka_rate yr ON yu.level = yr.user_level AND yr.pay_method = 'quick'
        WHERE
            yu.id IN
        <foreach collection="array" item="pathIds" index="index"
                 open="(" close=")" separator=",">
            #{pathIds}
        </foreach>
        AND yu.level IN ('C', 'D', 'E')
        ORDER BY recommended_path DESC
        LIMIT 3
    </select>

    <select id="getSABUsers" resultType="com.thinkgem.jeesite.modules.pay.entity.UserRateEntity">
        SELECT
            yu.code AS "userCode",
            yu.level AS "userLevel",
            yr.rate AS "rate"
        FROM
            youka_users yu
        LEFT JOIN youka_rate yr ON yu.level = yr.user_level AND yr.pay_method = 'quick'  AND yr.pay_channel = #{payChannel}
        WHERE
            yu.id IN
        <foreach collection="pathIds" item="pathId" index="index"
                 open="(" close=")" separator=",">
            #{pathId}
        </foreach>
        AND yu.level IN ('S', 'A', 'B')
        ORDER BY recommended_path DESC
        LIMIT 3
    </select>
    <select id="getOrder" resultType="com.thinkgem.jeesite.modules.pay.entity.OrderEntity">
        SELECT
            code,
            user_code,
            pay_no,
            pay_code,
            pay_channel,
            pay_method,
            pay_status,
            amount,
            subject,
            trade_no,
            pay_type,
            card_code
        FROM youka_orders
        WHERE
        del_flag = 0
        <if test="payNo != null and payNo != ''">
            AND pay_no = #{payNo}
        </if>
        <if test="payCode != null and payCode != ''">
            AND pay_code = #{payCode}
        </if>

    </select>

    <select id="getOrderListTimeing" resultType="com.thinkgem.jeesite.modules.pay.entity.OrderEntity">
        SELECT
            code,
            user_code,
            pay_no,
            pay_code,
            pay_channel,
            pay_status,
            amount,
            subject,
            trade_no,
            pay_type,
            card_code
        FROM youka_orders
        WHERE pay_status = 1
        <if test="endTime != null and endTime != ''">
            AND create_date &lt;= #{endTime}
        </if>
        <if test="startTime != null and startTime != ''">
            AND create_date >= #{startTime}
        </if>
        AND del_flag = 0
    </select>


    <select id="getBenefitSum" resultType="com.thinkgem.jeesite.modules.pay.entity.BenefitEntity">
        SELECT
            SUM(b.amount) AS amount
        FROM youka_benefit b
        LEFT JOIN youka_orders o ON o.`code` = b.order_code
        WHERE b.user_code = #{userCode}
        AND b.del_flag = 0
        AND b.benefit_type = #{benefitType}
        AND o.pay_status = 2
        <if test="benefitStatus != null and benefitStatus != ''">
            AND b.benefit_status = #{benefitStatus}
        </if>
        <if test="startTime != null and startTime != ''">
            AND b.create_date >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND b.create_date &lt;= #{endTime}
        </if>
    </select>


    <insert id="saveBenefitInfo">
       INSERT INTO youka_benefit(
            code,
            user_code,
            order_code,
            pay_method,
            pay_channel,
            benefit_status,
            benefit_type,
            amount,
            card_code,
            create_by,
            update_by
        ) VALUES (#{code}, #{userCode}, #{orderCode},#{payMethod}, #{payChannel}, #{benefitStatus}, #{benefitType}, #{amount}, #{cardCode}, #{createdUser}, #{modifiedUser})
    </insert>

    <update id="updateBenefit">
        UPDATE youka_benefit
        SET benefit_status = #{benefitStatus}
        WHERE order_code = #{orderCode}
    </update>

    <update id="deleteBenefit">
        UPDATE youka_benefit
        SET del_flag = 1
        WHERE order_code = #{orderCode}
    </update>
</mapper>