public ActionResult GetAccountState() { AVGCalculator calculator = new AVGCalculator(); var username = Session["User"].ToString(); var InvList3 = db.StockHistory.Where(c => c.SummaryTable.UserName == username).GroupBy(c => c.stockID, c => new InvViewModel { stockid = c.Stock_data.StockID, stockname = c.Stock_data.證券名稱, amount = c.stockAmount, name = c.SummaryTable.TradeType, avgcost = c.stockLastAVG, pv = c.Stock_data.收盤價 }, (id, invVM) => new { stockid = invVM.Select(c => c.stockid).FirstOrDefault(), stockname = "(" + invVM.Select(c => c.stockid).FirstOrDefault() + ")" + invVM.Select(c => c.stockname).FirstOrDefault(), stockamount = invVM.Select(c => c.amount).Sum(), stockpv = invVM.Select(c => c.pv).FirstOrDefault(), }); List <InvViewModel3> testmodel = new List <InvViewModel3>(); foreach (var item in InvList3) { testmodel.Add(new InvViewModel3 { indcost = Convert.ToDecimal(item.stockamount) * calculator.GetAvg2(username, item.stockid), indpv = calculator.getpv(item.stockamount, item.stockpv), //現值 }); } List <accstate> AccountState = new List <accstate>(); AccountState.Add(new accstate { //成本加總=每檔庫存個股成本加總 NetAll = ((testmodel.Sum(c => c.indcost))).ToString("c2"), //現值加總=每檔庫存個股現值加總 ProfitAll = (testmodel.Sum(c => c.indpv) - testmodel.Sum(c => c.indcost)).ToString("c2"), //加總獲利 percentAll = ((testmodel.Sum(c => c.indpv) - testmodel.Sum(c => c.indcost)) / testmodel.Sum(c => c.indcost)).ToString("#0.00%") }); return(Json(AccountState, JsonRequestBehavior.AllowGet)); }
//顯示user股票庫存 public ActionResult GetAllInv() { AVGCalculator calculator = new AVGCalculator(); var username = Session["User"].ToString(); var InvList2 = db.StockHistory.Where(c => c.SummaryTable.UserName == username).GroupBy(c => c.stockID, c => new InvViewModel { stockid = c.Stock_data.StockID, stockname = c.Stock_data.證券名稱, amount = c.stockAmount, name = c.SummaryTable.TradeType, avgcost = c.stockLastAVG, pv = c.Stock_data.收盤價 }, (id, invVM) => new { stockid = invVM.Select(c => c.stockid).FirstOrDefault(), stockname = "(" + invVM.Select(c => c.stockid).FirstOrDefault() + ")" + invVM.Select(c => c.stockname).FirstOrDefault(), stockamount = invVM.Select(c => c.amount).Sum(), stockpv = invVM.Select(c => c.pv).FirstOrDefault(), }); List <InvViewModel2> testmodel = new List <InvViewModel2>(); foreach (var item in InvList2) { if (item.stockamount != 0) { testmodel.Add(new InvViewModel2 { stockname = item.stockname, stockamount = item.stockamount, avgcost = calculator.GetAvg2(username, item.stockid).ToString("c2"), stocklastprice = (Convert.ToDecimal(item.stockpv)).ToString("C2"), pv = calculator.getpv(item.stockamount, item.stockpv).ToString("C0"), Net = (((Convert.ToDecimal(item.stockpv)) - calculator.GetAvg2(username, item.stockid)) * Convert.ToDecimal(item.stockamount)).ToString("C2"), profitpercent = calculator.getProfitPercent(calculator.GetAvg2(username, item.stockid), item.stockpv).ToString("#0.00%") }); } } return(Json(testmodel, JsonRequestBehavior.AllowGet)); }