<?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_pass_record.TbPassRecordMapper">

	<!-- 增 [G] -->
	<insert id="add">
		insert into 
		tb_pass_record (id, business_id, customer_id, customer_name, card_no, status, in_time, out_time, update_time)
		values (#{id}, #{businessId}, #{customerId}, #{customerName}, #{cardNo}, #{status}, #{inTime}, #{outTime}, #{updateTime}) 
	</insert>

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

	<!-- 改 [G] -->
	<update id="update">
		update tb_pass_record set
		id = #{id}, 
		business_id = #{businessId}, 
		customer_id = #{customerId}, 
		customer_name = #{customerName}, 
		card_no = #{cardNo}, 
		status = #{status}, 
		in_time = #{inTime}, 
		out_time = #{outTime}, 
		update_time = #{updateTime}
		where id = #{id}
	</update>


	<!-- ================================== 查询相关 ================================== -->
	<!-- select id, business_id, customer_id, customer_name, card_no, status, in_time, out_time, update_time from tb_pass_record  -->
	
	<!-- 通用映射：手动模式 -->
	<resultMap id="model" type="com.pj.project.tb_pass_record.TbPassRecord">
		<result property="id" column="id" />
		<result property="businessId" column="business_id" />
		<result property="customerId" column="customer_id" />
		<result property="customerName" column="customer_name" />
		<result property="cardNo" column="card_no" />
		<result property="status" column="status" />
		<result property="inTime" column="in_time" />
		<result property="outTime" column="out_time" />
		<result property="updateTime" column="update_time" />
	</resultMap>
	
	<!-- 公共查询sql片段 -->
	<sql id="select_sql">
		select * 
		from tb_pass_record 
	</sql>
	
	<!-- 查 - 根据id -->
    <select id="getById" resultMap="model">
        <include refid="select_sql"></include>
        where id = #{id}
    </select>

    <!-- 查 - 根据businessId -->
    <select id="getByBusinessId" resultMap="model">
        <include refid="select_sql"></include>
        where business_id = #{businessId}
    </select>
	
	<!-- 查集合 - 根据条件（参数为空时代表忽略指定条件） [G] -->
	<select id="getList" resultMap="model">
		<include refid="select_sql"></include>
		<where>
            status != 1
			<if test=' this.has("id") '> and id = #{id} </if>
			<if test=' this.has("businessId") '> and business_id = #{businessId} </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("cardNo") '> and card_no like concat('%',#{cardNo},'%') </if>
			<if test=' this.has("status") '> and status = #{status} </if>
			<if test=' this.has("inTime") '> and in_time = #{inTime} </if>
			<if test=' this.has("outTime") '> and out_time = #{outTime} </if>
			<if test=' this.has("updateTime") '> and update_time = #{updateTime} </if>
		</where>
		order by
		<choose>
			<when test='sortType == 1'> id desc </when>
			<when test='sortType == 2'> business_id desc </when>
			<when test='sortType == 3'> customer_id desc </when>
			<when test='sortType == 4'> customer_name desc </when>
			<when test='sortType == 5'> card_no desc </when>
			<when test='sortType == 6'> status desc </when>
			<when test='sortType == 7'> in_time desc </when>
			<when test='sortType == 8'> out_time desc </when>
			<when test='sortType == 9'> update_time desc </when>
			<otherwise> id desc </otherwise>
		</choose>
	</select>
	
	
	
	
	
	
	
	
	

</mapper>
