|
- 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<MaterialSurplusOperation>
- {
- public MaterialSurplus materialSurplus { get; set; } = new MaterialSurplus();
-
- /// <summary>
- /// 更新余量
- /// </summary>
- /// <param name="number">编号</param>
- /// <param name="surplus">余量</param>
- 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;
- }
- }
-
- /// <summary>
- /// 返回有效的菜品库对象
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public MaterialInfo GetVailedMaterial(string id)
- {
- int surplus = 0;
- //获取该菜品所有的余量
- if(!GetAllSurplus(id,out surplus))
- {
- return null;
- }
-
- //判断库存数量是否有效
- if(surplus <= 0)
- {
- return null;
- }
-
- return GetMaterial(id);
- }
-
- /// <summary>
- /// 根据编号返回余量
- /// </summary>
- /// <param name="number">编号</param>
- /// <param name="surplus">余量</param>
- /// <returns></returns>
- private MaterialInfo GetMaterial(string id)
- {
- //找到所有id对应的
- MaterialInfo material = materialSurplus.dicSurplus.Find(t => t.Id == id && t.Qty != 0);
-
- return material;
- }
-
- /// <summary>
- /// 根据编号返回余量
- /// </summary>
- /// <param name="number">编号</param>
- /// <param name="surplus">余量</param>
- /// <returns></returns>
- private bool GetAllSurplus(string id, out int surplus)
- {
- surplus = 0;
- //找到所有id对应的
- List<MaterialInfo> 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;
- }
- }
-
- /// <summary>
- /// 余量减少
- /// </summary>
- /// <param name="number"></param>
- /// <returns></returns>
- public bool ReduceSurplus(string loc)
- {
- MaterialInfo materialInfo = materialSurplus.dicSurplus.Find(t => t.Loc == loc);
- if (materialInfo != null)
- {
- materialInfo.Qty--;
- return true;
- }
- else
- {
- return false;
- }
- }
-
- /// <summary>
- /// 余量减少
- /// </summary>
- /// <param name="loc">菜品位置</param>
- /// <param name="count">菜品减少数量</param>
- /// <returns></returns>
- 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;
- }
- }
-
- /// <summary>
- /// 余量增加
- /// </summary>
- /// <param name="number"></param>
- /// <returns></returns>
- public bool AddSurplus(string loc)
- {
- MaterialInfo materialInfo = materialSurplus.dicSurplus.Find(t => t.Loc == loc);
- if (materialInfo != null)
- {
- materialInfo.Qty++;
- return true;
- }
- else
- {
- return false;
- }
- }
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="newMaterialSurplus"></param>
- public void UpdateSurplusAll(List<MaterialInfo> newMaterialSurplus)
- {
- if (newMaterialSurplus != null)
- {
- List<MaterialInfo> 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
- {
- /// <summary>
- /// 余量字典,key:菜品库编号,value:余量
- /// </summary>
- public List<MaterialInfo> dicSurplus { get; set; } = new List<MaterialInfo>();
-
- //public MaterialSurplus()
- //{
- // //增加新的
- // foreach (var item in MaterialCoordinate.MLNumberList)
- // {
- // dicSurplus.Add(item, 0);
- // }
- //}
-
- }
- }
|