示例#1
0
        public async Task <ActionResultResponse <string> > Insert(string eventId, string creatorId, string creatorFullName, string creatorAvatar,
                                                                  RegisterMeta registerMeta)
        {
            var infoEvent = await _eventRepository.GetInfo(eventId);

            if (infoEvent == null)
            {
                return(new ActionResultResponse <string>(-1, _websiteEventResourceService.GetString("Event does not exists. Please try again.")));
            }

            if (infoEvent.Status != ApproverStatus.Approved)
            {
                return(new ActionResultResponse <string>(-2, _websiteEventResourceService.GetString("Event not approved. Please try again.")));
            }

            var apiUrls = _configuration.GetApiUrl();

            if (apiUrls == null)
            {
                return(new ActionResultResponse <string>(-3,
                                                         _sharedResourceService.GetString(
                                                             "Missing some configuration. Please contact with administrator.")));
            }

            var registerId = Guid.NewGuid().ToString();
            var register   = new Register
            {
                Id = registerId,
                ConcurrencyStamp = registerId,
                EventId          = eventId,
                Note             = registerMeta.Note,
                RegisterTime     = DateTime.Now,
                IsStaff          = registerMeta.IsStaff,
                CreatorId        = creatorId,
                CreatorFullName  = creatorFullName,
                CreatorAvatar    = creatorAvatar,
                UserId           = null,
                Role             = registerMeta.Role,
                Avatar           = registerMeta.Avatar,
            };

            switch (registerMeta.Role)
            {
            case RegisterRole.Employee:
                if (!string.IsNullOrEmpty(registerMeta.UserId))
                {
                    register.FullName    = registerMeta.FullName;
                    register.PhoneNumber = registerMeta.PhoneNumber;
                    register.Email       = registerMeta.Email;
                    register.Address     = registerMeta.Address;
                    register.UserId      = registerMeta.UserId;
                }
                break;

            case RegisterRole.Customer:
                if (!string.IsNullOrEmpty(registerMeta.UserId))
                {
                    var resultCustomer = await new HttpClientService()
                                         .GetAsync <CustomerViewModel>($"{apiUrls.CustomerApiUrl}/Customers/{infoEvent.TenantId}/{registerMeta.UserId}");

                    register.FullName    = resultCustomer.FullName;
                    register.PhoneNumber = resultCustomer.PhoneNumber;
                    register.Email       = resultCustomer.Email;
                    register.Address     = resultCustomer.Address;
                    register.UserId      = registerMeta.UserId;
                }
                else
                {
                    var resultCustomer = await new HttpClientService()
                                         .GetAsync <string>($"{apiUrls.CustomerApiUrl}/Customers/GetCustomerId/{infoEvent.TenantId}/{registerMeta.FullName}/{registerMeta.PhoneNumber}/{registerMeta.Email}/{registerMeta.Address}");

                    register.FullName    = registerMeta.FullName;
                    register.PhoneNumber = registerMeta.PhoneNumber;
                    register.Email       = registerMeta.Email;
                    register.Address     = registerMeta.Address;
                    register.UserId      = resultCustomer;
                }
                break;

            case RegisterRole.WalkInGuest:
            case RegisterRole.Invitation:
            case RegisterRole.Professional:
                register.FullName    = registerMeta.FullName;
                register.PhoneNumber = registerMeta.PhoneNumber;
                register.Email       = registerMeta.Email;
                register.Address     = registerMeta.Address;
                break;
            }

            var isExists = await _registerRepository.CheckExists(registerId, eventId, register.FullName, register.PhoneNumber);

            if (isExists)
            {
                return(new ActionResultResponse <string>(-4,
                                                         _websiteEventResourceService.GetString("Register name: \"{0}\" already exists.",
                                                                                                register.FullName)));
            }

            var result = await _registerRepository.Insert(register);

            if (result <= 0)
            {
                return(new ActionResultResponse <string>(result,
                                                         _sharedResourceService.GetString(ErrorMessage.SomethingWentWrong)));
            }

            #region insertEventDayRegister.
            if (registerMeta.EventDayRegisters != null && registerMeta.EventDayRegisters.Any())
            {
                var resultInsertEventDayRegister = await InsertEventDayRegister();

                if (resultInsertEventDayRegister.Code <= 0)
                {
                    return(resultInsertEventDayRegister);
                }
            }
            #endregion

            var listEventDay = await _eventDayRepository.GetListEvenDayByEventId(eventId);

            foreach (var eventDay in listEventDay)
            {
                await UpdateTotal(eventDay.Id);
            }

            return(new ActionResultResponse <string>(1, _websiteEventResourceService.GetString("Add new register successful."),
                                                     string.Empty, eventId));

            async Task <ActionResultResponse <string> > InsertEventDayRegister()
            {
                var eventDayRegisters = new List <EventDayRegister>();

                foreach (var eventDayRegister in registerMeta.EventDayRegisters)
                {
                    var infoEventDay = await _eventDayRepository.GetInfo(eventDayRegister.EventDayId);

                    var isAllowAccompanyPerson = infoEventDay.IsAllowAccompanyPerson ?? infoEvent.IsAllowAccompanyPerson;
                    var limitedUsers           = infoEventDay.LimitedUsers != null
                        ? infoEventDay.LimitedUsers
                        : infoEvent.LimitedUsers;
                    var limitedAccompanyUsers       = infoEventDay.LimitedAccompanyPersons ?? infoEvent.LimitedAccompanyPersons;
                    var isAllowRegisterWhenOverload = infoEvent.IsAllowRegisterWhenOverload;

                    // neu ko bat cho dang ky vuot qua so nguoi
                    if (!isAllowRegisterWhenOverload)
                    {
                        if (limitedUsers > 0)
                        {
                            var userCount = infoEventDay.TotalRegisters + registerMeta.EventDayRegisters.Count;
                            if (userCount > limitedUsers)
                            {
                                return(new ActionResultResponse <string>(-5,
                                                                         _websiteEventResourceService.GetString(
                                                                             "Limited users when overload. Please contact with administrator.")));
                            }
                        }
                    }

                    var isEventDayRegisterExists =
                        await _eventDayRegisterRepository.CheckExists(eventDayRegister.EventDayId,
                                                                      registerId);

                    if (isEventDayRegisterExists)
                    {
                        continue;
                    }
                    var eventDayRegisterInsert = new EventDayRegister
                    {
                        RegisterId = registerId,
                        EventDayId = eventDayRegister.EventDayId,
                        Status     = EventDayStatus.NotYet
                    };
                    eventDayRegisters.Add(eventDayRegisterInsert);

                    if (isAllowAccompanyPerson && eventDayRegister.AccompanyPersons != null && eventDayRegister.AccompanyPersons.Any())
                    {
                        var accompanyPersons = new List <AccompanyPerson>();
                        foreach (var accompanyPerson in eventDayRegister.AccompanyPersons)
                        {
                            if (limitedAccompanyUsers > 0)
                            {
                                var accompanyUserCount = eventDayRegister.AccompanyPersons.Count;
                                if (accompanyUserCount > limitedAccompanyUsers)
                                {
                                    return(new ActionResultResponse <string>(-6,
                                                                             _websiteEventResourceService.GetString(
                                                                                 "Accompany persons when overload. Please contact with administrator.")));
                                }
                            }

                            var accompanyPersonInsert = new AccompanyPerson
                            {
                                Id          = Guid.NewGuid().ToString(),
                                RegisterId  = registerId,
                                FullName    = accompanyPerson.FullName,
                                PhoneNumber = accompanyPerson.PhoneNumber,
                                EventDayId  = eventDayRegister.EventDayId,
                                Status      = EventDayStatus.NotYet
                            };
                            accompanyPersons.Add(accompanyPersonInsert);
                        }
                        await _accompanyPersonRepository.Inserts(accompanyPersons);
                    }
                }
                var resulteventDayRegister = await _eventDayRegisterRepository.Inserts(eventDayRegisters);

                if (resulteventDayRegister > 0)
                {
                    return(new ActionResultResponse <string>(resulteventDayRegister,
                                                             _websiteEventResourceService.GetString("Add new event day register successful.")));
                }

                await RollbackInsertEventDayRegister();
                await RollbackInsertRegister();

                return(new ActionResultResponse <string>(-7,
                                                         _websiteEventResourceService.GetString(
                                                             "Can not insert event day register. Please contact with administrator.")));
            }

            async Task RollbackInsertRegister()
            {
                await _registerRepository.Delete(registerId);
            }

            async Task RollbackInsertEventDayRegister()
            {
                await _eventDayRegisterRepository.DeleteByRegisterId(registerId);
            }
        }