<?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.SysDictColumnMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.dict.SysDictColumnDO">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="dict_group_id" jdbcType="BIGINT" property="dictGroupId" />
    <result column="dict_table_id" jdbcType="BIGINT" property="dictTableId" />
    <result column="column_name" jdbcType="VARCHAR" property="columnName" />
    <result column="column_code" jdbcType="VARCHAR" property="columnCode" />
    <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" />
    <result column="auth_flag" jdbcType="INTEGER" property="authFlag" />
    <result column="sort_num" jdbcType="INTEGER" property="sortNum" />
  </resultMap>
  <sql id="Base_Column_List">
    id, dict_group_id, dict_table_id, column_name, column_code, open_status, remark, 
    deleted, create_user_id, update_user_id, create_time, update_time, auth_flag, sort_num
  </sql>
  <insert id="insertSelective" keyColumn="id" keyProperty="record.id" useGeneratedKeys="true">
    insert into sys_dict_column_${enterpriseId}
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="record.dictGroupId != null">
        dict_group_id,
      </if>
      <if test="record.dictTableId != null">
        dict_table_id,
      </if>
      <if test="record.columnName != null">
        column_name,
      </if>
      <if test="record.columnCode != null">
        column_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.dictTableId != null">
        #{record.dictTableId},
      </if>
      <if test="record.columnName != null">
        #{record.columnName},
      </if>
      <if test="record.columnCode != null">
        #{record.columnCode},
      </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_column_${enterpriseId}
    <set>
      <if test="record.dictGroupId != null">
        dict_group_id = #{record.dictGroupId},
      </if>
      <if test="record.dictTableId != null">
        dict_table_id = #{record.dictTableId},
      </if>
      <if test="record.columnName != null">
        column_name = #{record.columnName},
      </if>
      <if test="record.columnCode != null">
        column_code = #{record.columnCode},
      </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="selectExistColumn" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM sys_dict_column_${enterpriseId}
    <where>
      <if test="id != null">
        AND id != #{id}
      </if>
      AND column_code = #{columnCode}
      AND deleted = 0
      LIMIT 1
    </where>
  </select>

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

  <select id="selectList" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM sys_dict_column_${enterpriseId}
    <where>
      deleted = 0
      <if test="dto.dictGroupId != null">
        AND dict_group_id = #{dto.dictGroupId}
      </if>
      <if test="dto.dictTableId != null">
        AND dict_table_id = #{dto.dictTableId}
      </if>
      <if test="dto.columnName != null and dto.columnName != ''">
        AND column_name LIKE CONCAT('%', #{dto.columnName}, '%')
      </if>
      <if test="dto.columnCode != null and dto.columnCode != ''">
        AND column_code = #{dto.columnCode}
      </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 sort_num ASC
    </where>
  </select>

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

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

  <select id="selectColumnNumByTableIds" resultType="java.util.Map">
    SELECT COUNT(*) num, dict_table_id
    FROM sys_dict_column_${enterpriseId}
    WHERE dict_table_id IN
    <foreach collection="tableIds" item="tableId" separator="," open="(" close=")">
      #{tableId}
    </foreach>
    AND deleted = 0
    GROUP BY dict_table_id
  </select>

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

  <select id="filterEnableColumnByTableIds" resultType="java.util.Map">
    SELECT DISTINCT dict_table_id
    FROM sys_dict_column_${enterpriseId}
    WHERE dict_table_id IN
    <foreach collection="tableIds" item="tableId" open="(" separator="," close=")">
      #{tableId}
    </foreach>
    AND open_status = 1
    AND deleted = 0
  </select>

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

  <update id="deleteByTableIds">
    UPDATE sys_dict_column_${enterpriseId}
    SET deleted = 1
    WHERE dict_table_id IN
    <foreach item="tableId" collection="tableIds" separator="," open="(" close=")">
      #{tableId}
    </foreach>
    AND deleted = 0
  </update>

  <select id="selectOpenColumnByCode" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM sys_dict_column_${enterpriseId}
    WHERE column_code = #{columnCode} AND deleted = 0 AND open_status = 1 LIMIT 1
  </select>

  <select id="selectOpenColumnByCodes" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM sys_dict_column_${enterpriseId}
    WHERE column_code IN
    <foreach collection="columnCodes" item="item" separator="," open="(" close=")">
      #{item}
    </foreach>
    AND deleted = 0 AND open_status = 1
  </select>

  <select id="selectColumnByCodesIncludeDeleted" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM sys_dict_column_${enterpriseId}
    WHERE column_code IN
    <foreach collection="columnCodes" item="item" separator="," open="(" close=")">
      #{item}
    </foreach>
  </select>

  <select id="selectOpenColumnListByTableIds" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM sys_dict_column_${enterpriseId}
    WHERE dict_table_id IN
    <foreach item="tableId" collection="tableIds" separator="," open="(" close=")">
      #{tableId}
    </foreach>
    AND deleted = 0 AND open_status = 1
  </select>

  <select id="getRelationColumnIds" resultType="java.util.Map">
    SELECT column_id, relation_id
    FROM sys_dict_column_relation_${enterpriseId}
    WHERE column_id IN
    <foreach item="columnId" collection="columnIds" separator="," open="(" close=")">
      #{columnId}
    </foreach>
  </select>
</mapper>