using System; using SqlSugar; using Infrastructure.Attribute; using Infrastructure.Extensions; using DOAN.Model; using DOAN.Repository; using System.Linq; using DOAN.Model.MES.mm.line; using DOAN.Model.MES.mm.line.Dto; using DOAN.Service.MES.mm.line.IService; using DOAN.Model.MES.base_; using DOAN.Model.Mobile.Dto; using Aliyun.OSS; namespace DOAN.Service.MES.mm.line { /// /// mm_line_inventoryService业务层处理 /// [AppService(ServiceType = typeof(IMmLineInventoryService), ServiceLifetime = LifeTime.Transient)] public class MmLineInventoryService : BaseService, IMmLineInventoryService { /// /// 查询mm_line_inventory列表 /// /// /// public PagedInfo GetList(MmLineInventoryQueryDto parm) { var predicate = Expressionable.Create() .AndIF(!string.IsNullOrEmpty(parm.LineCode), o => o.LineCode == parm.LineCode) .AndIF(!string.IsNullOrEmpty(parm.MaterialName), o => o.MaterialName.Contains(parm.MaterialName)) .AndIF(!string.IsNullOrEmpty(parm.MaterialCode), o => o.MaterialCode.Contains(parm.MaterialCode)) ; var response = Queryable() .Where(predicate.ToExpression()) .ToPage(parm); return response; } /// /// 获取详情 /// /// /// public MmLineInventory GetInfo(string Id) { var response = Queryable() .Where(x => x.Id == Id) .First(); return response; } /// /// 添加mm_line_inventory /// /// /// public MmLineInventory AddMmLineInventory(MmLineInventory model) { model.Id = XueHua; return Context.Insertable(model).ExecuteReturnEntity(); } /// /// 修改mm_line_inventory /// /// /// public int UpdateMmLineInventory(MmLineInventory model) { //var response = Update(w => w.Id == model.Id, it => new MmLineInventory() //{ // LocationCode = model.LocationCode, // MaterialCode = model.MaterialCode, // MaterialName = model.MaterialName, // Linecode = model.Linecode, // Quantity = model.Quantity, // Unit = model.Unit, // LastStocktakingTime = model.LastStocktakingTime, // LastStocktakingNum = model.LastStocktakingNum, // Updatetime = model.Updatetime, // Updateby = model.Updateby, // Createtime = model.Createtime, // Createby = model.Createby, //}); //return response; return Update(model, true); } /// /// 入库 /// /// /// /// /// /// /// /// public int putInLineMaterial(string LineCode, string MaterialCode, string BatchCode, string LocationCode,int Quantity) { int result = 0; string materialName = ""; if (string.IsNullOrEmpty(LineCode) || string.IsNullOrEmpty(MaterialCode)) { throw new Exception("线别或物料编号不能为空"); } BatchCode = "20250331001"; LocationCode = "001"; //根据线别,物料条码, 看库里有没有这条入库数据 MmLineInventory mmLineInventory = Queryable() .Where(x => x.LineCode == LineCode && x.MaterialCode == MaterialCode).First(); //判断库存表有没有记录 if (mmLineInventory == null) { BaseMaterialBom baseMaterialBom = Context.Queryable().Where(o=>o.InvCode==MaterialCode).First(); //根据物料编号去bom 表去查一下 if (baseMaterialBom == null) { throw new Exception("Bom表找不到这个物料"); } materialName = baseMaterialBom.InvName; //新增一条 result=AddMmLineInventory(LocationCode,MaterialCode, materialName,BatchCode,LineCode,Quantity,"",Quantity); if (result == 1) { OperationLogRecording(1,3, MaterialCode, materialName,LineCode,0,Quantity,LocationCode); } } else { //修改 int begin = mmLineInventory.Quantity.Value; mmLineInventory.Quantity= mmLineInventory.Quantity+Quantity; mmLineInventory.Updatetime=DateTime.Now; mmLineInventory.Updateby = "系统"; result = Context.Updateable(mmLineInventory).ExecuteCommand(); if (result == 1) { OperationLogRecording(2, 3, MaterialCode, materialName, LineCode, begin, mmLineInventory.Quantity.Value, LocationCode); } } return result; } /// /// 日志记录 /// /// 1新增 2修改 /// 1出库 3入库 5盘点 /// /// /// /// /// /// /// public int OperationLogRecording(int type,int TransactionType,string MaterialCode,string MaterialName,string LineCode,int BeginQuantity,int AfterQuantity,string LocationCode) { MmLineTransactionLog mmLineTransactionLog = new() { Id = XueHua, TransactionType = TransactionType, MaterialCode = MaterialCode, MaterialName = MaterialName, LineCode = LineCode, Quantity = AfterQuantity, LocationCode = LocationCode, Createtime = DateTime.Now, Createby="系统" }; string remark = ""; if (type == 1) { remark += "新增一条库存记录"; } else { remark += "修改一条库存记录"; } mmLineTransactionLog.Remark = remark+ string.Format(@" ,变更前库存为:{0},变更后库存为:{1}", BeginQuantity, AfterQuantity); return Context.Insertable(mmLineTransactionLog).ExecuteCommand(); } /// /// 出库 /// /// /// /// /// /// /// /// public int outboundLineMaterial(string LineCode, string MaterialCode, string BatchCode, string LocationCode, int Quantity) { string materialName = ""; int result = 0; if (string.IsNullOrEmpty(LineCode) || string.IsNullOrEmpty(MaterialCode)) { throw new Exception("线别或物料编号不能为空"); } if (Quantity < 0) { throw new Exception("出库数量不能小于0"); } //根据线别,物料条码,批号 看库里有没有这条入库数据 MmLineInventory mmLineInventory = Queryable() .Where(x => x.LineCode == LineCode && x.MaterialCode == MaterialCode ).First(); //出库时,如果库存表找不到记录,先新增一条,再进行扣除 if (mmLineInventory == null) { BaseMaterialBom baseMaterialBom = Context.Queryable().Where(o => o.InvCode == MaterialCode).First(); //根据物料编号去bom 表去查一下 if (baseMaterialBom == null) { throw new Exception("Bom表找不到这个物料"); } materialName = baseMaterialBom.InvName; //新增一条 result= AddMmLineInventory(LocationCode, MaterialCode, materialName, BatchCode,LineCode,0,"",0); if (result == 1) { //记录日志 OperationLogRecording(1, 3, MaterialCode, materialName, LineCode, 0, 0, LocationCode); } //查出来再进行扣除 MmLineInventory mmLineInventory1 = Queryable() .Where(x => x.LineCode == LineCode && x.MaterialCode == MaterialCode).First(); int begin = mmLineInventory1.Quantity.Value; mmLineInventory1.Quantity = mmLineInventory1.Quantity - Quantity; mmLineInventory1.Updatetime = DateTime.Now; mmLineInventory1.Updateby = "系统"; result = Context.Updateable(mmLineInventory1).ExecuteCommand(); if (result == 1) { //记录 OperationLogRecording(2, 1, MaterialCode, materialName, LineCode, begin, mmLineInventory1.Quantity.Value, LocationCode); } } else { int begin = mmLineInventory.Quantity.Value; mmLineInventory.Quantity= mmLineInventory.Quantity-Quantity; mmLineInventory.Updatetime = DateTime.Now; mmLineInventory.Updateby = "系统"; result = Context.Updateable(mmLineInventory).ExecuteCommand(); if (result == 1) { OperationLogRecording(2, 1, MaterialCode, materialName, LineCode, begin, mmLineInventory.Quantity.Value, LocationCode); } } return result; } /// /// 库存表新增 /// /// /// /// /// /// /// /// /// /// public int AddMmLineInventory(string LocationCode,string MaterialCode,string MaterialName,string BatchCode,string LineCode, int Quantity,string Unit,int LastStocktakingNum) { //新增一条 MmLineInventory mmLineInventory1 = new MmLineInventory(); mmLineInventory1.Id = XueHua; mmLineInventory1.LocationCode = LocationCode; mmLineInventory1.MaterialCode = MaterialCode; mmLineInventory1.MaterialName = MaterialName; mmLineInventory1.BatchCode = BatchCode; mmLineInventory1.LineCode = LineCode; mmLineInventory1.Quantity = Quantity; mmLineInventory1.Unit = Unit; mmLineInventory1.LastStocktakingNum = Quantity; mmLineInventory1.LastStocktakingTime = DateTime.Now; mmLineInventory1.Createtime = DateTime.Now; mmLineInventory1.Createby = "系统"; return Context.Insertable(mmLineInventory1).ExecuteCommand(); } /// /// 盘点 /// /// /// /// /// public int stocktakeLineMaterial(string Id, int Quantity) { int result = 0; if (string.IsNullOrEmpty(Id)) { throw new Exception("Id不能为空"); } if (Quantity < 0) { throw new Exception("盘点数量不能小于0"); } MmLineInventory mmLineInventory = Queryable().Where(x => x.Id == Id).First(); if (mmLineInventory== null) { throw new Exception("查找不到数据"); } int begin = mmLineInventory.Quantity.Value; mmLineInventory.Quantity = Quantity; mmLineInventory.Updatetime = DateTime.Now; mmLineInventory.Updateby = "系统"; result = Context.Updateable(mmLineInventory).ExecuteCommand(); if (result == 1) { OperationLogRecording(2, 5, mmLineInventory.MaterialCode, mmLineInventory.MaterialName, mmLineInventory.LineCode, begin, Quantity, mmLineInventory.LocationCode); } return result; } } }