
using MECF.Framework.Common.DataCenter;
using MECF.Framework.Common.MECF.Framework.Common.Utilities;
using MECF.Framework.Common.Utilities;
using System;
using System.Collections.Generic;

namespace MECF.Framework.UI.Client.RecipeEditorLib.RecipeModel
{
    public class RecipeHistory
    {
        /// <summary>
        /// GUID编号
        /// </summary>
        public string StrGuid { get; set; }

        /// <summary>
        /// 登录的用户名
        /// </summary>
        public string RoleName { get; set; }

        /// <summary>
        /// Recipe类型
        /// </summary>
        public string ProcessType { get; set; }

        /// <summary>
        /// 快照时Recipe名称
        /// </summary>
        public string RecipeName { get; set; }

        /// <summary>
        /// 从XML转成string类型的Recipe内容
        /// </summary>
        public string RecipeFromXML { get; set; }

        /// <summary>
        /// Recipe使用GZip压缩
        /// </summary>
        private string RecipeGZip;

        /// <summary>
        /// Recipe内容压缩后转成哈希值
        /// </summary>
        public string HashCode { get; set; }

        /// <summary>
        /// 保存Receip时记录时间
        /// </summary>
        public string CreateTime { get; set; }

        /// <summary>
        /// 预留空白1，默认为null
        /// </summary>
        public string Spare1 { get; set; }

        /// <summary>
        /// 预留空白2，默认为null
        /// </summary>
        public string Spare2 { get; set; }



        #region 数据库操作
        public bool SaveDB()
        {
            //只有存储时，才自动给这些属性赋值
            CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            StrGuid = Guid.NewGuid().ToString();
            HashCode = Sha256.EncodeString(RecipeFromXML);
            RecipeGZip = StringGZip.CompressString(RecipeFromXML);


            return QueryDataClient.Instance.Service.ExcuteTransAction(new List<string> { SaveCmd() });
        }

        /// <summary>
        /// 根据Recipe名称，返回此名称产生的所有历史记录
        /// </summary>
        /// <param name="recipeName"></param>
        /// <returns></returns>
        public List<RecipeHistory> QueryDB(string recipeName)
        {
            string cmd = $"select * from recipe_history_data where recipe_name = '{recipeName}'";
            var dt = QueryDataClient.Instance.Service.QueryData(cmd);
            if (dt != null && dt.Rows.Count > 0)
            {
                List<RecipeHistory> recipeMemories = new List<RecipeHistory>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    recipeMemories.Add(new RecipeHistory()
                    {
                        StrGuid = dt.Rows[i]["str_guid"].ToString(),
                        RoleName = dt.Rows[i]["role_name"].ToString(),
                        ProcessType = dt.Rows[i]["process_type"].ToString(),
                        RecipeName = dt.Rows[i]["recipe_name"].ToString(),
                        HashCode = dt.Rows[i]["hash_code"].ToString(),
                        //RecipeFromXML = StringGZip.DecompressString( dt.Rows[0]["recipe_gzip"].ToString()), //一般情况下这个内容都很大，返回集合中不对该属性赋值。
                        CreateTime = dt.Rows[i]["create_time"].ToString(),
                        Spare1 = dt.Rows[i]["spare1"].ToString(),
                        Spare2 = dt.Rows[i]["spare2"].ToString()
                    });
                    if (i >= 25)
                        break;
                }

                return recipeMemories;
            }
            return null;
        }

        /// <summary>
        /// 根据Recipe名称+createTime，返回特定记录
        /// </summary>
        /// <param name="recipeName"></param>
        /// <param name="createTime"></param>
        /// <returns></returns>
        public RecipeHistory QueryDB(RecipeHistory recipeHistory)
        {
            string cmd = $"select * from recipe_history_data where str_guid = '{recipeHistory.StrGuid}'";
            var dt = QueryDataClient.Instance.Service.QueryData(cmd);
            if (dt != null && dt.Rows.Count > 0)
            {
                return new RecipeHistory()
                {
                    StrGuid = dt.Rows[0]["str_guid"].ToString(),
                    RoleName = dt.Rows[0]["role_name"].ToString(),
                    ProcessType = dt.Rows[0]["process_type"].ToString(),
                    RecipeName = dt.Rows[0]["recipe_name"].ToString(),
                    HashCode = dt.Rows[0]["hash_code"].ToString(),
                    RecipeFromXML = StringGZip.DecompressString( dt.Rows[0]["recipe_gzip"].ToString()),//数据库查询时返回解压后的正确数据
                    CreateTime = dt.Rows[0]["create_time"].ToString(),
                    Spare1 = dt.Rows[0]["spare1"].ToString(),
                    Spare2 = dt.Rows[0]["spare2"].ToString()
                };
            }
            return null;
        }

        private string SaveCmd()
        {
            string cmd_data_insert =
            "insert into recipe_history_data" +
            "(str_guid,role_name,process_type,recipe_name,hash_code,recipe_gzip, create_time,spare1,spare2)" +
            $"values ('{StrGuid}'," +
            $"'{RoleName}'," +
            $"'{ProcessType}'," +
            $"'{RecipeName}'," +
            $"'{HashCode}'," +
            $"'{RecipeGZip}'," +
            $"'{CreateTime}'," +
            $"'{Spare1}'," +
            $"'{Spare2}')";

            return cmd_data_insert;
        }

       
        #endregion


    }
}