Esempio n. 1
0
        public void Run()
        {
            var allTables = GetAllTable();

            foreach (DataRow tableRow in allTables.Rows)
            {
                var tableName = tableRow["name"].ToString();

                var apiName = tableNameToApiName(tableName, apiPrefixStr);

                SetMappingList(apiName);

                var apiMethodInfo = proxy.GetType().GetMethod(apiName, BindingFlags.Public | BindingFlags.Instance);
                var requestType   = apiMethodInfo.GetParameters()[1].ParameterType;

                var columnsNameTable = db.GetColumnsName(tableName);

                #region Just Write Report and log
                var reportModel = new ReportModel();
                reportModel.Title   = $"{tableName} ({specifyCondition})";
                reportModel.Columns = columnsNameTable.AsEnumerable().Select(
                    x => (name: x["COLUMN_NAME"].ToString(), type: x["DATA_TYPE"].ToString())
                    )
                                      .Where(x => (x.type != "bit" && x.type != "int") &&
                                             (x.name.Contains("Name") || x.name.Contains("ID") || x.name.Contains("Code"))
                                             ).Take(5).Select(x => x.name).ToList();
                reportModel.Columns.Add("ErrorMessage");
                log.Warn($"{tableName} Start");
                #endregion

                var dataTable     = db.GetData(tableName, specifyCondition);
                var indexSet      = new HashSet <int>(Enumerable.Range(0, dataTable.Rows.Count));
                var retryCount    = 0;
                var countMaxLimit = 10;
                var retryFlag     = true;
                while (retryFlag && retryCount < countMaxLimit)
                {
                    var retrySet = new HashSet <int>();
                    retryFlag = false;

                    foreach (var i in indexSet)
                    {
                        var requestInstance = Activator.CreateInstance(requestType);
                        try
                        {
                            foreach (DataRow columnRow in columnsNameTable.Rows)
                            {
                                var columnNmae = columnRow["COLUMN_NAME"].ToString();
                                var columntype = columnRow["DATA_TYPE"].ToString();

                                var preProcess = PreProcessSetValue(columnNmae, dataTable.Rows[i][columnNmae]);

                                log.Info($"Set {preProcess.FullColumnName} => {dataTable.Rows[i][columnNmae].ToString()} Start");
                                SetColumnValue(preProcess.FullColumnName, preProcess.FullColumnName, preProcess.Value, requestType, requestInstance, apiName);
                                log.Info($"Set {preProcess.FullColumnName} => {dataTable.Rows[i][columnNmae].ToString()} End");

                                #region Just Write Report
                                if (reportModel.Columns.Contains(columnNmae))
                                {
                                    reportModel.PutValue(i, columnNmae, dataTable.Rows[i][columnNmae].ToString());
                                }
                                #endregion
                            }
                        }
                        catch (CustomValueNullException e)
                        {
                            log.Warn(e.Message);
                            if (retryCount == countMaxLimit - 1)
                            {
                                log.Warn($"Fail Retry more than {countMaxLimit} index: {i}");
                                errorFunc();
                                reportModel.PutValue(i, "ErrorMessage", $"Fail Retry more than {countMaxLimit}");
                            }
                            else
                            {
                                retryFlag = true;
                                retrySet.Add(i);
                            }
                            continue;
                        }

                        try
                        {
                            object[] parametersArray = new object[] { header, requestInstance };

                            log.Warn($"{apiName} Send Request index: {i}");
                            log.Debug($"{ToJson(parametersArray)}");
                            var apiResult = apiMethodInfo.Invoke(proxy, parametersArray);
                            log.Warn($"{apiName} Send Request Finish");
                        }
                        catch (Exception e)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.ResetColor();
                            errorFunc();
                            log.Warn(e.ToString());
                            reportModel.PutValue(i, "ErrorMessage", e.InnerException == null ? e.Message : e.InnerException.Message);
                        }
                    }
                    indexSet = retrySet;
                    retryCount++;
                }
                log.Info($"{tableName} End");
                reportModel.ExportCSV();
                reportModel.ExportHTML();
            }
        }