package com.ywt.outpatient.core;

import com.ywt.biz.common.util.StringHelper;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.persistence.Table;
import java.util.List;

/**
 * Created by huangguoping.
 */
public class SqlHelper {
    JdbcTemplate jdbcTemplate;

    public SqlHelper(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    public <T> PagedList<T> getPagedList(int pageIndex, int pageSize, String fields, String where, String orderBy, Class<T> entityClass, Object[] params) {
        if (pageIndex < 1) {
            pageIndex = 1;
        }

        if (pageSize < 1) {
            pageSize = 10;
        }

        Table table = entityClass.getAnnotation(Table.class);
        String tableName = table != null ? table.name() : entityClass.getSimpleName();
        tableName = String.format("`%s`", tableName);

        String whereSql = buildWhereSql(where);
        String countSql = String.format("select count(1) from %s %s;", tableName, whereSql);


        /**
         * 总记录数
         */
        int total = jdbcTemplate.queryForObject(countSql, params, Integer.class);

        StringBuilder sqlBuilder = new StringBuilder();
//        sqlBuilder.append("select count(1) from " + tableName);
//        sqlBuilder.append(whereSql);
//        sqlBuilder.append(";");

        sqlBuilder.append("select ");

        if(StringHelper.isNullOrEmpty(fields)){
            sqlBuilder.append("*");
        }else {
            sqlBuilder.append(fields);
        }

        sqlBuilder.append(" from " + tableName);
        sqlBuilder.append(whereSql);

        if(StringHelper.isNullOrEmpty(orderBy)){

        }else {
            sqlBuilder.append(" order by " + orderBy);
        }

        sqlBuilder.append(String.format(" limit %d, %d;", (pageIndex - 1) * pageSize, pageSize));
        List<T> list = jdbcTemplate.query(sqlBuilder.toString(), params, new BeanPropertyRowMapper<>(entityClass));

        return new PagedList<T>(pageIndex, pageSize, total, list);
    }

    public <T> PagedList<T> getPagedList(int pageIndex, int pageSize, String fields, String where, String orderBy, Class<T> entityClass, Object[] params, int[] argTypes) {
        if (pageIndex < 1) {
            pageIndex = 1;
        }

        if (pageSize < 1) {
            pageSize = 10;
        }

        Table table = entityClass.getAnnotation(Table.class);
        String tableName = table != null ? table.name() : entityClass.getSimpleName();
        tableName = String.format("`%s`", tableName);

        String whereSql = buildWhereSql(where);
        String countSql = String.format("select count(1) from %s %s;", tableName, whereSql);


        /**
         * 总记录数
         */
        int total = jdbcTemplate.queryForObject(countSql, params, Integer.class);

        StringBuilder sqlBuilder = new StringBuilder();
//        sqlBuilder.append("select count(1) from " + tableName);
//        sqlBuilder.append(whereSql);
//        sqlBuilder.append(";");

        sqlBuilder.append("select ");

        if(StringHelper.isNullOrEmpty(fields)){
            sqlBuilder.append("*");
        }else {
            sqlBuilder.append(fields);
        }

        sqlBuilder.append(" from " + tableName);
        sqlBuilder.append(whereSql);

        if(StringHelper.isNullOrEmpty(orderBy)){

        }else {
            sqlBuilder.append(" order by " + orderBy);
        }

        sqlBuilder.append(String.format(" limit %d, %d;", (pageIndex - 1) * pageSize, pageSize));
        List<T> list = jdbcTemplate.query(sqlBuilder.toString(), params, argTypes, new BeanPropertyRowMapper<>(entityClass));

        return new PagedList<T>(pageIndex, pageSize, total, list);
    }

    private String buildWhereSql(String where){
        if(StringHelper.isNullOrEmpty(where)){
            return " ";
        }

        return String.format(" where %s ", where);
    }

    /**
     * 获取分页列表数据，可以按需自定义select field 语句
     * @param pageIndex page index
     * @param pageSize page size
     * @param fields select field statement
     * @param from from statement
     * @param where where statement
     * @param orderBy order by statement
     * @param entityClass entity class
     * @param params parameters array
     * @param <T> generic
     * @return {@link PagedList}
     */
    public <T> PagedList<T> getPagedList(int pageIndex, int pageSize, String fields, String from, String where,
                                         String orderBy, Class<T> entityClass, Object[] params) {
        if (pageIndex < 1) {
            pageIndex = 1;
        }

        if (pageSize < 1) {
            pageSize = 10;
        }

        String whereSql = buildWhereSql(where);
        String countSql = String.format("select count(1) from %s %s;", from, whereSql);

        //总记录数
        int total = jdbcTemplate.queryForObject(countSql, params, Integer.class);

        StringBuilder sqlBuilder = new StringBuilder();

        sqlBuilder.append("select ");

        if(StringHelper.isNullOrEmpty(fields)){
            sqlBuilder.append("*");
        }else {
            sqlBuilder.append(fields);
        }

        sqlBuilder.append(" from " + from);
        sqlBuilder.append(whereSql);

        if(!StringHelper.isNullOrEmpty(orderBy)){
            sqlBuilder.append(" order by " + orderBy);
        }

        sqlBuilder.append(String.format(" limit %d, %d;", (pageIndex - 1) * pageSize, pageSize));
        List<T> list = jdbcTemplate.query(sqlBuilder.toString(), params, new BeanPropertyRowMapper<>(entityClass));

        return new PagedList<>(pageIndex, pageSize, total, list);
    }


}
