package com.ozs.rpm.dto.util;

import lombok.Data;

@Data
public class SqlDTO {

	// 查询条件
	private final String where = " #where# ";
	// 查询条件：分页
	private final String page = " #page# ";
	// 查询条件：排序
	private final String order = " #order# ";

	/**
	 * 分组聚合
	 */
	private final String group = " #group# ";

	private final String selectTemp = "@slt^";
	private final String whereTemp = "@whr^";
	private final String groupByTemp = "@gry^";

	private StringBuffer selectSql = new StringBuffer("select ").append(selectTemp);
	private StringBuffer fromSql = new StringBuffer(selectTemp).append(" from ");
	private StringBuffer whereSql = new StringBuffer(whereTemp).append(" where 1=1 ");
	private StringBuffer groupBySql = new StringBuffer("");

	public String toTempSql() {
		return selectSql.append(fromSql).append(whereSql).append(where).append(groupBySql).append(group).append(order)
				.append(page).toString();
	}

	public String toSql(String tempSql) {
		tempSql = tempSql.replaceAll("@gry\\^.*@gry\\^", "");
		return toResultSql(tempSql);
	}

	/**
	 * 柱折图图表Sql
	 * 
	 * @param tempSql
	 * @return
	 */
	public String toChartSql(String tempSql) {
		return toResultSql(tempSql);
	}

	public String toCountSql(String tempSql) {
		String countSql = tempSql.replaceAll("@slt\\^.*@slt\\^", "count(1)").replaceAll("@gry\\^.*", "");
		return toResultSql(countSql);
	}

	public String toTotalSql(String tempSql) {
		String totalSql = tempSql.replaceAll("@gry\\^.*@gry\\^", "").replaceAll("@text1\\^.*?@text2\\^", "\"——\"")
				.replaceAll(group, "");
		return toResultSql(totalSql);
	}

	private String toResultSql(String sql) {
		String resultSql = sql.replaceAll("@slt\\^", "").replaceAll("@whr\\^", "").replaceAll("@gry\\^", "")
				.replaceAll("@text1\\^", "").replaceAll("@text2\\^", "");
		return resultSql;
	}

}
