public BaseRequestCreator(IDaDataHelper daDataHelper)
{
_daDataHelper = daDataHelper;
}
protected dynamic request { get; set; }
protected int DealObjectIndex { get; set; }
private const string _kaskoProductCode = "KASKO";
public CreateRequestResult CreateRequest(dynamic oldRequest, Deal deal, DateTime? dateStart)
{
var prolongationChanges = new ProlongationChanges();
InitRequest(oldRequest, deal, dateStart, prolongationChanges);
dynamic model = request.model;
dynamic oldModel = oldRequest.model;
AddMainEntity(model, oldModel, deal, prolongationChanges);
AddInsurant(model, oldModel, deal, prolongationChanges);
AddInsuranceObject(model, oldModel, deal, prolongationChanges);
return new CreateRequestResult() { RequestData = request, ProlongationChanges = prolongationChanges };
}
protected void AddInsurant(dynamic model, dynamic oldModel, Deal deal, ProlongationChanges prolongationChanges)
{
dynamic requestDeal = model.deals[0];
requestDeal.insurant = new JObject();
requestDeal.insurant.dealObjects = DealObjectIndex + 1;
var insurant = deal.Insurant;
var insurantPerson = insurant.Subject.IndividualEntity.Person;
var document = insurant.IdentityDocument;
long? oldInsurantId = oldModel?.deals[0]?
.insurant?
.dealObjects?.Value;
dynamic oldInsurantPerson = ((JArray)oldModel.dealsObjects)
.Select(p => (dynamic)p)
.FirstOrDefault(p => p.id == oldInsurantId)?
.person ?? new JObject();
AddPerson(model, oldInsurantPerson, insurantPerson, document, prolongationChanges);
}
protected void InitRequest(dynamic oldRequest, Deal deal, DateTime? dateStart, ProlongationChanges prolongationChanges)
{
var requestObject = new JObject();
if (oldRequest is JObject oldRequestObject)
{
requestObject = oldRequestObject.DeepClone() as JObject;
requestObject.Remove("model");
}
request = requestObject;
request.model = new JObject();
dynamic model = request.model;
model.deals = new JArray();
model.dealsObjects = new JArray();
model.deals.Add(new JObject());
dynamic requestDeal = model.deals[0];
requestDeal.dealType = _kaskoProductCode;
InitRequestInternal(model, oldRequest.model, deal, dateStart, prolongationChanges);
}
protected void AddInsuranceObject(dynamic model, dynamic oldModel, Deal deal, ProlongationChanges prolongationChanges)
{
model.deals[0].insuranceObjects = new JArray();
model.deals[0].insuranceObjects.Add(new JObject());
AddAuto(model, oldModel, deal, prolongationChanges);
var owner = deal.GetOwner();
// Фикс для LKA-1638.
var document = owner.IsInsurant == true
? deal.Insurant.IdentityDocument
: owner.IdentityDocument;
AddOwner(model, oldModel, owner, document, prolongationChanges);
}
protected void AddAuto(dynamic model, dynamic oldModel, Deal deal, ProlongationChanges prolongationChanges)
{
model.deals[0].insuranceObjects[0].auto = new JObject();
model.deals[0].insuranceObjects[0].auto.dealObjects = DealObjectIndex + 1;
model.dealsObjects.Add(new JObject());
dynamic dealObject = model.dealsObjects[DealObjectIndex];
dealObject.id = DealObjectIndex + 1;
dealObject.type = "auto";
dealObject.auto = new JObject();
dynamic requestAuto = dealObject.auto;
dynamic oldRequestAuto = ((JArray)oldModel.dealsObjects)
.Children()
.Select(c => (dynamic)c)
.FirstOrDefault(c => c.type == "auto")?
.auto;
AddAutoInternal(requestAuto, oldRequestAuto, deal, prolongationChanges);
DealObjectIndex++;
}
protected void AddOwner(dynamic model, dynamic oldModel, Owner owner, IdentityDocument document, ProlongationChanges prolongationChanges)
{
model.deals[0].insuranceObjects[0].owners = new JArray();
dynamic requestOwners = model.deals[0].insuranceObjects[0].owners;
requestOwners.Add(new JObject());
requestOwners[0].dealObjects = DealObjectIndex + 1;
var ownerPerson = owner.GetOwnerPerson();
long oldOwnerId = oldModel?.deals[0]?
.insuranceObjects[0]?
.owners[0]?
.dealObjects?.Value;
dynamic oldOwnerPerson = ((JArray)oldModel.dealsObjects)
.Select(p => (dynamic)p)
.FirstOrDefault(p => p.id == oldOwnerId)
.person;
AddPerson(model, oldOwnerPerson, ownerPerson, document, prolongationChanges);
}
protected void AddPerson(dynamic model, dynamic oldRequestPerson, Person person, IdentityDocument document, ProlongationChanges prolongationChanges)
{
model.dealsObjects.Add(new JObject());
dynamic dealObject = model.dealsObjects[DealObjectIndex];
dealObject.id = DealObjectIndex + 1;
dealObject.type = "person";
dealObject.person = new JObject();
dynamic requestPerson = dealObject.person;
ReplaceOrAddValueIfExists(requestPerson, oldRequestPerson, "lastName", person?.LastName?.ToLower()?.UpperFirstLetter(), prolongationChanges);
ReplaceOrAddValueIfExists(requestPerson, oldRequestPerson, "name", person?.Name?.ToLower()?.UpperFirstLetter(), prolongationChanges);
ReplaceOrAddValueIfExists(requestPerson, oldRequestPerson, "middleName", person.MiddleName?.ToLower()?.UpperFirstLetter(), prolongationChanges);
ReplaceOrAddValueIfExists(requestPerson, oldRequestPerson, "dateOfBirth", person?.DateOfBirth?.ToShortDateString(), prolongationChanges);
AddPersonInternal(requestPerson, oldRequestPerson, person, document, prolongationChanges);
requestPerson.addresses = new JArray();
requestPerson.addresses.Add(new JObject());
dynamic requestAddress = new JObject();
dynamic oldRequestAddress = oldRequestPerson["addresses"]?[0] ?? new JObject();
if (((JObject)oldRequestAddress).Count == 0)
{
requestAddress = requestPerson.addresses[0];
}
else
{
requestPerson.addresses[0] = oldRequestAddress;
}
var address = person.Addresses.FirstOrDefault();
if (address?.Region == null)
{
var daDataAddress = _daDataHelper.ParseAddress($"{address?.City}, {address?.Settlement}, {address?.Street}, {address?.House}, {address?.Building}, {address?.Office}");
address.Region = daDataAddress?.Region;
}
ReplaceOrAddValueIfExists(requestAddress, oldRequestAddress, "kladrId", address?.KladrId, prolongationChanges);
ReplaceOrAddValueIfExists(requestAddress, oldRequestAddress, "country", address?.Country, prolongationChanges);
ReplaceOrAddValueIfExists(requestAddress, oldRequestAddress, "region", address?.Region, prolongationChanges);
ReplaceOrAddValueIfExists(requestAddress, oldRequestAddress, "city", address?.City, prolongationChanges);
ReplaceOrAddValueIfExists(requestAddress, oldRequestAddress, "settlement", address?.Settlement, prolongationChanges);
ReplaceOrAddValueIfExists(requestAddress, oldRequestAddress, "street", address?.Street, prolongationChanges);
ReplaceOrAddValueIfExists(requestAddress, oldRequestAddress, "house", address?.House, prolongationChanges);
ReplaceOrAddValueIfExists(requestAddress, oldRequestAddress, "building", address?.Building, prolongationChanges);
ReplaceOrAddValueIfExists(requestAddress, oldRequestAddress, "officeOrFlat", address?.Office, prolongationChanges);
DealObjectIndex++;
}
protected abstract void InitRequestInternal(dynamic model, dynamic oldModel, Deal deal, DateTime? dateStart, ProlongationChanges prolongationChanges);
protected void AddMainEntity(dynamic model, dynamic oldModel, Deal deal, ProlongationChanges prolongationChanges)
{
model.deals[0].drivers = new JArray();
dynamic requestDrivers = model.deals[0].drivers;
var oldRequestDrivers = ((JArray)oldModel.dealsObjects)
.Children()
.Select(c => ((dynamic)c))
.Where(c => c.type == "driver");
int driversIndex = 0;
var drivers = deal.AutoDeal.Drivers;
if (oldRequestDrivers.Count() != drivers?.Count ||
oldRequestDrivers.Any(x => drivers?.All(d => d.ProlongationId != x.prolongationId?.Value) == true) ||
drivers?.Any(x => oldRequestDrivers.All(d => x.ProlongationId != d.prolongationId?.Value)) == true)
{
prolongationChanges.ChangeList.Add(("Drivers", $"{drivers?.Count}", $"{oldRequestDrivers.Count()}"));
}
foreach (var oldDealObject in oldRequestDrivers)
{
var driver = drivers?.SingleOrDefault(d => d.ProlongationId == oldDealObject.prolongationId?.Value);
requestDrivers.Add(new JObject());
requestDrivers[driversIndex].dealObjects = DealObjectIndex + 1;
model.dealsObjects.Add(new JObject());
dynamic dealObject = model.dealsObjects[DealObjectIndex];
dealObject.id = DealObjectIndex + 1;
dealObject.type = "driver";
dealObject.driver = new JObject();
dynamic requestDriver = dealObject.driver;
dynamic oldRequestDriver = oldDealObject.driver;
var person = driver?.Subject?.IndividualEntity?.Person;
ReplaceOrAddValueIfExists(requestDriver, oldRequestDriver, "dateOfBirth", person?.DateOfBirth?.ToShortDateString(), prolongationChanges);
ReplaceOrAddValueIfExists(requestDriver, oldRequestDriver, "sexCode", person?.SexCode, prolongationChanges);
ReplaceOrAddValueIfExists(requestDriver, oldRequestDriver, "name", person?.Name?.ToLower()?.UpperFirstLetter(), prolongationChanges);
ReplaceOrAddValueIfExists(requestDriver, oldRequestDriver, "lastName", person?.LastName?.ToLower()?.UpperFirstLetter(), prolongationChanges);
ReplaceOrAddValueIfExists(requestDriver, oldRequestDriver, "middleName", person?.MiddleName?.ToLower()?.UpperFirstLetter(), prolongationChanges);
ReplaceOrAddValueIfExists(requestDriver, oldRequestDriver, "driverLicenseSeries", driver?.DriverLicenseSeries, prolongationChanges);
ReplaceOrAddValueIfExists(requestDriver, oldRequestDriver, "driverLicenseNumber", driver?.DriverLicenseNumber, prolongationChanges);
ReplaceOrAddValueIfExists(requestDriver, oldRequestDriver, "drivingExperienceDateStart", driver?.DrivingExperiences.FirstOrDefault()?.DrivingExperienceDateStart?.ToShortDateString(), prolongationChanges);
ReplaceOrAddValueIfExists(requestDriver, oldRequestDriver, "driverIsForeignDrivingLicense", driver?.IsForeignDrivingLicense, prolongationChanges);
DealObjectIndex++;
driversIndex++;
}
}
protected abstract void AddAutoInternal(dynamic requestAuto, dynamic oldRequestAuto, Deal deal, ProlongationChanges prolongationChanges);
protected abstract void AddPersonInternal(dynamic requestPerson, dynamic oldRequestPerson, Person person, IdentityDocument document, ProlongationChanges prolongationChanges);
protected void ReplaceOrAddValueIfExists<T>(dynamic request, dynamic oldRequest, string fieldName, T value, ProlongationChanges prolongationChanges)
{
if (oldRequest != null && oldRequest[fieldName] != null)
{
request[fieldName] = oldRequest[fieldName].Value;
prolongationChanges?.CheckChanges(fieldName, oldRequest[fieldName].Value?.ToString(), value?.ToString());
}
else if (typeof(T) == typeof(string) && !string.IsNullOrEmpty(value as string))
{
request[fieldName] = value;
prolongationChanges?.CheckChanges(fieldName, null, value as string);
}
else if (typeof(T) != typeof(string) && value != null)
{
request[fieldName] = value;
prolongationChanges?.CheckChanges(fieldName, null, value.ToString());
}
}
protected void ReplaceOrAddValueIfExistsWithCheckChangesInMainField<T>(dynamic request, dynamic oldRequest, string fieldName, T value, string mainFieldName, ProlongationChanges prolongationChanges)
{
if (!prolongationChanges.HasChangesIn(mainFieldName))
{
ReplaceOrAddValueIfExists<T>(request, oldRequest, fieldName, value, prolongationChanges);
}
else
{
ReplaceOrAddValueIfExists<T>(request, oldRequest, fieldName, default(T), prolongationChanges);
}
}
protected void AddAntiTheftSystemsInternal(dynamic requestAuto, dynamic oldRequestAuto, Auto auto)
{
if (oldRequestAuto[PropertyNames.AntiTheftSystemsPropertyName] != null)
{
var oldRequestAntiTheftSystems = (JArray)oldRequestAuto[PropertyNames.AntiTheftSystemsPropertyName];
if (oldRequestAntiTheftSystems.Count > 0)
{
requestAuto[PropertyNames.AntiTheftSystemsPropertyName] = new JArray();
oldRequestAntiTheftSystems
.Children()
.Select(s => (dynamic)s)
.ToList()
.ForEach(oldRequestAntiTheftSystem =>
{
var requestAntiTheftSystem = new JObject();
AddAntiTheftSystemInternal(requestAntiTheftSystem, oldRequestAntiTheftSystem, null);
requestAuto[PropertyNames.AntiTheftSystemsPropertyName].Add(requestAntiTheftSystem);
});
}
}
else
{
if (auto.AntiTheftSystems.Count > 0)
{
requestAuto[PropertyNames.AntiTheftSystemsPropertyName] = new JArray();
auto.AntiTheftSystems
.ForEach(antiTheftSystem =>
{
var requestAntiTheftSystem = new JObject();
AddAntiTheftSystemInternal(requestAntiTheftSystem, null, antiTheftSystem);
requestAuto[PropertyNames.AntiTheftSystemsPropertyName].Add(requestAntiTheftSystem);
});
}
}
}
protected void AddAntiTheftSystemInternal(dynamic requestAntiTheftSystem, dynamic oldRequestAntiTheftSystem, AntiTheftSystem antiTheftSystem)
{
ReplaceOrAddValueIfExists(requestAntiTheftSystem, oldRequestAntiTheftSystem, PropertyNames.AntiTheftSystemBrandCodePropertyName, antiTheftSystem?.AntiTheftSystemBrandCode, null);
ReplaceOrAddValueIfExists(requestAntiTheftSystem, oldRequestAntiTheftSystem, PropertyNames.AntiTheftSystemModelCodePropertyName, antiTheftSystem?.AntiTheftSystemModelCode, null);
}
}