using DOAN.Infrastructure.PLC; using DOAN.Model.PBL; using DOAN.Model.PBL.Dto; using DOAN.Service.PBL.IService; using Infrastructure.Attribute; using Mapster; using Newtonsoft.Json.Linq; namespace DOAN.Service.PBL { /// /// 料架表Service业务层处理 /// [AppService(ServiceType = typeof(IMESInteractionServcie), ServiceLifetime = LifeTime.Transient)] public class MESInteractionServcie : BaseService, IMESInteractionServcie { public bool TestPLc(string address, PLCTool pLCTool) { bool isSucesss = pLCTool.ReadBit(address); return isSucesss; } /// /// MES亮灯 /// /// /// /// public bool MESLightUp(LightUpDto light, PLCTool pLCTool) { int result = 0; // 1.记录MES交互记录 MES_Interation_Log item = light.Adapt(); item.Id = XUEHUA; item.CreatedTime = DateTime.Now; Context.Insertable(item).ExecuteCommand(); // 2.根据总成零件号 ,版本 查询对应零件号,使得对应料架亮灯 // 同一个会有多个料架 //镜壳 料架 List MirrorshellShelfList = Context.Queryable().Where(it => it.Partnumber == SqlFunc.Subqueryable().Where(It => It.Productcode == light.AssemblyPartNumber && It.Version == light.Version).Select(it => it.MirrorshellCode)).ToList(); //镜体 料架 // Storagelocation MirrorshellBody = Context.Queryable().Where(it => it.Partnumber == // SqlFunc.Subqueryable().Where(It => It.Productcode == light.AssemblyPartNumber && It.Version == light.Version).Select(it => it.MirrorbodyCode)).First(); if (MirrorshellShelfList != null || MirrorshellShelfList.Count() > 0) { foreach (var item1 in MirrorshellShelfList) { // 3.对应料架亮灯 //TODO PLC 交互 // 对应料架 亮灯字段 // 通知PLC bool isSucesss = pLCTool.WriteBit(item1.PlcAddress, true); if (isSucesss) { item1.IsLight = 1; Context.Updateable(item1).ExecuteCommand(); } //亮灯日志 Light_Log light_Log = new Light_Log(); light_Log.Id = XUEHUA; light_Log.LightOperation = 1; light_Log.ShelfCode = item1.RackCode; light_Log.LayerNum = item1.LayerNum; light_Log.Operationer = "PBL"; light_Log.CreatedTime = DateTime.Now; light_Log.IsSuccess = isSucesss; //Light_Log light_Log2 = new Light_Log(); //light_Log2.Id = XUEHUA; //light_Log2.LightOperation = 1; //light_Log2.ShelfCode = item1.RackCode; //light_Log2.Operationer = "PBL"; //light_Log2.CreatedTime = DateTime.Now; //light_Log2.IsSuccess = isSucesss; //Context.Insertable(light_Log2).ExecuteCommand(); result += Context.Insertable(light_Log).ExecuteCommand(); } } else { return false; } return result > 0; } /// /// MES灭灯 /// /// /// public bool MESLightDown(string scan_code, PLCTool pLCTool) { int result = 0; // 1.记录MES交互记录 MES_Interation_Log item = new MES_Interation_Log(); item.Id = XUEHUA; item.ScanCode = scan_code; item.CreatedTime = DateTime.Now; Context.Insertable(item).ExecuteCommand(); //2 找到对应的库存最大料架 灭灯 Storagelocation storagelocation = Context.Queryable().Where(it => it.Partnumber == scan_code).OrderByDescending(it => it.PackageNum).First(); if (storagelocation != null) { // TODO PLC 交互 bool isSuccess = pLCTool.WriteBit(storagelocation.PlcAddress, false); if (isSuccess) { storagelocation.IsLight = 0; // storagelocation.PackageNum -= 1; if (storagelocation.PackageNum < 0) { storagelocation.PackageNum = 0; } //3 扣减对应的库存 // 合并货架的id int[] ids = { 1, 2, 3, 4, 19, 20, 21, 22 }; if (ids.Contains(storagelocation.Id)) { Context.Updateable().SetColumns(it=>it.IsLight == 0).Where(it=>it.Partnumber == storagelocation.Partnumber).ExecuteCommand(); } else { Context.Updateable(storagelocation).ExecuteCommand(); } } //灭灯日志 Light_Log light_Log = new Light_Log(); light_Log.Id = XUEHUA; light_Log.LightOperation = 2; light_Log.LayerNum = 1; light_Log.ShelfCode = storagelocation.RackCode; light_Log.IsSuccess = isSuccess; light_Log.Operationer = "PBL"; light_Log.CreatedTime = DateTime.Now; result += Context.Insertable(light_Log).ExecuteCommand(); } else { return false; } return result > 0; } } }