Ejemplo n.º 1
0
        public async Task <IActionResult> UpdateUserApplyPeriodAsync(string id, int period, bool?status, bool sendNotification = false)
        {
            try
            {
                var user = await _userManager.FindByIdAsync(id);

                // 确保用户存在有效申请
                if (user.Application == null)
                {
                    user.Application = new Application
                    {
                        User         = user,
                        PeriodId     = 1,
                        IsSuccessful = null
                    };
                }
                // 处理阶段更新
                var prevPeriod = user.Application.Period?.Title ?? "申请";
                user.Application.Period = await _dbContext.ApplicationPeriod.FindAsync(period);

                user.Application.IsSuccessful = status;
                // 发送通知
                if (sendNotification == true)
                {
                    // 发送邮件通知
                    if (user.EmailConfirmed)
                    {
                        if (status != null)
                        {
                            if (status == false)
                            {
                                await _emailSender.SendStatusChangeAsync(user.PhoneNumber, false, user.Application.Period?.Title ?? "申请");
                            }
                            else if (status == true)
                            {
                                await _emailSender.SendStatusChangeAsync(user.PhoneNumber, "全部面试通过");
                            }
                        }
                        else
                        {
                            await _emailSender.SendStatusChangeAsync(user.PhoneNumber, prevPeriod, user.Application.Period?.Title ?? "申请");
                        }
                    }

                    // 发送短信通知
                    if (user.PhoneNumberConfirmed)
                    {
                        if (status != null)
                        {
                            if (status == false)
                            {
                                await _smsSender.SendStatusChangeAsync(user.PhoneNumber, false, user.Application.Period?.Title ?? "申请");
                            }
                            else if (status == true)
                            {
                                await _smsSender.SendStatusChangeAsync(user.PhoneNumber, "全部面试通过");
                            }
                        }
                        else
                        {
                            await _smsSender.SendStatusChangeAsync(user.PhoneNumber, prevPeriod, user.Application.Period?.Title ?? "申请");
                        }
                    }
                }
                await _dbContext.SaveChangesAsync();

                return(Json(new { succeeded = true }));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "UpdateUserApplyPeriodAsync");
                return(Json(new { succeeded = false, message = ex.Message }));
            }
        }