Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Processing.WebApp.Engines.CardService {
- using Processing.Core.Constants.Processing;
- using Processing.Core.BaseEngine;
- using Processing.Core.Interfaces;
- using Processing.Core.Model.Request;
- using Processing.Core.Model.Response;
- using Processing.Core.Model.Services;
- using Processing.Core.Utilites;
- using Processing.Model.Entity.Processing;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- #region Class: GetContactLoyaltyLevelInfoEngine
- public class GetContactLoyaltyLevelInfoEngine : BaseEngine {
- public override object Execute(object obj) {
- GetContactLoyaltyLevelInfoRequest request = obj.ForceType<GetContactLoyaltyLevelInfoRequest>();
- GetContactLoyaltyLevelInfoResponse response = new GetContactLoyaltyLevelInfoResponse();
- IProcessingRepository repository = EngineParameters.GetRepository<IProcessingRepository>();
- Contact contact = null;
- Card dbCard = null;
- if (string.IsNullOrEmpty(request.CardNumber) == false) {
- dbCard = repository.GetEntityByColumn<Card>("Number", request.CardNumber);
- if (dbCard?.Id == null) {
- ContactService.Error.CardNotFound.FillBaseResponse(response);
- return response;
- }
- if (dbCard.StatusId?.SequenceEqual(Core.Constants.CardConsts.Status.BlockedIdB) == true) {
- ContactService.Error.CardNotFound.FillBaseResponse(response);
- return response;
- }
- if (dbCard?.Id != null) {
- contact = repository.GetEntityByColumn<Contact>("Id", dbCard.ContactId);
- ResponseFiller rp = new ResponseFiller(contact, dbCard, repository);
- response.CardNumber = rp.CardNumber;
- response.CurrentLevel = rp.CurrentLevelName;
- response.NextLevel = rp.NextLevelName;
- response.LevelDetails.Add(rp.LevelDetail);
- response.Cards = null;
- /*var responseObj = FillResponse(contact, dbCard, repository);
- response.CardNumber = responseObj.Item1;
- response.CurrentLevel = responseObj.Item2;
- response.NextLevel = responseObj.Item3;
- response.LevelDetails.Add(responseObj.Item4);
- response.Cards = null;*/
- }
- } else if (string.IsNullOrEmpty(request.ContactPhone) == false) {
- contact = repository.GetEntityByColumn<Contact>("Phone", request.ContactPhone);
- } else if (string.IsNullOrEmpty(request.ContactId) == false) {
- if (Guid.TryParse(request.ContactId, out Guid contactIdGuid)) {
- contact = repository.GetEntityByColumn<Contact>("Id", contactIdGuid.ToByteArray());
- }
- }
- if (contact?.Id == null) {
- ContactService.Error.ContactNotFound.FillBaseResponse(response);
- return response;
- }
- // contact loyaltylevel
- if (contact.Id != null && dbCard == null) {
- IEnumerable<Card> contactCards = repository.GetEntityListByColumn<Card>(
- "ContactId",
- contact.Id,
- "Id, LoyaltyLevelId, Number"
- );
- foreach (var card in contactCards)
- {
- if (card.Id != null && card.LoyaltyLevelId != null && card.Number != null)
- {
- var cardIntoResponse = new CardInfo();
- ResponseFiller rp = new ResponseFiller(contact, card, repository);
- cardIntoResponse.CardNumber = rp.CardNumber;
- cardIntoResponse.CurrentLevel = rp.CurrentLevelName;
- cardIntoResponse.NextLevel = rp.NextLevelName;
- cardIntoResponse.LevelDetails.Add(rp.LevelDetail);
- /*var responseObj = FillResponse(contact, card, repository);
- cardIntoResponse.CardNumber = responseObj.Item1;
- cardIntoResponse.CurrentLevel = responseObj.Item2;
- cardIntoResponse.NextLevel = responseObj.Item3;
- cardIntoResponse.LevelDetails.Add(responseObj.Item4);*/
- response.Cards.Add(cardIntoResponse);
- response.LevelDetails = null;
- }
- }
- }
- return response;
- }
- /*private (string, string, string, LoyaltyLevelInfo) FillResponse(Contact contact, Card card, IProcessingRepository repository) {
- string CardNumber = card.Number;
- var loyaltyLevels = repository.GetEntityList<LoyaltyLevel>();
- // get current LoyaltyLevel
- var currentLevel = loyaltyLevels.FirstOrDefault(l => l.Id.SequenceEqual(card.LoyaltyLevelId)) ?? new LoyaltyLevel();
- string currentLevelName = currentLevel.Name;
- // get next LoyaltyLevel
- var nextLevel = loyaltyLevels
- .OrderBy(l => l.Priority)
- .FirstOrDefault(l => l.Priority > currentLevel.Priority) ?? new LoyaltyLevel();
- string nextLevelName = nextLevel.Name;
- // get previous LoyaltyLevel(s)
- var prevLevels = loyaltyLevels
- //.OrderByDescending(l => l.Priority)
- .Where(l => l.Priority < currentLevel.Priority);
- decimal prevLevelsAmountSpentMoney = 0.0m;
- decimal prevLevelsAmountAccumulatedBonuses = 0.0m;
- if (currentLevel.IsResetRateWhenLevelUp) {
- prevLevelsAmountSpentMoney = currentLevel.AmountSpentMoney
- + prevLevels.Sum(pl => pl.AmountSpentMoney);
- prevLevelsAmountAccumulatedBonuses = currentLevel.AmountAccumulatedBonuses
- + prevLevels.Sum(pl => pl.AmountAccumulatedBonuses);
- }
- var levelDetail = new LoyaltyLevelInfo();
- DateTime endDate = DateTime.Now;
- DateTime startDate = endDate;
- LoyaltyPeriodHelper.GetDatesByPeriodType(
- currentLevel.LoyaltyRuleDatePeriodId,
- DateTime.Now,
- out startDate, out endDate
- );
- // данные по контакту
- decimal accumulatedMoney = repository.GetContactAccumulatedMoney(
- new List<byte[]>() { card.Id },
- startDate,
- endDate,
- currentLevel.IsConsiderReturn
- );
- var moneyRemainsToNextLevel = nextLevel.AmountSpentMoney > 0 ?
- nextLevel.AmountSpentMoney
- + prevLevelsAmountSpentMoney
- - accumulatedMoney : 0.0m;
- if (moneyRemainsToNextLevel < 0.0m) {
- moneyRemainsToNextLevel = 0.0m;
- }
- levelDetail.Money.Add(new LoyaltyLevelMoneyInfo() {
- MoneySpent = accumulatedMoney,
- RemainsToNextLevel = moneyRemainsToNextLevel
- });
- IEnumerable<ResultBonusInfo> cardBonuses = repository.GetBonusesInfoFromContact(contact.Id, cardId: card.Id);
- decimal accumulatedBonuses = 0;
- if (cardBonuses.Count() > 0) {
- accumulatedBonuses = cardBonuses
- .FirstOrDefault(b => b.BonusStatus == Processing.Core.Constants.Common.BonusStatusDictionary["Active"])
- .BonusAmount;
- }
- var bonusesRemainsToNextLevel = nextLevel.AmountAccumulatedBonuses > 0 ?
- nextLevel.AmountAccumulatedBonuses
- + prevLevelsAmountAccumulatedBonuses
- - accumulatedBonuses : 0.0m;
- if (bonusesRemainsToNextLevel < 0.0m) {
- bonusesRemainsToNextLevel = 0.0m;
- }
- levelDetail.Bonuses.Add(new LoyaltyLevelBonusesInfo() {
- BonusesAccumulated = accumulatedBonuses,
- RemainsToNextLevel = bonusesRemainsToNextLevel
- });
- return (CardNumber, currentLevelName, nextLevelName, levelDetail);
- }*/
- }
- public class ResponseFiller {
- private readonly Contact _contact;
- private readonly Card _card;
- private readonly IProcessingRepository _repository;
- public string CardNumber { get; private set; }
- public string CurrentLevelName { get; private set; }
- public string NextLevelName { get; private set; }
- public LoyaltyLevelInfo LevelDetail { get; }
- public ResponseFiller(Contact contact, Card card, IProcessingRepository repository) {
- this._contact = contact;
- this._card = card;
- this._repository = repository;
- this.LevelDetail = new LoyaltyLevelInfo();
- SetFields();
- }
- private void SetFields() {
- this.CardNumber = _card.Number;
- var loyaltyLevels = _repository.GetEntityList<LoyaltyLevel>();
- // get current LoyaltyLevel
- var currentLevel = loyaltyLevels.FirstOrDefault(l => l.Id.SequenceEqual(_card.LoyaltyLevelId)) ?? new LoyaltyLevel();
- this.CurrentLevelName = currentLevel.Name;
- // get next LoyaltyLevel
- var nextLevel = loyaltyLevels
- .OrderBy(l => l.Priority)
- .FirstOrDefault(l => l.Priority > currentLevel.Priority) ?? new LoyaltyLevel();
- this.NextLevelName = nextLevel.Name;
- // get previous LoyaltyLevel(s)
- var prevLevels = loyaltyLevels
- //.OrderByDescending(l => l.Priority)
- .Where(l => l.Priority < currentLevel.Priority);
- decimal prevLevelsAmountSpentMoney = 0.0m;
- decimal prevLevelsAmountAccumulatedBonuses = 0.0m;
- if (currentLevel.IsResetRateWhenLevelUp) {
- prevLevelsAmountSpentMoney = currentLevel.AmountSpentMoney
- + prevLevels.Sum(pl => pl.AmountSpentMoney);
- prevLevelsAmountAccumulatedBonuses = currentLevel.AmountAccumulatedBonuses
- + prevLevels.Sum(pl => pl.AmountAccumulatedBonuses);
- }
- DateTime endDate = DateTime.Now;
- DateTime startDate = endDate;
- LoyaltyPeriodHelper.GetDatesByPeriodType(
- currentLevel.LoyaltyRuleDatePeriodId,
- DateTime.Now,
- out startDate, out endDate
- );
- // данные по контакту
- decimal accumulatedMoney = _repository.GetContactAccumulatedMoney(
- new List<byte[]>() { _card.Id },
- startDate,
- endDate,
- currentLevel.IsConsiderReturn
- );
- var moneyRemainsToNextLevel = nextLevel.AmountSpentMoney > 0 ?
- nextLevel.AmountSpentMoney
- + prevLevelsAmountSpentMoney
- - accumulatedMoney : 0.0m;
- if (moneyRemainsToNextLevel < 0.0m) {
- moneyRemainsToNextLevel = 0.0m;
- }
- this.LevelDetail.Money.Add(new LoyaltyLevelMoneyInfo() {
- MoneySpent = accumulatedMoney,
- RemainsToNextLevel = moneyRemainsToNextLevel
- });
- IEnumerable<ResultBonusInfo> cardBonuses = _repository.GetBonusesInfoFromContact(_contact.Id, cardId: _card.Id);
- decimal accumulatedBonuses = 0;
- if (cardBonuses.Count() > 0) {
- accumulatedBonuses = cardBonuses
- .FirstOrDefault(b => b.BonusStatus == Processing.Core.Constants.Common.BonusStatusDictionary["Active"])
- .BonusAmount;
- }
- var bonusesRemainsToNextLevel = nextLevel.AmountAccumulatedBonuses > 0 ?
- nextLevel.AmountAccumulatedBonuses
- + prevLevelsAmountAccumulatedBonuses
- - accumulatedBonuses : 0.0m;
- if (bonusesRemainsToNextLevel < 0.0m) {
- bonusesRemainsToNextLevel = 0.0m;
- }
- this.LevelDetail.Bonuses.Add(new LoyaltyLevelBonusesInfo() {
- BonusesAccumulated = accumulatedBonuses,
- RemainsToNextLevel = bonusesRemainsToNextLevel
- });
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement