<?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.fs.doctor.mapper.WithdrawRecordsMapper">
    
    <resultMap type="WithdrawRecords" id="WithdrawRecordsResult">
        <result property="withdrawId"    column="withdraw_id"    />
        <result property="doctorId"    column="doctor_id"    />
        <result property="doctorName"    column="doctor_name"    />
        <result property="pointsAmount"    column="points_amount"    />
        <result property="cashAmount"    column="cash_amount"    />
        <result property="applyTime"    column="apply_time"    />
        <result property="auditStatus"    column="audit_status"    />
        <result property="auditorId"    column="auditor_id"    />
        <result property="auditTime"    column="audit_time"    />
        <result property="auditRemark"    column="audit_remark"    />
        <result property="paymentStatus"    column="payment_status"    />
        <result property="paymentTime"    column="payment_time"    />
        <result property="bankReceiptNo"    column="bank_receipt_no"    />
        <result property="remark"    column="remark"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyName" column="company_name"/>
        <result property="auditInstanceId" column="audit_instance_id"/>
        <result property="bankReceiptUrl" column="bank_receipt_url"/>
    </resultMap>

    <sql id="selectWithdrawRecordsVo">
        select
        wr.withdraw_id,
        wr.doctor_id,
        wr.company_id,
        c.company_name,
        wr.doctor_name,
        wr.points_amount,
        wr.cash_amount,
        wr.apply_time,
        wr.audit_status,
        wr.auditor_id,
        wr.audit_time,
        wr.audit_remark,
        wr.audit_instance_id,
        wr.payment_status,
        wr.payment_time,
        wr.bank_receipt_no,
        wr.bank_receipt_url,
        wr.remark
        from withdraw_records wr
        left join company c on c.company_id = wr.company_id
    </sql>


    <select id="selectWithdrawRecordsList" parameterType="WithdrawRecords" resultMap="WithdrawRecordsResult">
        <include refid="selectWithdrawRecordsVo"/>
        <where>
            <if test="doctorId != null and doctorId != ''"> and wr.doctor_id = #{doctorId}</if>
            <if test="doctorName != null and doctorName != ''"> and wr.doctor_name like concat('%', #{doctorName}, '%')</if>
            <if test="pointsAmount != null and pointsAmount != ''"> and wr.points_amount = #{pointsAmount}</if>
            <if test="cashAmount != null"> and wr.cash_amount = #{cashAmount}</if>

            <!-- 这里就是导致歧义的罪魁祸首：必须写 wr.company_id -->
            <if test="companyId != null"> and wr.company_id = #{companyId}</if>

            <if test="applyTime != null"> and wr.apply_time = #{applyTime}</if>
            <if test="auditStatus != null"> and wr.audit_status = #{auditStatus}</if>
            <if test="auditorId != null and auditorId != ''"> and wr.auditor_id = #{auditorId}</if>
            <if test="auditTime != null"> and wr.audit_time = #{auditTime}</if>
            <if test="auditRemark != null and auditRemark != ''"> and wr.audit_remark = #{auditRemark}</if>
            <if test="paymentStatus != null"> and wr.payment_status = #{paymentStatus}</if>
            <if test="paymentTime != null"> and wr.payment_time = #{paymentTime}</if>
            <if test="bankReceiptNo != null and bankReceiptNo != ''"> and wr.bank_receipt_no = #{bankReceiptNo}</if>
        </where>
        <!-- 添加排序：最新申请的在前 -->
        order by wr.apply_time desc
    </select>
    
    <select id="selectWithdrawRecordsByWithdrawId" parameterType="java.lang.Long" resultMap="WithdrawRecordsResult">
        <include refid="selectWithdrawRecordsVo"/>
        where wr.withdraw_id = #{withdrawId}
    </select>
        
    <insert id="insertWithdrawRecords" parameterType="WithdrawRecords" useGeneratedKeys="true" keyProperty="withdrawId">
        insert into withdraw_records
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="doctorId != null and doctorId != ''">doctor_id,</if>
            <if test="doctorName != null and doctorName != ''">doctor_name,</if>
            <if test="companyId != null ">company_id,</if>
            <if test="pointsAmount != null and pointsAmount != ''">points_amount,</if>
            <if test="cashAmount != null">cash_amount,</if>
            <if test="applyTime != null">apply_time,</if>
            <if test="auditStatus != null">audit_status,</if>
            <if test="auditorId != null">auditor_id,</if>
            <if test="auditTime != null">audit_time,</if>
            <if test="auditRemark != null">audit_remark,</if>
            <if test="paymentStatus != null">payment_status,</if>
            <if test="paymentTime != null">payment_time,</if>
            <if test="bankReceiptNo != null">bank_receipt_no,</if>
            <if test="remark != null">remark,</if>
            <if test="auditInstanceId != null">audit_instance_id,</if>
            <if test="bankReceiptUrl != null">bank_receipt_url,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="doctorId != null and doctorId != ''">#{doctorId},</if>
            <if test="doctorName != null and doctorName != ''">#{doctorName},</if>
            <if test="companyId != null ">#{companyId},</if>
            <if test="pointsAmount != null and pointsAmount != ''">#{pointsAmount},</if>
            <if test="cashAmount != null">#{cashAmount},</if>
            <if test="applyTime != null">#{applyTime},</if>
            <if test="auditStatus != null">#{auditStatus},</if>
            <if test="auditorId != null">#{auditorId},</if>
            <if test="auditTime != null">#{auditTime},</if>
            <if test="auditRemark != null">#{auditRemark},</if>
            <if test="paymentStatus != null">#{paymentStatus},</if>
            <if test="paymentTime != null">#{paymentTime},</if>
            <if test="bankReceiptNo != null">#{bankReceiptNo},</if>
            <if test="remark != null">#{remark},</if>
            <if test="auditInstanceId != null">#{auditInstanceId},</if>
            <if test="bankReceiptUrl != null">#{bankReceiptUrl},</if>
         </trim>
    </insert>

    <update id="updateWithdrawRecords" parameterType="WithdrawRecords">
        update withdraw_records
        <trim prefix="SET" suffixOverrides=",">
            <if test="doctorId != null and doctorId != ''">doctor_id = #{doctorId},</if>
            <if test="doctorName != null and doctorName != ''">doctor_name = #{doctorName},</if>
            <if test="companyId != null ">company_id = #{companyId},</if>
            <if test="pointsAmount != null and pointsAmount != ''">points_amount = #{pointsAmount},</if>
            <if test="cashAmount != null">cash_amount = #{cashAmount},</if>
            <if test="applyTime != null">apply_time = #{applyTime},</if>
            <if test="auditStatus != null">audit_status = #{auditStatus},</if>
            <if test="auditorId != null">auditor_id = #{auditorId},</if>
            <if test="auditTime != null">audit_time = #{auditTime},</if>
            <if test="auditRemark != null">audit_remark = #{auditRemark},</if>
            <if test="paymentStatus != null">payment_status = #{paymentStatus},</if>
            <if test="paymentTime != null">payment_time = #{paymentTime},</if>
            <if test="bankReceiptNo != null">bank_receipt_no = #{bankReceiptNo},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="auditInstanceId != null">audit_instance_id = #{auditInstanceId},</if>
            <if test="bankReceiptUrl != null">bank_receipt_url = #{bankReceiptUrl},</if>
        </trim>
        where withdraw_id = #{withdrawId}
    </update>

    <delete id="deleteWithdrawRecordsByWithdrawId" parameterType="java.lang.Long">
        delete from withdraw_records where withdraw_id = #{withdrawId}
    </delete>

    <delete id="deleteWithdrawRecordsByWithdrawIds" parameterType="java.lang.Long">
        delete from withdraw_records where withdraw_id in 
        <foreach item="withdrawId" collection="array" open="(" separator="," close=")">
            #{withdrawId}
        </foreach>
    </delete>
</mapper>