<?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_channel,
            pay_status,
            amount,
            subject,
            trade_no,
            pay_type,
            card_code,
            create_by,
            update_by
        ) VALUES (#{code}, #{userCode}, #{payNo}, #{payCode}, #{payChannel}, #{payStatus}, #{amount}, #{subject}, #{tradeNo}, #{payType}, #{cardCode}, #{createdUser}, #{modifiedUser})
    </insert>
    <update id="updateOrderStatus">
        UPDATE youka_orders
        SET pay_status = #{payStatus}
        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'
        WHERE
            yu.id IN
        <foreach collection="array" item="pathIds" index="index"
                 open="(" close=")" separator=",">
            #{pathIds}
        </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_status,
            amount,
            subject,
            trade_no,
            pay_type,
            card_code
        FROM youka_orders
        WHERE pay_no = #{payNo}
        AND del_flag = 0
    </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
        AND create_date &lt;= #{create}
        AND del_flag = 0
    </select>
    <select id="getBenefitSum" resultType="com.thinkgem.jeesite.modules.pay.entity.BenefitEntity">
        SELECT
            SUM(amount) AS amount
        FROM youka_benefit
        WHERE user_code = #{userCode}
        AND del_flag = 0
        AND benefit_type = #{benefitType}
        <if test="benefitStatus != null and benefitStatus != ''">
            AND benefit_status = #{benefitStatus}
        </if>
        <if test="startTime != null and startTime != ''">
            AND create_date >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND create_date &lt;= #{endTime}
        </if>
    </select>


    <insert id="saveBenefitInfo">
       INSERT INTO youka_benefit(
            code,
            user_code,
            order_code,
            pay_channel,
            benefit_status,
            benefit_type,
            amount,
            card_code,
            create_by,
            update_by
        ) VALUES (#{code}, #{userCode}, #{orderCode}, #{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>