<?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.DoctorBalanceLogMapper">
    
    <resultMap type="DoctorBalanceLog" id="DoctorBalanceLogResult">
        <result property="id"    column="id"    />
        <result property="doctorId"    column="doctor_id"    />
        <result property="doctorName"    column="doctor_name"    />
        <result property="changeMoney"    column="change_money"    />
        <result property="changeType"    column="change_type"    />
        <result property="relatedTaskId"    column="related_task_id"    />
        <result property="changeReason"    column="change_reason"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateAt"    column="update_at"    />
        <result property="createAt"    column="create_at"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyName" column="company_name"/>
        <result property="businessId"   column="business_id" />
        <result property="businessType" column="business_type" />
    </resultMap>

    <sql id="selectDoctorBalanceLogVo">
        select
        t.id,
        t.company_id,
        c.company_name,
        t.business_id,
        t.business_type,
        t.doctor_id,
        t.doctor_name,
        t.change_money,
        t.change_type,
        t.related_task_id,
        t.change_reason,
        t.create_by,
        t.update_by,
        t.update_at,
        t.create_at
        from doctor_balance_log t
        left join company c on c.company_id = t.company_id
    </sql>

    <select id="selectDoctorBalanceLogList" parameterType="DoctorBalanceLog" resultMap="DoctorBalanceLogResult">
        <include refid="selectDoctorBalanceLogVo"/>
        <where>
            <if test="doctorId != null and doctorId != ''"> and t.doctor_id = #{doctorId}</if>
            <if test="doctorName != null and doctorName != ''"> and t.doctor_name like concat('%', #{doctorName}, '%')</if>
            <if test="changeMoney != null"> and t.change_money = #{changeMoney}</if>
            <if test="changeType != null"> and t.change_type = #{changeType}</if>
            <if test="relatedTaskId != null and relatedTaskId != ''"> and t.related_task_id = #{relatedTaskId}</if>
            <if test="changeReason != null and changeReason != ''"> and t.change_reason = #{changeReason}</if>
            <if test="updateAt != null"> and t.update_at = #{updateAt}</if>
            <if test="createAt != null"> and t.create_at = #{createAt}</if>
            <if test="companyId != null"> and t.company_id = #{companyId}</if>
            <if test="businessType != null"> and t.business_type = #{businessType}</if>

            <!-- 可选：按公司名模糊查（如果你前端以后要加） -->
            <!-- <if test="companyName != null and companyName != ''"> and c.company_name like concat('%', #{companyName}, '%')</if> -->
        </where>
        order by t.id desc

    </select>


    <select id="selectDoctorBalanceLogById" parameterType="String" resultMap="DoctorBalanceLogResult">
        <include refid="selectDoctorBalanceLogVo"/>
        where t.id = #{id}
    </select>
        
    <insert id="insertDoctorBalanceLog" parameterType="DoctorBalanceLog" useGeneratedKeys="true" keyProperty="id">
        insert into doctor_balance_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="doctorId != null and doctorId != ''">doctor_id,</if>
            <if test="doctorName != null and doctorName != ''">doctor_name,</if>
            <if test="changeMoney != null">change_money,</if>
            <if test="changeType != null">change_type,</if>
            <if test="relatedTaskId != null">related_task_id,</if>
            <if test="changeReason != null">change_reason,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateAt != null">update_at,</if>
            <if test="createAt != null">create_at,</if>
            <if test="companyId != null">company_id,</if>
            <if test="businessId != null">business_id,</if>
            <if test="businessType != null">business_type,</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="changeMoney != null">#{changeMoney},</if>
            <if test="changeType != null">#{changeType},</if>
            <if test="relatedTaskId != null">#{relatedTaskId},</if>
            <if test="changeReason != null">#{changeReason},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateAt != null">#{updateAt},</if>
            <if test="createAt != null">#{createAt},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="businessId != null">#{businessId},</if>
            <if test="businessType != null">#{businessType},</if>
         </trim>
    </insert>

    <update id="updateDoctorBalanceLog" parameterType="DoctorBalanceLog">
        update doctor_balance_log
        <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="changeMoney != null">change_money = #{changeMoney},</if>
            <if test="changeType != null">change_type = #{changeType},</if>
            <if test="relatedTaskId != null">related_task_id = #{relatedTaskId},</if>
            <if test="changeReason != null">change_reason = #{changeReason},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateAt != null">update_at = #{updateAt},</if>
            <if test="createAt != null">create_at = #{createAt},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="businessId != null">business_id = #{businessId},</if>
            <if test="businessType != null">business_type = #{businessType},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDoctorBalanceLogById" parameterType="String">
        delete from doctor_balance_log where id = #{id}
    </delete>

    <delete id="deleteDoctorBalanceLogByIds" parameterType="String">
        delete from doctor_balance_log where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>