ISEbd-21_Melnikov_I.O._CarS.../CarService/CarServiceBusinessLogic/BusinessLogics/CustomerRoleImitationLogic.cs

167 lines
4.2 KiB
C#

using CarServiceContracts.BusinessLogicsContracts;
namespace CarServiceBusinessLogic.BusinessLogics
{
/// <summary>
/// Заглушка для роли клиента
/// </summary>
public class CustomerRoleImitationLogic
{
private readonly ICustomerLogic _customerLogic;
private readonly IVehicleLogic _vehicleLogic;
private readonly IRepairRequestLogic _repairRequestLogic;
private readonly IWorkInRequestLogic _workInRequestLogic;
private readonly IWorkLogic _workLogic;
private readonly IWorkPaymentLogic _workPaymentLogic;
public CustomerRoleImitationLogic(ICustomerLogic customerLogic, IVehicleLogic vehicleLogic, IRepairRequestLogic repairRequestLogic, IWorkInRequestLogic workInRequestLogic, IWorkLogic workLogic, IWorkPaymentLogic workPaymentLogic)
{
_customerLogic = customerLogic;
_vehicleLogic = vehicleLogic;
_repairRequestLogic = repairRequestLogic;
_workInRequestLogic = workInRequestLogic;
_workLogic = workLogic;
_workPaymentLogic = workPaymentLogic;
}
private bool GenerateCustomerData()
{
if (_customerLogic.ReadList(null)?.Count != 0)
{
return false;
}
using (StreamReader sr = new("customers.txt"))
{
string? currentString;
while ((currentString = sr.ReadLine()) != null)
{
var customerRecData = currentString.Split(',');
_customerLogic.Create(new()
{
Login = customerRecData[0],
Password = customerRecData[1],
Name = customerRecData[2],
Surname = customerRecData[3]
});
}
}
return true;
}
private bool GenerateVehicleData()
{
if (_vehicleLogic.ReadList(null)?.Count != 0)
{
return false;
}
using (StreamReader sr = new("vehicles.txt"))
{
string? currentString;
while ((currentString = sr.ReadLine()) != null)
{
var vehicleRecData = currentString.Split(',');
_vehicleLogic.Create(new()
{
Name = vehicleRecData[0],
Plate = vehicleRecData[1],
VIN = vehicleRecData[2],
CustomerId = Convert.ToInt32(vehicleRecData[4])
});
}
}
return true;
}
private bool GenerateRepairRequestData()
{
if (_repairRequestLogic.ReadList(null)?.Count != 0)
{
return false;
}
using (StreamReader sr = new("requests.txt"))
{
string? currentString;
while ((currentString = sr.ReadLine()) != null)
{
var repairRequestRecData = currentString.Split(',');
_repairRequestLogic.Create(new()
{
DateCreated = Convert.ToDateTime(repairRequestRecData[0]),
VehicleId = Convert.ToInt32(repairRequestRecData[1])
});
}
}
return true;
}
public bool GenerateRequestData()
{
if (!GenerateCustomerData())
{
return false;
}
if (!GenerateVehicleData())
{
return false;
}
if (!GenerateRepairRequestData())
{
return false;
}
return true;
}
public bool GenerateWorksInRequest()
{
var WorksList = _workLogic.ReadList(null);
if (WorksList == null)
{
return false;
}
if (WorksList.Count == 0)
{
return false;
}
var RequestList = _repairRequestLogic.ReadList(null);
if (RequestList == null)
{
return false;
}
if (RequestList.Count == 0)
{
return false;
}
Random r = new();
for (int i = 0; i < 2; i++)
{
_workInRequestLogic.Create(new()
{
RepairRequestId = r.Next(0, RequestList.Count),
WorkId = r.Next(0, WorksList.Count),
Count = r.Next(1, 2)
});
}
return true;
}
public bool GeneratePayments()
{
var WorksInRequestList = _workInRequestLogic.ReadList(null);
if (WorksInRequestList == null)
{
return false;
}
if (WorksInRequestList.Count == 0)
{
return false;
}
Random r = new();
for (int i = 0; i < 2; i++)
{
int paidWorkId = r.Next(0, WorksInRequestList.Count);//определяем, какую работу оплачиваем
bool payFullPrice = Convert.ToBoolean(r.Next(0, 1));//определяем, оплачиваем заявку полностью или наполовину
_workPaymentLogic.Create(new()
{
DatePayment = DateTime.Now,
Sum = payFullPrice ? WorksInRequestList[paidWorkId].Cost : WorksInRequestList[paidWorkId].Cost / 2,
WorkInRequestId = paidWorkId
});
}
return true;
}
}
}