package com.common.mapper;


import com.common.entity.DmColumn;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface DmColumnMapper {

    // 查询所有列
    @Select("SELECT * FROM dm_column WHERE deleted_at IS NULL")
    List<DmColumn> findAll();

    // 根据ID查询
   // @Select("SELECT * FROM dm_column WHERE id = #{id} AND deleted_at IS NULL")
    DmColumn findById(Long id);

    // 根据名称查询
    @Select("SELECT * FROM dm_column WHERE name = #{name} AND deleted_at IS NULL")
    DmColumn findByName(String name);

    // 根据显示名称模糊查询
    @Select("SELECT * FROM dm_column WHERE display_name LIKE CONCAT('%', #{displayName}, '%') AND deleted_at IS NULL")
    List<DmColumn> findByDisplayName(String displayName);

    // 根据是否内置查询
    @Select("SELECT * FROM dm_column WHERE is_built_in = #{isBuiltIn} AND deleted_at IS NULL")
    List<DmColumn> findByIsBuiltIn(Boolean isBuiltIn);

    // 插入数据
    @Insert("INSERT INTO dm_column (created_at, updated_at, name, display_name, storage_data_type, " +
            "business_data_type, is_built_in, is_hidden, is_not_normalizable, is_required, category_id, " +
            "custom_asset_category_id, is_virtual, table_id, asset_table_id, column_set_id, base_type, " +
            "user_task_id, created_by_id, create_dept, create_by, create_time, update_by, update_time) " +
            "VALUES (NOW(), NOW(), #{name}, #{displayName}, #{storageDataType}, #{businessDataType}, " +
            "#{isBuiltIn}, #{isHidden}, #{isNotNormalizable}, #{isRequired}, #{categoryId}, " +
            "#{customAssetCategoryId}, #{isVirtual}, #{tableId}, #{assetTableId}, #{columnSetId}, " +
            "#{baseType}, #{userTaskId}, #{createdById}, #{createDept}, #{createBy}, #{createTime}, " +
            "#{updateBy}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(DmColumn dmColumn);

    // 批量插入
    @Insert("<script>" +
            "INSERT INTO dm_column (created_at, updated_at, name, display_name, is_built_in, is_hidden) " +
            "VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(NOW(), NOW(), #{item.name}, #{item.displayName}, #{item.isBuiltIn}, #{item.isHidden})" +
            "</foreach>" +
            "</script>")
    int batchInsert(List<DmColumn> dmColumns);

    // 查询条件构造（使用XML方式）
    List<DmColumn> findByCondition(DmColumn condition);

    // 统计数量
    @Select("SELECT COUNT(*) FROM dm_column WHERE deleted_at IS NULL")
    Long count();


    /**
     *  查询所有数据标准化字段
     * @param
     * @return 数据标准化字段
     */
    List<Map<String, Object>>  selectAllNormal();
}