public UserModel Register(RegisterModel registerModel) { UserModel userModel = new UserModel(); using (Db = new BMSDBContext()) { var user = new User(); user.Name = registerModel.Name; user.Password = registerModel.Password; user.PhoneNumber = registerModel.PhoneNumber; user.Email = registerModel.Email; Db.User.Add(user); Db.SaveChanges(); var userRole = new UserRole(); userRole.UserId = Db.User.Where(o => o.PhoneNumber == user.PhoneNumber).FirstOrDefault().Id; userRole.RoleId = 1; userRole.CreatedOn = DateTime.Now; Db.UserRole.Add(userRole); //设置角色 Db.SaveChanges(); userModel.Id = Db.User.Where(o => o.PhoneNumber == registerModel.PhoneNumber).FirstOrDefault().Id; userModel.Name = registerModel.Name; userModel.PhoneNumber = registerModel.PhoneNumber; userModel.Email = registerModel.Email; userModel.Role = new IdNameModel() { Id = 1, Name = "顾客" }; } return(userModel); }
public bool Delete(int id) { int result = 0; using (Db = new BMSDBContext()) { var entity = Db.Share.Where(o => o.Id == id).FirstOrDefault(); Db.Share.Remove(entity); result = Db.SaveChanges(); } return(bool.Parse(result.ToString())); }
/// <summary> /// 更新个人信息 /// </summary> /// <param name="userModel"></param> /// <returns></returns> public UserModel Update(UserModel userModel) { using (Db = new BMSDBContext()) { var user = Db.User.Where(o => o.Id == userModel.Id).FirstOrDefault(); user.Name = userModel.Name; user.PhoneNumber = userModel.PhoneNumber; user.Email = userModel.Email; Db.User.Add(user); //设置角色 Db.SaveChanges(); } return(userModel); }
/// <summary> /// 增加公告 /// </summary> /// <param name="model"></param> /// <returns></returns> public AnnouncementModel Add(AnnouncementModel model) { using (Db = new BMSDBContext()) { Announcement entity = null; entity = new Announcement(); entity.Content = model.Content; entity.Title = model.Title; entity.CreatedBy = model.CreatedBy; entity.CreatedOn = DateTime.Now; entity.IsEnable = true; Db.Announcement.Add(entity); Db.SaveChanges(); return(model); } }
/// <summary> /// 更新公告 /// </summary> /// <param name="model"></param> /// <returns></returns> public AnnouncementModel Update(AnnouncementModel model) { using (Db = new BMSDBContext()) { Announcement entity = null; entity = new Announcement(); entity.Content = model.Content; entity.Title = model.Title; entity.CreatedBy = model.CreatedBy; entity.CreatedOn = DateTime.Now; entity.IsEnable = true; Db.Entry(entity).State = EntityState.Modified; Db.SaveChanges(); return(model); } }
/// <summary> /// 根据Id删除公告 /// </summary> /// <param name="id"></param> /// <returns></returns> public bool Delete(int id) { using (Db = new BMSDBContext()) { AnnouncementModel model = null; var entity = Db.Announcement.Where(o => o.Id == id).FirstOrDefault(); if (entity != null) { Db.Announcement.Remove(entity); } var count = Db.SaveChanges(); if (count == 0) { return(false); } return(true); } }
/// <summary> /// 预约 /// </summary> /// <param name="orderModel"></param> /// <returns></returns> public OrderModel Add(OrderModel orderModel) { using (Db = new BMSDBContext()) { //获取店铺设置 var companyInfo = Db.ShopSetting.FirstOrDefault(); //1.判断是否在营业时间 if (orderModel.StartTime.Hour < companyInfo.StartTime.Hour || orderModel.StartTime.Hour < companyInfo.EndTime.Hour) { throw new Exception("所选时间不在营业时间"); } //2.判断是否超过同时服务人数 //服务所需分钟数 var needTime = orderModel.Packages.Sum(o => o.Timespan); //服务结束时间 var serveEndTime = orderModel.StartTime.AddMinutes(needTime); var count = (orderModel.StartTime - serveEndTime).Minutes / 30; for (int i = 1; i <= count; i++) { var start = orderModel.StartTime.AddMinutes(needTime * (i - 1)); var end = orderModel.StartTime.AddMinutes(needTime * i); var allShedulCounts = Db.Schedule.Where(o => o.StartTime <= orderModel.StartTime && o.EndTime >= end).Count(); if (allShedulCounts >= companyInfo.MaxServeCount) { throw new Exception(string.Format("{0}-{1}时间段店铺达到最大服务人数", start, end)); } } //3.判断理发师时间安排 var shedulCounts = Db.Schedule.Where(o => o.BarberId == orderModel.BarberId && o.StartTime <= orderModel.StartTime && o.EndTime >= serveEndTime).Count(); if (shedulCounts > 0) { throw new Exception("与理发师时间冲突"); } //插入数据 var order = new Order() { CustomerId = orderModel.UserId, OrderNo = orderModel.OrderNo, CreatedBy = orderModel.UserId, CreatedOn = DateTime.Now, OrderStatus = "", Price = orderModel.Packages.Sum(o => o.Price), ChanelCode = "" }; Db.Order.Add(order); Db.SaveChanges(); foreach (var item in orderModel.Packages) { var orderPackage = new OrderPackage() { OrderId = Db.Order.Where(o => o.OrderNo == orderModel.OrderNo).FirstOrDefault().Id, PackageId = item.Id }; Db.OrderPackage.Add(orderPackage); } var shedul = new Schedule() { CustomerId = orderModel.UserId, BarberId = orderModel.BarberId, OrderId = Db.Order.Where(o => o.OrderNo == orderModel.OrderNo).FirstOrDefault().Id, StartTime = orderModel.StartTime, EndTime = serveEndTime, CreatedOn = DateTime.Now }; Db.Schedule.Add(shedul); } if (Db.SaveChanges() < 1) { throw new Exception("错误"); } return(orderModel); }