<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="example.OrderMapper">
  <resultMap id="order" type="example.Order">
    <id column="id" property="id"/>
    <result column="status_code" property="status" typeHandler="example.StatusHandler"/>
    <result column="label" property="label"/>
    <result column="amount" property="amount"/>
  </resultMap>
  <select id="search" resultMap="order">
    SELECT id, status_code, label, amount FROM purchase_order
    <where>
      tenant_id = #{tenantId,jdbcType=BIGINT}
      <if test="status != null">
        AND status_code = #{status,jdbcType=INTEGER,typeHandler=example.StatusHandler}
      </if>
      <if test="ids != null">
        <choose>
          <when test="ids.size() > 0">
            AND id IN
            <foreach collection="ids" item="item" open="(" separator="," close=")">
              #{item,jdbcType=BIGINT}
            </foreach>
          </when>
          <otherwise>AND 1 = 0</otherwise>
        </choose>
      </if>
      <if test="pattern != null">AND label LIKE #{pattern} ESCAPE '!'</if>
    </where>
    ORDER BY
    <choose><when test="sort == 'amount'">amount DESC, id ASC</when><otherwise>id ASC</otherwise></choose>
  </select>
  <select id="find" resultMap="order">
    SELECT id, status_code, label, amount FROM purchase_order WHERE id = #{id}
  </select>
  <update id="updateStatus">
    UPDATE purchase_order <set>status_code = #{status,jdbcType=INTEGER,typeHandler=example.StatusHandler}</set>
    WHERE id = #{id}
  </update>
  <!-- Only used to inspect BoundSql with a fixed harmless fragment in the lab. Never accepts external input. -->
  <select id="rawOrderForComparison" resultMap="order">
    SELECT id, status_code, label, amount FROM purchase_order ORDER BY ${sortFragment}
  </select>
</mapper>
