package com.boman.report.mapper;

import com.boman.domain.GenTable;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;

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

public interface TableMapper {

    /**
     * 查询表数据
     * @return 业务集合
     */
    @SelectProvider(type = SqlProvider.class, method = "selectList")
    public List<GenTable> selectList(@Param("tableName") String tableName,
                                     @Param("tableComment") String tableComment);

    @SelectProvider(type = SqlProvider.class, method = "selectCount")
    public Integer selectCount(@Param("tableName") String tableName,
                               @Param("tableComment") String tableComment);

    class SqlProvider {

        public String selectCount(Map<String, Object> params) {
            String tableName = (String) params.get("tableName");
            String tableComment = (String) params.get("tableComment");

            StringBuilder sql = new StringBuilder();
            sql.append("select count(1) from gen_table ").append(" where 1 = 1 ");
            if(StringUtils.isNotEmpty(tableName)) {
                sql.append("AND lower(table_name) like lower(concat('%',").append(tableName).append(", '%'))");
            }
            if(StringUtils.isNotEmpty(tableComment)) {
                sql.append("AND lower(table_comment) like lower(concat('%',").append(tableComment).append(", '%'))");
            }
            return sql.toString();
        }

        public String selectList(Map<String, Object> params) {
            String tableName = (String) params.get("tableName");
            String tableComment = (String) params.get("tableComment");

            StringBuilder sql = new StringBuilder();
            sql.append("select id, table_name as tableName, table_comment as tableComment, sub_table_name as subTableName, sub_table_fk_name  as subTableFkName, ")
                    .append("class_name as className, tpl_category as tplCategory, package_name as packageName, module_name as moduleName, business_name as businessName,  ")
                    .append("function_name, function_author, gen_type, gen_path, options, ")
                    .append(" create_by, create_time, update_by, update_time, remark, is_menu,  ")
                    .append("menu_role, ak_column, dk_column, trigger_create, trigger_retrieve, ")
                    .append("trigger_update, trigger_delete, trigger_submit, real_table_name,  ")
                    .append("filter_conditions, extended_attributes, table_primary_key, table_column as tableColumn")
                    .append(" from gen_table ").append(" where 1 = 1 ");
            if(StringUtils.isNotEmpty(tableName)) {
               sql.append("AND lower(table_name) like lower(concat('%',").append("'").append(tableName).append("'").append(", '%'))");
            }
            if(StringUtils.isNotEmpty(tableComment)) {
                sql.append("AND lower(table_comment) like lower(concat('%',").append("'").append(tableComment).append("'").append(", '%'))");
            }
            return sql.toString();
        }
    }
}
