Files
Backend-Api/ServiceHost/Areas/Admin/Pages/Accounts/Account/Index.cshtml.cs
2025-12-15 13:26:09 +03:30

365 lines
9.8 KiB
C#

using _0_Framework.Application;
using _0_Framework.Application.Sms;
using AccountManagement.Application.Contracts.Account;
using AccountManagement.Application.Contracts.ProgramManagerApiResult;
using AccountManagement.Application.Contracts.Role;
using AccountManagement.Domain.AccountAgg;
using AccountManagement.Domain.InternalApiCaller;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
using ServiceHost.Hubs;
using Shared.Contracts.PmUser.Queries;
namespace ServiceHost.Areas.Admin.Pages.Accounts.Account;
[Authorize]
public class IndexModel : PageModel
{
private readonly IAccountApplication _accountApplication;
private readonly IAuthHelper _authHelper;
private readonly IHubContext<SendAccountMessage> _hubContext;
private readonly IRoleApplication _roleApplication;
private readonly ISmsService _smsService;
public List<AccountViewModel> AdminAccounts;
public List<AccountViewModel> ClientAccounts;
public long CurrntAccountId;
public SelectList Roles;
public List<RoleViewModel> Roless;
public AccountSearchModel SearchModel;
public IndexModel(IAccountApplication accountApplication, IRoleApplication roleApplication, IAuthHelper authHelper,
ISmsService smsService, IHubContext<SendAccountMessage> hubContext)
{
_accountApplication = accountApplication;
_roleApplication = roleApplication;
_authHelper = authHelper;
_smsService = smsService;
_hubContext = hubContext;
}
public void OnGet(AccountSearchModel searchModel)
{
Roles = new SelectList(_roleApplication.List(), "Id", "Name");
AdminAccounts = _accountApplication.Search(searchModel).Where(x => x.RoleId != 15).ToList();
ClientAccounts = _accountApplication.Search(searchModel).Where(x => x.RoleId == 15).ToList();
Roless = _roleApplication.List();
CurrntAccountId = _authHelper.CurrentAccountId();
}
public IActionResult OnGetCreate()
{
var pmRolseSelectList = _roleApplication.GetPmRoleList(null).GetAwaiter().GetResult();
var command = new CreateAccount
{
Roles = _roleApplication.List(),
RoleList = pmRolseSelectList,
};
//var key = SecretKeys.ProgramManagerInternalApi;
//var response = InternalApiCaller.GetAsync<RoleResponse>(
// "api/role",
// key,
// new Dictionary<string, object>
// {
// { "RoleName", "" },
// { "GozareshgirRoleId", "" }
// }
//);
//if (response.Success)
//{
// if (response.Result.isSuccess)
// {
// command.RoleList = new SelectList(response.Result.data.role, "id", "roleName");
// }
//}
return Partial("./Create", command);
}
public IActionResult OnPostCreate(CreateAccount command)
{
var result = _accountApplication.Create(command).GetAwaiter().GetResult();
return new JsonResult(result);
}
public IActionResult OnGetCreateRole()
{
var command = new CreateRole();
return Partial("./CreateRole", command);
}
public IActionResult OnPostCreateRole(CreateRole command)
{
var result = _roleApplication.Create(command).GetAwaiter().GetResult();
return new JsonResult(result);
}
public IActionResult OnGetEdit(long id)
{
var account = _accountApplication.GetDetails(id);
//var key = SecretKeys.ProgramManagerInternalApi;
//var apiResult = InternalApiCaller.GetAsync<SingleUserResponseResult>(
// $"api/user/{account.Id}",
// key
//);
var result = _accountApplication.GetPmUserAsync(account.Id).GetAwaiter().GetResult();
// مثل قبل:
if (result.AccountId > 0)
{
account.IsProgramManagerUser = (result.AccountId== account.Id && result.IsActive);
account.UserRoles = result.Roles;
}
else
{
account.IsProgramManagerUser = false;
}
var pmRolseSelectList = _roleApplication.GetPmRoleList(null).GetAwaiter().GetResult();
//سلکت لیست تمام رول های پروگرام منیجر
account.RoleList = pmRolseSelectList;
//var response = InternalApiCaller.GetAsync<RoleResponse>(
// "api/role",
// key,
// new Dictionary<string, object>
// {
// { "RoleName", "" },
// { "GozareshgirRoleId", "" }
// }
//);
//if (response.Success)
//{
// if (response.Result.isSuccess)
// {
// account.RoleList = new SelectList(response.Result.data.role, "id", "roleName");
// }
//}
account.Roles = _roleApplication.List();
return Partial("Edit", account);
}
public JsonResult OnPostEdit(EditAccount command)
{
var result = _accountApplication.Edit(command).GetAwaiter().GetResult();
return new JsonResult(result);
}
public IActionResult OnGetEditRole(long id)
{
var role = _roleApplication.GetDetails(id);
var rol = new List<int>();
foreach (var item in role.MappedPermissions) rol.Add(item.Code);
//var key = SecretKeys.ProgramManagerInternalApi;
//var apiResult = InternalApiCaller.GetAsync<RoleResponse>(
// "api/role",
// key,
// new Dictionary<string, object>
// {
// { "RoleName", "" },
// { "GozareshgirRoleId", role.Id }
// }
//);
var pmRoleResult = _roleApplication.GetPmRoleListToEdit(role.Id).GetAwaiter().GetResult();
role.Permissions = rol;
if (pmRoleResult != null)
{
if (pmRoleResult.Any())
{
var pmPermission = pmRoleResult.FirstOrDefault()!.Permissions;
role.Permissions.AddRange(pmPermission);
}
}
return Partial("EditRole", role);
}
public JsonResult OnPostEditRole(EditRole command)
{
var result = _roleApplication.Edit(command).GetAwaiter().GetResult();
return new JsonResult(result);
}
public IActionResult OnGetChangePassword(long id)
{
var command = new ChangePassword { Id = id };
return Partial("ChangePassword", command);
}
public IActionResult OnGetSendAccountSms()
{
var currntAcc = _authHelper.CurrentAccountId();
var fix = _accountApplication.GetClientsAccount();
var falseIds = new List<long> { 182, 191, 192, 219, 229, 254, 268, 276, 289 };
var accountsList = fix.Where(x => falseIds.Contains(x.Id)).ToList();
double maxNumber = accountsList.Count;
var n = 1;
foreach (var account in accountsList)
{
var sendResult = _smsService.SendAccountsInfo(account.Mobile, account.Fullname, account.Username);
if (sendResult)
Console.WriteLine(sendResult + " - id : " + account.Id + " - " + account.Username);
else
Console.WriteLine(sendResult + " - id : " + account.Id + " - " + account.Username);
var percent = n / maxNumber * 100;
_hubContext.Clients.Group(SendAccountMessage.GetGroupName(currntAcc))
.SendAsync("showStatus", (int)percent);
n += 1;
Thread.Sleep(3000);
}
if (n >= accountsList.Count)
return new JsonResult(new
{
isSuccess = true,
contractCount = n
});
var failds = accountsList.Count - n;
return new JsonResult(new
{
isSuccess = false,
contractCount = failds
});
}
public JsonResult OnPostChangePassword(ChangePassword command)
{
var result = _accountApplication.ChangePassword(command);
return new JsonResult(result);
}
public IActionResult OnGetDeActive(long id)
{
var result = _accountApplication.DeActive(id);
if (result.IsSuccedded)
return new JsonResult(new
{
isSuccedded = true
});
return new JsonResult(new
{
isSuccedded = false
});
}
public IActionResult OnGetActive(long id)
{
var result = _accountApplication.Active(id);
if (result.IsSuccedded)
return new JsonResult(new
{
isSuccedded = true
});
return new JsonResult(new
{
isSuccedded = false
});
}
public IActionResult OnPostLoginToClient(long id)
{
var result = _accountApplication.DirectLogin(id);
if (result.IsSuccedded)
return new JsonResult(new
{
isSuccess = true
});
return new JsonResult(new
{
isSuccess = false
});
}
public IActionResult OnGetAccountWorkshop(long id)
{
var res = _accountApplication.WorkshopList(id);
return Partial("AccountLeftWork", res);
}
public IActionResult OnPostCreateAccountLeftWork(
List<WorkshopAccountlistViewModel> workshopAccountlistViewModel,
string startDate,
string leftDate,
long accountId)
{
if (string.IsNullOrWhiteSpace(startDate))
return (IActionResult)new JsonResult((object)new
{
isSuccess = false,
mess = "تاریخ شروع بکار خالی است"
});
OperationResult operationResult = _accountApplication.SaveWorkshopAccount(workshopAccountlistViewModel, startDate, leftDate, accountId);
return operationResult.IsSuccedded ? (IActionResult)new JsonResult((object)new
{
isSuccess = true,
mess = operationResult.Message
}) : (IActionResult)new JsonResult((object)new
{
isSuccess = false,
mess = operationResult.Message
});
}
public IActionResult OnGetCopyWorkshopToNewAccount(long currentAccountId, long newAccountId)
{
OperationResult newWorkshopAccount = this._accountApplication.CreateNewWorkshopAccount(currentAccountId, newAccountId);
return newWorkshopAccount.IsSuccedded ? (IActionResult)new JsonResult((object)new
{
isSuccess = true
}) : (IActionResult)new JsonResult((object)new
{
isSuccess = false,
message = newWorkshopAccount.Message
});
}
}