<?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.dict.SysDictTableMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.dict.SysDictTableDO">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="dict_group_id" jdbcType="BIGINT" property="dictGroupId" />
    <result column="table_name" jdbcType="VARCHAR" property="tableName" />
    <result column="table_code" jdbcType="VARCHAR" property="tableCode" />
    <result column="open_status" jdbcType="TINYINT" property="openStatus" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="deleted" jdbcType="BIT" property="deleted" />
    <result column="create_user_id" jdbcType="VARCHAR" property="createUserId" />
    <result column="update_user_id" jdbcType="VARCHAR" property="updateUserId" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>
  <sql id="Base_Column_List">
    id, dict_group_id, table_name, table_code, open_status, remark, deleted, create_user_id, 
    update_user_id, create_time, update_time
  </sql>
  <insert id="insertSelective" keyColumn="id" keyProperty="record.id" useGeneratedKeys="true">
    insert into sys_dict_table_${enterpriseId}
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="record.dictGroupId != null">
        dict_group_id,
      </if>
      <if test="record.tableName != null">
        table_name,
      </if>
      <if test="record.tableCode != null">
        table_code,
      </if>
      <if test="record.openStatus != null">
        open_status,
      </if>
      <if test="record.remark != null">
        remark,
      </if>
      <if test="record.deleted != null">
        deleted,
      </if>
      <if test="record.createUserId != null">
        create_user_id,
      </if>
      <if test="record.updateUserId != null">
        update_user_id,
      </if>
      <if test="record.createTime != null">
        create_time,
      </if>
      <if test="record.updateTime != null">
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="record.dictGroupId != null">
        #{record.dictGroupId},
      </if>
      <if test="record.tableName != null">
        #{record.tableName},
      </if>
      <if test="record.tableCode != null">
        #{record.tableCode},
      </if>
      <if test="record.openStatus != null">
        #{record.openStatus},
      </if>
      <if test="record.remark != null">
        #{record.remark},
      </if>
      <if test="record.deleted != null">
        #{record.deleted},
      </if>
      <if test="record.createUserId != null">
        #{record.createUserId},
      </if>
      <if test="record.updateUserId != null">
        #{record.updateUserId},
      </if>
      <if test="record.createTime != null">
        #{record.createTime},
      </if>
      <if test="record.updateTime != null">
        #{record.updateTime},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective">
    update sys_dict_table_${enterpriseId}
    <set>
      <if test="record.dictGroupId != null">
        dict_group_id = #{record.dictGroupId},
      </if>
      <if test="record.tableName != null">
        table_name = #{record.tableName},
      </if>
      <if test="record.tableCode != null">
        table_code = #{record.tableCode},
      </if>
      <if test="record.openStatus != null">
        open_status = #{record.openStatus},
      </if>
      <if test="record.remark != null">
        remark = #{record.remark},
      </if>
      <if test="record.deleted != null">
        deleted = #{record.deleted},
      </if>
      <if test="record.createUserId != null">
        create_user_id = #{record.createUserId},
      </if>
      <if test="record.updateUserId != null">
        update_user_id = #{record.updateUserId},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime},
      </if>
      <if test="record.updateTime != null">
        update_time = #{record.updateTime},
      </if>
    </set>
    where id = #{record.id}
  </update>

  <select id="selectExistTable" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM sys_dict_table_${enterpriseId}
    <where>
      <if test="id != null">
        AND id != #{id}
      </if>
      AND table_code = #{tableCode}
      AND deleted = 0
      LIMIT 1
    </where>
  </select>

  <update id="deleteByIds">
    UPDATE sys_dict_table_${enterpriseId}
    SET deleted = 1
    WHERE id IN
    <foreach item="id" collection="ids" open="(" separator="," close=")">
      #{id}
    </foreach>
    AND deleted = 0
  </update>

  <select id="selectList" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM sys_dict_table_${enterpriseId}
    WHERE deleted = 0
    <if test="dto.dictGroupId != null">
      AND dict_group_id = #{dto.dictGroupId}
    </if>
    <if test="dto.tableName != null and dto.tableName != ''">
      AND table_name LIKE CONCAT('%', #{dto.tableName}, '%')
    </if>
    <if test="dto.tableCode != null and dto.tableCode != ''">
      AND table_code = #{dto.tableCode}
    </if>
    <if test="dto.openStatus != null">
      AND open_status = #{dto.openStatus}
    </if>
    <if test="dto.remark != null and dto.remark != ''">
      AND remark LIKE CONCAT('%', #{dto.remark}, '%')
    </if>
    ORDER BY create_time ASC
  </select>

  <select id="selectById" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM sys_dict_table_${enterpriseId}
    WHERE id = #{id} AND deleted = 0 LIMIT 1
  </select>

  <select id="selectByIds" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM sys_dict_table_${enterpriseId}
    WHERE id IN
    <foreach collection="ids" item="id" open="(" separator="," close=")">
      #{id}
    </foreach>
    AND deleted = 0
  </select>

  <select id="selectByGroupIds" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM sys_dict_table_${enterpriseId}
    <where>
      dict_group_id IN
      <foreach item="groupId" collection="groupIds" open="(" separator="," close=")">
        #{groupId}
      </foreach>
      <if test="openStatus != null">
        AND open_status = #{openStatus}
      </if>
      AND deleted = 0
    </where>
  </select>

  <select id="filterEnableTableByGroupIds" resultType="java.util.Map">
    SELECT DISTINCT dict_group_id
    FROM sys_dict_table_${enterpriseId}
    WHERE dict_group_id IN
    <foreach collection="groupIds" item="groupId" open="(" separator="," close=")">
      #{groupId}
    </foreach>
    AND open_status = 1
    AND deleted = 0
  </select>

  <update id="deleteByGroupIds">
    UPDATE sys_dict_table_${enterpriseId}
    SET deleted = 1
    WHERE dict_group_id IN
    <foreach item="groupId" collection="groupIds" open="(" separator="," close=")">
      #{groupId}
    </foreach>
    AND deleted = 0
  </update>

  <select id="selectByTableCode" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM sys_dict_table_${enterpriseId}
    WHERE table_code = #{tableCode} AND deleted = 0 LIMIT 1
  </select>

  <select id="selectOpenTableListByTableCodes" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM sys_dict_table_${enterpriseId}
    WHERE table_code IN
    <foreach item="tableCode" collection="tableCodes" open="(" separator="," close=")">
      #{tableCode}
    </foreach>
    AND deleted = 0 AND open_status = 1
  </select>
</mapper>