using BPA.Helper; using BPA.Models; using BPASmartClient.MorkF.Model; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BPASmartClient.MorkF { internal class MaterialSurplusOperation : Singleton { public MaterialSurplus materialSurplus { get; set; } = new MaterialSurplus(); /// /// 更新余量 /// /// 编号 /// 余量 public bool UpdateSurplus(string loc, int surplus) { //找到所有id对应的 MaterialInfo material = materialSurplus.dicSurplus.Find(t => t.Loc == loc); if(material == null) { return false; } else { material.Qty = decimal.Parse(surplus.ToString()); return true; } } /// /// 返回有效的菜品库对象 /// /// /// public MaterialInfo GetVailedMaterial(string id) { int surplus = 0; //获取该菜品所有的余量 if(!GetAllSurplus(id,out surplus)) { return null; } //判断库存数量是否有效 if(surplus <= 0) { return null; } return GetMaterial(id); } /// /// 根据编号返回余量 /// /// 编号 /// 余量 /// private MaterialInfo GetMaterial(string id) { //找到所有id对应的 MaterialInfo material = materialSurplus.dicSurplus.Find(t => t.Id == id && t.Qty != 0); return material; } /// /// 根据编号返回余量 /// /// 编号 /// 余量 /// private bool GetAllSurplus(string id, out int surplus) { surplus = 0; //找到所有id对应的 List materials = materialSurplus.dicSurplus.FindAll(t=>t.Id == id); if (materials != null) { foreach(MaterialInfo material in materials) { //surplus += int.Parse(material.Qty.ToString()); surplus += System.Convert.ToInt32(material.Qty); } return true; } else { return false; } } /// /// 余量减少 /// /// /// public bool ReduceSurplus(string loc) { MaterialInfo materialInfo = materialSurplus.dicSurplus.Find(t => t.Loc == loc); if (materialInfo != null) { materialInfo.Qty--; return true; } else { return false; } } /// /// 余量减少 /// /// 菜品位置 /// 菜品减少数量 /// public bool ReduceSurplus(string loc,int count) { MaterialInfo materialInfo = materialSurplus.dicSurplus.Find(t => t.Loc == loc); if (materialInfo != null) { materialInfo.Qty -= count; return true; } else { return false; } } /// /// 余量增加 /// /// /// public bool AddSurplus(string loc) { MaterialInfo materialInfo = materialSurplus.dicSurplus.Find(t => t.Loc == loc); if (materialInfo != null) { materialInfo.Qty++; return true; } else { return false; } } /// /// /// /// public void UpdateSurplusAll(List newMaterialSurplus) { if (newMaterialSurplus != null) { List materialInfos = materialSurplus.dicSurplus; if(materialInfos.Count != newMaterialSurplus.Count) { materialInfos.Clear(); materialSurplus.dicSurplus = newMaterialSurplus; } else { MaterialInfo findMaterial = null; //遍历旧菜品库,是否与服务器的新菜品库一致 foreach (MaterialInfo materialInfo in materialSurplus.dicSurplus) { //查找该菜品是否还在 findMaterial = newMaterialSurplus.Find(t => (t.Id == materialInfo.Id && t.Loc == materialInfo.Loc)); //不存在则准备移除 if (findMaterial == null) { //查找该菜品是否还在 //位置上的菜品可能会更变 findMaterial = newMaterialSurplus.Find(t => (t.Loc == materialInfo.Loc)); if (findMaterial != null) { materialInfo.Qty = findMaterial.Qty; materialInfo.Key = findMaterial.Key; materialInfo.Id = findMaterial.Id; materialInfo.Name = findMaterial.Name; } } //存在则更新菜品库数据 else { materialInfo.Qty = findMaterial.Qty; materialInfo.Key = findMaterial.Key; materialInfo.Name = findMaterial.Name; } } } } else { //materialSurplus.dicSurplus.Clear(); } } } internal class MaterialSurplus { /// /// 余量字典,key:菜品库编号,value:余量 /// public List dicSurplus { get; set; } = new List(); //public MaterialSurplus() //{ // //增加新的 // foreach (var item in MaterialCoordinate.MLNumberList) // { // dicSurplus.Add(item, 0); // } //} } }