Exemplo n.º 1
0
        private void CreateNewClient(Background.SyncRonService.Model.Contragent.Client client)
        {
            var userInfos = _context.Set <UserInfo>()
                            .Where(x => x.UserId != 1 && x.UserId != 11)
                            .ToList();

            if (client != null)
            {
                NumberOfCalls numberOfCalls = client.Periodichnost_Zvonkov == "0" ? NumberOfCalls.WithoutType : client.Periodichnost_Zvonkov == "10" ? NumberOfCalls.OnePerMonth : client.Periodichnost_Zvonkov == "20"
                            ? NumberOfCalls.OnePerTwoWeek : client.Periodichnost_Zvonkov == "30" ? NumberOfCalls.ThreePerMonth : client.Periodichnost_Zvonkov == "40" ? NumberOfCalls.OnePerWeek : client.Periodichnost_Zvonkov == "50" ?
                                              NumberOfCalls.FivePerMonth : client.Periodichnost_Zvonkov == "60" ? NumberOfCalls.SixPerMonth : client.Periodichnost_Zvonkov == "90" ? NumberOfCalls.TwoPerWeek : NumberOfCalls.WithoutType;

                NumberOfShipments numberOfShipments = client.Periodichnost_Otgruzok == "0" ? NumberOfShipments.WithoutType : client.Periodichnost_Zvonkov == "10" ? NumberOfShipments.OnePerMonth : client.Periodichnost_Zvonkov == "20"
                   ? NumberOfShipments.OnePerTwoWeek : client.Periodichnost_Zvonkov == "30" ? NumberOfShipments.ThreePerMonth : client.Periodichnost_Zvonkov == "40" ? NumberOfShipments.OnePerWeek : client.Periodichnost_Zvonkov == "50" ?
                                                      NumberOfShipments.FivePerMonth : client.Periodichnost_Zvonkov == "60" ? NumberOfShipments.SixPerMonth : client.Periodichnost_Zvonkov == "90" ? NumberOfShipments.TwoPerWeek : NumberOfShipments.WithoutType;
                ClientGroup clientGroup = ClientGroup.NewOrReanimated;

                if (this.client1 != null)
                {
                    numberOfCalls     = this.client1.NumberOfCalls;
                    numberOfShipments = this.client1.NumberOfShipments;
                    clientGroup       = this.client1.Group;
                }

                Data.Entities.Clients.Client client1 = _context.Set <Client>()
                                                       .Add(new Client(new ClientCreate()
                {
                    ClientType        = ClientTypes.Middle1,
                    Title             = client.Contragent,
                    LegalEntity       = client.Contragent_NameFull,
                    NumberOfCalls     = numberOfCalls,
                    NumberOfShipments = numberOfShipments,
                    Group             = clientGroup
                })).Entity;
                client1.IsAcctive = true;
                _context.Set <Client>().Add(client1);
                _context.SaveChanges();
                ClientInfo clientInfo = _context.Set <ClientInfo>()
                                        .Add(new ClientInfo(client1.Id, Guid.Parse(client.Contragent_ID), client.Phones)).Entity;
                _context.SaveChanges();
                if (client.GR_Contragent != "" && _context.Set <ClientGR>().FirstOrDefault(c => c.NameGr == client.GR_Contragent) == null)
                {
                    _context.Set <ClientGR>().Add(new ClientGR()
                    {
                        Clients = new List <Data.Entities.Clients.Client>()
                        {
                            client1
                        },
                        NameGr = client.GR_Contragent
                    });
                }
                else if (client.GR_Contragent != "")
                {
                    _context.Set <ClientGR>().FirstOrDefault(c => c.NameGr == client.GR_Contragent).Clients.Add(client1);
                }
                _context.SaveChanges();
                if (client.Manager_ID != null && client.Manager_ID != "")
                {
                    var managersGuidStr = client.Manager_ID.Split(',');
                    var managersGuid    = new List <Guid>();
                    foreach (var str in managersGuidStr)
                    {
                        try
                        {
                            managersGuid.Add(Guid.Parse(str));
                        }
                        catch
                        {
                        }
                    }
                    foreach (Guid guid in managersGuid)
                    {
                        var userInfo = userInfos.FirstOrDefault(x => x.OneCId == guid);
                        if (userInfo != null)
                        {
                            var workGroup = _context.Set <WorkGroup>()
                                            .FirstOrDefault(x => x.RegionalManagerId == userInfo.UserId ||
                                                            x.EscortManagerId == userInfo.UserId);
                            if (workGroup != null && workGroup.Clients.FirstOrDefault(c => c.ClientId == clientInfo.ClientId) == null)
                            {
                                workGroup.BindClient(new BindClient()
                                {
                                    ClientId    = client1.Id,
                                    WorkgroupId = workGroup.Id
                                });
                            }
                        }
                    }
                }
                _context.SaveChanges();
                if (client.Phones != null && client.Phones != "")
                {
                    var e            = client.Phones.Split(',');
                    var clientPhones = _context.Set <ClientPhone>().Where(c => c.ClientId == clientInfo.ClientId);
                    foreach (var phone in e)
                    {
                        string newPhone = Regex.Replace(phone, @"[^0-9]", "");
                        if (clientPhones == null || clientPhones.FirstOrDefault(c => c.Phone != null && (c.Phone == newPhone || c.Phone == newPhone.Substring(1) || c.Phone == newPhone.Substring(2))) == null)
                        {
                            ClientPhone clientPhone = new ClientPhone()
                            {
                                Client = client1,
                                Phone  = newPhone,
                            };
                            //if (newPhone.Length == 10)
                            //{
                            //}
                            //else if (newPhone.Length == 11)
                            //{
                            //    newPhone = newPhone.Substring(1);
                            //    clientPhone = new ClientPhone()
                            //    {
                            //        Client = client1,
                            //        Phone = newPhone,
                            //    };
                            //}
                            //else if (newPhone.Length == 12)
                            //{
                            //    newPhone = newPhone.Substring(2);
                            //    clientPhone = new ClientPhone()
                            //    {
                            //        Client = client1,
                            //        Phone = newPhone,
                            //    };
                            //}
                            _context.Set <ClientPhone>().Add(clientPhone);
                            _context.SaveChanges();
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private async void CreateClient(List <Model.Contragent.Client> clients)
        {
            //_context.Set<Data.Entities.OneCInfo.ClientInfo>().RemoveRange(_context.Set<Data.Entities.OneCInfo.ClientInfo>());
            //_context.Set<Data.Entities.Clients.Client>().RemoveRange(_context.Set<Data.Entities.Clients.Client>());
            //_context.Set<Data.Entities.Clients.ClientPhone>().RemoveRange(_context.Set<Data.Entities.Clients.ClientPhone>());
            //_context.Set<Data.Entities.Clients.ClientGR>().RemoveRange(_context.Set<Data.Entities.Clients.ClientGR>());
            //_context.SaveChanges();
            int i = 0;
            //clients.RemoveRange(0, 800);
            var userInfos = _context.Set <Data.Entities.OneCInfo.UserInfo>()
                            .ToList();

            foreach (Model.Contragent.Client client in clients)
            {
                i++;
                File.WriteAllText("i.txt", i.ToString());
                try
                {
                    ClientInfo clientInfo1 = _context.Set <ClientInfo>().FirstOrDefault(c => c.OneCId.ToString() == client.Contragent_ID);
                    Data.Entities.Clients.Client client1 = _context.Set <Data.Entities.Clients.Client>().FirstOrDefault(c => clientInfo1 != null && clientInfo1.ClientId == c.Id);
                    if (client1 == null)
                    {
                        NumberOfCalls numberOfCalls = client.Periodichnost_Zvonkov == "0" ? NumberOfCalls.WithoutType : client.Periodichnost_Zvonkov == "10" ? NumberOfCalls.OnePerMonth : client.Periodichnost_Zvonkov == "20"
                            ? NumberOfCalls.OnePerTwoWeek : client.Periodichnost_Zvonkov == "30" ? NumberOfCalls.ThreePerMonth : client.Periodichnost_Zvonkov == "40" ? NumberOfCalls.OnePerWeek : client.Periodichnost_Zvonkov == "50" ?
                                                      NumberOfCalls.FivePerMonth : client.Periodichnost_Zvonkov == "60" ? NumberOfCalls.SixPerMonth : client.Periodichnost_Zvonkov == "90" ? NumberOfCalls.TwoPerWeek : NumberOfCalls.WithoutType;

                        NumberOfShipments numberOfShipments = client.Periodichnost_Otgruzok == "0" ? NumberOfShipments.WithoutType : client.Periodichnost_Zvonkov == "10" ? NumberOfShipments.OnePerMonth : client.Periodichnost_Zvonkov == "20"
                           ? NumberOfShipments.OnePerTwoWeek : client.Periodichnost_Zvonkov == "30" ? NumberOfShipments.ThreePerMonth : client.Periodichnost_Zvonkov == "40" ? NumberOfShipments.OnePerWeek : client.Periodichnost_Zvonkov == "50" ?
                                                              NumberOfShipments.FivePerMonth : client.Periodichnost_Zvonkov == "60" ? NumberOfShipments.SixPerMonth : client.Periodichnost_Zvonkov == "90" ? NumberOfShipments.TwoPerWeek : NumberOfShipments.WithoutType;

                        client1 = _context.Set <Data.Entities.Clients.Client>()
                                  .Add(new Data.Entities.Clients.Client(new ClientCreate()
                        {
                            ClientType        = ClientTypes.Middle1,
                            Title             = client.Contragent,
                            LegalEntity       = client.Contragent_NameFull,
                            NumberOfCalls     = numberOfCalls,
                            NumberOfShipments = numberOfShipments,
                            Group             = ClientGroup.NewOrReanimated
                        })).Entity;
                        client1.IsAcctive = true;
                        _context.Set <Data.Entities.Clients.Client>().Add(client1);
                        _context.SaveChanges();
                        clientInfo1 = _context.Set <ClientInfo>()
                                      .Add(new ClientInfo(client1.Id, Guid.Parse(client.Contragent_ID), client.Phones)).Entity;
                        await _context.SaveChangesAsync();

                        if (client.GR_Contragent != "" && _context.Set <ClientGR>().FirstOrDefault(c => c.NameGr == client.GR_Contragent) == null)
                        {
                            _context.Set <ClientGR>().Add(new ClientGR()
                            {
                                Clients = new List <Data.Entities.Clients.Client>()
                                {
                                    client1
                                },
                                NameGr = client.GR_Contragent
                            });
                        }
                        else if (client.GR_Contragent != "")
                        {
                            _context.Set <ClientGR>().FirstOrDefault(c => c.NameGr == client.GR_Contragent).Clients.Add(client1);
                        }
                        await _context.SaveChangesAsync();

                        if (client.Manager_ID != null && client.Manager_ID != "")
                        {
                            var managersGuidStr = client.Manager_ID.Split(',');
                            var managersGuid    = new List <Guid>();
                            foreach (var str in managersGuidStr)
                            {
                                try
                                {
                                    managersGuid.Add(Guid.Parse(str));
                                }
                                catch
                                {
                                }
                            }
                            foreach (Guid guid in managersGuid)
                            {
                                var userInfo = userInfos.FirstOrDefault(x => x.OneCId == guid);
                                if (userInfo != null)
                                {
                                    var workGroup = _context.Set <WorkGroup>()
                                                    .FirstOrDefault(x => x.RegionalManagerId == userInfo.UserId ||
                                                                    x.EscortManagerId == userInfo.UserId);
                                    if (workGroup != null && workGroup.Clients.FirstOrDefault(c => c.ClientId == clientInfo1.ClientId) == null)
                                    {
                                        workGroup.BindClient(new BindClient()
                                        {
                                            ClientId    = client1.Id,
                                            WorkgroupId = workGroup.Id
                                        });
                                    }
                                }
                            }
                            await _context.SaveChangesAsync();
                        }
                        if (client.Phones != null && client.Phones != "")
                        {
                            var e            = client.Phones.Split(',');
                            var clientPhones = _context.Set <ClientPhone>().Where(c => c.ClientId == clientInfo1.ClientId);
                            foreach (var phone in e)
                            {
                                string newPhone = Regex.Replace(phone, @"[^0-9]", "");
                                if (clientPhones == null || clientPhones.FirstOrDefault(c => c.Phone != null && (c.Phone == newPhone || c.Phone == newPhone.Substring(1) || c.Phone == newPhone.Substring(2))) == null)
                                {
                                    ClientPhone clientPhone = null;
                                    clientPhone = new ClientPhone()
                                    {
                                        Client = client1,
                                        Phone  = newPhone,
                                    };
                                    if (clientPhone != null)
                                    {
                                        _context.Set <ClientPhone>().Add(clientPhone);
                                    }
                                    await _context.SaveChangesAsync();
                                }
                                else
                                {
                                }
                            }
                        }
                        else
                        {
                        }
                    }
                }
                catch (Exception e)
                {
                }
            }
        }