コード例 #1
0
        public void InsertVegetableQuotation(VegetablesQuotationEntity veg)
        {
            string connStr = null;

            connStr = ConfigurationManager.ConnectionStrings["SciCrop.AgroAPI.Connector.Properties.Settings.dbConnectionString"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(connStr))
            {
                try
                {
                    conn.Open();

                    string sql = "INSERT INTO [dbo].[SCICROP_BRVEGPRICES_T]" +
                                 "           ([ProvIbge], [VegName], [VegPrice], [PriceDate], [DataSrc])" +
                                 "     VALUES" +
                                 "           (@0" +
                                 "           ,@1" +
                                 "           ,@2" +
                                 "           ,@3" +
                                 "           ,@4)";

                    SqlCommand insertCommand = new SqlCommand(sql, conn);

                    DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(Convert.ToDouble(veg.PriceDate));

                    insertCommand.Parameters.Add(new SqlParameter("0", veg.IbgeUfId));
                    insertCommand.Parameters.Add(new SqlParameter("1", veg.VegName));
                    insertCommand.Parameters.Add(new SqlParameter("2", veg.VegPrice));
                    insertCommand.Parameters.Add(new SqlParameter("3", dt));
                    insertCommand.Parameters.Add(new SqlParameter("4", veg.DataSource));

                    insertCommand.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw;
                }
                finally
                {
                    if (conn != null)
                    {
                        conn.Close();
                    }
                }
            }
        }
コード例 #2
0
        private void Run()
        {
            try
            {
                progressBar1.Value = 0;
                GetLastRun();
                string restFreight    = "freight/dailyUpdate";
                string restVegetables = "vegetables/dailyUpdateQuotation/" + collectDateVeg.ToString("yyyy-MM-dd");
                if (collectDateFreight == DateTime.MinValue)
                {
                    restFreight = "freight/historic";
                    updateStatus("Downloading freight historical data. ");
                }
                else
                {
                    updateStatus("Looking for freight data since: " + strDateFreight);
                }

                if (collectDateVeg == DateTime.MinValue)
                {
                    restVegetables = "vegetables/historicQuotation";
                    updateStatus("Downloading vegetables historical data. ");
                }
                else
                {
                    updateStatus("Looking for vegetables data since: " + strDateVeg);
                }


                //textBox1.Text = "";
                button1.Text    = "Processing...";
                button1.Enabled = false;


                ScicropEntity se            = new ScicropEntity();
                PayloadEntity payloadEntity = new PayloadEntity();

                Freight freight = new Freight();
                freight.Date = strDateFreight;
                List <Freight> freightList = new List <Freight>();
                freightList.Add(freight);
                payloadEntity.FreightLst = freightList;

                VegetablesQuotationEntity vegetablesQuotationEntity = new VegetablesQuotationEntity();
                vegetablesQuotationEntity.PriceDate = strDateVeg;
                List <VegetablesQuotationEntity> vegs = new List <VegetablesQuotationEntity>();
                vegs.Add(vegetablesQuotationEntity);
                payloadEntity.VegetablesQuotationEntities = vegs;

                se.PayloadEntity = payloadEntity;



                string jsonStr = UrlHelper.Instance.PostScicropEntityJsonBA(restFreight, se, authEntity.UserEntity.Email, authEntity.UserEntity.Hash);
                se          = ScicropEntity.FromJson(jsonStr);
                freightList = se.PayloadEntity.FreightLst;


                if (freightList.Count > 0)
                {
                    int i = 1;
                    updateStatus("Inserting " + freightList.Count + " freight offer(s).");
                    foreach (var item in freightList)
                    {
                        updateStatus(item.Load.LoadName + ": " + item.SourceCity.Name + " > " + item.DestinationCity.Name);
                        DbConnector dbc = new DbConnector();
                        try
                        {
                            dbc.InsertFreight(item);
                        }
                        catch (Exception e)
                        {
                            updateStatus(e.Message);
                        }
                        progressBar1.Maximum = freightList.Count;
                        progressBar1.Value   = i;
                        i++;
                    }

                    updateStatus("All freight data inserted.");
                }
                else
                {
                    updateStatus("No new freight data was found.");
                }
                WriteEventLog("Freight data collected (" + freightList.Count + " | REST: " + restFreight + " | " + isSilent + ")");
                GetLastRun();

                jsonStr = UrlHelper.Instance.GetScicropEntityJsonBA(restVegetables, authEntity.UserEntity.Email, authEntity.UserEntity.Hash);
                se      = ScicropEntity.FromJson(jsonStr);
                vegs    = se.PayloadEntity.VegetablesQuotationEntities;

                if (vegs.Count > 0)
                {
                    int i = 1;
                    updateStatus("Inserting " + vegs.Count + " vegetables quotation(s).");
                    foreach (var item in vegs)
                    {
                        updateStatus(item.VegName + ": " + item.VegPrice + " from  " + item.IbgeUfId);
                        DbConnector dbc = new DbConnector();
                        try
                        {
                            dbc.InsertVegetableQuotation(item);
                        }
                        catch (Exception e)
                        {
                            updateStatus(e.Message);
                        }
                        progressBar1.Maximum = vegs.Count;
                        progressBar1.Value   = i;
                        i++;
                    }

                    updateStatus("All Vegetables data inserted.");
                }
                else
                {
                    updateStatus("No new Vegetables data was found.");
                }
                WriteEventLog("Vegetables data collected (" + vegs.Count + " | REST: " + restVegetables + " | " + isSilent + ")");
                GetLastRun();
            }
            catch (Exception ex)
            {
                updateStatus("Error: " + ex.Message);
            }
            finally
            {
                button1.Enabled = true;
                button1.Text    = "Run";
                if (this.isSilent)
                {
                    System.Environment.Exit(0);
                }
            }
        }