Ejemplo n.º 1
0
        public async Task <(HttpStatusCode statusCode, string ownerId, Guid confirmationToken, string ownerEmailId, string fullName)> RegisterOwner(AddOwnerDto addOwnerDto)
        {
            _logger.LogInformation($"Service-RegisterOwner-Executing RegisterOwner started at {DateTime.UtcNow}");

            addOwnerDto.CheckArgumentIsNull(nameof(addOwnerDto));

            var    statusCode        = HttpStatusCode.Created;
            string ownerId           = string.Empty;
            string emailId           = string.Empty;
            string fullName          = string.Empty;
            Guid   confirmationToken = Guid.Empty;

            var ownerSpec = new OwnerWithAreaSpecification(addOwnerDto.Email);

            var owner = (await _ownerRepository.ListAsync(ownerSpec).ConfigureAwait(false)).FirstOrDefault();

            if (owner != null)
            {
                _logger.LogInformation($"owner with email {addOwnerDto.Email} already exists!!!");
                statusCode = HttpStatusCode.BadRequest;
            }
            else
            {
                Owner ownerEntity = await _ownerRepository.GetMaxOfPrimaryKey();

                string newOwnerDisplayId        = ownerEntity.GetNextPrimaryKey();
                Dictionary <string, Guid> roles = _dictionaryRepository.RolesDictionary;

                Guid roleId = roles.Where(s => s.Key == addOwnerDto.RoleName).First().Value;

                var ownerDto = new SaveOwnerDto(roleId, newOwnerDisplayId)
                {
                    FirstName = addOwnerDto.FirstName,
                    LastName  = addOwnerDto.LastName,
                    Email     = addOwnerDto.Email
                };

                var ownerToAddToDb = ownerDto.ToEntity(true);
                ownerToAddToDb.Password = _securityService.GetSha256Hash(addOwnerDto.Password);
                await _ownerRepository.AddAsync(ownerToAddToDb).ConfigureAwait(false);

                //Add entry in ownerconfirmation table
                var ownerConfirmation = new OwnerConfirmation();
                ownerConfirmation.GenerateLinkAndToken(ownerToAddToDb.OwnerId);
                await _ownerConfirmationRepository.AddAsync(ownerConfirmation);

                await _ownerRepository.SaveAllwithAudit();

                ownerId           = newOwnerDisplayId;
                confirmationToken = ownerConfirmation.confirmationToken;
                emailId           = ownerToAddToDb.EmailId;
                fullName          = ownerToAddToDb.FullName;
            }

            _logger.LogInformation($"Service-RegisterOwner-Executing RegisterOwner completed at {DateTime.UtcNow}");

            return(statusCode, ownerId, confirmationToken, emailId, fullName);
        }
        public async Task <OwnerResponse> SaveAsync(Owner owner)
        {
            try
            {
                await _ownerRepository.AddAsync(owner);

                await _unitOfWork.CompleteAsync();

                return(new OwnerResponse(owner));
            }
            catch (Exception ex)
            {
                return(new OwnerResponse($"An error ocurred while saving the Owner: {ex.Message}"));
            }
        }
Ejemplo n.º 3
0
        public async Task RegisterAsync(OwnerOnCreateDto ownerDto)
        {
            var owner = await _ownerRepository.GetAsync(ownerDto.Login);

            if (owner != null)
            {
                throw new ServiceException(ErrorCodes.OwnerAlreadyExists, "Login is already in use.");
            }

            var salt = _encrypter.GetSalt(ownerDto.Password);
            var hash = _encrypter.GetHash(ownerDto.Password, salt);

            var ownersRestaurant = await _restaurantRepository.GetAsync(ownerDto.Restaurant.Id);

            var ownerToRegister = new Owner(ownerDto.Login, ownerDto.FirstName, ownerDto.LastName, ownerDto.Email,
                                            ownerDto.Phone, hash, salt, ownersRestaurant);

            await _ownerRepository.AddAsync(ownerToRegister);
        }
Ejemplo n.º 4
0
        public async Task <ApiResult <AddOwnerOutput> > Add([FromBody] AddOwnerInput input, CancellationToken cancelToken)
        {
            if (Authorization == null)
            {
                return(new ApiResult <AddOwnerOutput>(APIResultCode.Unknown, new AddOwnerOutput {
                }, APIResultMessage.TokenNull));
            }
            if (string.IsNullOrWhiteSpace(input.Name))
            {
                throw new NotImplementedException("业主姓名信息为空!");
            }

            if (string.IsNullOrWhiteSpace(input.Birthday))
            {
                throw new NotImplementedException("业主生日信息为空!");
            }

            if (string.IsNullOrWhiteSpace(input.Gender))
            {
                throw new NotImplementedException("业主性别信息为空!");
            }

            if (string.IsNullOrWhiteSpace(input.PhoneNumber))
            {
                throw new NotImplementedException("业主手机号信息为空!");
            }

            if (string.IsNullOrWhiteSpace(input.IDCard))
            {
                throw new NotImplementedException("业主身份证信息为空!");
            }
            if (string.IsNullOrWhiteSpace(input.IndustryId))
            {
                throw new NotImplementedException("业主业户Id信息为空!");
            }
            if ((await _ownerRepository.GetAllAsync(new OwnerDto
            {
                IndustryId = input.IndustryId
            })).Any())
            {
                throw new NotImplementedException("该业户下存在业主!暂不支持多业主,不能添加");
            }
            await _ownerRepository.GetAllAsync(new OwnerDto
            {
                IndustryId = input.IndustryId
            });

            var user = _tokenRepository.GetUser(Authorization);

            if (user == null)
            {
                return(new ApiResult <AddOwnerOutput>(APIResultCode.Unknown, new AddOwnerOutput {
                }, APIResultMessage.TokenError));
            }

            var entity = await _ownerRepository.AddAsync(new OwnerDto
            {
                Name            = input.Name,
                PhoneNumber     = input.PhoneNumber,
                IndustryId      = input.IndustryId,
                IDCard          = input.IDCard,
                Gender          = input.Gender,
                Birthday        = input.Birthday,
                OperationTime   = DateTimeOffset.Now,
                OperationUserId = user.Id.ToString()
            }, cancelToken);

            return(new ApiResult <AddOwnerOutput>(APIResultCode.Success, new AddOwnerOutput {
                Id = entity.Id.ToString()
            }));
        }
Ejemplo n.º 5
0
        public async Task <ApiResult <dynamic> > UploadOwnerInfoFile(CancellationToken cancelToken)
        {
            try
            {
                #region Token

                if (Authorization == null)
                {
                    return(new ApiResult <dynamic>(APIResultCode.Unknown, new { }, APIResultMessage.TokenNull));
                }
                var user = _tokenRepository.GetUser(Authorization);
                if (user == null)
                {
                    return(new ApiResult <dynamic>(APIResultCode.Unknown, new { }, APIResultMessage.TokenError));
                }
                if (user.DepartmentValue != Department.WuYe.Value)
                {
                    return(new ApiResult <dynamic>(APIResultCode.Success_NoB, new { }, "只能通过物业导入业主信息!"));
                }

                #endregion

                string typeName = "OwnerInfoFile";
                if (!Request.Content.IsMimeMultipartContent())
                {
                    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                }

                string fileSaveLocation = HttpContext.Current.Server.MapPath("~/Upload/" + typeName);

                CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(fileSaveLocation);
                List <UploadOutput> files = new List <UploadOutput>();

                await Request.Content.ReadAsMultipartAsync(provider, cancelToken);

                foreach (MultipartFileData file in provider.FileData)
                {
                    string   orfilename = file.Headers.ContentDisposition.FileName.TrimStart('"').TrimEnd('"');
                    string   fileExt    = orfilename.Substring(orfilename.LastIndexOf('.'));
                    FileInfo fileinfo   = new FileInfo(file.LocalFileName);
                    var      newPath    = DateTime.Now.ToString("yyyyMMddhhmmssffffff") + fileExt;
                    fileinfo.CopyTo(Path.Combine(fileSaveLocation, newPath), true);
                    fileinfo.Delete();
                    files.Add(await NewAddUpload(typeName, newPath, user.Id.ToString(), cancelToken));
                }

                var data = AsposeCellsUtility.ImportExcel(HttpContext.Current.Server.MapPath("~/Upload/") + files[0].Url);

                //不是本小区数据跳过数
                var notSmallDistrictCount = 0;
                //业户下包含业主信息跳过数
                var containOwnerCount = 0;
                //身份证号为空跳过数
                var iDCardCount = 0;
                //成功添加数
                var successCount = 0;

                if (data.Rows.Count > 0)
                {
                    foreach (DataRow item in data.Rows)
                    {
                        if (string.IsNullOrEmpty(item["省"].ToString()))
                        {
                            continue;
                        }
                        var state             = item["省"].ToString();
                        var city              = item["市"].ToString();
                        var region            = item["区"].ToString();
                        var streetOfficeName  = item["街道办"].ToString();
                        var communityName     = item["社区"].ToString();
                        var smallDistrictName = item["小区"].ToString();
                        var NumberOfLayers    = Convert.ToInt32(item["层"].ToString());

                        if (user.SmallDistrictName != smallDistrictName || user.CommunityName != communityName || user.StreetOfficeName != streetOfficeName || user.Region != region || user.City != city || user.State != state)
                        {
                            notSmallDistrictCount += 1;
                            continue;
                        }

                        //根据当前的小区id查询 小区下是否存在 楼宇信息 不存在添加
                        var build = (await _buildingRepository.GetListAsync(new BuildingDto {
                            SmallDistrictId = user.SmallDistrictId
                        })).Where(x => x.Name == item["楼宇"].ToString()).FirstOrDefault();
                        if (build == null)
                        {
                            build = await _buildingRepository.AddAsync(
                                new BuildingDto
                            {
                                SmallDistrictId   = user.SmallDistrictId,
                                SmallDistrictName = user.SmallDistrictName,
                                OperationTime     = DateTime.Now,
                                OperationUserId   = user.Id.ToString(),
                                Name = item["楼宇"].ToString()
                            });
                        }

                        //根据楼宇信息查询单元信息
                        var buildingUnit = (await _buildingUnitRepository.GetListAsync(new BuildingUnitDto {
                            BuildingId = build.Id.ToString()
                        })).Where(x => x.UnitName == item["单元"].ToString()).FirstOrDefault();
                        if (buildingUnit == null)
                        {
                            buildingUnit = await _buildingUnitRepository.AddAsync(
                                new BuildingUnitDto
                            {
                                UnitName        = item["单元"].ToString(),
                                BuildingId      = build.Id.ToString(),
                                NumberOfLayers  = NumberOfLayers,
                                OperationTime   = DateTime.Now,
                                OperationUserId = user.Id.ToString()
                            });
                        }
                        else if (buildingUnit.NumberOfLayers < NumberOfLayers)
                        {
                            buildingUnit = await _buildingUnitRepository.UpdateNumberOfLayersAsync(
                                new BuildingUnitDto
                            {
                                Id              = buildingUnit.Id.ToString(),
                                NumberOfLayers  = NumberOfLayers,
                                OperationTime   = DateTime.Now,
                                OperationUserId = user.Id.ToString()
                            });
                        }

                        //查找业户 业户不存在添加业户
                        var industry = (await _industryRepository.GetListAsync(
                                            new IndustryDto
                        {
                            NumberOfLayers = NumberOfLayers,
                            BuildingUnitId = buildingUnit.Id.ToString()
                        })).Where(x => x.Name == item["门牌号"].ToString()).FirstOrDefault();
                        if (industry == null)
                        {
                            industry = await _industryRepository.AddAsync(
                                new IndustryDto
                            {
                                Name                         = item["门牌号"].ToString(),
                                Acreage                      = item["面积"].ToString(),
                                BuildingId                   = build.Id.ToString(),
                                BuildingName                 = build.Name,
                                BuildingUnitId               = buildingUnit.Id.ToString(),
                                BuildingUnitName             = buildingUnit.UnitName,
                                NumberOfLayers               = NumberOfLayers,
                                Oriented                     = item["朝向"].ToString(),
                                OperationTime                = DateTime.Now,
                                OperationUserId              = user.Id.ToString(),
                                OperationUserSmallDistrictId = user.SmallDistrictId
                            });
                        }

                        //查询当前业户下业主
                        var owner = (await _ownerRepository.GetListAsync(
                                         new OwnerDto
                        {
                            IndustryId = industry.Id.ToString()
                        })).FirstOrDefault();
                        if (owner != null)
                        {
                            containOwnerCount += 1;
                            continue;
                        }
                        if (string.IsNullOrEmpty(item["身份证号"].ToString()))
                        {
                            iDCardCount += 1;
                            continue;
                        }
                        var birthday = "";
                        if (DateTime.TryParse(item["生日"].ToString(), out var format))
                        {
                            birthday = format.ToString("yyyyMMdd");
                        }
                        owner = await _ownerRepository.AddAsync(
                            new OwnerDto
                        {
                            IDCard     = item["身份证号"].ToString(),
                            Birthday   = birthday,
                            Gender     = item["性别"].ToString(),
                            IndustryId = industry.Id.ToString(),
                            //IndustryName = industry.Name,
                            Name            = item["姓名"].ToString(),
                            PhoneNumber     = item["手机号"].ToString(),
                            OperationUserId = user.Id.ToString(),
                            OperationTime   = DateTime.Now
                        });

                        successCount += 1;
                    }
                }
                var mgs = "成功导入:" + successCount + ",身份证号为空跳过数:" + iDCardCount + ",业户下包含业主信息跳过数:" + containOwnerCount + ",不是本小区数据跳过数:" + notSmallDistrictCount;
                return(new ApiResult <dynamic>(APIResultCode.Success, new { mgs }, APIResultMessage.Success));
            }
            catch (Exception e)
            {
                return(new ApiResult <dynamic>(APIResultCode.Error, new { }, "导入失败:" + e.Message));
            }
        }