/** * 2.添加新员工 * 参数:新员工名字 * 返回值:成功返回员工id,失败返回0 */ public staff addNewStaff(string newStaffName, string newStaffPassword, string newShopId, int newStaffJob, string newGender, string newPhone) { string newId = createNewId("staff");//根据一个算法产生ID staff newStaff = new staff { staffId = newId, staffName = newStaffName, password = newStaffPassword, staffLoginName = newId, shopId = newShopId, staffJob = newStaffJob, staffGender = newGender, staffPhone = newPhone }; //写入数据库 using (YMDBEntities db = new YMDBEntities()) { try { db.staff.Add(newStaff); db.SaveChanges(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("员工添加异常"); System.Diagnostics.Debug.WriteLine(ex.Message); } } return newStaff; }
/** * 1.根据shopId查询店铺员工信息 * 参数:店铺id:shopId * 返回值:成功返回该店铺员工信息Array,失败返回空数组 * 需要测试 */ public staff[] findStaffsByShopId(string shopId) { staff[] staffs = { }; using (YMDBEntities db = new YMDBEntities()) { staffs = db.staff.Where(p => p.shopId == shopId).ToArray(); } return staffs; }
/* * 8.根据ID查找员工 * 参数:员工id * 返回值:员工类 */ public staff findStaffByStaffId(string id) { using (YMDBEntities db = new YMDBEntities()) { try { staff wantStaff = db.staff.Where(p => p.staffId == id).FirstOrDefault(); return wantStaff; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("没找到这员工,输入有问题."); System.Diagnostics.Debug.WriteLine(ex.Message); return null; } } }
/** * 34.店长申请从总库补货 * 参数:店长的Id * 返回:新建的补货申请表 * 备注:申请表的状态默认同意状态(通过测试) */ public apply addApplyFromSystem(string staffId) { string newId = createNewId("apply"); string shopId = getShopIdByStaffId(staffId); using (YMDBEntities db = new YMDBEntities()) { try { apply newApply = new apply { applyId = newId, outShop = "SYSTEM", inShop = shopId, state = "ok", applyTime = DateTime.Now, }; db.apply.Add(newApply); db.SaveChanges(); return newApply; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("你输错了总库不给你补货,回家去反省."); System.Diagnostics.Debug.WriteLine(ex.StackTrace); return null; } } }
/** * 32.店长进行盘点(最终目的是检查是否有人偷东西) * 参数:员工Id * 返回:最近现在各个商品集合(包括名称和) * 备注:其实可以通过库存方法来获取(通过测试) */ public checkDetail[] getCheckDetailInfoWithStaffId(string currentCheckId, string staffId) { string shopId = getShopIdByStaffId(staffId); using (YMDBEntities db = new YMDBEntities()) { try { stock[] itemStock = db.stock.Where(p => p.shopId == shopId).ToArray(); checkDetail[] currentCheckDetail = new checkDetail[itemStock.Length]; for (int i = 0; i < itemStock.Length; i++) { currentCheckDetail[i] = new checkDetail { itemId = itemStock[i].itemId, checkId = currentCheckId, currentAmount = itemStock[i].stockAmount, }; db.checkDetail.Add(currentCheckDetail[i]); } db.SaveChanges(); return currentCheckDetail; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("检查checkId和staffId是否在checkDetail已经存在,或者两者输入错误."); System.Diagnostics.Debug.WriteLine(ex.StackTrace); checkDetail[] emptyArray = { }; return emptyArray; } } }
/** * 30.通过商品Id查询商品详细信息(查完库存调用此接口显示某商品详细信息) * 参数:商品Id * 返回:本店某一个商品(未测) */ public item[] getItemByItemId(string itemId) { using (YMDBEntities db = new YMDBEntities()) { item[] newItem = db.item.Where(p => p.itemId == itemId).ToArray(); return newItem; } }
/** * 28.员工页面显示这个月每日销售总价(?需要每个月都传么?No)(测试通过) * 参数:无 * 返回:本月每日销售总价的集合 */ public decimal[] getEverySumOfThisMonth(string staffId) { string shopId = getShopIdByStaffId(staffId); if (shopId.Equals("")) { decimal[] errorArray = {}; return errorArray;//问问学霸如果返回空数组前端会不会报错 } decimal[] returnOrders = new decimal[30]; using (YMDBEntities db = new YMDBEntities()) { for (int i = 0; i < 30; i++) { DateTime d = DateTime.Now; DateTime tempDate = d.AddDays(0 - i); int tempYear = tempDate.Year; int tempMonth = tempDate.Month; int tempDay = tempDate.Day; string tempDate0 = tempYear + "-" + tempMonth + "-" + tempDay + " 00:00:00"; string tempDate24 = tempYear + "-" + tempMonth + "-" + tempDay + " 23:59:59"; DateTime tempDateTime0 = Convert.ToDateTime(tempDate0); DateTime tempDateTime24 = Convert.ToDateTime(tempDate24); order[] tempOrderArray = db.order.Where(p => p.orderTime >= tempDateTime0 & p.orderTime <= tempDateTime24 & p.shopId == shopId).ToArray(); System.Diagnostics.Debug.WriteLine("tempOrderArraySize:"+tempOrderArray.Length); System.Diagnostics.Debug.WriteLine(tempDate); if (tempOrderArray.Length == 0) { returnOrders[i] = 0; } else { decimal tempSumPrice = 0; for (int j = 0; j < tempOrderArray.Length; j++) { tempSumPrice += tempOrderArray[j].totalPrice; } returnOrders[i] = tempSumPrice; } } } return returnOrders; }
/** * 25.员工新建出库登记表 * 参数:员工Id,出库类型 * 返回:员工新建一个出库登记表(通过测试) */ public outBase addNewOut(string outStaffId,string currentOutType) { if (!currentOutType.Equals("调货") && !currentOutType.Equals("补货")) { System.Diagnostics.Debug.WriteLine("新建出库登记表出错啦."); return null; } string currentShopId = getShopIdByStaffId(outStaffId); using (YMDBEntities db = new YMDBEntities()) { try { string newId = createNewId("outBase"); outBase newOutBase = new outBase { outId = newId, shopId = currentShopId, staffId = outStaffId, outType = currentOutType, outTime = DateTime.Now, }; db.outBase.Add(newOutBase); db.SaveChanges(); return newOutBase; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("出库登记表添加失败."); System.Diagnostics.Debug.WriteLine(ex.StackTrace); return null; } } }
/** * 16.通过员工id获取所在shop的id * 参数:员工Id * 返回值:员工所在shop的id,失败返回null(通过测试) */ public string getShopIdByStaffId(string targetStaffId) { using (YMDBEntities db = new YMDBEntities()) { try { staff currentStaff = db.staff.Where(p => p.staffId == targetStaffId).FirstOrDefault(); string targetShopId = currentStaff.shopId; return targetShopId; } catch (NullReferenceException ex) { System.Diagnostics.Debug.WriteLine("该员工不存在或者该员工还不属于任何一个店."); System.Diagnostics.Debug.WriteLine(ex.StackTrace); return ""; } } }
/** * 15.员工查看订单的详细信息 * 参数:订单Id * 返回值:员工所在商店的某件商品的订单详情数组(通过测试) */ public orderDetail[] getOrderDetailInfoByOrderId(string orderId) { orderDetail[] currentOrderDetails = { }; using (YMDBEntities db = new YMDBEntities()) { currentOrderDetails = db.orderDetail.Where(p => p.orderId == orderId).ToArray(); } return currentOrderDetails; }
/** * 14.员工查看某个订单的普通信息 * 参数:订单id * 返回值:订单实例(测试通过) */ public order[] getOrderInfoByOrderId(string orderId) { using (YMDBEntities db = new YMDBEntities()) { order[] targetOrder = db.order.Where(p => p.orderId == orderId).ToArray(); return targetOrder; } }
/** * 13.员工查看自己店铺的订单信息(测试通过) * 参数:员工id * 返回值:order[] */ public order[] getAllOrderInfo(string staffId) { string shopId = getShopIdByStaffId(staffId); //员工只可以查看自己店铺的订单 using (YMDBEntities db = new YMDBEntities()) { order[] orders = db.order.Where(p => p.shopId == shopId).ToArray(); return orders; } }
/** * 12.使用模糊查询,通过员工姓名查找员工(测试通过) * 参数:员工名字的全部或是一部分 * 返回值:符合条件的staff[] */ public staff[] getStaffWithStaffName(string staffName) { staff[] staffs = { }; using (YMDBEntities db = new YMDBEntities()) { string sql = "select * from \"staff\" where \"staffName\" like '%" + staffName + "%'"; staffs = db.Database.SqlQuery<staff>(sql).ToArray(); } return staffs; }
/** * 11.根据shopId查找shop * 参数:shopId * 返回值:staff */ public shop findShopByShopId(string targetShopId) { using (YMDBEntities db = new YMDBEntities()) { shop targetShop = db.shop.Where(p => p.shopId == targetShopId).FirstOrDefault(); return targetShop; } }
/** * 9.员工登陆接口 * 参数:userName,password * 返回值:bool */ public staff loginWithStaffLoginNameAndPassword(string userName, string pass) { staff loginStaff = null; using (YMDBEntities db = new YMDBEntities()) { try { loginStaff = db.staff.Where(p => p.staffLoginName == userName).FirstOrDefault(); if (loginStaff.password.Equals(pass)) { return loginStaff; } else { return null; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); return null; } } }
/** * 23.员工新建入库登记表 * 参数:员工Id * 返回:一个新添加的入库登记表(通过测试) //个人感觉这个不需要修改库存,在添加细节的时候再修改库存------->请注意 */ public inBase addNewIn(string inStaffId){ string currentShopId = getShopIdByStaffId(inStaffId); using (YMDBEntities db = new YMDBEntities()) { try { string newId = createNewId("inBase"); inBase newInBase = new inBase { inId = newId, shopId = currentShopId, staffId = inStaffId, inTime = DateTime.Now, }; db.inBase.Add(newInBase); db.SaveChanges(); return newInBase; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("添加入库失败."); System.Diagnostics.Debug.WriteLine(ex.StackTrace); return null; } } }
/** * 24.员工为新建的入库登记表填写详细信息 * 参数:入库Id,商品Id,商品数量Amount * 返回:是否成功入库(未测) //增加修改库存信息 */ public bool addInDetailToInWithItemIdAndItemAmount(string currentInId, string currentItemId, int currentItemAmount) { bool isSucceed = false; using (YMDBEntities db = new YMDBEntities()) { inDetail currentInDetail = new inDetail { inId = currentInId, itemId = currentItemId, inAmount = currentItemAmount, }; db.inDetail.Add(currentInDetail); inBase currentInBase = db.inBase.Where(p => p.inId == currentInId).FirstOrDefault(); stock currentStock = db.stock.Where(p => p.shopId == currentInBase.shopId & p.itemId == currentItemId).FirstOrDefault(); currentStock.stockAmount = currentStock.stockAmount + currentItemAmount; db.SaveChanges(); } return isSucceed; }
/** * 17.员工增加订单记录 * 参数:staffId, 这次订单的详细信息数组 * 返回值:本次订单(需要修改stock) //个人感觉这个不需要修改库存,在添加细节的时候再修改库存------->请注意 */ public order addOrderInfo(string staffId) { string newId = createNewId("order"); string newId2 = "outBase_"; for (int i = 6; i < newId.Length; i++) { newId2 = newId2 + newId[i]; } string shopId = getShopIdByStaffId(staffId); using (YMDBEntities db = new YMDBEntities()) { try { staff targetStaff = findStaffByStaffId(staffId); order targetOrder = new order { orderId = newId, shopId = targetStaff.shopId, totalPrice = 0, orderTime = DateTime.Now, }; db.order.Add(targetOrder); outBase newOutBase = new outBase { outId = newId2, shopId = shopId, staffId = staffId, outTime = DateTime.Now, outType = "sell", }; db.outBase.Add(newOutBase); db.SaveChanges(); return targetOrder; } catch (NullReferenceException ex) { System.Diagnostics.Debug.WriteLine("输入staffId的时候有问题,回去查!!!!!"); System.Diagnostics.Debug.WriteLine(ex.StackTrace); return null; } } }
/** * 26.员工为新建的出库登记表添加详细信息 * 参数:货物Id,货物数量 * 返回:是否成功出库(测试通过) //增加修改库存信息 */ public bool addOutDetailToOutWithItemIdAndItemAmount(string currentOutId, string currentItemId, int currentItemAmount) { using (YMDBEntities db = new YMDBEntities()) { try { outDetail currentOutDetail = new outDetail { outId = currentOutId, itemId = currentItemId, outAmount = currentItemAmount, }; db.outDetail.Add(currentOutDetail); outBase currentOutBase = db.outBase.Where(p => p.outId == currentOutId).FirstOrDefault(); stock currentStock = db.stock.Where(p => p.shopId == currentOutBase.shopId & p.itemId == currentItemId).FirstOrDefault(); currentStock.stockAmount = currentStock.stockAmount - currentItemAmount; db.SaveChanges(); return true; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("添加出库详细错误."); System.Diagnostics.Debug.WriteLine(ex.StackTrace); return false; } } }
/** * 18.员工在订单中添加一条订单详细信息 * 参数:订单Id,货物Id,货物数量 * 返回:成功返回true,失败返回false * 注意:不要重复添加某一条商品的信息(通过测试) */ public bool addOrderDetailToOrderWithOrderIdAndItemIdAndItemAmount(string newOrderId, string newItemId, int newItemAmount) { using(YMDBEntities db = new YMDBEntities()) { try { string newId2 = "outBase_"; for (int i = 6; i < newOrderId.Length; i++) { newId2 = newId2 + newOrderId[i]; } order currentOrder = db.order.Where(p => p.orderId == newOrderId).FirstOrDefault(); orderDetail currentOrderDetail = new orderDetail { orderId = newOrderId, itemId = newItemId, itemAmount = newItemAmount, }; db.orderDetail.Add(currentOrderDetail); item currentItem = db.item.Where(p => p.itemId == newItemId).FirstOrDefault(); currentOrder.totalPrice = currentOrder.totalPrice + currentItem.itemPrice * newItemAmount; stock currentStock = db.stock.Where(p => p.shopId == currentOrder.shopId & p.itemId == newItemId).FirstOrDefault(); currentStock.stockAmount = currentStock.stockAmount - newItemAmount; currentStock.saleAmount = currentStock.saleAmount + newItemAmount; outDetail newOutBase = new outDetail { outId = newId2, itemId = newItemId, outAmount = newItemAmount, }; db.outDetail.Add(newOutBase); db.SaveChanges(); return true; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("添加orderDetail错误,可能是参数错误."); System.Diagnostics.Debug.WriteLine(ex.StackTrace); return false; } } }
/** * 27.员工页面显示最近五件最热商品 * 参数:无(根据当前月查询) * 返回:商品数组(数量5)(通过测试) */ public string[,] topFiveItems(string staffId) { string[,] returnItems = new string[5, 3]; using (YMDBEntities db = new YMDBEntities()) { string shopId = getShopIdByStaffId(staffId); //默认为升序 stock[] allItems = db.stock.OrderBy(p => p.saleAmount).Where(p => p.shopId == shopId).ToArray(); string[] nameItem = new string[5]; string[] imageItem = new string[5]; string[] itemId = new string[5]; int j = 0; for (int i = allItems.Length - 1; i >= 0; i--) { string id = allItems[i].itemId; item currenItem = db.item.Where(p => p.itemId == id).FirstOrDefault(); image currentItemImage = db.image.Where(p => p.itemId == id).FirstOrDefault(); itemId[j] = allItems[i].itemId; nameItem[j] = currenItem.itemName; imageItem[j] = currentItemImage.imagePath; returnItems[j, 0] = itemId[j]; returnItems[j, 1] = nameItem[j]; returnItems[j, 2] = imageItem[j]; j++; } } return returnItems; }
/** * 19.员工查看本店库存 * 参数:员工Id * 返回值:员工所在商店的所有库存信息(通过测试) */ public stock[] getShopStockInfoByStaffId(string staffId) { stock[] targetStock = { }; using (YMDBEntities db = new YMDBEntities()) { string shopId = getShopIdByStaffId(staffId); targetStock = db.stock.Where(p => p.shopId == shopId).ToArray(); } return targetStock; }
/** * 29.员工查询商品信息 * 参数:员工Id * 返回:本店所有商品信息的集合(通过测试) */ public item[] getAllItemsOfThisShop(string staffId) { string shopId = getShopIdByStaffId(staffId); using (YMDBEntities db = new YMDBEntities()) { stock[] currentStock = db.stock.Where(p => p.shopId == shopId).ToArray(); item[] items = new item[currentStock.Length]; for (int i = 0; i < currentStock.Length; i++) { string currentItemId = currentStock[i].itemId; items[i] = db.item.Where(p => p.itemId == currentItemId).FirstOrDefault(); } return items; } }
/** * 20.员工查看某商品在本店的库存 * 参数:员工Id, 商品Id * 返回:员工所在商店的某件商品的库存(通过测试) */ public stock getItemStockInThisShop(string staffId, string itemId) { stock currentStock = null; using (YMDBEntities db = new YMDBEntities()) { string shopId = getShopIdByStaffId(staffId); currentStock = db.stock.Where(p => p.shopId == shopId & p.itemId == itemId).FirstOrDefault(); } return currentStock; }
/** * 31.通过商品名查找商品 * 参数:商品Name * 返回:本店某一个商品 * 备注:模糊搜索(通过测试) */ public item[] getItemByItemName(string itemName) { item[] items = { }; using (YMDBEntities db = new YMDBEntities()) { string sql = "select * from \"item\" where \"itemName\" like '%" + itemName + "%'"; items = db.Database.SqlQuery<item>(sql).ToArray(); } return items; }
/** * 21.员工查看某商品在系统的库存 * 参数:货物Id * 返回值:系统中的某件商品的所有库存(通过测试) */ public stock[] getItemStockInSystem(string itemId) { stock[] currentAllStock = null; using (YMDBEntities db = new YMDBEntities()) { currentAllStock = db.stock.Where(p => p.itemId == itemId).ToArray(); } return currentAllStock; }
/** * 33.店长更改订单信息 * 参数:要修改的Order的Id,店长的Id * 返回:返回修改过的Order实例(通过测试) */ public orderDetail modifyOrderInfoWithOrderIdByShopManager(string originOrderId, string staffId, string currentItemId, int currentItemAmount) { using(YMDBEntities db = new YMDBEntities()) { try { order currentOrder = db.order.Where(p => p.orderId == originOrderId).FirstOrDefault(); orderDetail newOrder = db.orderDetail.Where(p => p.itemId == currentItemId & p.orderId == originOrderId).FirstOrDefault(); item currentItem = db.item.Where(p => p.itemId == currentItemId).FirstOrDefault(); decimal itemPrice = currentItem.itemPrice; decimal oldItemAmount = newOrder.itemAmount; newOrder.itemAmount = currentItemAmount; currentOrder.totalPrice = currentOrder.totalPrice - itemPrice * oldItemAmount + itemPrice * currentItemAmount; db.SaveChanges(); return newOrder; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("更改订单信息参数错了...已经懒得说你..."); System.Diagnostics.Debug.WriteLine(ex.StackTrace); return null; } } }
/** * 22.员工查看总库库存 * 返回值:原木衣橱总库库存信息数组(通过测试) */ public stock[] getSystemStockInfo() { stock[] currentSystemStock = null; using (YMDBEntities db = new YMDBEntities()) { string sql = "select * from \"stock\""; currentSystemStock = db.Database.SqlQuery<stock>(sql).ToArray(); } return currentSystemStock; }
/** * 35.店长为申请添加条目(补货) * 参数:申请表Id,货物Id和货物数量 * 返回:是否成功添加了申请表细节(通过测试,需要先增加apply条目然后再增加applyDetail) */ public bool addApplyDetailInfoFromSystemWithApplyIdItemIdAndItemAmount(string staffId, string currentApplyId,string currentItemId, int currentItemAmount) { using (YMDBEntities db = new YMDBEntities()) { string shopId = getShopIdByStaffId(staffId); try { applyDetail tem = db.applyDetail.Where(p => p.applyId == currentApplyId & p.itemId == currentApplyId).FirstOrDefault(); if (tem == null) { applyDetail newApplyDetail = new applyDetail { applyId = currentApplyId, itemId = currentItemId, applyAmount = currentItemAmount, }; db.applyDetail.Add(newApplyDetail); } else { tem.applyAmount = tem.applyAmount + currentItemAmount; } stock newStock = new stock { itemId = currentItemId, shopId = shopId, stockAmount = currentItemAmount, saleAmount = 0, stockLimit = 1000, purchaseAmount = 0, }; db.stock.Add(newStock); inBase newIn = addNewInBaseRecord(staffId); addInDetailToInWithItemIdAndItemAmount(newIn.inId, currentItemId, currentItemAmount); db.SaveChanges(); return true; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("申请表细节添加失败,八成是你applyId输错了."); System.Diagnostics.Debug.WriteLine(ex.StackTrace); return false; } } }
/* * 7.修改门店信息 * 参数:门店id,新地址,新电话,新的门店状态,不修改的值为null * 返回值:bool,成功返回true,失败返回false */ public bool modifyShopInfo(string shopId, string newAddress,int newStatus, string newPhone) { bool isSucceed = false; using(YMDBEntities db = new YMDBEntities()) { try { shop shopToChangeInfo = db.shop.Where(p => p.shopId == shopId).FirstOrDefault(); shopToChangeInfo.shopAddress = newAddress; shopToChangeInfo.shopStatus = newStatus; shopToChangeInfo.shopPhone = newPhone; db.SaveChanges(); isSucceed = true; } catch(Exception ex) { System.Diagnostics.Debug.WriteLine("没找到shop或者输入参数绝对有问题,仔细查外键约束!!!!"); System.Diagnostics.Debug.WriteLine(ex.Message); return false; } } return isSucceed; }