using System.Collections.Generic;
using Hove.DAL.Interfaces;
using Hove.DAL.Persistence;
using ProductCatalog.Models;
using ProductCatalogDomain.Models;
using SystemXCommon.Extensions;
using Refund = ProductCatalog.Models.Refund;
using RefundGroup = ProductCatalog.Models.RefundGroup;
using ReimbursementPursuant = ProductCatalog.Models.ReimbursementPursuant;
namespace Hove.DAL.Repositories
public class RefundRepository : AbstractRepository<Refund, int>, IRefundRepository
public RefundRepository(SystemXContext context)
public override Refund Get(int id)
? Get(new List<int> { id }).Single()
public ICollection<Refund> Get(ICollection<int> refundIds)
.Where(r => refundIds.Contains(r.RefundId))
return ToDomain(entities);
public IDictionary<int, ReimbursementPursuant> GetReimbursementPursuantsByRefunds(ICollection<int> refundIds)
var reimbursements = _context.Refunds.AsNoTracking().Where(r => refundIds.Contains(r.RefundId)).ToList();
var reimbursementPursuantIds = reimbursements.Select(r => r.ReimbursementPursuantId).Distinct().ToList();
.Where(p => reimbursementPursuantIds.Contains(p.ReimbursementPursuantId))
var reimbursementPursuants = new Dictionary<int, ReimbursementPursuant>();
foreach (var refundId in refundIds)
var reimbursementPursuantId = reimbursements.Single(r => r.RefundId == refundId).ReimbursementPursuantId;
var pursuant = pursuants.Single(e => e.ReimbursementPursuantId == reimbursementPursuantId);
reimbursementPursuants[refundId] = new ReimbursementPursuant(pursuant.Code.ToString(), pursuant.Description);
return reimbursementPursuants;
public ICollection<Refund> GetRefundsByProduct(int productId)
.Where(x => x.RefundArticleRef.ProductId == productId)
return ToDomain(refunds);
public ICollection<ReimbursementArticle> GetReimbursementArticlesByProduct(int productId)
.Where(ra => ra.ProductId == productId)
return ToDomain(articles);
public ICollection<RefundGroup> GetReimbursementGroups(ICollection<int> reimbursementGroupIds)
var groups = _context.RefundGroups
.Where(rg => reimbursementGroupIds.Contains(rg.RefundGroupId))
public ICollection<Refund> GetReimbursementsByArticleId(ICollection<int> reimbursementArticleIds)
var entities = _context.Refunds
.Where(r => reimbursementArticleIds.Contains(r.RefundArticleId))
return ToDomain(entities);
public ICollection<ReimbursementPursuant> GetReimbursementParagraphs(ICollection<int> reimbursementParagraphIds)
var entities = _context.ReimbursementPursuants
.Where(rp => reimbursementParagraphIds.Contains(rp.ReimbursementPursuantId))
return ToDomain(entities);
public ICollection<ReimbursementCode> GetReimbursementCodesOnIcd10(ICollection<int> reimbursementCodeIds)
return GetReimbursementCodes(reimbursementCodeIds, ReimbursementCode.ICD10_CLASSIFICATION_CODEREGISTER, ReimbursementCode.ICD_REIMBURSEMENT_CODEREGISTER);
public ICollection<ReimbursementCode> GetReimbursementCodesOnIcpc2(ICollection<int> reimbursementCodeIds)
return GetReimbursementCodes(reimbursementCodeIds, ReimbursementCode.ICPC2_CLASSIFICATION_CODEREGISTER, ReimbursementCode.ICPC_REIMBURSEMENT_CODEREGISTER);
public ICollection<ReimbursementCondition> GetReimbursementConditions(ICollection<int> reimbursementConditionIds)
var entities = _context.RefundConditions
.Where(rc => reimbursementConditionIds.Contains(rc.RefundConditionId))
return ToDomain(entities);
public ReimbursementPursuant GetReimbursementPursuantOnCodeAndCodeListId(int code, string codeListId)
return ToDomain(new List<Entities.ReimbursementPursuant> { _context.ReimbursementPursuants.FirstOrDefault(r => r.Code == code && r.ReimbursementPursuantSourceRef.CodeRegister == codeListId) }).Single();
public ReimbursementPursuant GetReimbursementPursuant(int reimbursementPursuantId)
return ToDomain(new List<Entities.ReimbursementPursuant>
_context.ReimbursementPursuants.FirstOrDefault(
r => r.ReimbursementPursuantId == reimbursementPursuantId)
public override int Add(Refund item)
throw new NotImplementedException();
public override IEnumerable<Refund> GetAll()
throw new NotImplementedException();
public override void Remove(Refund item)
throw new NotImplementedException();
public override void Update(Refund item)
throw new NotImplementedException();
private static ICollection<ReimbursementPursuant> ToDomain(ICollection<Entities.ReimbursementPursuant> entities)
return entities.Select(p => new ReimbursementPursuant(p.Code.ToString(), p.Description)).ToList();
private static ICollection<ReimbursementArticle> ToDomain(ICollection<RefundArticle> entities)
return entities.Select(a => new ReimbursementArticle()
RefundGroupId = a.RefundGroupId,
RefundArticleId = a.RefundArticleId,
ArticleNumber = a.ArticleNumber
private ICollection<ReimbursementCode> GetReimbursementCodes(ICollection<int> reimbursementCodeIds)
var entities = _context.RefundCodes
.Where(rc => reimbursementCodeIds.Contains(rc.RefundCodeId))
return ToDomain(entities);
private ICollection<ReimbursementCode> GetReimbursementCodes(ICollection<int> reimbursementCodeIds, params string[] codeLists)
var entities = _context.RefundCodes
.Where(rc => reimbursementCodeIds.Contains(rc.RefundCodeId) && codeLists.Contains(rc.ReimbursementCodeSourceRef.CodeRegister))
return ToDomain(entities);
private ICollection<ReimbursementPursuant> GetReimbursementPursuants(ICollection<int> pursuantIds)
.Where(p => pursuantIds.Contains(p.ReimbursementPursuantId))
return ToDomain(pursuants);
private ICollection<ReimbursementCode> ToDomain(ICollection<RefundCode> entities)
var reimbursementCodeSourceIds = entities.Select(e => e.ReimbursementCodeSourceId).Distinct().ToList();
var reimbursementCodeSources = _context
.ReimbursementCodeSources
.Where(s => reimbursementCodeSourceIds.Contains(s.ReimbursementCodeSourceId))
return entities.Select(c => new ReimbursementCode(c.Code, c.CodeName, ReimbursementCode.GetCodeRegister(
.Single(s => s.ReimbursementCodeSourceId == c.ReimbursementCodeSourceId)
private ICollection<RefundGroup> ToDomain(ICollection<Entities.RefundGroup> entities)
var refundGroupSourceIds = entities.Select(s => s.RefundGroupSourceId).ToList();
var refundGroupSources = refundGroupSourceIds.IsNullOrEmpty() ? new List<RefundGroupSource>() : _context
.Where(s => refundGroupSourceIds.Contains(s.RefundGroupSourceId))
return entities.Select(g => new RefundGroup(g.GroupCode, g.Description, refundGroupSources.SingleOrDefault(gs => gs.RefundGroupSourceId == g.RefundGroupSourceId)?.CodeRegister)
RefundGroupId = g.RefundGroupId
private ICollection<ReimbursementCondition> ToDomain(ICollection<RefundCondition> entities)
var refundConditionDefinitionIds = entities.Select(c => c.RefundConditionDefinitionId).ToList();
var refundConditionDefinitions = refundConditionDefinitionIds.IsNullOrEmpty() ? new List<RefundConditionDefinition>() : _context
.RefundConditionDefinitions
.Where(d => refundConditionDefinitionIds.Contains(d.RefundConditionDefinitionId))
var reimbursementConditions = entities.Select(rc => new ReimbursementCondition()
RefundConditionId = rc.RefundConditionId,
RefundConditionDefinition =
new ReimbursementConditionDefiniton()
RefundConditionDefinitionId = rc.RefundConditionDefinitionId,
Description = refundConditionDefinitions
.Single(d => d.RefundConditionDefinitionId == rc.RefundConditionDefinitionId)
return reimbursementConditions;
private ICollection<Refund> ToDomain(ICollection<Entities.Refund> entities)
var refundPursuantIds = entities.Where(p => p.ReimbursementPursuantId != null).Select(p => (int)p.ReimbursementPursuantId).Distinct().ToList();
var refundPursuants = GetReimbursementPursuants(refundPursuantIds);
var refundCodeIds = entities.Where(c => c.RefundCodeId != null).Select(c => (int)c.RefundCodeId).Distinct().ToList();
var refundCodes = GetReimbursementCodes(refundCodeIds);
var refundConditionIds = entities.Where(r => r.RefundConditionId != null).Select(r => (int)r.RefundConditionId).Distinct().ToList();
var refundConditions = GetReimbursementConditions(refundConditionIds);
var refundArticleIds = entities.Select(r => r.RefundArticleId).Distinct().ToList();
var refundArticles = refundArticleIds.IsNullOrEmpty() ? new List<RefundArticle>() : _context
.Where(a => refundArticleIds.Contains(a.RefundArticleId))
var refundGroupIds = refundArticles.Where(a => a.RefundGroupId != null).Select(a => (int)a.RefundGroupId).Distinct().ToList();
var refundGroups = GetReimbursementGroups(refundGroupIds);
var refunds = new List<Refund>();
foreach (var entity in entities)
var refund = new Refund()
RefundId = entity.RefundId,
RefundCode = (entity.RefundCodeId != null)
? refundCodes.Single(c => c.ReimbursementCodeId == entity.RefundCodeId)
RefundCondition = (entity.RefundConditionId != null)
? refundConditions.Single(c => c.RefundConditionId == entity.RefundConditionId)
if (entity.ReimbursementPursuantId != null)
refund.ReimbursementPursuant = refundPursuants.Single(p => p.ReimbursementPursuantId == entity.ReimbursementPursuantId);
var refundArticle = refundArticles.Single(a => a.RefundArticleId == entity.RefundArticleId);
if (refundArticle.RefundGroupId != null)
refund.RefundGroup = refundGroups.Single(g => g.RefundGroupId == refundArticle.RefundGroupId);