<?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.cool.store.mapper.config.ConfigItemMapper">

    <resultMap id="BaseResultMap" type="com.cool.store.entity.config.ConfigItemDO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="item_code" property="itemCode" jdbcType="VARCHAR"/>
        <result column="item_name" property="itemName" jdbcType="VARCHAR"/>
        <result column="category_id" property="categoryId" jdbcType="BIGINT"/>
        <result column="sort_no" property="sortNo" jdbcType="INTEGER"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <result column="is_deleted" property="isDeleted" jdbcType="INTEGER"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="expense_type_code" property="expenseTypeCode" jdbcType="VARCHAR"/>
        <result column="multiple_choice" property="multipleChoice" jdbcType="TINYINT"/>
        <result column="created_user_id" property="createdUserId" jdbcType="VARCHAR"/>
        <result column="created_time" property="createdTime" jdbcType="TIMESTAMP"/>
        <result column="updated_user_id" property="updatedUserId" jdbcType="VARCHAR"/>
        <result column="updated_time" property="updatedTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="ItemVOResultMap" type="com.cool.store.vo.config.ConfigItemVO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="item_code" property="itemCode" jdbcType="VARCHAR"/>
        <result column="item_name" property="itemName" jdbcType="VARCHAR"/>
        <result column="category_id" property="categoryId" jdbcType="BIGINT"/>
        <result column="category_code" property="categoryCode" jdbcType="VARCHAR"/>
        <result column="category_name" property="categoryName" jdbcType="VARCHAR"/>
        <result column="sort_no" property="sortNo" jdbcType="INTEGER"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="expense_type_code" property="expenseTypeCode" jdbcType="VARCHAR"/>
        <result column="multiple_choice" property="multipleChoice" jdbcType="TINYINT"/>
        <result column="created_user_id" property="createdUserId" jdbcType="VARCHAR"/>
        <result column="created_time" property="createdTime" jdbcType="TIMESTAMP"/>
        <result column="updated_user_id" property="updatedUserId" jdbcType="VARCHAR"/>
        <result column="updated_time" property="updatedTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <select id="queryByCondition" resultMap="ItemVOResultMap">
        SELECT i.id,
               i.item_code,
               i.item_name,
               i.category_id,
               c.category_code,
               c.category_name,
               i.sort_no,
               i.status,
               i.remark,
               i.expense_type_code,
               i.multiple_choice,
               i.created_user_id,
               i.created_time,
               i.updated_user_id,
               i.updated_time
        FROM zxjp_cfg_item i
                 LEFT JOIN zxjp_cfg_item_category c ON i.category_id = c.id AND c.is_deleted = 0
        WHERE i.is_deleted = 0
        <if test="itemName != null and itemName != ''">
            AND i.item_name LIKE CONCAT('%', #{itemName}, '%')
        </if>
        <if test="itemCode != null and itemCode != ''">
            AND i.item_code = #{itemCode}
        </if>
        <if test="categoryId != null">
            AND i.category_id = #{categoryId}
        </if>
        <if test="status != null">
            AND i.status = #{status}
        </if>
        ORDER BY i.sort_no ASC, i.id DESC
    </select>

    <select id="queryAllActiveList" resultType="com.cool.store.vo.config.ConfigItemBaseInfoVO">
        SELECT item_code, item_name
        FROM zxjp_cfg_item
        WHERE is_deleted = 0 AND status = 1 ORDER BY sort_no ASC, id DESC
    </select>

    <select id="countByCategoryIds" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM zxjp_cfg_item
        WHERE is_deleted = 0
          AND category_id IN
        <foreach collection="categoryIds" item="categoryId" open="(" separator="," close=")">
            #{categoryId}
        </foreach>
    </select>

</mapper>
