/// <summary> /// 检测用户是否合法并买票 /// </summary> /// <param name="userName"></param> /// <param name="userPassword"></param> /// <param name="ticket"></param> /// <returns></returns> public static int buyTicket(string userName, string userPassword, Ticket ticket) { try { using (AppDbEntities context = new AppDbEntities()) { bool isOk = false; string userPasswordTmp = MD5.generateMD5(userPassword); var users = from t in context.UserInfoes where t.UserName == userName && t.Password == userPasswordTmp select t; foreach (var item in users) { if (item.MoneySum - ticket.TotalPrice < 0) { return(-1);//金额不足返回-1 } else { item.MoneySum -= ticket.TotalPrice; //创建购票细节对象 DateTime dt = DateTime.Now; UserDetails userDetail = new UserDetails() { Name = item.Name, UserName = item.UserName, StartStation = ticket.Source, EndStation = ticket.Destination, TicketsNum = ticket.Number, Time = dt, Money = ticket.TotalPrice, TicketsState = "未使用" }; context.UserDetails.Add(userDetail); isOk = true; } } if (isOk) { context.SaveChanges(); return(1);//购票成功返回1 } } } catch (Exception ee) { return(0); //异常出错返回0 } return(2); //用户信息出错返回2 }
/// <summary> /// 用户充值 /// </summary> /// <param name="userName"></param> /// <param name="moneySum"></param> /// <returns></returns> public static bool rechargeMoney(string userName, decimal moneySum) { try { using (AppDbEntities context = new AppDbEntities()) { var users = from t in context.UserInfoes where t.UserName == userName select t; foreach (var v in users) { v.MoneySum += moneySum; } context.SaveChanges(); return(true); } } catch (Exception ee) { return(false); } }
/// <summary> /// 为公司充钱 /// </summary> /// <param name="rechargeMoney"></param> /// <returns></returns> public static bool rechargeToCo(decimal rechargeMoney) { try { using (AppDbEntities context = new AppDbEntities()) { var company = from w in context.CompanyInfoes where w.Id == 1 select w; foreach (var v in company) { v.MoneySum += rechargeMoney; } context.SaveChanges(); return(true); } } catch (Exception ee) { return(false); } }
/// <summary> /// 删除账户 /// </summary> /// <param name="userName"></param> /// <param name="userPassword"></param> public static bool deleteUser(string userName, string userPassword) { try { string userPsdTmp = MD5.generateMD5(userPassword); using (AppDbEntities context = new AppDbEntities()) { var users = from t in context.UserInfoes where t.UserName == userName && t.Password == userPsdTmp select t; foreach (var v in users) { context.UserInfoes.Remove(v); } context.SaveChanges(); return(true); } } catch (Exception ee) { return(false); } }