Esempio n. 1
0
        public virtual async Task <IHttpActionResult> FreshAccessKeys(long id, string userName, string token, int?tenantId = null)
        {
            base.AuthUser();
            using (CurrentUnitOfWork.SetTenantId(tenantId))
            {
                var homeOwer = _homeOwerManager.HomeOwerRepository.FirstOrDefault(h => h.Id == id);
                if (homeOwer == null)
                {
                    throw ErrorCodeTypeUtils.ThrowError(ErrorCodeType.HomeOwerNotExists);
                }
                var homeOwerUser = await _homeOwerUserManager.HomeOwerUserRepository.FirstOrDefaultAsync(hu => hu.HomeOwerId == homeOwer.Id);

                if (homeOwerUser == null)
                {
                    throw ErrorCodeTypeUtils.ThrowError(ErrorCodeType.HomeOwerUserNotExists);
                }
                if (homeOwer.Status == EHomeOwerStatusType.Waiting)
                {
                    throw ErrorCodeTypeUtils.ThrowError(ErrorCodeType.HomeOwerUserIsAuthing);
                }
                //获取业主门禁,判断是否已经添加钥匙,是否已经认证
                foreach (var door in homeOwer.Doors)
                {
                    var key = await _accessKeyManager.AccessKeyRepository.FirstOrDefaultAsync(k => k.Door == door && k.HomeOwer.Id == homeOwer.Id);

                    if (key == null)
                    {
                        key = new AccessKey(CurrentUnitOfWork.GetTenantId(), door, homeOwer, DateTime.Now.AddYears(50), homeOwer.CommunityId);

                        try
                        {
                            await _accessKeyManager.CreateAsync(key);
                        }
                        catch (UserFriendlyException ex)
                        {
                            if (ex.Message == "10")
                            {
                                ErrorCodeTypeUtils.ThrowError(ErrorCodeType.CreatedAccessKeyIsExistsButIsNotAuth);
                            }
                            else if (ex.Message == "11")
                            {
                                ErrorCodeTypeUtils.ThrowError(ErrorCodeType.CreatedAccessKeyIsExists);
                            }
                        }
                        key.GetKey(door.PId, homeOwer.Phone, key.Validity);
                    }
                    else if (!key.IsAuth)
                    {
                        key.GetKey(door.PId, homeOwer.Phone, key.Validity);
                    }
                }

                var list = (from a in _accessKeyManager.AccessKeyRepository.GetAll()
                            join d in _doorManager.DoorRepository.GetAll() on a.Door equals d
                            where a.HomeOwer.Id == homeOwer.Id && d.IsAuth
                            select new { KeyId = a.LockId, KeyValidity = a.Validity, CommunityId = d.DepartId, KeyName = d.Name, KeyType = d.DoorType, IsAuth = a.IsAuth }
                            ).ToList();

                var result = new ArrayList();
                list.ForEach(i =>
                {
                    result.Add(new
                    {
                        i.KeyId,
                        i.KeyValidity,
                        i.CommunityId,
                        i.KeyName,
                        KeyType = EDoorTypeUtils.GetEnum(i.KeyType),
                        i.IsAuth
                    });
                });

                return(Ok(new
                {
                    AppKey = _doorSystemSdk.Params["app_key"],
                    UserId = homeOwer.Phone,
                    AccessKeys = result
                }));
            }
        }