Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.86 KB | None | 0 0
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using ConsoleApp1.Models;
  6. using System.ComponentModel.DataAnnotations;
  7. using System.Linq;
  8. using Microsoft.EntityFrameworkCore;
  9.  
  10. namespace ConsoleApp1
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             using (StreamReader r = new StreamReader("data1.json"))
  17.             {
  18.                 string json = r.ReadToEnd();
  19.                 List<DealItem> items = JsonConvert.DeserializeObject<List<DealItem>>(json);
  20.                 List<Deal> deals = new List<Deal>();
  21.                 using (DataBaseContext db = new DataBaseContext())
  22.                 {
  23.                     foreach (DealItem item in items)
  24.                 {
  25.  
  26.                         Deal deal = new Deal();
  27.                         switch (item.dealStatus)
  28.                         {
  29.                             case "в работе":
  30.                                 deal.dealStatus = db.DealStatus.Find(2);
  31.                                 break;
  32.                             default:
  33.                                 deal.dealStatus = db.DealStatus.Find(4);
  34.                                 break;
  35.                         }
  36.                         deal.GUID = Guid.NewGuid().ToString();
  37.                         deal.ChildNumber = item.ChildNumber;
  38.                         deal.CarStatus = item.CarStatus;
  39.                         deal.Season = item.Season;
  40.                         deal.DealType = db.DealTypes.Where(d => d.Title == item.DealType).FirstOrDefault();
  41.                         if (item.DealDS == "")
  42.                         {
  43.                             deal.DealDS = DateTime.Now;
  44.                         }
  45.                         else deal.DealDS = DateTime.Parse(item.DealDS);
  46.                         deal.ParentDealID = item.DealIDs;
  47.                         deal.Department = db.Departments.Where(d => d.DepartmentTitle == item.Department).FirstOrDefault();
  48.                         //deals.Add(deal);
  49.                         deal.Season = item.Season;
  50.                         deal.DealType = db.DealTypes.Where(d => d.Title == item.DealType).FirstOrDefault();
  51.                         deal.SelledId = db.ContrAgents.Where(d => d.ContrAgent_Name == item.SelledId).FirstOrDefault();
  52.                         deal.ShipperId = db.ContrAgents.Where(d => d.ContrAgent_Name == item.ShipperId).FirstOrDefault();
  53.                         deal.ConsumerId = db.ContrAgents.Where(d => d.ContrAgent_Name == item.ConsumerId).FirstOrDefault();
  54.                         deal.BuyerCash = db.Cash.Where(c => c.Title == item.BuyerCash).FirstOrDefault();
  55.                         deal.SellerCash = db.Cash.Where(c => c.Title == item.SellerCash).FirstOrDefault();
  56.                         deal.ShipperCash = db.Cash.Where(c => c.Title == item.ShipperCash).FirstOrDefault();
  57.                         deal.Product = db.Products.Where(p => p.ProductTitle.ToLower() == item.Product).FirstOrDefault();
  58.                         deal.Carname = item.Carname;
  59.                         deal.Rasstoyanie = item.Rasstoyanie;
  60.                         deal.Price = item.Price;
  61.                         deal.DonePrice = item.DonePrice;
  62.                         deal.ShipPrice = item.ShipPrice;
  63.                         deal.BuyPriceWithNDSForOneKG = item.BuyPriceWithNDSForOneKG;
  64.                         deal.SellPriceWithNDSForOneKG = item.SellPriceWithNDSForOneKG;
  65.                         deal.ShipPriceWithNDSForOneKG = item.ShipPriceWithNDSForOneKG;
  66.                         deal.SafePriceForOneKG = item.SafePriceForOneKG;
  67.                         deal.SellPriceNDS = item.SellPriceNDS;
  68.                         deal.BuyPriceNDS = item.BuyPriceNDS;
  69.                         deal.ShipPriceNDS = item.ShipPriceNDS;
  70.                         deal.MoneyNDS = item.MoneyNDS;
  71.                         deal.DeltaShipping = item.DeltaShipping;
  72.                         deal.DeltaProshenaya = item.DeltaProshenaya;
  73.                         deal.SummDeltaShipp = item.SummDeltaShipp;
  74.                         switch (item.DeltaCompinsation)
  75.                         {
  76.                             case "на ТК":
  77.                                 deal.DeltaCompinsation = 0;
  78.                                 break;
  79.                             case "на продавце":
  80.                                 deal.DeltaCompinsation = 1;
  81.                                 break;
  82.                             default:
  83.                                 deal.DeltaCompinsation = 2;
  84.                                 break;
  85.                         }
  86.  
  87.                         deal.DeltaPrice = item.DeltaPrice;
  88.                         deal.SubmitDelta = item.SubmitDelta;
  89.                         switch (item.transoprtScheme)
  90.                         {
  91.                             case "по отгруженному количеству":
  92.                                 deal.transoprtScheme = TransoprtScheme.BySellCount;
  93.                                 break;
  94.                             default:
  95.                                 deal.transoprtScheme = TransoprtScheme.ByShipCount;
  96.                                 break;
  97.                         }
  98.                         deal.ShipPriceWithNDS = item.ShipPriceWithNDS;
  99.                         deal.ShipSancForNum = item.ShipSancForNum;
  100.                         deal.TransportdopRaschod = item.TransportdopRaschod;
  101.                         deal.transoprtSummWithNDS = item.transoprtSummWithNDS;
  102.                         deal.transoprtShipNDS = item.transoprtShipNDS;
  103.                         deal.transoprtShipWithOutNDS = item.transoprtShipWithOutNDS;
  104.                         deal.ACTBUNumber = item.ACTBUNumber;
  105.                         if (item.ACTBUDate == "")
  106.                         {
  107.                             deal.ACTBUDate = DateTime.Now;
  108.                         }else deal.ACTBUDate =DateTime.Parse(item.ACTBUDate);
  109.  
  110.                         deal.TTNType = item.TTNType;
  111.                         deal.transoprtPayWithNDS = item.transoprtPayWithNDS;
  112.                         switch (item.buyScheme)
  113.                         {
  114.                             case "по отгруженному количеству":
  115.                                 deal.buyScheme = TransoprtScheme.BySellCount;
  116.                                 break;
  117.                             default:
  118.                                 deal.buyScheme = TransoprtScheme.ByShipCount;
  119.                                 break;
  120.                         }
  121.                         deal.buyWithNDS = item.buyWithNDS;
  122.                         deal.buySancForLost = item.buySancForLost;
  123.                         deal.buyWithCompensation = item.buyWithCompensation;
  124.                         deal.withOutNDS = item.withOutNDS;    
  125.                         deal.buyNDS = item.buyNDS;
  126.                         deal.buyActBU = item.buyActBU;
  127.                         if (item.buyActDate == "")
  128.                         {
  129.                             deal.buyActDate = DateTime.Now;
  130.                         }
  131.                         else deal.buyActDate = DateTime.Parse(item.buyActDate);
  132.                         deal.buySummWithNDS = item.buySummWithNDS;
  133.                         deal.cSNumber = item.cSNumber;
  134.                         deal.cSShipPoint = item.cSShipPoint;
  135.                         deal.cSOtgruz = item.cSOtgruz;
  136.                         deal.cSOneKGPriceWithoutNDS = item.cSOneKGPriceWithoutNDS;
  137.                         deal.cSSumm = item.cSSumm;
  138.                         if (item.safeContr == "")
  139.                         {
  140.                             deal.safeContr = null;
  141.                         }
  142.                         else deal.safeContr = db.ContrAgents.Where(s => s.ContrAgent_Name == item.safeContr).FirstOrDefault();// DateTime.Parse(item.buyActDate);
  143.                         deal.safePriceWithNDS = item.safePriceWithNDS;
  144.                         deal.safeNDSSUMM = item.safeNDSSUMM;
  145.                         deal.safeSUMMWithOutNDS = item.safeSUMMWithOutNDS;
  146.                         deal.safeActBU = item.safeActBU;
  147.                         if (item.safeActDate == "")
  148.                         {
  149.                             deal.safeActDate = DateTime.Now;
  150.                         }
  151.                         else deal.safeActDate = DateTime.Parse(item.safeActDate);
  152.                         deal.raschAgentskije = item.raschAgentskije;
  153.                         deal.raschProchee = item.raschProchee;
  154.                         deal.raschSummAll = item.raschSummAll;
  155.                         deal.prodWithNDS = item.prodWithNDS;
  156.                         deal.prodWithOutNDS = item.prodWithOutNDS;
  157.                         deal.prodNDSSumm = item.prodNDSSumm;
  158.                         deal.prodDogovor = item.prodDogovor;
  159.                         deal.prodActBU = item.prodActBU;
  160.                         if (item.prodActDate == "")
  161.                         {
  162.                             deal.prodActDate = DateTime.Now;
  163.                         }
  164.                         else deal.prodActDate = DateTime.Parse(item.prodActDate);
  165.                         deal.prodPayWithNDS = item.prodPayWithNDS;
  166.                         deal.nakRaschWithOutNDS = item.nakRaschWithOutNDS;
  167.                         deal.bazaRaspredByDepartment = item.bazaRaspredByDepartment;
  168.                         deal.nakRaschOfDepartmentWithOutNP = item.nakRaschOfDepartmentWithOutNP;
  169.                         deal.bazaRaspredelenijaAllDeals = item.bazaRaspredelenijaAllDeals;
  170.                         deal.UKZatraty = item.UKZatraty;
  171.                         deal.nakRasch = item.nakRasch;
  172.                         deal.nakRaschSUMM = item.nakRaschSUMM;
  173.                         deal.marjPribyl = item.marjPribyl;
  174.                         deal.marjiNalnost = item.marjiNalnost;
  175.                         deal.marjaOneKg = item.marjaOneKg;
  176.                         deal.MPMinusNDSNP = item.MPMinusNDSNP;
  177.                         deal.marjiNalnostNP = item.marjiNalnostNP;
  178.                         deal.marjaOneKgNP = item.marjaOneKgNP;
  179.                         deal.prybDoNP = item.prybDoNP;
  180.                         deal.nalogNP = item.nalogNP;
  181.                         deal.chistPryb = item.chistPryb;
  182.                         deal.chistPrybOneKG = item.chistPrybOneKG;
  183.                         deal.chistPrybRenta = item.chistPrybRenta;
  184.                         deal.poteriPriDostWithOutCompensation = item.poteriPriDostWithOutCompensation;
  185.                         deal.poteriPribPriDostWithOutCompensation = item.poteriPribPriDostWithOutCompensation;
  186.                         deal.poteriDostavPrijem = item.poteriDostavPrijem;
  187.                         deal.poteriPryb = item.poteriPryb;
  188.                         deal.chistPrybnedPryb = item.chistPrybnedPryb;
  189.                         deal.chistPrybOneKGFinish = item.chistPrybOneKGFinish;
  190.                         deal.finishRenta = item.finishRenta;
  191.                         deals.Add(deal);
  192.                     }
  193.                     int itemscount = deals.GroupBy(test => test.ParentDealID)
  194.                    .Select(grp => grp.First()).ToList().Count();
  195.                     for (int i = 1; i <= 67; i++)
  196.                     {
  197.                         Deal[] deals1 = deals.Where(d => d.ParentDealID == i).ToArray();
  198.                         if (deals1.Length != 0)
  199.                         {
  200.                             Deal parentDealInfo = new Deal();
  201.                             parentDealInfo = Deal.CalculateDealInfo(deals1);
  202.                             parentDealInfo.Product = deals1[0].Product;
  203.                             parentDealInfo.dealStatus = deals1[0].dealStatus;
  204.                             parentDealInfo.DealType = deals1[0].DealType;
  205.                             parentDealInfo.Season = deals1[0].Season;
  206.                             parentDealInfo.Department = deals1[0].Department;
  207.                             parentDealInfo.DealDS = deals1[0].DealDS;
  208.                             parentDealInfo.ConsumerId = deals1[0].ConsumerId;
  209.                             parentDealInfo.BuyerCash = deals1[0].BuyerCash;
  210.                             parentDealInfo.GUID = Guid.NewGuid().ToString();
  211.                             db.Add(parentDealInfo);
  212.                             db.SaveChanges();
  213.                             Console.WriteLine("Доабвлена родительская сделка: {0}", db.Deals.Where(d => d.GUID == parentDealInfo.GUID).FirstOrDefault().GUID);
  214.  
  215.                             deals1 = deals1.OrderBy(d => d.ChildNumber).ToArray();
  216.                             foreach(Deal deal in deals)
  217.                             {
  218.                                 Console.WriteLine(deal.DealID);
  219.                                 if (deal.DealID != 0) {
  220.                                     deal.DealID = 0;
  221.                                     db.Add(deal);
  222.                                     db.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Deals OFF");
  223.                                     db.SaveChanges();
  224.                                     Console.WriteLine("Добавлена дочерняя сделка ID:{0}. GUID: {1}", db.Deals.Where(d => d.GUID == deal.GUID).FirstOrDefault().DealID, deal.GUID);
  225.                                     db.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Deals OFF");
  226.                                 }
  227.                                 else
  228.                                 {
  229.                                     db.Add(deal);
  230.                                     db.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Deals OFF");
  231.                                     db.SaveChanges();
  232.                                     Console.WriteLine("Добавлена дочерняя сделка ID:{0}. GUID: {1}", db.Deals.Where(d => d.GUID == deal.GUID).FirstOrDefault().DealID, deal.GUID);
  233.                                     db.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Deals OFF");
  234.                                 }
  235.                                
  236.                             }
  237.                            
  238.                            
  239.                         }
  240.  
  241.                     }
  242.                 }
  243.                 Console.ReadLine();
  244.             }
  245.         }
  246.  
  247.  
  248.         public class DealItem
  249.         {
  250.             public int DealIDs { get; set; }
  251.             public int ChildNumber { get; set; }
  252.             public string dealStatus { get; set; }
  253.             public string CarStatus { get; set; }
  254.             public string Season { get; set; }
  255.             public string DealUID { get; set; }
  256.             public string GUID { get; set; }
  257.             public string DealType { get; set; }
  258.             public string buyDate { get; set; }
  259.             public string sellDate { get; set; }
  260.             public string DealDS { get; set; }
  261.             public string Department { get; set; }
  262.             public string Product { get; set; }
  263.             public string ShipAddress { get; set; }
  264.             public string SelledId { get; set; }
  265.             public string ConsumerId { get; set; }
  266.             public string ShipperId { get; set; }
  267.             public string Shippername { get; set; }
  268.             public string Carname { get; set; }
  269.             public float Rasstoyanie { get; set; }
  270.             public float Price { get; set; }
  271.             public float ShipPrice { get; set; }
  272.             public float DonePrice { get; set; }
  273.             public float BuyPriceWithNDSForOneKG { get; set; }
  274.             public float SellPriceWithNDSForOneKG { get; set; }
  275.             public float ShipPriceWithNDSForOneKG { get; set; }
  276.             public float SafePriceForOneKG { get; set; }
  277.             public float SellPriceNDS { get; set; }
  278.             public float BuyPriceNDS { get; set; }
  279.             public float ShipPriceNDS { get; set; }
  280.             public float SafePriceNDS { get; set; }
  281.             public float MoneyNDS { get; set; }
  282.             public float DeltaShipping { get; set; }
  283.             public float DeltaProshenaya { get; set; }
  284.             public float SummDeltaShipp { get; set; }
  285.             public string DeltaCompinsation { get; set; }
  286.             public float DeltaPrice { get; set; }
  287.             public float  SubmitDelta { get; set; }
  288.             public string transoprtScheme { get; set; }
  289.             public float  ShipPriceWithNDS { get; set; }
  290.             public float  ShipSancForNum { get; set; }
  291.             public float  TransportdopRaschod { get; set; }
  292.             public float  transoprtSummWithNDS { get; set; }
  293.             public float  transoprtShipNDS { get; set; }
  294.             public float  transoprtShipWithOutNDS { get; set; }
  295.             public string ACTBUNumber { get; set; }
  296.             public string ACTBUDate { get; set; }
  297.             public string TTNType { get; set; }
  298.             public float  transoprtPayWithNDS { get; set; }
  299.             public string buyScheme { get; set; }
  300.             public float  buyWithNDS { get; set; }
  301.             public float  buySancForLost { get; set; }
  302.             public float  buyWithCompensation { get; set; }
  303.             public float  withOutNDS { get; set; }
  304.             public float  buyNDS
  305.             {
  306.                 get; set;
  307.             }
  308.             public string buyActBU { get; set; }
  309.             public string buyActDate { get; set; }
  310.             public float buySummWithNDS { get; set; }
  311.             public string cSNumber { get; set; }
  312.             public string cSShipPoint { get; set; }
  313.             public float cSOtgruz { get; set; }
  314.             public float cSOneKGPriceWithoutNDS { get; set; }
  315.             public float cSSumm { get; set; }
  316.             public string safeContr { get; set; }
  317.             public float safePriceWithNDS { get; set; }
  318.             public float safeNDSSUMM { get; set; }
  319.             public float safeSUMMWithOutNDS { get; set; }
  320.             public string safeActBU { get; set; }
  321.             public string safeActDate { get; set; }
  322.             public float raschAgentskije { get; set; }
  323.             public float raschProchee { get; set; }
  324.             public float raschSummAll
  325.             {
  326.                 get; set;
  327.             }
  328.             public float prodWithNDS { get; set; }
  329.             public float prodWithOutNDS { get; set; }
  330.             public float prodNDSSumm { get; set; }
  331.             public string prodDogovor { get; set; }
  332.             public string prodActBU { get; set; }
  333.             public string prodActDate { get; set; }
  334.             public float prodPayWithNDS { get; set; }
  335.             public float nakRaschWithOutNDS { get; set; }
  336.             public float bazaRaspredByDepartment { get; set; }
  337.             public float nakRaschOfDepartmentWithOutNP { get; set; }
  338.             public float bazaRaspredelenijaAllDeals { get; set; }
  339.             public float UKZatraty { get; set; }
  340.             public float nakRasch { get; set; }
  341.             public float nakRaschSUMM { get; set; }
  342.             public float marjPribyl { get; set; }
  343.             public float marjiNalnost { get; set; }
  344.             public float marjaOneKg { get; set; }
  345.             public float MPMinusNDSNP { get; set; }
  346.             public float marjiNalnostNP { get; set; }
  347.             public float marjaOneKgNP { get; set; }
  348.             public float prybDoNP { get; set; }
  349.             public float nalogNP { get; set; }
  350.             public float chistPryb { get; set; }
  351.             public float chistPrybOneKG { get; set; }
  352.             public float chistPrybRenta { get; set; }
  353.             public float poteriPriDostWithOutCompensation { get; set; }
  354.             public float poteriPribPriDostWithOutCompensation { get; set; }
  355.             public float poteriDostavPrijem { get; set; }
  356.             public float poteriPryb { get; set; }
  357.             public float chistPrybnedPryb { get; set; }
  358.             public float chistPrybOneKGFinish { get; set; }
  359.             public float finishRenta { get; set; }
  360.             public string SellerCash { get; set; }
  361.             public string ShipperCash { get; set; }
  362.             public string BuyerCash { get; set; }
  363.         }
  364.     }
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement