using MySystem.PxcModels;
using System;
using System.Data;
using Library;

namespace MySystem
{
    public class PublicTableHelper
    {
        public readonly static PublicTableHelper Instance = new PublicTableHelper();
        private PublicTableHelper()
        { }

        public string Start(string table)
        {
            bool op = true;
            OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
            string result = "success";
            int startId = 0; //RedisDbconn.Instance.Get<int>("Start" + table + "Id");
            // int total = 0;
            while (op)
            {
                try
                {
                    DataTable dt = OtherMySqlConn.dtable("select * from " + table + " where Id>" + startId + " order by Id");
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            startId = int.Parse(function.CheckInt(dr["Id"].ToString()));
                            string fields = "";
                            string values = "";
                            DataTable localTable = dbconn.dtable("select * from " + table + " where Id=" + startId);
                            if (localTable.Rows.Count > 0)
                            {
                                foreach (DataColumn dc in localTable.Columns)
                                {
                                    if (!string.IsNullOrEmpty(dr[dc.ColumnName].ToString()))
                                    {
                                        if (dc.DataType == typeof(int) || dc.DataType == typeof(decimal) || dc.DataType == typeof(ulong))
                                        {
                                            fields += dc.ColumnName + "=" + dr[dc.ColumnName].ToString() + ",";
                                        }
                                        else
                                        {
                                            string val = "";
                                            if (dc.DataType == typeof(DateTime))
                                            {
                                                val = DateTime.Parse(dr[dc.ColumnName].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                                            }
                                            else
                                            {
                                                val = dr[dc.ColumnName].ToString();
                                            }
                                            fields += dc.ColumnName + "='" + val + "',";
                                        }
                                    }
                                }
                                fields = fields.TrimEnd(',');
                                string sql = "update " + table + " set " + fields + " where Id=" + startId;
                                dbconn.op(sql);
                            }
                            else
                            {
                                foreach (DataColumn dc in localTable.Columns)
                                {
                                    if (!string.IsNullOrEmpty(dr[dc.ColumnName].ToString()))
                                    {
                                        fields += dc.ColumnName + ",";
                                        if (dc.DataType == typeof(int) || dc.DataType == typeof(decimal) || dc.DataType == typeof(ulong))
                                        {
                                            values += dr[dc.ColumnName].ToString() + ",";
                                        }
                                        else
                                        {
                                            string val = "";
                                            if (dc.DataType == typeof(DateTime))
                                            {
                                                val = DateTime.Parse(dr[dc.ColumnName].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                                            }
                                            else
                                            {
                                                val = dr[dc.ColumnName].ToString();
                                            }
                                            values += "'" + val + "',";
                                        }
                                    }
                                }
                                fields = fields.TrimEnd(',');
                                values = values.TrimEnd(',');
                                string sql = "insert into " + table + " (" + fields + ") values (" + values + ")";
                                dbconn.op(sql);
                            }
                            startId = int.Parse(function.CheckInt(dr["Id"].ToString()));
                        }
                    }
                    else
                    {
                        op = false;
                        RedisDbconn.Instance.Set("Start" + table + "Id", startId);
                    }
                }
                catch (Exception ex)
                {
                    ErrorMsg msg = new ErrorMsg();
                    msg.Time = DateTime.Now;
                    msg.ErrorContent = ex.ToString();
                    RedisDbconn.Instance.AddList(table + ":Error", msg);
                }
            }
            // OtherMySqlConn.connstr = ;
            return result;
        }
    }
}