<?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.pj.project.tb_charge_record.TbChargeRecordMapper">

    <!-- 增 [G] -->
    <insert id="add">
		insert into 
		tb_charge_record (id, customer_id, customer_name, money, charge_channel, create_time, charge_people, no) 
		values (#{id}, #{customerId}, #{customerName}, #{money}, #{chargeChannel}, #{createTime}, #{chargePeople}, #{no}) 
	</insert>

    <!-- 删 -->
    <delete id="delete">
		delete from tb_charge_record 
		where id = #{id}
	</delete>

    <!-- 改 [G] -->
    <update id="update">
		update tb_charge_record set
		id = #{id}, 
		customer_id = #{customerId}, 
		customer_name = #{customerName}, 
		money = #{money}, 
		charge_channel = #{chargeChannel}, 
		create_time = #{createTime}, 
		charge_people = #{chargePeople}, 
		no = #{no}
		where id = #{id}
	</update>


    <!-- ================================== 查询相关 ================================== -->
    <!-- select id, customer_id, customer_name, money, charge_channel, create_time, charge_people, no from tb_charge_record  -->

    <!-- 通用映射：手动模式 -->
    <resultMap id="model" type="com.pj.project.tb_charge_record.TbChargeRecord">
        <result property="id" column="id"/>
        <result property="customerId" column="customer_id"/>
        <result property="customerName" column="customer_name"/>
        <result property="money" column="money"/>
        <result property="chargeChannel" column="charge_channel"/>
        <result property="createTime" column="create_time"/>
        <result property="chargePeople" column="charge_people"/>
        <result property="no" column="no"/>
    </resultMap>

    <!-- 公共查询sql片段 -->
    <sql id="select_sql">
		select * 
		from tb_charge_record 
	</sql>

    <!-- 查 - 根据id -->
    <select id="getById" resultMap="model">
        <include refid="select_sql"></include>
        where id = #{id}
    </select>

    <!-- 查集合 - 根据条件（参数为空时代表忽略指定条件） [G] -->
    <select id="getList" resultMap="model">
        <include refid="select_sql"></include>
        <where>
            <if test=' this.has("id") '>and id = #{id}</if>
            <if test=' this.has("customerId") '>and customer_id = #{customerId}</if>
            <if test=' this.has("customerName") '>and customer_name like concat('%', #{customerName},'%')</if>
            <if test=' this.has("money") '>and money = #{money}</if>
            <if test=' this.has("chargeChannel") '>and charge_channel = #{chargeChannel}</if>
            <if test=' this.has("createTime") '>and create_time = #{createTime}</if>
            <if test=' this.has("chargePeople") '>and charge_people = #{chargePeople}</if>
            <if test=' this.has("no") '>and no like concat('%',#{no},'%')</if>
        </where>
        order by
        <choose>
            <when test='sortType == 1'>id desc</when>
            <when test='sortType == 2'>customer_id desc</when>
            <when test='sortType == 3'>customer_name desc</when>
            <when test='sortType == 4'>money desc</when>
            <when test='sortType == 5'>charge_channel desc</when>
            <when test='sortType == 6'>create_time desc</when>
            <when test='sortType == 7'>charge_people desc</when>
            <when test='sortType == 8'>no desc</when>
            <otherwise>id desc</otherwise>
        </choose>
    </select>


</mapper>
