using JNPF;
using JNPF.Common.Core.Manager;
using JNPF.Common.Dtos.VisualDev;
using JNPF.Common.Extension;
using JNPF.Common.Filter;
using JNPF.Common.Security;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.FriendlyException;
using JNPF.Logging;
using JNPF.Systems.Entitys.System;
using JNPF.Systems.Interfaces.System;
using JNPF.VisualDev;
using JNPF.VisualDev.Entitys;
using JNPF.VisualDev.Entitys.Dto.VisualDevModelData;
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;
using Tnb.Common.Redis;
using Tnb.ProductionMgr.Entities.Dto.PrdManage;
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;
private readonly RedisData _redisData;
public PrdRawMaterialBarcodeService(ISqlSugarRepository repository,
IUserManager userManager,
IBillRullService billRuleService,
IVisualDevService visualDevService,
RedisData redisData,
IRunService runService,
IDictionaryDataService dictionaryDataService)
{
_db = repository.AsSugarClient();
_userManager = userManager;
_billRuleService = billRuleService;
_dictionaryDataService = dictionaryDataService;
_visualDevService = visualDevService;
_runService = runService;
_redisData = redisData;
OverideFuncs.UpdateAsync = Update;
OverideFuncs.GetListAsync = GetList;
}
private async Task GetList(VisualDevModelListQueryInput input)
{
Dictionary? queryJson = !string.IsNullOrEmpty(input.queryJson) ? Newtonsoft.Json.JsonConvert.DeserializeObject>(input.queryJson) : new Dictionary();
string billCode = queryJson.ContainsKey("bill_code") ? queryJson["bill_code"].ToString() : "";
string equipTypeId = queryJson.ContainsKey("f_flowid") ? queryJson["f_flowid"].ToString() : "";
string isUpdate = queryJson.ContainsKey("is_update") ? queryJson["is_update"].ToString() : "";
SqlSugarPagedList result = await _db.Queryable()
.LeftJoin((a, b) => a.material_id == b.id)
.LeftJoin((a, b,c) => a.equip_id == c.id)
.LeftJoin((a,b,c,d)=>c.equip_type_id==d.id)
.WhereIF(!string.IsNullOrEmpty(billCode),(a,b,c)=>a.bill_code.Contains(billCode))
.WhereIF(!string.IsNullOrEmpty(equipTypeId),(a,b,c)=>c.equip_type_id==equipTypeId)
.WhereIF(!string.IsNullOrEmpty(isUpdate),(a,b,c)=>a.is_update.ToString()==isUpdate)
.Where((a,b,c)=>a.type=="1")
.Select((a, b, c,d) => new PrdRawMaterialBarcodeListDto
{
id = a.id,
bill_code = a.bill_code,
equip_id = c.name,
material_id = b.name,
absorb_material_finish = a.absorb_material_finish==1 ? "是" : "否",
is_update = a.is_update==1 ? "是" : "否",
start_time = a.start_time!=null ? a.start_time.Value.ToString(DbTimeFormat.SS) : "",
end_time = a.end_time!=null ? a.end_time.Value.ToString(DbTimeFormat.SS) : "",
remark = a.remark,
f_flowid = d.name,
equip_id_id = c.id,
material_id_id = b.id,
f_flowtaskid = b.material_specification,
extras = b.material_standard
}).ToPagedListAsync(input.currentPage, int.MaxValue);
return PageResult.SqlSugarPageResult(result);
}
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}");
Dictionary dicCommand4 = new(StringComparer.OrdinalIgnoreCase)
{
["DevName"] = "挤出集中供料",
["token"] = _eleCtlCfg.token,
["TagName"] = "开关1",
["Value"] = "true",
};
string responseresult4 = await HttpClientHelper.GetRequestAsync(_eleCtlCfg.WriteTagUrl, dicCommand4, false);
Log.Information($"挤出集中供料开关1开启返回结果:{responseresult4}");
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}未设置原料条码数据采集项");
}
BasMaterial basMaterial = await _db.Queryable().SingleAsync(x => x.id == item.material_id);
Dictionary dicCommand3 = new(StringComparer.OrdinalIgnoreCase)
{
["DevName"] = eqpDaq.equip_code,
["token"] = _eleCtlCfg.token,
["TagName"] = eqpDaq.label_name,
["Value"] = basMaterial?.code ?? "",
};
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}");
Dictionary dicCommand4 = new(StringComparer.OrdinalIgnoreCase)
{
["DevName"] = "挤出集中供料",
["token"] = _eleCtlCfg.token,
["TagName"] = "开关1",
["Value"] = "false",
};
string responseresult4 = await HttpClientHelper.GetRequestAsync(_eleCtlCfg.WriteTagUrl, dicCommand4, false);
Log.Information($"挤出集中供料开关1关闭返回结果:{responseresult4}");
}
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("JZGL")).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 == "")
.SetColumns(x=>x.absorb_material_finish==0)
.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 "成功";
}
///
/// 更新截料阀
///
///
[HttpPost]
public async Task UpdateStopValve(List ids)
{
try
{
await _db.Ado.BeginTranAsync();
List prdRawMaterialBarcodeList = await _db.Queryable()
.Where(x => ids.Contains(x.id)).ToListAsync();
foreach (var item in prdRawMaterialBarcodeList)
{
if (string.IsNullOrEmpty(item.stop_valve))
{
throw Oops.Bah($"{item.bill_code}未设置截料阀");
}
if (string.IsNullOrEmpty(item.equip_ids))
{
throw Oops.Bah($"{item.bill_code}未设置机台");
}
List equipIdList = JsonConvert.DeserializeObject>(item.equip_ids);
List eqpEquipments = await _db.Queryable().Where(x => equipIdList.Contains(x.id)).ToListAsync();
foreach (var equipment in eqpEquipments)
{
EqpDaq eqpDaq = await _db.Queryable().Where(x => x.equip_id == equipment.id && x.label_name.Contains("原料选择")).FirstAsync();
if (eqpDaq == null)
{
throw Oops.Bah($"{equipment.name}未设置原料选择数据采集项");
}
Dictionary dicCommand3 = new(StringComparer.OrdinalIgnoreCase)
{
["DevName"] = eqpDaq.equip_code,
["token"] = _eleCtlCfg.token,
["TagName"] = eqpDaq.label_name,
["Value"] = item.stop_valve,
};
Log.Information($"{eqpDaq.equip_code}{eqpDaq.label_name}更新参数:{JsonConvert.SerializeObject(dicCommand3)}");
string responseresult3 = await HttpClientHelper.GetRequestAsync(_eleCtlCfg.WriteTagUrl, dicCommand3, false);
Log.Information($"{eqpDaq.equip_code}{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);
}
return "成功";
}
///
/// 管道状态
///
///
[HttpPost]
public async Task GetPipingStatus(List ids)
{
try
{
await _db.Ado.BeginTranAsync();
List prdRawMaterialBarcodeList = await _db.Queryable()
.Where(x => ids.Contains(x.id)).ToListAsync();
foreach (var item in prdRawMaterialBarcodeList)
{
if (string.IsNullOrEmpty(item.equip_ids))
{
throw Oops.Bah($"{item.bill_code}未设置机台");
}
List equipIdList = JsonConvert.DeserializeObject>(item.equip_ids);
List eqpEquipments = await _db.Queryable().Where(x => equipIdList.Contains(x.id)).ToListAsync();
foreach (var equipment in eqpEquipments)
{
EqpDaq eqpDaq = await _db.Queryable().Where(x => x.equip_id == equipment.id && x.label_name.Contains("配管状态")).FirstAsync();
if (eqpDaq == null)
{
throw Oops.Bah($"{equipment.name}未设置配管状态数据采集项");
}
bool data = await _redisData.TryGetValueByKeyField(eqpDaq.equip_code, eqpDaq.label_name);
int pipingStatus = data ? 1 : 0;
await _db.Updateable()
.SetColumns(x => x.piping_status == pipingStatus)
.Where(x => x.id == item.id)
.ExecuteCommandAsync();
}
}
await _db.Ado.CommitTranAsync();
}
catch (Exception e)
{
await _db.Ado.RollbackTranAsync();
throw Oops.Bah(e.Message);
}
return "成功";
}
///
/// 机台原料设定吸料完成
///
///
[HttpPost]
public async Task MaterialSuctionCompleted2(List ids)
{
try
{
await _db.Ado.BeginTranAsync();
List prdRawMaterialBarcodeList = await _db.Queryable()
.Where(x => ids.Contains(x.id)).ToListAsync();
List insertList = new List();
foreach (var item in prdRawMaterialBarcodeList)
{
List equipIdList = JsonConvert.DeserializeObject>(item.equip_ids);
List eqpEquipments = await _db.Queryable().Where(x => equipIdList.Contains(x.id)).ToListAsync();
foreach (var equipment in eqpEquipments)
{
EqpDaq eqpDaq = await _db.Queryable().Where(x => x.equip_id == equipment.id && x.label_name.Contains("原料选择")).FirstAsync();
if (eqpDaq == null)
{
throw Oops.Bah($"{equipment.name}未设置原料选择数据采集项");
}
Dictionary dicCommand3 = new(StringComparer.OrdinalIgnoreCase)
{
["DevName"] = eqpDaq.equip_code,
["token"] = _eleCtlCfg.token,
["TagName"] = eqpDaq.label_name,
["Value"] = "",
};
Log.Information($"{eqpDaq.equip_code}{eqpDaq.label_name}吸料完成参数:{JsonConvert.SerializeObject(dicCommand3)}");
string responseresult3 = await HttpClientHelper.GetRequestAsync(_eleCtlCfg.WriteTagUrl, dicCommand3, false);
Log.Information($"{eqpDaq.equip_code}{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.equip_ids == null)
.SetColumns(x=>x.piping_status == 0)
.SetColumns(x=>x.absorb_material_finish==0)
.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);
}
return "成功";
}
}
}