﻿using LibDatabaseCodeMaker.Database.Interface;
using System;
using System.Collections.Generic;

namespace LibDatabaseCodeMaker.Database.SqlStatement
{
	/// <summary>
	/// MariaDb/MySQL查询语句对象
	/// </summary>
	public partial class MariaDbSelectStatement : SelectStatement
	{
		/// <summary>
		/// 创建MariaDbSelectStatement对象和Java对象
		/// </summary>
		/// <returns>MariaDbSelectStatement对象</returns>
		public static MariaDbSelectStatement CreateWithJava()
		{
			MariaDbSelectStatement statement = new MariaDbSelectStatement();
			statement.Target = new JavaLanguage(statement);
			return statement;
		}

		/// <summary>
		/// 创建默认对象
		/// </summary>
		/// <param name="table">表对象</param>
		/// <param name="database">数据库对象</param>
		/// <returns>MariaDbSelectStatement对象</returns>
		public static SelectStatement CreateJavaSelectAll(DatabaseTableObject table, DatabaseObject database)
		{
			MariaDbSelectStatement statement = new MariaDbSelectStatement(table, database);
			JavaLanguage language = new JavaLanguage(statement);

			statement.IsReturnOne = false;

			language.FunctionName = "selectAll";
			language.FunctionComment = string.Format("获取 {0} 表中所有的数据", table);
			language.ReturnComment = string.Format("{0} 表中所有的数据", table);

			statement.Target = language;

			return statement;
		}

		/// <summary>
		/// 创建默认对象
		/// </summary>
		/// <param name="table">表对象</param>
		/// <param name="database">数据库对象</param>
		/// <param name="fields">表的所有字段</param>
		/// <returns>MariaDbSelectStatement对象; 如果没有主键, 则返回null</returns>
		public static SelectStatement CreateJavaSelectByKey(DatabaseTableObject table, DatabaseObject database, List<Field> fields)
		{
			foreach (var key in fields)
			{
				if (key.IsPrimaryKey)
				{
					MariaDbSelectStatement statement = new MariaDbSelectStatement(table, database);
					JavaLanguage language = new JavaLanguage(statement);

					statement.IsReturnOne = true;

					language.FunctionName = string.Format("selectBy{0}", key.Target.MemberName.Substring(1));
					language.FunctionComment = string.Format("根据指定的 {0}({1}), 获取 {2} 表中对应的数据", key.Name, key.Comment, table);
					language.ReturnComment = string.Format("指定的 {0}({1}) 对应的数据, 如果对应数据不存在则返回 null", key.Name, key.Comment);

					var words = new List<Tuple<string, ConditionVariable>>();

					words.Add(new Tuple<string, ConditionVariable>(key.ExplicitName + " =", null));
					words.Add(new Tuple<string, ConditionVariable>(null, ConditionVariable.CreateJavaVariable(key.Target.Type, key.Target.Name, key.CommentInCode)));

					statement.Conditions.Add(words);

					statement.Target = language;

					return statement;
				}
			}
			return null;
		}

		/// <summary>
		/// 创建默认对象
		/// </summary>
		/// <param name="table">表对象</param>
		/// <param name="database">数据库对象</param>
		/// <returns>SelectStatement对象</returns>
		public static MariaDbSelectStatement CreateJavaSelectRange(DatabaseTableObject table, DatabaseObject database)
		{
			MariaDbSelectStatement statement = new MariaDbSelectStatement(table, database);
			JavaLanguage language = new JavaLanguage(statement);

			statement.IsReturnOne = false;
			statement.IsSelectRange = true;

			language.FunctionName = "selectRange";
			language.FunctionComment = string.Format("获取 {0} 表中某个范围的数据", table);
			language.ReturnComment = string.Format("{0} 表中从 start 行开始的 count 条数据", table);

			statement.StartVariable = ConditionVariable.CreateJavaVariable("long", "start", "查询的开始位置, 其最小值是0");
			statement.CountVariable = ConditionVariable.CreateJavaVariable("long", "count", "查询的数据数量, 其最小值是0");

			return statement;
		}
	}
}
