<?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="beilv.userbill.mapper.SysUserBillMapper">

    <resultMap type="SysUserBill" id="SysUserBillResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="pm" column="pm"/>
        <result property="title" column="title"/>
        <result property="number" column="number"/>
        <result property="balance" column="balance"/>
        <result property="useCategory" column="use_category"/>
        <result property="remark" column="remark"/>
        <result property="status" column="status"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="delFlag" column="del_flag"/>
        <result property="realName" column="real_name"/>
    </resultMap>

    <sql id="selectSysUserBillVo">
        select sub.id,
               sub.user_id,
               sub.pm,
               sub.title,
               sub.number,
               sub.balance,
               sub.use_category,
               sub.remark,
               sub.status,
               sub.create_by,
               sub.create_time,
               sub.update_by,
               sub.update_time,
               sub.del_flag,
               sm.real_name
        from sys_user_bill sub
                 left join sys_member sm on sub.user_id = sm.id
    </sql>

    <select id="selectSysUserBillList" parameterType="SysUserBill" resultMap="SysUserBillResult">
        <include refid="selectSysUserBillVo"/>
        <where>
            <if test="title != null  and title != ''">and sub.title like concat('%', #{title}, '%')</if>
        </where>
        order by sub.create_time desc
    </select>

    <select id="selectSysUserBillById" parameterType="Long" resultMap="SysUserBillResult">
        <include refid="selectSysUserBillVo"/>
        where id = #{id}
    </select>

    <insert id="insertSysUserBill" parameterType="SysUserBill" useGeneratedKeys="true" keyProperty="id">
        insert into sys_user_bill
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="pm != null">pm,</if>
            <if test="title != null and title != ''">title,</if>
            <if test="number != null">number,</if>
            <if test="balance != null">balance,</if>
            <if test="useCategory != null">use_category,</if>
            <if test="remark != null and remark != ''">remark,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="pm != null">#{pm},</if>
            <if test="title != null and title != ''">#{title},</if>
            <if test="number != null">#{number},</if>
            <if test="balance != null">#{balance},</if>
            <if test="useCategory != null">#{useCategory},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
        </trim>
    </insert>

    <update id="updateSysUserBill" parameterType="SysUserBill">
        update sys_user_bill
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="pm != null">pm = #{pm},</if>
            <if test="title != null and title != ''">title = #{title},</if>
            <if test="number != null">number = #{number},</if>
            <if test="balance != null">balance = #{balance},</if>
            <if test="useCategory != null">use_category = #{useCategory},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysUserBillById" parameterType="Long">
        delete
        from sys_user_bill
        where id = #{id}
    </delete>

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

    <select id="listByUserId" parameterType="SysUserBill" resultMap="SysUserBillResult">
        select sub.id,
        sub.user_id,
        sub.pm,
        sub.title,
        sub.number,
        sub.balance,
        sub.use_category,
        sub.remark,
        sub.status,
        sub.create_by,
        sub.create_time,
        sub.update_by,
        sub.update_time,
        sub.del_flag,
        sm.real_name
        from sys_user_bill sub
        left join sys_member sm on sub.user_id = sm.id
        <where>
            <if test="userId != null ">and sub.user_id = #{userId}</if>
        </where>
        order by sub.create_time desc
    </select>
</mapper>