<?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_item.TbItemMapper">

    <!-- 增 [G] -->
    <insert id="add">
		insert into 
		tb_item (id, type_id, type_name, item_code, item_name, price, unit, car_length, max_weight, status)
		values (#{id}, #{typeId}, #{typeName}, #{itemCode}, #{itemName}, #{price}, #{unit}, #{carLength}, #{maxWeight}, #{status})
	</insert>

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

    <!-- 改 [G] -->
    <update id="update">
		update tb_item set
		id = #{id},
		type_id = #{typeId},
		type_name = #{typeName},
		item_code = #{itemCode},
		item_name = #{itemName},
		price = #{price},
		unit = #{unit},
		car_length =  #{carLength},
		max_weight = #{maxWeight},
		status = #{status}
		where id = #{id}
	</update>


    <!-- ================================== 查询相关 ================================== -->
    <!-- select id, type_id, type_name, item_code, item_name, price, unit from tb_item  -->

    <!-- 通用映射：手动模式 -->
    <resultMap id="model" type="com.pj.project.tb_item.TbItem">
        <result property="id" column="id" />
        <result property="typeId" column="type_id" />
        <result property="typeName" column="type_name" />
        <result property="itemCode" column="item_code" />
        <result property="itemName" column="item_name" />
        <result property="price" column="price" />
        <result property="unit" column="unit" />
        <result property="carLength" column="car_length" />
        <result property="maxWeight" column="max_weight" />
        <result property="status" column="status" />
    </resultMap>

    <!-- 公共查询sql片段 -->
    <sql id="select_sql">
		select *
		from tb_item
	</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("typeId") '> and type_id = #{typeId} </if>
            <if test=' this.has("typeName") '> and type_name = #{typeName} </if>
            <if test=' this.has("itemCode") '> and item_code = #{itemCode} </if>
            <if test=' this.has("itemName") '> and item_name like concat('%', #{itemName} ,'%') </if>
			<if test=' this.has("price") '> and price = #{price} </if>
			<if test=' this.has("status") '> and status = #{status} </if>
		</where>
		order by
		<choose>
            <when test='sortType == 1'> id desc </when>
            <when test='sortType == 2'> type_id desc </when>
            <when test='sortType == 3'> type_name desc </when>
            <when test='sortType == 4'> item_code desc </when>
            <when test='sortType == 5'> item_name desc </when>
            <when test='sortType == 6'> price desc </when>
            <when test='sortType == 7'> unit desc </when>
			<otherwise> id desc </otherwise>
		</choose>
	</select>










    <!-- 查 - 根据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("typeId") '> and type_id = #{typeId} </if>
            <if test=' this.has("typeName") '> and type_name = #{typeName} </if>
            <if test=' this.has("itemCode") '> and item_code = #{itemCode} </if>
            <if test=' this.has("itemName") '> and item_name like concat('%', #{itemName} ,'%') </if>
            <if test=' this.has("price") '> and price = #{price} </if>
            <if test=' this.has("unit") '> and unit = #{unit} </if>
        </where>
        order by
        <choose>
            <when test='sortType == 1'> id desc </when>
            <when test='sortType == 2'> type_id desc </when>
            <when test='sortType == 3'> type_name desc </when>
            <when test='sortType == 4'> item_code desc </when>
            <when test='sortType == 5'> item_name desc </when>
            <when test='sortType == 6'> price desc </when>
            <when test='sortType == 7'> unit desc </when>
            <otherwise> id desc </otherwise>
        </choose>
    </select>










</mapper>
