Files
Backend-Api/ServiceHost/Areas/Admin/Pages/Index.cshtml.cs
2025-04-20 20:43:45 +03:30

214 lines
8.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using _0_Framework.Application;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using PersianTools.Core;
using System.IO;
using System.Linq;
using backService;
using CompanyManagment.EFCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using CompanyContext = CompanyManagment.EFCore.CompanyContext;
using AccountManagement.Application.Contracts.Account;
using AccountManagement.Application.Contracts.Task;
using AccountManagement.Application.Contracts.Ticket;
using CompanyManagment.App.Contracts.Report;
using CompanyManagment.App.Contracts.Workshop;
using CompanyManagment.App.Contracts.YearlySalary;
using CompanyManagment.Application;
using Microsoft.Extensions.Configuration.UserSecrets;
using _0_Framework.Application.Sms;
namespace ServiceHost.Areas.Admin.Pages
{
[Authorize]
public class IndexModel : PageModel
{
private readonly IReportApplication _reportApplication;
private readonly IYearlySalaryApplication _yearlySalaryApplication;
private readonly IWorkshopApplication _workshopApplication;
private readonly IAccountApplication _accountApplication;
private readonly ITaskApplication _taskApplication;
private readonly ITicketApplication _ticketApplication;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly IConfiguration _configuration;
private readonly IAuthHelper _authHelper;
private readonly ISmsService _smsService;
public string today;
public string month;
public string day;
public string year;
public string filedirectory = string.Empty;
public string FolderPath;
public int TaskCount;
public int TicketCount;
public int SmsRemaining;
public IndexModel(IWebHostEnvironment webHostEnvironment, IConfiguration configuration, IAuthHelper authHelper, IReportApplication reportApplication, IYearlySalaryApplication yearlySalaryApplication, IWorkshopApplication workshopApplication, IAccountApplication accountApplication, ITaskApplication taskApplication, ITicketApplication ticketApplication, ISmsService smsService)
{
_webHostEnvironment = webHostEnvironment;
_configuration = configuration;
_authHelper = authHelper;
_reportApplication = reportApplication;
_yearlySalaryApplication = yearlySalaryApplication;
_workshopApplication = workshopApplication;
_accountApplication = accountApplication;
_taskApplication = taskApplication;
_ticketApplication = ticketApplication;
_smsService = smsService;
}
public async Task OnGet()
{
long userId = _authHelper.CurrentAccountId();
var todayGr = DateTime.Now;
var todayFa = todayGr.ToFarsi();
var syear = Convert.ToInt32(todayFa.Substring(0, 4));
var smonth = Convert.ToInt32(todayFa.Substring(5, 2));
var sday = Convert.ToInt32(todayFa.Substring(8, 2));
var d1 = new PersianDateTime(syear, smonth, sday);
today = d1.DayOfWeek;
month = d1.MonthOfYear;
day = todayFa.Substring(8, 2);
year = todayFa.Substring(0, 4);
TaskCount = await _taskApplication.RequestedAndOverdueTasksCount(userId);
TicketCount = _ticketApplication.GetAdminTicketsCount();
SmsRemaining = (int)await _smsService.GetCreditAmount();
//foreach (string fileEntry in fileEntries)
//{
// Console.WriteLine(Path.GetFileName(fileEntry));
//}
}
public async Task<IActionResult> OnGetReportDataAjax()
{
var allReports = await _reportApplication.GetAllReports(null, null);
var now = new PersianDateTime(Convert.ToInt32(allReports.Year), Convert.ToInt32(allReports.Month), 1);
var before = now.AddMonths(1);
return new JsonResult(new
{
success = true,
AllPercent = allReports.AllPercent,
ContractPercent = allReports.ContractPercent,
ContractSignaturePercent = allReports.ContractSignaturePercent,
CheckoutPercent = allReports.CheckoutPercent,
CheckoutSignaturePercent = allReports.CheckoutSignaturePercent,
ReportYear = allReports.Year,
ReportMonth = allReports.Month,
ReportCurrentMonth = allReports.Month.ToFarsiMonthByNumber(),
ReportNextMonth = before.Month.ToFarsiMonthByIntNumber(),
AllContract = allReports.AllContract,
ContractNotDone = allReports.ContractNotDone,
ContrcatDone = allReports.ContrcatDone,
ContractSignNotDone = allReports.ContractSignNotDone,
ContractSignDone = allReports.ContractSignDone,
AllCheckout = allReports.AllCheckout,
CheckoutDone = allReports.CheckoutDone,
CheckoutNotDone = allReports.CheckoutNotDone,
CheckoutSignNotDone = allReports.CheckoutSignNotDone,
CheckoutSignDone = allReports.CheckoutSignDone,
});
}
//public IActionResult OnGetCreateDatabaseBackup()
//{
// ServerConnection serverConnection = new ServerConnection("46.4.37.226\\MSSQLSERVER2019,51019", "mesbah_db", "fD!4vm64");
// Server server = new Server(serverConnection);
// Database database = server.Databases["mesbah_db"];
// Backup backup = new Backup();
// backup.Action = BackupActionType.Database;
// backup.Database = "mesbah_db";
// BackupDeviceItem deviceItem = new BackupDeviceItem("mesbah_db.bak", DeviceType.File);
// backup.Devices.Add(deviceItem);
// backup.Incremental = false;
// backup.SqlBackup(server);
// return RedirectToPage("./Index");
//}
//public IActionResult OnGetCreateBackup()
//{
// var directoryPath = "C:\\Program Files\\Microsoft SQL Server\\MSSQL15.MSNEW\\MSSQL\\Backup";
// ServerConnection connection = new ServerConnection("46.4.37.226\\MSSQLSERVER2019,51019", "mesbah_db", "fD!4vm64");
// Server server = new Server(connection);
// Backup backup = new Backup();
// backup.Action = BackupActionType.Database;
// backup.Database = "mesbah_db";
// backup.Devices.AddDevice(Path.Combine(directoryPath, "mesbah_db" + "_" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".bak"), DeviceType.File);
// backup.Initialize = true;
// backup.SqlBackup(server);
// return RedirectToPage("./Index");
//}
//public IActionResult OnGetBackup()
//{
// FolderPath = $"{_webHostEnvironment.WebRootPath}";
// SaveBackupFile();
// return Page();
//}
//private void SaveBackupFile()
//{
// var backupPath = "C:\\Dell";
// var databaseName = "Mesbah_db";
// var connectionString =
// "Data Source=DESKTOP-NUE119G\\MSNEW;Initial Catalog=Mesbah_db;Integrated Security=True";
// // Connect to SQL Server database
// SqlConnection connection = new SqlConnection(connectionString);
// // Open database connection
// connection.Open();
// // Create backup command
// string backupQuery = $"BACKUP DATABASE {databaseName} TO DISK='{backupPath}\\Mesbah_db.bak'";
// SqlCommand command = new SqlCommand(backupQuery, connection);
// // Execute backup command
// command.ExecuteNonQuery();
// //// create backup command
// //var backupCommand = $"BACKUP DATABASE {databaseName} TO DISK = '{backupPath}\\Mesbah_db.bak'";
// // // execute backup command
// // _context.Database.ExecuteSqlRaw(backupCommand);
// // // save backup file to disk
// // var fileStream = new FileStream($"{backupPath}\\Mesbah_db.bak", FileMode.Create);
// // using (var binaryWriter = new BinaryWriter(fileStream))
// // {
// // binaryWriter.Write(_context.Database.GetDbConnection().Database);
// // }
//}
}
}