Advertisement
Odense

Review

May 12th, 2021
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.03 KB | None | 0 0
  1. namespace Processing.WebApp.Engines.CardService {
  2.     using Processing.Core.Constants.Processing;
  3.     using Processing.Core.BaseEngine;
  4.     using Processing.Core.Interfaces;
  5.     using Processing.Core.Model.Request;
  6.     using Processing.Core.Model.Response;
  7.     using Processing.Core.Model.Services;
  8.     using Processing.Core.Utilites;
  9.     using Processing.Model.Entity.Processing;
  10.     using System;
  11.     using System.Collections.Generic;
  12.     using System.Linq;
  13.  
  14.     #region Class: GetContactLoyaltyLevelInfoEngine
  15.  
  16.     public class GetContactLoyaltyLevelInfoEngine : BaseEngine {
  17.         public override object Execute(object obj) {
  18.             GetContactLoyaltyLevelInfoRequest request = obj.ForceType<GetContactLoyaltyLevelInfoRequest>();
  19.             GetContactLoyaltyLevelInfoResponse response = new GetContactLoyaltyLevelInfoResponse();
  20.             IProcessingRepository repository = EngineParameters.GetRepository<IProcessingRepository>();
  21.             Contact contact = null;
  22.             Card dbCard = null;
  23.             if (string.IsNullOrEmpty(request.CardNumber) == false) {
  24.                 dbCard = repository.GetEntityByColumn<Card>("Number", request.CardNumber);
  25.                 if (dbCard?.Id == null) {
  26.                     ContactService.Error.CardNotFound.FillBaseResponse(response);
  27.                     return response;
  28.                 }
  29.                 if (dbCard.StatusId?.SequenceEqual(Core.Constants.CardConsts.Status.BlockedIdB) == true) {
  30.                     ContactService.Error.CardNotFound.FillBaseResponse(response);
  31.                     return response;
  32.                 }
  33.                 if (dbCard?.Id != null) {
  34.                     contact = repository.GetEntityByColumn<Contact>("Id", dbCard.ContactId);
  35.  
  36.                     ResponseFiller rp = new ResponseFiller(contact, dbCard, repository);
  37.                     response.CardNumber = rp.CardNumber;
  38.                     response.CurrentLevel = rp.CurrentLevelName;
  39.                     response.NextLevel = rp.NextLevelName;
  40.                     response.LevelDetails.Add(rp.LevelDetail);
  41.                     response.Cards = null;
  42.                     /*var responseObj = FillResponse(contact, dbCard, repository);
  43.                     response.CardNumber = responseObj.Item1;
  44.                     response.CurrentLevel = responseObj.Item2;
  45.                     response.NextLevel = responseObj.Item3;
  46.  
  47.                     response.LevelDetails.Add(responseObj.Item4);
  48.  
  49.                     response.Cards = null;*/
  50.                 }
  51.             } else if (string.IsNullOrEmpty(request.ContactPhone) == false) {
  52.                 contact = repository.GetEntityByColumn<Contact>("Phone", request.ContactPhone);
  53.             } else if (string.IsNullOrEmpty(request.ContactId) == false) {
  54.                 if (Guid.TryParse(request.ContactId, out Guid contactIdGuid)) {
  55.                     contact = repository.GetEntityByColumn<Contact>("Id", contactIdGuid.ToByteArray());
  56.                 }
  57.             }
  58.             if (contact?.Id == null) {
  59.                 ContactService.Error.ContactNotFound.FillBaseResponse(response);
  60.                 return response;
  61.             }
  62.             // contact loyaltylevel
  63.             if (contact.Id != null && dbCard == null) {
  64.                 IEnumerable<Card> contactCards = repository.GetEntityListByColumn<Card>(
  65.                     "ContactId",
  66.                     contact.Id,
  67.                     "Id, LoyaltyLevelId, Number"
  68.                 );
  69.                 foreach (var card in contactCards)
  70.                 {
  71.                     if (card.Id != null && card.LoyaltyLevelId != null && card.Number != null)
  72.                     {
  73.                         var cardIntoResponse = new CardInfo();
  74.                         ResponseFiller rp = new ResponseFiller(contact, card, repository);
  75.                         cardIntoResponse.CardNumber = rp.CardNumber;
  76.                         cardIntoResponse.CurrentLevel = rp.CurrentLevelName;
  77.                         cardIntoResponse.NextLevel = rp.NextLevelName;
  78.  
  79.                         cardIntoResponse.LevelDetails.Add(rp.LevelDetail);
  80.                         /*var responseObj = FillResponse(contact, card, repository);
  81.                         cardIntoResponse.CardNumber = responseObj.Item1;
  82.                         cardIntoResponse.CurrentLevel = responseObj.Item2;
  83.                         cardIntoResponse.NextLevel = responseObj.Item3;
  84.  
  85.                         cardIntoResponse.LevelDetails.Add(responseObj.Item4);*/
  86.  
  87.                         response.Cards.Add(cardIntoResponse);
  88.  
  89.                         response.LevelDetails = null;
  90.                     }
  91.                 }
  92.             }
  93.  
  94.             return response;
  95.         }
  96.  
  97.  
  98.         /*private (string, string, string, LoyaltyLevelInfo) FillResponse(Contact contact, Card card, IProcessingRepository repository) {
  99.             string CardNumber = card.Number;
  100.             var loyaltyLevels = repository.GetEntityList<LoyaltyLevel>();
  101.             // get current LoyaltyLevel
  102.             var currentLevel = loyaltyLevels.FirstOrDefault(l => l.Id.SequenceEqual(card.LoyaltyLevelId)) ?? new LoyaltyLevel();
  103.             string currentLevelName = currentLevel.Name;
  104.             // get next LoyaltyLevel
  105.             var nextLevel = loyaltyLevels
  106.                 .OrderBy(l => l.Priority)
  107.                 .FirstOrDefault(l => l.Priority > currentLevel.Priority) ?? new LoyaltyLevel();
  108.             string nextLevelName = nextLevel.Name;
  109.             // get previous LoyaltyLevel(s)
  110.             var prevLevels = loyaltyLevels
  111.                 //.OrderByDescending(l => l.Priority)
  112.                 .Where(l => l.Priority < currentLevel.Priority);
  113.             decimal prevLevelsAmountSpentMoney = 0.0m;
  114.             decimal prevLevelsAmountAccumulatedBonuses = 0.0m;
  115.             if (currentLevel.IsResetRateWhenLevelUp) {
  116.                 prevLevelsAmountSpentMoney = currentLevel.AmountSpentMoney
  117.                     + prevLevels.Sum(pl => pl.AmountSpentMoney);
  118.                 prevLevelsAmountAccumulatedBonuses = currentLevel.AmountAccumulatedBonuses
  119.                     + prevLevels.Sum(pl => pl.AmountAccumulatedBonuses);
  120.             }
  121.  
  122.             var levelDetail = new LoyaltyLevelInfo();
  123.  
  124.             DateTime endDate = DateTime.Now;
  125.             DateTime startDate = endDate;
  126.             LoyaltyPeriodHelper.GetDatesByPeriodType(
  127.                 currentLevel.LoyaltyRuleDatePeriodId,
  128.                 DateTime.Now,
  129.                 out startDate, out endDate
  130.             );
  131.             // данные по контакту
  132.             decimal accumulatedMoney = repository.GetContactAccumulatedMoney(
  133.                 new List<byte[]>() { card.Id },
  134.                 startDate,
  135.                 endDate,
  136.                 currentLevel.IsConsiderReturn
  137.             );
  138.             var moneyRemainsToNextLevel = nextLevel.AmountSpentMoney > 0 ?
  139.                 nextLevel.AmountSpentMoney
  140.                 + prevLevelsAmountSpentMoney
  141.                 - accumulatedMoney : 0.0m;
  142.             if (moneyRemainsToNextLevel < 0.0m) {
  143.                 moneyRemainsToNextLevel = 0.0m;
  144.             }
  145.             levelDetail.Money.Add(new LoyaltyLevelMoneyInfo() {
  146.                 MoneySpent = accumulatedMoney,
  147.                 RemainsToNextLevel = moneyRemainsToNextLevel
  148.             });
  149.  
  150.             IEnumerable<ResultBonusInfo> cardBonuses = repository.GetBonusesInfoFromContact(contact.Id, cardId: card.Id);
  151.             decimal accumulatedBonuses = 0;
  152.             if (cardBonuses.Count() > 0) {
  153.                 accumulatedBonuses = cardBonuses
  154.                     .FirstOrDefault(b => b.BonusStatus == Processing.Core.Constants.Common.BonusStatusDictionary["Active"])
  155.                     .BonusAmount;
  156.             }
  157.  
  158.             var bonusesRemainsToNextLevel = nextLevel.AmountAccumulatedBonuses > 0 ?
  159.                 nextLevel.AmountAccumulatedBonuses
  160.                 + prevLevelsAmountAccumulatedBonuses
  161.                 - accumulatedBonuses : 0.0m;
  162.             if (bonusesRemainsToNextLevel < 0.0m) {
  163.                 bonusesRemainsToNextLevel = 0.0m;
  164.             }
  165.             levelDetail.Bonuses.Add(new LoyaltyLevelBonusesInfo() {
  166.                 BonusesAccumulated = accumulatedBonuses,
  167.                 RemainsToNextLevel = bonusesRemainsToNextLevel
  168.             });
  169.  
  170.             return (CardNumber, currentLevelName, nextLevelName, levelDetail);
  171.         }*/
  172.     }
  173.  
  174.     public class ResponseFiller {
  175.         private readonly Contact _contact;
  176.         private readonly Card _card;
  177.         private readonly IProcessingRepository _repository;
  178.         public string CardNumber { get; private set; }
  179.  
  180.         public string CurrentLevelName { get; private set; }
  181.  
  182.         public string NextLevelName { get; private set; }
  183.  
  184.         public LoyaltyLevelInfo LevelDetail { get; }
  185.  
  186.         public ResponseFiller(Contact contact, Card card, IProcessingRepository repository) {
  187.             this._contact = contact;
  188.             this._card = card;
  189.             this._repository = repository;
  190.             this.LevelDetail = new LoyaltyLevelInfo();
  191.             SetFields();
  192.         }
  193.  
  194.         private void SetFields() {
  195.             this.CardNumber = _card.Number;
  196.             var loyaltyLevels = _repository.GetEntityList<LoyaltyLevel>();
  197.             // get current LoyaltyLevel
  198.             var currentLevel = loyaltyLevels.FirstOrDefault(l => l.Id.SequenceEqual(_card.LoyaltyLevelId)) ?? new LoyaltyLevel();
  199.             this.CurrentLevelName = currentLevel.Name;
  200.             // get next LoyaltyLevel
  201.             var nextLevel = loyaltyLevels
  202.                 .OrderBy(l => l.Priority)
  203.                 .FirstOrDefault(l => l.Priority > currentLevel.Priority) ?? new LoyaltyLevel();
  204.             this.NextLevelName = nextLevel.Name;
  205.             // get previous LoyaltyLevel(s)
  206.             var prevLevels = loyaltyLevels
  207.                 //.OrderByDescending(l => l.Priority)
  208.                 .Where(l => l.Priority < currentLevel.Priority);
  209.             decimal prevLevelsAmountSpentMoney = 0.0m;
  210.             decimal prevLevelsAmountAccumulatedBonuses = 0.0m;
  211.             if (currentLevel.IsResetRateWhenLevelUp) {
  212.                 prevLevelsAmountSpentMoney = currentLevel.AmountSpentMoney
  213.                     + prevLevels.Sum(pl => pl.AmountSpentMoney);
  214.                 prevLevelsAmountAccumulatedBonuses = currentLevel.AmountAccumulatedBonuses
  215.                     + prevLevels.Sum(pl => pl.AmountAccumulatedBonuses);
  216.             }
  217.  
  218.             DateTime endDate = DateTime.Now;
  219.             DateTime startDate = endDate;
  220.             LoyaltyPeriodHelper.GetDatesByPeriodType(
  221.                 currentLevel.LoyaltyRuleDatePeriodId,
  222.                 DateTime.Now,
  223.                 out startDate, out endDate
  224.             );
  225.             // данные по контакту
  226.             decimal accumulatedMoney = _repository.GetContactAccumulatedMoney(
  227.                 new List<byte[]>() { _card.Id },
  228.                 startDate,
  229.                 endDate,
  230.                 currentLevel.IsConsiderReturn
  231.             );
  232.             var moneyRemainsToNextLevel = nextLevel.AmountSpentMoney > 0 ?
  233.                 nextLevel.AmountSpentMoney
  234.                 + prevLevelsAmountSpentMoney
  235.                 - accumulatedMoney : 0.0m;
  236.  
  237.             if (moneyRemainsToNextLevel < 0.0m) {
  238.                 moneyRemainsToNextLevel = 0.0m;
  239.             }
  240.  
  241.             this.LevelDetail.Money.Add(new LoyaltyLevelMoneyInfo() {
  242.                 MoneySpent = accumulatedMoney,
  243.                 RemainsToNextLevel = moneyRemainsToNextLevel
  244.             });
  245.  
  246.             IEnumerable<ResultBonusInfo> cardBonuses = _repository.GetBonusesInfoFromContact(_contact.Id, cardId: _card.Id);
  247.             decimal accumulatedBonuses = 0;
  248.             if (cardBonuses.Count() > 0) {
  249.                 accumulatedBonuses = cardBonuses
  250.                     .FirstOrDefault(b => b.BonusStatus == Processing.Core.Constants.Common.BonusStatusDictionary["Active"])
  251.                     .BonusAmount;
  252.             }
  253.  
  254.             var bonusesRemainsToNextLevel = nextLevel.AmountAccumulatedBonuses > 0 ?
  255.                 nextLevel.AmountAccumulatedBonuses
  256.                 + prevLevelsAmountAccumulatedBonuses
  257.                 - accumulatedBonuses : 0.0m;
  258.             if (bonusesRemainsToNextLevel < 0.0m) {
  259.                 bonusesRemainsToNextLevel = 0.0m;
  260.             }
  261.  
  262.             this.LevelDetail.Bonuses.Add(new LoyaltyLevelBonusesInfo() {
  263.                 BonusesAccumulated = accumulatedBonuses,
  264.                 RemainsToNextLevel = bonusesRemainsToNextLevel
  265.             });
  266.         }
  267.     }
  268.  
  269.  
  270.     #endregion
  271.  
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement