Files
Backend-Api/ServiceHost/Areas/AdminNew/Pages/Company/Task/Create.cshtml.cs
2025-04-07 19:44:22 +03:30

359 lines
13 KiB
C#

using _0_Framework.Application;
using AccountManagement.Application.Contracts.Account;
using AccountManagement.Application.Contracts.Position;
using AccountManagement.Application.Contracts.Task;
using AccountManagement.Application.Contracts.TaskSubject;
using CompanyManagment.App.Contracts.Holiday;
using CompanyManagment.App.Contracts.HolidayItem;
using CompanyManagment.App.Contracts.PersonalContractingParty;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Generic;
using System.Globalization;
using AccountManagement.Application.Contracts.TaskSchedule;
using Microsoft.AspNetCore.Authorization;
using Company.Domain.HolidayAgg;
using CompanyManagment.App.Contracts.Employer;
using CompanyManagment.App.Contracts.Workshop;
using AccountManagement.Domain.TicketAgg;
namespace ServiceHost.Areas.AdminNew.Pages.Company.Task
{
[Authorize]
public class CreateModel : PageModel
{
private readonly ITaskApplication _taskApplication;
private readonly IAccountApplication _accountApplication;
private readonly IAuthHelper _authHelper;
private readonly IPersonalContractingPartyApp _contractingPartyApp;
private readonly IPositionApplication _positionApplication;
private readonly ITaskSubjectApplication _taskSubjectApplication;
private readonly IHolidayItemApplication _holidayItemApplication;
private readonly IEmployerApplication _employerApplication;
private readonly IWorkshopApplication _workshopApplication;
private readonly ITaskScheduleApplication _taskScheduleApplication;
public CreateModel(ITaskApplication taskApplication, IAccountApplication accountApplication, IPersonalContractingPartyApp personalContractingPartyApp, IAuthHelper authHelper, IPersonalContractingPartyApp contractingPartyApp, IPositionApplication positionApplication, ITaskSubjectApplication taskSubjectApplication, IWebHostEnvironment webHostEnvironment, IHolidayApplication holidayApplication, IHolidayItemApplication holidayItemApplication, IEmployerApplication employerApplication, IWorkshopApplication workshopApplication, ITaskScheduleApplication taskScheduleApplication)
{
_taskApplication = taskApplication;
_accountApplication = accountApplication;
_authHelper = authHelper;
_contractingPartyApp = contractingPartyApp;
_positionApplication = positionApplication;
_taskSubjectApplication = taskSubjectApplication;
_holidayItemApplication = holidayItemApplication;
_employerApplication = employerApplication;
_workshopApplication = workshopApplication;
_taskScheduleApplication = taskScheduleApplication;
}
public CreateTask Command { get; set; }
public SelectList AccountsList { get; set; }
public List<PositionViewModel> PositionViewModels { get; set; }
public long Id { get; set; }
public string DateFa;
public IActionResult OnGet()
{
var positionValue = _authHelper.CurrentAccountInfo().PositionValue == null
? 0
: _authHelper.CurrentAccountInfo().PositionValue;
if (positionValue > 0)
{
if (_authHelper.GetPermissions().Any(x => x == 903))
{
var accounts = _accountApplication.GetAccountEqualToLowerPositionValue();
AccountsList = new SelectList(accounts, "Id", "Fullname");
PositionViewModels = _positionApplication.GetLowerPosition();
DateFa = DateTime.Now.ToFarsi();
Id = _authHelper.CurrentAccountId();
return Page();
}
else
{
return Forbid();
}
}
else
{
return Forbid();
}
}
public IActionResult OnPostCreateSaveTask(CreateTask Command)
{
Command.SenderId = _authHelper.CurrentAccountId();
Command.Description = Command.Description?.Replace("\n", "<br>");
if (Command.HasSchedule)
{
var scheduleResult = _taskScheduleApplication.Create(Command);
return new JsonResult(new
{
IsSuccedded = scheduleResult.IsSuccedded,
message = scheduleResult.Message,
});
}
var result = _taskApplication.CreateTask(Command);
return new JsonResult(new
{
IsSuccedded = result.IsSuccedded,
message = result.Message,
});
}
public IActionResult OnGetSearchContractingParties(string searchName)
{
var list = new List<string>();
var contractingParties = _contractingPartyApp.SearchByName(searchName);
var employers = _employerApplication.GetEmployersHasWorkshop().Select(x => x.FullName).ToList();
var workshops = _workshopApplication.GetWorkshopAll().Select(x => x.WorkshopFullName).ToList();
list.Capacity = list.Count + contractingParties.Count + employers.Count + workshops.Count;
list.AddRange(contractingParties);
list.AddRange(employers);
list.AddRange(workshops);
return new JsonResult(new
{
isSuccess = true,
list = list.GroupBy(x => x).Select(x => x.First()),
});
}
// برای محاسبه تاریخ و ساعت
#region ComputeDay
//public IActionResult OnGetComputeDay(string date, string time)
//{
// if (string.IsNullOrWhiteSpace(time))
// {
// time = "23:59";
// }
// if (!string.IsNullOrWhiteSpace(date))
// {
// var endTime = Convert.ToDateTime(time);
// var dateDeadline = date.ToGeorgianDateTime();
// dateDeadline = dateDeadline.AddHours(endTime.Hour).AddMinutes(endTime.Minute);
// var dateNow = DateTime.Now;
// if (dateDeadline > dateNow)
// {
// var daysSpan = ((dateDeadline.Date - dateNow.Date).TotalDays);
// if (!string.IsNullOrWhiteSpace(time))
// {
// var hourlyDate = dateDeadline.ToLocalTime() - DateTime.Now.ToLocalTime();
// var hours = dateDeadline.Hour - DateTime.Now.Hour;
// if (hours > 0&& (int)daysSpan > 0)
// {
// return new JsonResult(new
// {
// res = $"{(int)daysSpan} روز " + hours + " " + "ساعت ",
// });
// }
// else if (hours == 0&&(int)daysSpan>0)
// {
// return new JsonResult(new
// {
// res = $"{(int)daysSpan} روز "
// });
// }
// else if (hours>0&& (int)daysSpan == 0)
// {
// return new JsonResult(new
// {
// res = $"{hours} ساعت",
// });
// }
// }
// return new JsonResult(new
// {
// res = $"{(int)daysSpan} روز",
// });
// }
// else
// {
// return new JsonResult(new
// {
// status = false,
// res = "مهلت انجام پروژه نمی تواند کوچکتر از گذشته باشد.",
// });
// }
// }
// return new JsonResult(new
// {
// res = "",
// });
//}
#endregion
// برای باز کردن مودال عنوان دلبخواه
public IActionResult OnGetTaskSubject()
{
return Partial("CreateCRUDTaskSubjectModal");
}
// برای گرفتن عنوان دلبخواه
public IActionResult OnGetListTaskSubject()
{
var result = _taskSubjectApplication.GetAll("").OrderByDescending(x => x.Id);
return new JsonResult(new
{
result
});
}
// برای ذخیره عنوان دلبخواه
public IActionResult OnPostCreateTaskSubject(string subject)
{
var result = _taskSubjectApplication.Create(subject);
return new JsonResult(new
{
isSuccess = result.IsSuccedded,
message = result.Message,
});
}
// برای ویرایش عنوان دلبخواه
public IActionResult OnPostEditTaskSubject(long id, string subject)
{
var model = new TaskSubjectViewModel()
{
Id = id,
Subject = subject
};
var result = _taskSubjectApplication.Edit(model);
return new JsonResult(new
{
isSuccess = result.IsSuccedded,
message = result.Message,
});
}
// برای حذف عنوان دلبخواه
public IActionResult OnPostRemoveTaskSubject(long id)
{
var result = _taskSubjectApplication.Delete(id);
return new JsonResult(new
{
isSuccess = result.IsSuccedded,
message = result.Message,
});
}
public IActionResult OnGetSearchTaskSubject(string searchSubject)
{
var list = _taskSubjectApplication.GetAll(searchSubject);
return new JsonResult(new
{
isSuccess = true,
list = list,
});
}
public IActionResult OnGetCheckHoliday(string time)
{
var dateTime = time.ToGeorgianDateTime().Date;
var errorDate = new DateTime(3000, 12, 20, new PersianCalendar());
var operation = new OperationResult();
if (dateTime == errorDate)
{
operation.Failed("لطفا تاریخ خود را به درستی وارد کنید");
return new JsonResult(new
{
IsSuccedded = operation.IsSuccedded,
Message = operation.Message,
isHoliday = false
});
}
var isFriday = (dateTime.DayOfWeek == DayOfWeek.Friday);
var isHoliday = _holidayItemApplication.IsHoliday(dateTime);
if (isFriday)
{
operation.Succcedded();
return new JsonResult(new
{
IsSuccedded = operation.IsSuccedded,
isHoliday = true
});
}
operation.Succcedded();
return new JsonResult(new
{
IsSuccedded = operation.IsSuccedded,
isHoliday=isHoliday
});
}
public IActionResult OnPostUploadFile(IFormFile media)
{
var accountId = _authHelper.CurrentAccountId();
var operation = _taskApplication.UploadMedia(media, accountId);
return new JsonResult(new
{
IsSuccedded = operation.IsSuccedded,
message = operation.Message,
id = operation.SendId,
});
}
public IActionResult OnPostDeleteFile(long mediaId)
{
var operation = _taskApplication.RemoveMedia(mediaId);
return new JsonResult(new
{
IsSuccedded = operation.IsSuccedded,
message = operation.Message,
});
}
public IActionResult OnPostRemoveAllTempFiles()
{
var accId = _authHelper.CurrentAccountId();
var operation = new OperationResult();
_taskApplication.RemoveTempUploadedFiles(accId);
operation.Succcedded();
return new JsonResult(new
{
IsSuccedded = operation.IsSuccedded,
message = operation.Message,
});
}
}
}