Пример #1
0
        public async Task <ActionResultResponse> Update(string eventId, string lastUpdateUserId, string lastUpdateFullName, string lastUpdateAvatar,
                                                        string registerId, RegisterMeta registerMeta)
        {
            var infoEvent = await _eventRepository.GetInfo(eventId);

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

            var info = await _registerRepository.GetInfo(registerId);

            if (info == null)
            {
                return(new ActionResultResponse(-1, _websiteEventResourceService.GetString("Register does not exists.")));
            }

            if (info.EventId != eventId)
            {
                return(new ActionResultResponse(-2,
                                                _websiteEventResourceService.GetString(ErrorMessage.NotHavePermission)));
            }

            if (info.ConcurrencyStamp != registerMeta.ConcurrencyStamp)
            {
                return(new ActionResultResponse(-3,
                                                _websiteEventResourceService.GetString(
                                                    "The register already updated by other people. you are not allowed to edit the register information.")));
            }

            var apiUrls = _configuration.GetApiUrl();

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

            switch (registerMeta.Role)
            {
            case RegisterRole.Employee:
                if (!string.IsNullOrEmpty(registerMeta.UserId))
                {
                    info.FullName    = registerMeta.FullName;
                    info.PhoneNumber = registerMeta.PhoneNumber;
                    info.Email       = registerMeta.Email;
                    info.Address     = registerMeta.Address;
                    info.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}");

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

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

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

            info.Avatar             = registerMeta.Avatar;
            info.Role               = registerMeta.Role;
            info.Note               = registerMeta.Note;
            info.IsStaff            = registerMeta.IsStaff;
            info.ConcurrencyStamp   = Guid.NewGuid().ToString();
            info.LastUpdate         = DateTime.Now;
            info.LastUpdateUserId   = lastUpdateUserId;
            info.LastUpdateFullName = lastUpdateFullName;
            info.LastUpdateAvatar   = lastUpdateAvatar;

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

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

            await _registerRepository.Update(info);

            #region UpdateEventDayRegister.
            if (registerMeta.EventDayRegisters.Count > 0)
            {
                var resultUpdateEventDayRegister = await UpdateEventDayRegister();

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

            var listEventDay = await _eventDayRepository.GetListEvenDayByEventId(eventId);

            foreach (var eventDay in listEventDay)
            {
                await UpdateTotal(eventDay.Id);
            }
            return(new ActionResultResponse(1, _websiteEventResourceService.GetString("Update register successful.")));

            async Task <ActionResultResponse <string> > UpdateEventDayRegister()
            {
                await _eventDayRegisterRepository.DeleteByRegisterId(registerId);

                await _accompanyPersonRepository.DeleteByRegisterId(registerId);

                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 ?? 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>(-6,
                                                                         _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)
                    {
                        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>(-7,
                                                                             _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.")));
                }

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