<?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.repair.mapper.RepairPayRecordMapper">
    <resultMap type="com.repair.model.pojo.RepairPayRecord" id="repairPayMap">
        <result property="id" column="id"/>
        <result property="recordId" column="record_id"/>
        <result property="userId" column="user_id"/>
        <result property="payNo" column="pay_no"/>
        <result property="payPrice" column="pay_price"/>
        <result property="isSuccess" column="is_success"/>
        <result property="remark" column="remark"/>
    </resultMap>
    <select id="queryPaymentData" resultType="com.repair.model.pojo.RepairPayRecord" resultMap="repairPayMap">
        select rpr.id,rpr.record_id,rpr.user_id,rpr.pay_no,rpr.pay_price,rpr.is_success,rpr.remark from repair_pay_record rpr
        inner join repair_record rr on rr.deleted = 0 and rr.record_no = #{recordNo} and rr.id = rpr.record_id
        where rpr.deleted = 0
    </select>
    <select id="queryExport" resultType="com.repair.model.vo.QueryExportVo">
        SELECT row_number() over ( ORDER BY dc.pay_time ) AS id, dc.*
        FROM
        (SELECT
        rpr.create_time AS pay_time,
        rr.record_no AS record_no,
        rpr.pay_no AS pay_no,
        ru.user_name as user_name,
        rpr.pay_price as pay_price,
        (SELECT SUM(rrr.refund_price) FROM repair_refund_record rrr WHERE rrr.record_id=rpr.record_id and
        rrr.is_success!=2 and rrr.deleted=0) as refund_total_price
        FROM
        `repair_pay_record` rpr
        LEFT JOIN repair_record rr ON rr.deleted = 0
        AND rpr.record_id = rr.id
        LEFT JOIN repair_user ru on rpr.user_id=ru.id and ru.deleted=0
        where
        rpr.is_success=2 AND rpr.deleted=0
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and rpr.create_time between #{startTime} and #{endTime}
        </if>
        ) dc
        <where>
            <if test="keyWord != null and keyWord !='' ">
                and (dc.user_name like '%' #{keyWord} '%' or dc.record_no like '%' #{keyWord} '%')
            </if>

        </where>
    </select>

    <resultMap type="com.repair.model.vo.ProsperityRepairPayRecordsVo" id="repairPayPage">
        <result property="id" column="id"/>
        <result property="payTime" column="pay_time"/>
        <result property="recordId" column="record_id"/>
        <result property="recordNo" column="record_no"/>
        <result property="payNo" column="pay_no"/>
        <result property="userName" column="user_name"/>
        <result property="payPrice" column="pay_price"/>
        <result property="state" column="state"/>
    </resultMap>
    <select id="queryPayPage" resultType="com.repair.model.vo.ProsperityRepairPayRecordsVo" resultMap="repairPayPage">
        select rpr.id,rpr.create_time as pay_time,rpr.record_id,rr.record_no,rpr.pay_no,ru.user_name,rpr.pay_price,rpr.is_success as state from repair_pay_record rpr
        left join repair_record rr on rr.deleted = 0 and rr.id = rpr.record_id
        left join repair_user ru on ru.deleted = 0  and ru.id = rpr.user_id
        where rpr.deleted = 0 and rpr.is_success = 2
        <if test="keyWord != null and keyWord !='' ">
            and (rpr.user_id in (select id from repair_user where deleted = 0 and user_name like '%' #{keyWord} '%')
            or rpr.record_id in (select id from repair_record where deleted = 0 and record_no like '%' #{keyWord} '%'))
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and rpr.create_time between #{startTime} and #{endTime}
        </if>
        order by rpr.create_time desc
    </select>

</mapper>
