using JNPF; using JNPF.Common.Core.Manager; using JNPF.Common.Dtos.VisualDev; using JNPF.Common.Extension; using JNPF.Common.Security; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.FriendlyException; using JNPF.Logging; using JNPF.Systems.Interfaces.System; using JNPF.VisualDev; using JNPF.VisualDev.Entitys; using JNPF.VisualDev.Interfaces; using Mapster; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using Senparc.CO2NET.HttpUtility; using SqlSugar; using Tnb.BasicData; using Tnb.BasicData.Entities; using Tnb.Common.Extension; using Tnb.EquipMgr.Entities; using Tnb.ProductionMgr.Entities; using Tnb.WarehouseMgr.Entities.Configs; using Tnb.Common.Utils; using Tnb.WarehouseMgr.Entities.Consts; using HttpClientHelper = Tnb.Common.Utils.HttpClientHelper; namespace Tnb.ProductionMgr { /// /// 料仓原料条码服务 /// [ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 700)] [Route("api/[area]/[controller]/[action]")] [OverideVisualDev(ModuleId)] public class PrdRawMaterialBarcodeService: IOverideVisualDevService,IDynamicApiController, ITransient { private const string ModuleId = "35925395676181"; private readonly ISqlSugarClient _db; private readonly IDictionaryDataService _dictionaryDataService; private readonly IUserManager _userManager; private static Dictionary> _dicWorkLine = new(); private readonly ElevatorControlConfiguration _eleCtlCfg = App.Configuration.Build(); private readonly IBillRullService _billRuleService; public OverideVisualDevFunc OverideFuncs { get; } = new OverideVisualDevFunc(); private readonly IVisualDevService _visualDevService; private readonly IRunService _runService; public PrdRawMaterialBarcodeService(ISqlSugarRepository repository, IUserManager userManager, IBillRullService billRuleService, IVisualDevService visualDevService, IRunService runService, IDictionaryDataService dictionaryDataService) { _db = repository.AsSugarClient(); _userManager = userManager; _billRuleService = billRuleService; _dictionaryDataService = dictionaryDataService; _visualDevService = visualDevService; _runService = runService; OverideFuncs.UpdateAsync = Update; } private async Task Update(string id, VisualDevModelDataUpInput visualDevModelDataUpInput) { PrdRawMaterialBarcode prdRawMaterialBarcode = await _db.Queryable().SingleAsync(x=>x.id==id); string newMaterialId = visualDevModelDataUpInput.data.ContainsKey("material_id") ? visualDevModelDataUpInput.data["material_id"].ToString() : ""; VisualDevEntity? templateEntity = await _visualDevService.GetInfoById(ModuleId, true); await _runService.Update(id, templateEntity, visualDevModelDataUpInput); if (!string.IsNullOrEmpty(newMaterialId) && prdRawMaterialBarcode.material_id != newMaterialId) { string billCode = await _billRuleService.GetBillNumber(CodeTemplateConst.PRDRAWMATERIALBARCODE_CODE); await _db.Updateable() .SetColumns(x => x.bill_code == billCode) .Where(x => x.id == id) .ExecuteCommandAsync(); } return await Task.FromResult(true); } /// /// 更新 /// /// [HttpPost] public async Task UpdateBarcode(List ids) { try { await _db.Ado.BeginTranAsync(); Dictionary dicCommand = new(StringComparer.OrdinalIgnoreCase) { ["DevName"] = "注塑主控柜", ["token"] = _eleCtlCfg.token, ["TagName"] = "开工1", ["Value"] = "true", }; string responseresult = await HttpClientHelper.GetRequestAsync(_eleCtlCfg.WriteTagUrl, dicCommand, false); Log.Information($"注塑主控柜开工1开启返回结果:{responseresult}"); Dictionary dicCommand2 = new(StringComparer.OrdinalIgnoreCase) { ["DevName"] = "注塑主控柜", ["token"] = _eleCtlCfg.token, ["TagName"] = "开工2", ["Value"] = "true", }; string responseresult2 = await HttpClientHelper.GetRequestAsync(_eleCtlCfg.WriteTagUrl, dicCommand2, false); Log.Information($"注塑主控柜开工2开启返回结果:{responseresult2}"); List prdRawMaterialBarcodeList = await _db.Queryable() .Where(x => ids.Contains(x.id)).ToListAsync(); foreach (var item in prdRawMaterialBarcodeList) { if (string.IsNullOrEmpty(item.material_id)) { throw Oops.Bah($"{item.bill_code}未设置原材料"); } EqpEquipment eqpEquipment = await _db.Queryable().SingleAsync(x => x.id == item.equip_id); EqpDaq eqpDaq = await _db.Queryable() .Where(x => x.equip_id == item.equip_id && x.label_name.Contains("原料条码")).FirstAsync(); if (eqpDaq == null) { throw Oops.Bah($"{eqpEquipment.name}未设置原料条码数据采集项"); } Dictionary dicCommand3 = new(StringComparer.OrdinalIgnoreCase) { ["DevName"] = eqpDaq.equip_code, ["token"] = _eleCtlCfg.token, ["TagName"] = eqpDaq.label_name, ["Value"] = item.material_id, }; Log.Information($"注塑主控柜{eqpDaq.label_name}更新参数:{JsonConvert.SerializeObject(dicCommand3)}"); string responseresult3 = await HttpClientHelper.GetRequestAsync(_eleCtlCfg.WriteTagUrl, dicCommand3, false); Log.Information($"注塑主控柜{eqpDaq.label_name}更新返回结果:{responseresult3}"); await _db.Updateable() .SetColumns(x => x.start_time == DateTime.Now) .SetColumns(x => x.is_update == 1) .Where(x => x.id == item.id) .ExecuteCommandAsync(); } await _db.Ado.CommitTranAsync(); } catch (Exception e) { await _db.Ado.RollbackTranAsync(); throw Oops.Bah(e.Message); } finally { Dictionary dicCommand = new(StringComparer.OrdinalIgnoreCase) { ["DevName"] = "注塑主控柜", ["token"] = _eleCtlCfg.token, ["TagName"] = "开工1", ["Value"] = "false", }; string responseresult = await HttpClientHelper.GetRequestAsync(_eleCtlCfg.WriteTagUrl, dicCommand, false); Log.Information($"注塑主控柜开工1关闭返回结果:{responseresult}"); Dictionary dicCommand2 = new(StringComparer.OrdinalIgnoreCase) { ["DevName"] = "注塑主控柜", ["token"] = _eleCtlCfg.token, ["TagName"] = "开工2", ["Value"] = "false", }; string responseresult2 = await HttpClientHelper.GetRequestAsync(_eleCtlCfg.WriteTagUrl, dicCommand2, false); Log.Information($"注塑主控柜开工2关闭返回结果:{responseresult2}"); } return "成功"; } /// /// 刷新 同步料仓 /// /// [HttpPost] public async Task AsyncMaterialWarhouse() { List equipIds = await _db.Queryable().Where(x=>x.type=="1").Select(x=>x.equip_id).ToListAsync(); List equipments = await _db.Queryable().Where(x=>!equipIds.Contains(x.id) && x.code.StartsWith("abc")).ToListAsync(); List insertList = new List(); if (equipments != null && !equipments.IsEmpty()) { foreach (var item in equipments) { insertList.Add(new PrdRawMaterialBarcode() { id = SnowflakeIdHelper.NextId(), bill_code = await _billRuleService.GetBillNumber(CodeTemplateConst.PRDRAWMATERIALBARCODE_CODE), equip_id = item.id, type = "1", create_id = _userManager.UserId, create_time = DateTime.Now, org_id = WmsWareHouseConst.AdministratorOrgId, }); } } if (!insertList.IsEmpty()) { await _db.Insertable(insertList).ExecuteCommandAsync(); } return "成功"; } /// /// 吸料完成 /// /// [HttpPost] public async Task MaterialSuctionCompleted(List ids) { try { await _db.Ado.BeginTranAsync(); Dictionary dicCommand = new(StringComparer.OrdinalIgnoreCase) { ["DevName"] = "注塑主控柜", ["token"] = _eleCtlCfg.token, ["TagName"] = "开工1", ["Value"] = "true", }; string responseresult = await HttpClientHelper.GetRequestAsync(_eleCtlCfg.WriteTagUrl, dicCommand, false); Log.Information($"注塑主控柜开工1开启返回结果:{responseresult}"); Dictionary dicCommand2 = new(StringComparer.OrdinalIgnoreCase) { ["DevName"] = "注塑主控柜", ["token"] = _eleCtlCfg.token, ["TagName"] = "开工2", ["Value"] = "true", }; string responseresult2 = await HttpClientHelper.GetRequestAsync(_eleCtlCfg.WriteTagUrl, dicCommand2, false); Log.Information($"注塑主控柜开工2开启返回结果:{responseresult2}"); List prdRawMaterialBarcodeList = await _db.Queryable() .Where(x => ids.Contains(x.id)).ToListAsync(); List insertList = new List(); foreach (var item in prdRawMaterialBarcodeList) { EqpEquipment eqpEquipment = await _db.Queryable().SingleAsync(x => x.id == item.equip_id); EqpDaq eqpDaq = await _db.Queryable().Where(x => x.equip_id == item.equip_id && x.label_name.Contains("原料条码")).FirstAsync(); if (eqpDaq == null) { throw Oops.Bah($"{eqpEquipment.name}未设置原料条码数据采集项"); } Dictionary dicCommand3 = new(StringComparer.OrdinalIgnoreCase) { ["DevName"] = eqpDaq.equip_code, ["token"] = _eleCtlCfg.token, ["TagName"] = eqpDaq.label_name, ["Value"] = "", }; Log.Information($"注塑主控柜{eqpDaq.label_name}吸料完成参数:{JsonConvert.SerializeObject(dicCommand3)}"); string responseresult3 = await HttpClientHelper.GetRequestAsync(_eleCtlCfg.WriteTagUrl, dicCommand3, false); Log.Information($"注塑主控柜{eqpDaq.label_name}吸料完成返回结果:{responseresult3}"); PrdRawMaterialBarcodeRecord prdRawMaterialBarcodeRecord = item.Adapt(); prdRawMaterialBarcodeRecord.id = SnowflakeIdHelper.NextId(); prdRawMaterialBarcodeRecord.end_time = DateTime.Now; insertList.Add(prdRawMaterialBarcodeRecord); await _db.Updateable() .SetColumns(x => x.bill_code == "") .SetColumns(x => x.start_time == null) .SetColumns(x => x.end_time == null) .SetColumns(x => x.is_update == 0) .SetColumns(x => x.material_id == "") .Where(x => x.id == item.id) .ExecuteCommandAsync(); } if (!insertList.IsEmpty()) { await _db.Insertable(insertList).ExecuteCommandAsync(); } await _db.Ado.CommitTranAsync(); } catch (Exception e) { await _db.Ado.RollbackTranAsync(); throw Oops.Bah(e.Message); } finally { Dictionary dicCommand = new(StringComparer.OrdinalIgnoreCase) { ["DevName"] = "注塑主控柜", ["token"] = _eleCtlCfg.token, ["TagName"] = "开工1", ["Value"] = "false", }; string responseresult = await HttpClientHelper.GetRequestAsync(_eleCtlCfg.WriteTagUrl, dicCommand, false); Log.Information($"注塑主控柜开工1关闭返回结果:{responseresult}"); Dictionary dicCommand2 = new(StringComparer.OrdinalIgnoreCase) { ["DevName"] = "注塑主控柜", ["token"] = _eleCtlCfg.token, ["TagName"] = "开工2", ["Value"] = "false", }; string responseresult2 = await HttpClientHelper.GetRequestAsync(_eleCtlCfg.WriteTagUrl, dicCommand2, false); Log.Information($"注塑主控柜开工2关闭返回结果:{responseresult2}"); } return "成功"; } } }