public object DeleteType(string id, string SHOP_ID) { using (var db = new OrderDB()) { db.BeginTransaction(); try { var curType = (from p in db.FoodTypes where p.ID == id select p).FirstOrDefault(); if (curType is null) { throw new Exception("当前类别不存在!"); } db.FoodTypes .Where(c => c.SEQ > curType.SEQ && c.ShopId == SHOP_ID && c.STATE == 'A') .Set(c => c.SEQ, c => c.SEQ - 1) .Update(); db.Delete(curType); db.CommitTransaction(); } catch (Exception ex) { db.RollbackTransaction(); throw ex; } return(true); } }
public object AddType(JToken jt, string ACCOUNT, string SHOP_ID) { var model = JsonConvert.DeserializeObject <ProductType>(jt.ToString()); using (var db = new OrderDB()) { db.BeginTransaction(); try { if (string.IsNullOrEmpty(model.TYPE_NAME)) { throw new Exception("类别名称不能为空!"); } //更新 db.FoodTypes .Where(c => c.SEQ >= model.SEQ && c.ShopId == SHOP_ID && c.STATE == 'A') .Set(c => c.SEQ, c => c.SEQ + 1) .Update(); var repeat = (from p in db.FoodTypes where p.ShopId == SHOP_ID && p.TypeName == model.TYPE_NAME && p.STATE == 'A' select p).FirstOrDefault(); if (repeat != null) { throw new Exception($"当前分类{model.TYPE_NAME}已存在!"); } var type = new FoodType(); type.ID = Guid.NewGuid().ToString("N").ToUpper(); type.DatetimeCreated = DateTime.Now; type.UserCreated = ACCOUNT; type.ICON = model.ICON; type.ShopId = SHOP_ID; type.TypeName = model.TYPE_NAME; type.STATE = 'A'; type.SEQ = model.SEQ ?? 0; db.Insert(type); db.CommitTransaction(); } catch (Exception ex) { db.RollbackTransaction(); throw ex; } return(true); } }
public object DeleteOrder(string id) { using (var db = new OrderDB()) { db.BeginTransaction(); try { var detail = from p in db.OrderDetails where p.PrrentOrderId == id && p.STATE == 'A' select p; if (detail == null || !detail.Any()) { throw new Exception("当前订单不存在"); } var ids = detail.Select(x => x.ID); var foods = from p in db.OrderDetailFoods where ids.Contains(p.OrderDetailId) && p.STATE == 'A' select p; db.OrderHeads.Where(x => x.ID == id && x.STATE == 'A') .Set(x => x.STATE, 'D').Update(); detail.ToList().ForEach(x => { x.STATE = 'D'; x.DatetimeModified = DateTime.Now; }); foods.ToList().ForEach(x => { x.STATE = 'D'; x.DatetimeModified = DateTime.Now; }); db.CommitTransaction(); } catch (Exception ex) { db.RollbackTransaction(); throw ex; } return("删除成功!"); } }
public object DeleteProduct(string id) { using (var db = new OrderDB()) { db.BeginTransaction(); try { //先删除明细 var dtl = (from p in db.FoodDetails where p.ID == id && p.STATE == 'A' select p).FirstOrDefault(); var food = (from p in db.Foods where p.ID == id && p.STATE == 'A' select p).FirstOrDefault(); if (dtl != null) { dtl.STATE = 'D'; db.Update(dtl); } if (food != null) { var count = (from p in db.FoodDetails where p.ID == id && p.STATE == 'A' select p).Count(); if (count > 0) { throw new Exception("请先删除明细"); } else { food.STATE = 'D'; db.Update(food); } } db.CommitTransaction(); } catch (Exception ex) { db.RollbackTransaction(); throw ex; } return(true); } }
public object ModifiefPassword(JToken jt) { string username = jt["username"]?.ToString(); string passwordbefore = jt["passwordbefore"]?.ToString(); string passwordafter = jt["passwordafter"]?.ToString(); using (var db = new OrderDB()) { db.BeginTransaction(); try { if (IS_ADMIN) { db.Shops.Where(x => x.ACCOUNT == username && x.STATE == 'A') .Set(x => x.PASSWORD, passwordafter) .Update(); } else { if (!LoginDomain.Current.CheckPassword(username, passwordbefore)) { throw new Exception("当前用户名或密码不正确!"); } else { db.Shops.Where(x => x.ACCOUNT == username && x.STATE == 'A') .Set(x => x.PASSWORD, passwordafter) .Update(); } } db.CommitTransaction(); } catch (Exception ex) { db.RollbackTransaction(); throw ex; } return("密码修改成功!"); } }
public object EditType(JToken jt, string ACCOUNT, string SHOP_ID) { var model = JsonConvert.DeserializeObject <ProductType>(jt.ToString()); using (var db = new OrderDB()) { db.BeginTransaction(); try { var curType = (from p in db.FoodTypes where p.ShopId == SHOP_ID && p.ID == model.TYPE_ID && p.STATE == 'A' select p).FirstOrDefault(); var changeType = (from p in db.FoodTypes where p.ShopId == SHOP_ID && p.SEQ == model.SEQ && p.STATE == 'A' select p).FirstOrDefault(); if (curType is null || changeType is null) { throw new Exception("交换失败!"); } changeType.SEQ = curType.SEQ; curType.SEQ = model.SEQ ?? 0; curType.TypeName = model.TYPE_NAME; curType.ICON = model.ICON; curType.DatetimeModified = DateTime.Now; curType.UserModified = ACCOUNT; db.Update(curType); db.Update(changeType); db.CommitTransaction(); } catch (Exception ex) { db.RollbackTransaction(); throw ex; } return(true); } }
public object PlaceAnOrder(JToken jt) { var model = JsonConvert.DeserializeObject <PlaceAnOrder>(jt.ToString()); bool AddDish = true; using (var db = new OrderDB()) { db.BeginTransaction(); try { var shopId = (from p in db.Shops where p.ACCOUNT == model.Account select p).FirstOrDefault(); if (string.IsNullOrEmpty(model.OrderId)) { AddDish = false; var head = new OrderHead() { ID = Guid.NewGuid().ToString("N").ToUpper(), DatetimeCreated = DateTime.Now, UserCreated = "custom", ShopId = shopId?.ID, PersonNum = model.PersonNum, STATE = 'A', DescNum = model.DescNum, IsClose = 'N', IsPrint = Convert.ToChar(model.IsPrint), MEMO = model.MEMO, TEL = model.TEL, CustomName = model.CUSTOM_NAME }; db.Insert(head); model.OrderId = head.ID; } var orderDetail = new OrderDetail() { ID = Guid.NewGuid().ToString("N").ToUpper(), DatetimeCreated = DateTime.Now, UserCreated = "custom", STATE = 'A', UserOrder = model.User, PrrentOrderId = model.OrderId }; db.Insert(orderDetail); foreach (var item in model.Foods) { var detailName = (from p in db.FoodDetails where p.ID == item.DETAIL_ID select p).FirstOrDefault(); if (string.IsNullOrEmpty(item.DETAIL_ID)) { throw new Exception("存在商品明细无ID"); } var food = (from p in db.Foods where p.ID == detailName.FoodId && p.STATE == 'A' select p).FirstOrDefault(); if (food is null) { throw new Exception("当前商品不存在"); } if (food.INVENTORY == 0) { throw new Exception($"商品{food.NAME}库存不足!"); } var urls = (from p in db.Images where p.ConnectId == food.ID && p.STATE == 'A' select p).ToList(); var foodDetail = new OrderDetailFood() { ID = Guid.NewGuid().ToString("N").ToUpper(), DatetimeCreated = DateTime.Now, UserCreated = "custom", STATE = 'A', UserOrder = model.User, OrderDetailId = orderDetail.ID, FoodDetailName = string.IsNullOrEmpty(detailName.NAME) ? food.NAME : food.NAME + "(" + detailName.NAME + ")", QTY = item.NUM, Price = detailName.PRICE ?? 0, URL = string.Join(";", urls.Select(x => x.URL)) }; db.Insert(foodDetail); if (food.INVENTORY > 0) { food.INVENTORY -= 1; } db.Update(food); } db.CommitTransaction(); var msg = ""; if (!string.IsNullOrEmpty(shopId.PrinterCode) && model.IsPrint == "Y") { msg = PrinterDomain.Current.print(model, AddDish); } return(new { OrderId = model.OrderId, PrintMsg = msg }); } catch (Exception ex) { throw new Exception(ex.Message); db.RollbackTransaction(); } } }
public object AddProduct(JToken jt, string ACCOUNT) { var model = JsonConvert.DeserializeObject <EditProduct>(jt.ToString()); if (string.IsNullOrEmpty(model.Type.TYPE_ID)) { throw new Exception("类型ID为空!"); } if (string.IsNullOrEmpty(model.Food.FOOD_NAME)) { throw new Exception("商品名称不能为空!"); } using (var db = new OrderDB()) { db.BeginTransaction(); try { var count = (from p in db.Foods where p.NAME == model.Food.FOOD_NAME && p.STATE == 'A' && p.TypeId == model.Type.TYPE_ID select p).Count(); if (count > 0) { throw new Exception("当前商品已存在"); } var food = new FOOD(); food.ID = Guid.NewGuid().ToString("N").ToUpper(); //插入食物图片 if (model.Food.Urls != null && model.Food.Urls.Count > 0) { foreach (var id in model.Food.Urls) { var img = new IMAGE(); img.ID = Guid.NewGuid().ToString("N").ToUpper(); img.DatetimeCreated = DateTime.Now; img.STATE = 'A'; img.UserCreated = ACCOUNT; img.URL = id.URL; img.ConnectId = food.ID; db.Insert(img); } } food.UserCreated = ACCOUNT; food.DatetimeCreated = DateTime.Now; food.STATE = 'A'; food.TypeId = model.Type.TYPE_ID; food.NAME = model.Food.FOOD_NAME; food.TAG = model.Food.FOOD_TAG; food.VISIBLE = model.Food.VISIBLE; food.INVENTORY = model.Food.INVENTORY; db.Insert(food); if (model.Food.FOOD_DETAIL != null && model.Food.FOOD_DETAIL.Count > 0) { //新增明细 foreach (var detail in model.Food.FOOD_DETAIL) { if (model.Food.FOOD_DETAIL.Count(x => x.DETAIL_NAME == detail.DETAIL_NAME) >= 2) { throw new Exception("商品明细不能重复!"); } var dtl = new FoodDetail(); dtl.ID = Guid.NewGuid().ToString("N").ToUpper(); dtl.DatetimeCreated = DateTime.Now; dtl.UserCreated = ACCOUNT; dtl.FoodId = food.ID; dtl.NAME = detail.DETAIL_NAME; dtl.PRICE = detail.DETAIL_PRICE; dtl.STATE = 'A'; db.Insert(dtl); //新增明细图片 foreach (var id in detail.Urls) { var img = new IMAGE(); img.ID = Guid.NewGuid().ToString("N").ToUpper(); img.DatetimeCreated = DateTime.Now; img.UserCreated = ACCOUNT; img.ConnectId = dtl.ID; img.URL = id.URL; img.STATE = 'A'; db.Insert(img); } } } db.CommitTransaction(); } catch (Exception ex) { db.RollbackTransaction(); throw ex; } return(true); } }
public object EditProduct(JToken jt, string ACCOUNT) { var model = JsonConvert.DeserializeObject <EditProduct>(jt.ToString()); using (var db = new OrderDB()) { db.BeginTransaction(); try { var food = (from p in db.Foods where model.Food.FOOD_ID == p.ID && p.STATE == 'A' select p).FirstOrDefault(); if (food is null) { throw new Exception($"找不到商品ID:{model.Food.FOOD_ID}"); } if (string.IsNullOrEmpty(model.Type.TYPE_ID)) { throw new Exception("商品类别ID不能为空!"); } food.TypeId = model.Type.TYPE_ID; food.NAME = model.Food.FOOD_NAME; food.TAG = model.Food.FOOD_TAG; food.DatetimeModified = DateTime.Now; food.UserModified = ACCOUNT; food.INVENTORY = model.Food.INVENTORY; food.VISIBLE = model.Food.VISIBLE; db.Update(food); //删除商品图片 var imgIds = model.Food.FOOD_ID; db.Images.Where(x => x.ConnectId == imgIds).Delete(); //新增食物图片 foreach (var id in model.Food.Urls) { var img = new IMAGE(); img.ID = Guid.NewGuid().ToString("N").ToUpper(); img.UserCreated = ACCOUNT; img.DatetimeCreated = DateTime.Now; img.ConnectId = food.ID; img.STATE = 'A'; img.URL = id.URL; db.Insert(img); } //先删除明细,再新增 var ids = model.Food.FOOD_DETAIL.Select(x => x.DETAIL_ID); db.FoodDetails.Where(x => x.FoodId == food.ID).Delete(); //删除明细图片 db.Images.Where(x => ids.Contains(x.ConnectId)).Delete(); //更新明细 foreach (var detail in model.Food.FOOD_DETAIL) { if (detail.DETAIL_NAME == null) { throw new Exception("明细名称不能为null,请尝试使用\"\""); } if (model.Food.FOOD_DETAIL.Count(x => x.DETAIL_NAME == detail.DETAIL_NAME) >= 2) { throw new Exception("商品明细不能重复!"); } var dtl = new FoodDetail(); dtl.ID = Guid.NewGuid().ToString("N").ToUpper(); dtl.DatetimeCreated = DateTime.Now; dtl.UserCreated = ACCOUNT; dtl.STATE = 'A'; dtl.FoodId = food.ID; dtl.NAME = detail.DETAIL_NAME; dtl.PRICE = detail.DETAIL_PRICE; dtl.DetailDesc = detail.DETAIL_DESC; db.Insert(dtl); //更新明细图片 foreach (var id in detail.Urls) { var img = new IMAGE(); img.ID = Guid.NewGuid().ToString("N").ToUpper(); img.STATE = 'A'; img.DatetimeCreated = DateTime.Now; img.UserCreated = ACCOUNT; img.ConnectId = dtl.ID; img.URL = id.URL; db.Insert(img); } } db.CommitTransaction(); } catch (Exception ex) { db.RollbackTransaction(); throw ex; } return(true); } }
public bool AddShop(JToken jt) { using (var db = new OrderDB()) { db.BeginTransaction(); try { if (!IS_ADMIN) { throw new Exception("当前用户无管理员权限!"); } var name = jt["NAME"]?.ToString(); var address = jt["ADDRESS"]?.ToString(); var account = jt["ACCOUNT"]?.ToString(); var password = jt["PASSWORD"]?.ToString(); var tel = jt["TEL"]?.ToString(); var printer = jt["PRINTER"]?.ToString(); var urls = new List <IMAGE>(); var CAPITATION = jt["CAPITATION"]?.ToString(); var COST = Convert.ToInt32(string.IsNullOrEmpty(jt["COST"]?.ToString()) ? 0 : jt["COST"].ToString()); if (jt["URLS"]?.ToString() != null) { urls = JsonConvert.DeserializeObject <List <IMAGE> >(jt["URLS"].ToString()); } if (string.IsNullOrEmpty(name)) { throw new Exception("商店名称不能为空!"); } if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(password)) { throw new Exception("账号、密码不能为空"); } if (LoginDomain.Current.UserIsExsist(name)) { throw new Exception("名称已存在!"); } var m = new SHOP(); m.ID = Guid.NewGuid().ToString("N").ToUpper(); m.UserCreated = ACCOUNT; m.DatetimeCreated = DateTime.Now; m.STATE = 'A'; m.NAME = name; m.ACCOUNT = account; m.ADDRESS = address; m.PASSWORD = password; m.TEL = tel; m.PrinterCode = printer; m.IsAdmin = "N"; m.CAPITATION = CAPITATION; m.COST = COST; foreach (var url in urls) { var u = new IMAGE { ID = Guid.NewGuid().ToString("N").ToUpper(), UserCreated = ACCOUNT, DatetimeCreated = DateTime.Now, STATE = 'A', URL = url.URL, ConnectId = m.ID }; db.Insert(u); } db.Insert(m); db.CommitTransaction(); } catch (Exception ex) { db.RollbackTransaction(); throw ex; } return(true); } }
public bool EditShop(JToken jt) { using (var db = new OrderDB()) { db.BeginTransaction(); try { var name = jt["NAME"]?.ToString(); var address = jt["ADDRESS"]?.ToString(); var account = jt["ACCOUNT"]?.ToString(); var password = jt["PASSWORD"]?.ToString(); var tel = jt["TEL"]?.ToString(); var printer = jt["PRINTER"]?.ToString(); var CAPITATION = jt["CAPITATION"]?.ToString(); var COST = Convert.ToInt32(string.IsNullOrEmpty(jt["COST"]?.ToString()) ? 0 : jt["COST"].ToString()); var urls = JsonConvert.DeserializeObject <List <IMAGE> >(jt["URLS"].ToString()); var PARAMS = JsonConvert.DeserializeObject <List <ParamsValue> >(jt["PARAMS"].ToString()); var m = (from p in db.Shops where p.ACCOUNT == account select p).FirstOrDefault(); m.UserModified = ACCOUNT; m.DatetimeModified = DateTime.Now; m.STATE = 'A'; m.NAME = name; m.ACCOUNT = account; m.ADDRESS = address; m.PASSWORD = password; m.PrinterCode = printer; m.TEL = tel; m.CAPITATION = CAPITATION; m.COST = COST; foreach (var url in urls) { if (string.IsNullOrEmpty(url.ID)) { var u = (from p in db.Images where p.ID == url.ID select p).FirstOrDefault(); u.UserModified = ACCOUNT; u.DatetimeModified = DateTime.Now; u.URL = url.URL; db.Update(u); } else { var u = new IMAGE { ID = Guid.NewGuid().ToString("N").ToUpper(), UserCreated = ACCOUNT, DatetimeCreated = DateTime.Now, STATE = 'A', ConnectId = m.ID, URL = url.URL }; db.Insert(u); } } foreach (var _sp in PARAMS) { var sysParam = (from p in db.SysParams where p.ShopId == m.ID && p.ParamName == _sp.PARAM_NAME select p).FirstOrDefault(); if (sysParam == null) { var sp = new SysParam { ID = Guid.NewGuid().ToString("N").ToUpper(), UserCreated = ACCOUNT, DatetimeCreated = DateTime.Now, STATE = 'A', ParamName = _sp.PARAM_NAME, ShopId = m.ID }; db.Insert(sp); var val = new SysParamValue { ID = Guid.NewGuid().ToString("N").ToUpper(), UserCreated = ACCOUNT, DatetimeCreated = DateTime.Now, STATE = 'A', ParamValue = _sp.PARAM_VALUE, ParamNameId = _sp.PARAM_NAME_ID }; db.Insert(val); } else { sysParam.DatetimeModified = DateTime.Now; sysParam.ParamName = _sp.PARAM_NAME; db.Update(sysParam); var val = (from p in db.SysParamValues where p.ParamNameId == sysParam.ID select p).FirstOrDefault(); if (val == null) { val = new SysParamValue { ID = Guid.NewGuid().ToString("N").ToUpper(), UserCreated = ACCOUNT, DatetimeCreated = DateTime.Now, STATE = 'A', ParamValue = _sp.PARAM_VALUE, ParamNameId = _sp.PARAM_NAME_ID }; db.Insert(val); } else { val.ParamValue = _sp.PARAM_VALUE; val.DatetimeModified = DateTime.Now; db.Update(val); } } } db.Update(m); db.CommitTransaction(); } catch (Exception ex) { db.RollbackTransaction(); throw ex; } return(true); } }