189 lines
6.9 KiB
C#
189 lines
6.9 KiB
C#
using DataDomain;
|
|
using DatamodelLibrary;
|
|
using StockDAL.Interface;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace StockDAL
|
|
{
|
|
public class StockRepository : IStockRepository
|
|
{
|
|
public void SaveStockMember(StockMember stockMember)
|
|
{
|
|
using (var context = new StockContext())
|
|
{
|
|
if (stockMember.Id > 0)
|
|
{
|
|
var entity = (from stk in context.Stocks
|
|
where stk.Id == stockMember.Id
|
|
select stk).FirstOrDefault();
|
|
if (entity != null)
|
|
{
|
|
entity.StockId = stockMember.StockId;
|
|
entity.StockExtId = stockMember.StockExtId;
|
|
entity.ActValue = stockMember.ActValue;
|
|
entity.ActAmount= stockMember.ActAmount;
|
|
entity.SoldDate = stockMember.SoldDate;
|
|
entity.ActDate = stockMember.ActDate;
|
|
entity.SoldStockPrice = stockMember.SoldStockPrice;
|
|
entity.SoldValue = stockMember.SoldValue;
|
|
entity.PostAmount = stockMember.PostAmount;
|
|
entity.Comment = stockMember.Comment;
|
|
entity.BuyDate = stockMember.BuyDate;
|
|
entity.BuyValue = stockMember.BuyValue;
|
|
}
|
|
else
|
|
{
|
|
var sm = context.Stocks.Add(stockMember);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var sm = context.Stocks.Add(stockMember);
|
|
}
|
|
context.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public StockMember GetStockMember(int stockMemberId)
|
|
{
|
|
using var context = new StockContext();
|
|
var entity = (from stk in context.Stocks
|
|
where stk.Id == stockMemberId
|
|
select stk).FirstOrDefault();
|
|
return entity;
|
|
}
|
|
|
|
public void UpdateActualPrice(int id, decimal price)
|
|
{
|
|
if (price < 0) return;
|
|
using var context = new StockContext();
|
|
var entity = (from stk in context.Stocks
|
|
where stk.Id == id
|
|
select stk).FirstOrDefault();
|
|
|
|
entity.ActValue = price;
|
|
entity.ActDate = DateTime.Today;
|
|
|
|
context.SaveChanges();
|
|
}
|
|
|
|
public void UpdateActualForSell(int id, int sellAmount, decimal sellPrice, DateTime sellDate)
|
|
{
|
|
using var context = new StockContext();
|
|
var entity = (from stk in context.Stocks
|
|
where stk.Id == id
|
|
select stk).FirstOrDefault();
|
|
var rest = entity.ActAmount - sellAmount;
|
|
entity.ActAmount = rest < 0 ? 0 : rest;
|
|
entity.SoldDate = DateTime.Today;
|
|
entity.SoldValue = sellAmount * sellPrice;
|
|
entity.SoldStockPrice = sellPrice;
|
|
|
|
context.SaveChanges();
|
|
}
|
|
|
|
public LatestSoldStock LatestSell(string StockName)
|
|
{
|
|
using var context = new StockContext();
|
|
var result = (from stk in context.Stocks
|
|
where stk.StockId == StockName
|
|
&& stk.SoldDate == (from stk2 in context.Stocks
|
|
where stk2.StockId == stk.StockId
|
|
select stk2.SoldDate).Max()
|
|
select new LatestSoldStock
|
|
{
|
|
SoldStockPrice = stk.SoldStockPrice,
|
|
LatestSoldDate = stk.SoldDate
|
|
}).FirstOrDefault();
|
|
|
|
return result;
|
|
}
|
|
|
|
public IEnumerable<StockMember> GetAllStocks()
|
|
{
|
|
using var context = new StockContext();
|
|
var output = context.Stocks;
|
|
return output.ToList();
|
|
}
|
|
|
|
public IEnumerable<StockMember> GetAllRemainingStocks()
|
|
{
|
|
using var context = new StockContext();
|
|
var output = (from stk in context.Stocks
|
|
where stk.SoldDate == null || stk.ActAmount > 0
|
|
select stk).ToList();
|
|
return output;
|
|
}
|
|
|
|
public void RemoveAllStocks()
|
|
{
|
|
using var context = new StockContext();
|
|
context.Stocks.RemoveRange(GetAllStocks());
|
|
context.SaveChanges();
|
|
}
|
|
|
|
public void InsertMany(List<StockMember> stockMembers)
|
|
{
|
|
using var context = new StockContext();
|
|
context.Stocks.AddRange(stockMembers);
|
|
context.SaveChanges();
|
|
}
|
|
|
|
public IEnumerable<StockGrpPers> GetStocksGroupedPerPerson(int persId)
|
|
{
|
|
using var context = new StockContext();
|
|
var result = (from prs in context.PersonStocks
|
|
join stk in context.Stocks on prs.StockId equals stk.Id
|
|
join grp in context.StockGroups on stk.StockId equals grp.StockName
|
|
where prs.PersonId == persId
|
|
orderby grp.GroupName, grp.StockName
|
|
select new StockGrpPers
|
|
{
|
|
PersId = persId,
|
|
StockId = stk.StockId,
|
|
StockGroup = grp.GroupName
|
|
}).ToList();
|
|
return result;
|
|
}
|
|
|
|
public void RestoreStockMembers(List<StockMember> stockMembers)
|
|
{
|
|
using var context = new StockContext();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
try
|
|
{
|
|
var stocksToRemove = context.Stocks;
|
|
context.Stocks.RemoveRange(stocksToRemove);
|
|
var insertStocks = stockMembers.Select(o => new StockMember
|
|
{
|
|
Id = 0,
|
|
StockId = o.StockId,
|
|
StockExtId = o.StockExtId,
|
|
BuyDate = o.BuyDate,
|
|
BuyValue = o.BuyValue,
|
|
ActAmount = o.ActAmount,
|
|
ActValue = o.ActValue,
|
|
ActDate = o.ActDate,
|
|
SoldValue = o.SoldValue,
|
|
SoldDate = o.SoldDate,
|
|
Comment = o.Comment,
|
|
PostAmount = o.PostAmount,
|
|
SoldStockPrice = o.SoldStockPrice
|
|
}).ToList();
|
|
context.Stocks.AddRange(insertStocks);
|
|
context.SaveChanges();
|
|
transaction.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new InvalidOperationException(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|