/// <summary>
        /// 編輯成員選課資訊
        /// </summary>
        /// <param name="account"></param>
        /// <param name="circleKey"></param>
        /// <param name="token"></param>
        /// <param name="enable"></param>
        /// <returns></returns>
        public bool UpdateCircleMemberRole(CircleMemberRoleRequest data)
        {
            var db      = _uow.DbContext;
            var updater = (from m in db.Members
                           join ut in db.UserToken on m.Id equals ut.MemberId
                           where ut.Token == data.Token
                           select m).FirstOrDefault();

            var learningCircleInfo = db.LearningCircle.FirstOrDefault(t => t.LearningOuterKey == data.CircleKey);

            if (learningCircleInfo == null)
            {
                return(false);
            }
            foreach (var account in data.Accounts)
            {
                var memberInfo = db.Members.FirstOrDefault(t => t.Account.ToLower() == account.ToLower());
                if (memberInfo == null)
                {
                    return(false);
                }

                //判斷是否已存在關聯
                var checkData = db.CircleMemberRoleplay.FirstOrDefault(t => t.MemberId == memberInfo.Id && t.CircleId == learningCircleInfo.Id);
                if (checkData != null)
                {
                    checkData.RoleId = data.RoleId.Value;
                    db.SaveChanges();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
        /// <summary>
        /// 根據InviteCode 建立成員角色資訊 - 邀請碼加入
        /// </summary>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public InviteResponseData InsertCircleMemberRoleByInvite(CircleMemberRoleRequest requestData)
        {
            var db = _uow.DbContext;

            var inviteData   = db.MemberInvite.FirstOrDefault(t => t.Code.ToLower() == requestData.InviteCode.ToLower());
            var responseData = new InviteResponseData()
            {
                InviteStatus = InviteStatusEnum.inviteError
            };

            if (inviteData == null)
            {
                return(responseData);
            }

            var learningCircleInfo = db.LearningCircle.FirstOrDefault(t => t.LearningOuterKey == inviteData.CircleKey.ToLower());

            if (learningCircleInfo == null || learningCircleInfo.InviteEnable == false || learningCircleInfo.Enable == false)
            {
                responseData.InviteStatus = InviteStatusEnum.EndInvite;
                return(responseData);
            }
            responseData.CircleName = learningCircleInfo.Name;

            if (inviteData.Enable == false)
            {
                responseData.InviteStatus = InviteStatusEnum.inviteError;
                return(responseData);
            }

            var memberService = new MemberService();
            var memberInfo    = memberService.TokenToMember(requestData.Token).Result;

            if (memberInfo == null || memberInfo.OrgId != learningCircleInfo.OrgId.Value)
            {
                responseData.InviteStatus = InviteStatusEnum.AccountNotAllow;
                return(responseData);
            }
            var organizationInfo = _uow.DbContext.Organizations.FirstOrDefault(t => t.Id == memberInfo.OrgId);

            if (organizationInfo == null)
            {
                responseData.InviteStatus = InviteStatusEnum.AccountNotAllow;
                return(responseData);
            }
            responseData.OrgName = organizationInfo.Name;

            //查出學生角色資訊
            var learningRoleInfo = db.LearningRole.FirstOrDefault(t => t.Level == 3 && t.LearningId == learningCircleInfo.Id && t.IsFixed == false);

            if (learningRoleInfo == null)
            {
                responseData.InviteStatus = InviteStatusEnum.EndInvite;
                return(responseData);
            }

            //判斷要加的管理者是否跟Token帳號一致
            var checkAddAccountIsSuccess = requestData.Accounts.FirstOrDefault(t => t.ToString().ToLower() == memberInfo.Account.ToLower());

            if (checkAddAccountIsSuccess == null)
            {
                responseData.InviteStatus = InviteStatusEnum.inviteError;
                return(responseData);
            }
            //將驗證碼失效
            var getOldInviteData = _uow.DbContext.MemberInvite.FirstOrDefault
                                       (t => t.CircleKey.ToLower() == inviteData.CircleKey.ToLower() &&
                                       t.Enable == true &&
                                       t.Type == 0 &&
                                       t.Code.ToLower() == requestData.InviteCode.ToLower());

            if (getOldInviteData != null && getOldInviteData.IsCourseCode == false)
            {
                getOldInviteData.Enable = false;
            }
            else if (getOldInviteData.IsCourseCode == false)
            {
                responseData.InviteStatus = InviteStatusEnum.inviteError;
                return(responseData);
            }
            var checkJoined = db.CircleMemberRoleplay.FirstOrDefault(t => t.MemberId == memberInfo.Id && t.RoleId == learningRoleInfo.Id && t.CircleId == learningCircleInfo.Id);

            if (checkJoined == null)
            {
                var entity = new CircleMemberRoleplay()
                {
                    CircleId = learningCircleInfo.Id,
                    Enable   = true,
                    MemberId = memberInfo.Id,
                    ResType  = requestData.ResType,
                    RoleId   = learningRoleInfo.Id
                };
                db.CircleMemberRoleplay.Add(entity);
                var circleMemberEntity = new CircleMember()
                {
                    CircleId = learningCircleInfo.Id,
                    Enabled  = true,
                    MemberId = memberInfo.Id,
                    Created  = TimeData.Create(DateTime.UtcNow),
                    Deleted  = TimeData.Create(null),
                    Updated  = TimeData.Create(null)
                };
                db.CircleMember.Add(circleMemberEntity);
                db.CircleMemberRoleplay.Add(entity);
            }
            else
            {
                checkJoined.Enable = true;
            }
            db.SaveChanges();
            responseData.InviteStatus = InviteStatusEnum.success;
            return(responseData);
        }
        /// <summary>
        /// 建立成員角色資訊
        /// </summary>
        /// <param name="account"></param>
        /// <param name="circleKey"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public bool InsertMutipleCircleMemberRole(CircleMemberRoleRequest data)
        {
            var db           = _uow.DbContext;
            var tokenService = new TokenService();
            var tokenInfo    = tokenService.GetTokenInfo(data.Token).Result;

            if (tokenInfo == null)
            {
                return(false);
            }

            var learningCircleInfo = db.LearningCircle.FirstOrDefault(t => t.LearningOuterKey == data.CircleKey);

            if (learningCircleInfo == null)
            {
                return(false);
            }

            foreach (var account in data.Accounts)
            {
                var memberService = new MemberService();
                var memberInfo    = memberService.AccountToMember(account.ToLower(), learningCircleInfo.OrgId.Value);

                if (memberInfo == null)
                {
                    return(false);
                }

                //判斷是否已存在關聯
                var checkData = GetCircleMemberRoleInfo(memberInfo.Id, learningCircleInfo.Id, data.RoleId.Value);

                //已存在就不新增
                if (checkData != null)
                {
                    continue;
                }

                var entity = new Infrastructure.Entity.CircleMemberRoleplay()
                {
                    Enable   = true,
                    CircleId = learningCircleInfo.Id,
                    MemberId = memberInfo.Id,
                    RoleId   = data.RoleId.Value
                };

                var circlememberEntity = new CircleMember()
                {
                    MemberId   = memberInfo.Id,
                    CircleId   = learningCircleInfo.Id,
                    Enabled    = true,
                    Created    = TimeData.Create(DateTime.UtcNow),
                    Deleted    = TimeData.Create(null),
                    Updated    = TimeData.Create(null),
                    CreateUser = tokenInfo.MemberId
                };
                db.CircleMemberRoleplay.Add(entity);
                db.CircleMember.Add(circlememberEntity);
            }
            db.SaveChanges();
            return(true);
        }
        public IHttpActionResult Post(CircleMemberRoleRequest requestData)
        {
            var response = new Infrastructure.ViewModel.Base.BaseResponse <string>();

            var requestService = new Service.Utility.RequestDataHelper <CircleMemberRoleRequest>();
            var checkColumnKey = new string[1] {
                "token"
            };
            var checkEmpty = requestService.CheckColumnEmpty(requestData, checkColumnKey);

            response.Success = false;
            if (checkEmpty == false)
            {
                response.Message = "遺漏參數";
                response.State   = Infrastructure.ViewModel.Base.LogState.RequestDataError;
                return(Content(HttpStatusCode.BadRequest, response));
            }

            //後臺新增
            if (requestData.InviteCode == null && requestData.RoleId.HasValue)
            {
                var authService     = new AuthService();
                var checkManageAuth = authService.CheckCourseManageAuth(requestData.Token, requestData.CircleKey.ToLower());
                //除了課程管理者可以修改,其他角色都不得修改
                if (checkManageAuth == null || checkManageAuth.CircleMemberSetting.AddCircleMember == false)
                {
                    response.Message = "無法加入";
                    response.State   = Infrastructure.ViewModel.Base.LogState.Error;
                    return(Ok(response));
                }

                var proxyReponse = circleMemberService.InsertMutipleCircleMemberRole(requestData);
                response.Success = proxyReponse;
                if (proxyReponse)
                {
                    response.Message = "新增成功";
                    return(Ok(response));
                }
                else
                {
                    response.State   = Infrastructure.ViewModel.Base.LogState.Error;
                    response.Message = "新增失敗";
                    return(Ok(response));
                }
            }
            else
            { //邀請碼新增
                var inviteService = new MemberInviteService();
                var inviteInfo    = inviteService.GetDetail(requestData.InviteCode.ToLower());

                if (inviteInfo == null)
                {
                    response.Message = "無法加入";
                    response.State   = Infrastructure.ViewModel.Base.LogState.Error;
                    return(Ok(response));
                }
                var inviteResponse      = new Infrastructure.ViewModel.Base.BaseResponse <InviteResponseData>();
                var organizationService = new OrganizationService();
                var checkCanRegister    = organizationService.CheckCanRegister(requestData.OrgCode);
                if (checkCanRegister == false)
                {
                    inviteResponse.Message = "不允許加入課程";
                    inviteResponse.State   = Infrastructure.ViewModel.Base.LogState.Error;
                    inviteResponse.Data    = new InviteResponseData()
                    {
                        InviteStatus = InviteStatusEnum.EndInvite, IsOrgRegister = false, CircleKey = requestData.CircleKey
                    };
                    return(Ok(inviteResponse));
                }

                var responseData = circleMemberService.InsertCircleMemberRoleByInvite(requestData);

                inviteResponse.Success = true;
                inviteResponse.Data    = responseData;
                inviteResponse.Message = responseData.InviteStatus.ToString();
                return(Ok(inviteResponse));
            }
        }