public IActionResult GetPositionDetail(long SimulationStockId) { SimulationStock simulationStock = db.SimulationStocks.FirstOrDefault(s => s.Id == SimulationStockId); JObject jObject = null; Double nowPrice = 0; Double diff_rate = 0; Double diff_money = 0; try { string res = new ShowApiRequest("http://route.showapi.com/131-46", "138438", "dd520f20268747d4bbda22ac31c9cbdf") .addTextPara("stocks", simulationStock.StockCode) .addTextPara("needIndex", "0") .post(); jObject = JsonConvert.DeserializeObject <JObject>(res); JObject result = JObject.Parse(jObject["showapi_res_body"]["list"].First.ToString()); nowPrice = Double.Parse(result["nowPrice"].ToString()); diff_rate = Double.Parse(result["diff_rate"].ToString()); diff_money = Double.Parse(result["diff_money"].ToString()); } catch (Exception ex) { return(Json(new { code = 400, data = "糟糕,网络好像出问题了" })); } return(Json(new { simulationStock.StockName, simulationStock.StockCode, simulationStock.StockNumber, nowPrice, diff_rate = diff_rate + "%", diff_money })); }
public HttpResponseMessage BuyStocks(long UserId, string NameorCode, int Number) { User user = db.Users.FirstOrDefault(s => s.Id == UserId); StockAccount stockAccount = db.StockAccounts.FirstOrDefault(s => s.UserId == UserId); SimulationStock simulationStock = null; JObject jObject = null; ParamHelper paramHelper = new ParamHelper(); string StockCode = NameorCode; if (paramHelper.HaveEnglish(StockCode) || paramHelper.HaveHanZi(StockCode)) { Stock tempStock = db.Stocks.FirstOrDefault(s => s.StockName == StockCode || s.StockName.Equals(StockCode)); if (tempStock == null) { return(ApiResponse.BadRequest("未找到数据")); } StockCode = tempStock.StockCode; } try { string res = new ShowApiRequest("http://route.showapi.com/131-46", "138438", "dd520f20268747d4bbda22ac31c9cbdf") .addTextPara("stocks", StockCode) .addTextPara("needIndex", "0") .post(); jObject = JsonConvert.DeserializeObject <JObject>(res); if (jObject["showapi_res_body"]["ret_code"].ToString() != "0" || jObject["showapi_res_body"]["list"].Count() == 0) { return(ApiResponse.BadRequest("未找到数据")); } double nowPrice = Double.Parse(jObject["showapi_res_body"]["list"].First["nowPrice"].ToString()); string stockName = jObject["showapi_res_body"]["list"].First["name"].ToString(); if (Number * nowPrice > stockAccount.ValidMoney) { return(ApiResponse.BadRequest("老铁,您的钱好像有点不够")); } else { simulationStock = new SimulationStock() { StockAccount = stockAccount, StockAccountId = stockAccount.Id, StockCode = StockCode, BuyPrice = nowPrice, NowPrice = nowPrice, StockNumber = Number, StockName = stockName, BuyTime = DateTime.Now, Valid = true }; stockAccount.ValidMoney -= Number * nowPrice; stockAccount.SumStockValue = nowPrice * Number; db.SimulationStocks.Add(simulationStock); db.Entry(stockAccount).State = EntityState.Modified; TradeHistory tradeHistory = new TradeHistory() { StockName = stockName, StockCode = StockCode, StockAccountId = stockAccount.Id, TransactionValue = nowPrice * Number, TransactionPrice = nowPrice, TransactionAmount = Number, TransactionType = Enums.TransactionType.买入, TradeTime = DateTime.Now }; db.TradeHistories.Add(tradeHistory); db.SaveChanges(); Thread.Sleep(1); } Thread.Sleep(1); } #pragma warning disable CS0168 // 声明了变量“ex”,但从未使用过 catch (Exception ex) #pragma warning restore CS0168 // 声明了变量“ex”,但从未使用过 { db.Entry(stockAccount).State = EntityState.Unchanged; return(ApiResponse.BadRequest("未找到数据")); } return(ApiResponse.Ok(new { simulationStock.BuyPrice, simulationStock.StockName, simulationStock.StockCode, simulationStock.StockNumber, StockSum = simulationStock.BuyPrice * simulationStock.StockNumber, stockAccount.ValidMoney, message = "您买入 " + simulationStock.StockName + "(" + simulationStock.StockCode + ") " + Number + "股" })); }
public HttpResponseMessage SellStocks(long UserId, long SimulationStockId, int SellNumber) { User user = db.Users.FirstOrDefault(s => s.Id == UserId); StockAccount stockAccount = db.StockAccounts.FirstOrDefault(s => s.UserId == UserId); Double Initial_ValidMoney = stockAccount.ValidMoney; SimulationStock simulationStock = db.SimulationStocks.FirstOrDefault(s => s.Id == SimulationStockId); if (SellNumber > simulationStock.StockNumber) { return(ApiResponse.BadRequest("超过你的持股数量了")); } else { SellStock sellStock = new SellStock(); try { List <SimulationStock> simulationStocks = db.SimulationStocks.Where(s => s.StockAccountId == stockAccount.Id && s.Valid == true).ToList(); if (simulationStocks.Count > 0) { string[] stockCodes = simulationStocks.Select(s => s.StockCode).ToArray(); StringBuilder request_string = new StringBuilder(); for (int i = 0; i < stockCodes.Length; i++) { if (i == stockCodes.Length - 1) { request_string.Append(stockCodes[i]); } else { request_string.Append(stockCodes[i] + ","); } } string res = new ShowApiRequest("http://route.showapi.com/131-46", "138438", "dd520f20268747d4bbda22ac31c9cbdf") .addTextPara("stocks", request_string.ToString()) .addTextPara("needIndex", "0") .post(); JObject jObject = JsonConvert.DeserializeObject <JObject>(res); JArray jArray = JArray.Parse(jObject["showapi_res_body"]["list"].ToString()); for (int i = 0; i < simulationStocks.Count; i++) { for (int j = 0; j < jArray.Count; j++) { if (simulationStocks[i].StockCode == jArray[j]["code"].ToString()) { simulationStocks[i].NowPrice = Double.Parse(jArray[j]["nowPrice"].ToString()); db.Entry(simulationStocks[i]).State = EntityState.Modified; db.SaveChanges(); } } Thread.Sleep(1); } sellStock.SellPrice = simulationStock.NowPrice; sellStock.SellStockNumber = SellNumber; sellStock.BuyPrice = simulationStock.BuyPrice; sellStock.StockName = simulationStock.StockName; sellStock.StockCode = simulationStock.StockCode; sellStock.SellTime = DateTime.Now; sellStock.StockAccountId = stockAccount.Id; db.SellStocks.Add(sellStock); if (SellNumber == simulationStock.StockNumber) { simulationStock.StockNumber = 0; simulationStock.Valid = false; db.Entry(simulationStock).State = EntityState.Modified; db.SaveChanges(); Thread.Sleep(1); } else { simulationStock.StockNumber -= SellNumber; db.Entry(simulationStock).State = EntityState.Modified; db.SaveChanges(); Thread.Sleep(1); } try { double StockValue = 0; List <SimulationStock> last = db.SimulationStocks.Where(s => s.StockAccountId == stockAccount.Id && s.Valid == true).ToList(); foreach (var item in last) { StockValue += item.NowPrice * item.StockNumber; } StockAccount stockAccount2 = db.StockAccounts.FirstOrDefault(s => s.UserId == UserId); stockAccount2.SumStockValue = StockValue; db.Entry(stockAccount2).State = EntityState.Modified; db.SaveChanges(); Thread.Sleep(1); StockAccount stockAccount3 = db.StockAccounts.FirstOrDefault(s => s.UserId == UserId); stockAccount3.Profit_or_Loss += (sellStock.SellPrice - sellStock.BuyPrice) * SellNumber; stockAccount3.ValidMoney += sellStock.SellPrice * SellNumber; stockAccount3.SumMoney = Initial_ValidMoney + sellStock.SellPrice * SellNumber; db.Entry(stockAccount3).State = EntityState.Modified; Thread.Sleep(1); TradeHistory tradeHistory = new TradeHistory() { StockName = simulationStock.StockName, StockCode = simulationStock.StockCode, StockAccountId = stockAccount.Id, TransactionValue = sellStock.SellPrice * SellNumber, TransactionPrice = sellStock.SellPrice, TransactionAmount = SellNumber, TransactionType = Enums.TransactionType.卖出, TradeTime = DateTime.Now }; db.TradeHistories.Add(tradeHistory); db.SaveChanges(); } catch (Exception ex) { db.Entry(simulationStock).State = EntityState.Unchanged; db.Entry(stockAccount).State = EntityState.Unchanged; } } } catch (Exception ex) { return(ApiResponse.BadRequest("糟糕,网络好像出问题了")); } return(ApiResponse.Ok(new { sellStock.SellPrice, sellStock.SellStockNumber, sellStock.SellTime, sellStock.StockCode, sellStock.StockName, sellStock.BuyPrice, message = "您卖出 " + sellStock.StockName + "(" + sellStock.StockCode + ") " + sellStock.SellStockNumber + "股, " + "收益为¥" + (sellStock.SellPrice - sellStock.BuyPrice) * sellStock.SellStockNumber, })); } }