<?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="study.u42.mapper.ItemMapper">
  <update id="rename">UPDATE item <set><if test="name != null">name=#{name,jdbcType=VARCHAR},</if></set> WHERE id=#{id} AND tenant_id=#{tenant}</update>
  <select id="prefix" resultType="string"><bind name="prefix" value="name + '%'"/>SELECT name FROM item <where>tenant_id=#{tenant} AND name LIKE #{prefix}</where> ORDER BY id</select>
  <insert id="insertStatus">INSERT INTO item_status(id,state) VALUES(#{id},#{state,javaType=study.u42.mapper.Status,jdbcType=VARCHAR,typeHandler=org.apache.ibatis.type.EnumTypeHandler})</insert>
  <select id="status" resultType="study.u42.mapper.Status">SELECT state FROM item_status WHERE id=#{id}</select>
  <select id="dbName" databaseId="mysql" resultType="string">SELECT 'mysql'</select>
  <select id="dbName" resultType="string">SELECT 'generic'</select>
  <select id="page" resultType="string">SELECT name FROM item WHERE tenant_id=#{tenant} ORDER BY id LIMIT #{limit} OFFSET #{offset}</select>
  <select id="categoryBasic" resultType="study.u42.mapper.Category">SELECT id,label FROM category WHERE id=#{id} AND tenant_id=#{tenant}</select>
  <resultMap id="lazyItemMap" type="study.u42.mapper.Item">
    <id column="id" property="id"/><result column="name" property="name"/>
    <association property="category" column="{id=category_id,tenant=tenant_id}" select="categoryBasic" fetchType="lazy"/>
  </resultMap>
  <select id="lazyItem" resultMap="lazyItemMap">SELECT id,name,category_id,tenant_id FROM item WHERE id=#{id} AND tenant_id=#{tenant}</select>
  <resultMap id="categoryItemsMap" type="study.u42.mapper.Category">
    <id column="category_id" property="id"/><result column="category_label" property="label"/>
    <collection property="items" ofType="study.u42.mapper.Item"><id column="item_id" property="id"/><result column="item_name" property="name"/></collection>
  </resultMap>
  <select id="categoryItems" resultMap="categoryItemsMap">
    SELECT c.id category_id,c.label category_label,i.id item_id,i.name item_name FROM category c
    LEFT JOIN item i ON i.category_id=c.id AND i.tenant_id=c.tenant_id WHERE c.id=#{id} AND c.tenant_id=#{tenant} ORDER BY i.id
  </select>
  <resultMap id="itemCategoryMap" type="study.u42.mapper.Item">
    <id column="item_id" property="id"/><result column="item_name" property="name"/>
    <association property="category" javaType="study.u42.mapper.Category"><id column="category_id" property="id"/><result column="category_label" property="label"/></association>
  </resultMap>
  <select id="itemCategory" resultMap="itemCategoryMap">
    SELECT i.id item_id,i.name item_name,c.id category_id,c.label category_label FROM item i
    LEFT JOIN category c ON c.id=i.category_id AND c.tenant_id=i.tenant_id WHERE i.id=#{id} AND i.tenant_id=#{tenant}
  </select>
  <resultMap id="itemMap" type="study.u42.mapper.Item">
    <id column="id" property="id"/><result column="tenant_id" property="tenantId"/><result column="name" property="name"/>
  </resultMap>
  <select id="item" resultMap="itemMap">SELECT id,tenant_id,name FROM item WHERE id=#{id}</select>
  <insert id="insert">INSERT INTO item(id,tenant_id,name) VALUES(#{id},#{tenant},#{name})</insert>
  <select id="name" resultType="string">SELECT name FROM item WHERE id=#{id}</select>
  <select id="search" resultType="string">
    SELECT name FROM item
    <where>
      tenant_id=#{tenant}
      <choose><when test="names != null and !names.isEmpty()">
        AND name IN <foreach collection="names" item="n" open="(" close=")" separator=",">#{n}</foreach>
      </when><otherwise>AND 1=0</otherwise></choose>
    </where>
    ORDER BY id
  </select>
  <select id="cursor" resultType="string" useCache="false">SELECT name FROM item ORDER BY id</select>
</mapper>
