using Long.Common.System;
using Long.Dapper;

namespace Long.DAO.System
{
    /// <summary>
    /// 下一个值-数据库
    /// </summary>
    public class NextValueDatabase : Database<NextValue>, INextValueDatabase
    {
        #region 获取编码

        /// <summary>
        /// 获取编码
        /// </summary>
        /// <param name="prefix">前缀</param>
        /// <param name="tableName">表名</param>
        /// <param name="fieldName">字段名</param>
        /// <param name="defaultValue">默认值</param>
        /// <param name="length">编码长度</param>
        /// <returns></returns>
        public async Task<string> GetCodeAsync(string prefix, string tableName, string fieldName, int defaultValue = 1, int length = 5)
        {
            var value = await GetNextValueAsync(tableName, fieldName, defaultValue);
            string result = string.Format("{{0}}{{1:d" + length.ToString() + "}}", prefix, value);
            return result;
        }

        #endregion

        #region 获取日期编码

        /// <summary>
        /// 获取日期编码
        /// </summary>
        /// <param name="prefix">前缀</param>
        /// <param name="tableName">表名</param>
        /// <param name="fieldName">字段名</param>
        /// <param name="defaultValue">默认值</param>
        /// <param name="length">日期后编码长度</param>
        /// <returns></returns>
        public async Task<string> ExcuteGetCodeFromDateAsync(string prefix, string tableName, string fieldName, long defaultValue = 1, int length = 5)
        {
            var value = await GetNextValueAsync(tableName, fieldName, defaultValue);
            string result = string.Format("{{0}}{{1}}{{2:d" + length.ToString() + "}}", prefix, new DateTime().ToString("yyyyMMdd"), value);
            return result;
        }

        #endregion
    }
}
