You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
14 KiB
C#

1 year ago
using Admin.Core.IRepository;
using Admin.Core.IService;
using Admin.Core.Model;
using System.Threading.Tasks;
using System.Linq;
using System;
using Admin.Core.Model.ViewModels;
using log4net;
using System.Collections.Generic;
12 months ago
using Microsoft.Data.SqlClient;
1 year ago
using Admin.Core.PlcServer;
namespace Admin.Core.Service
{
public class Hw_WarehouseServices : BaseServices<Hw_Warehouse>, IHw_WarehouseServices
{
private static readonly log4net.ILog log = LogManager.GetLogger(typeof(Hw_WarehouseServices));
private readonly IBaseRepository<Hw_Warehouse> _dal;
private readonly IHw_WarehouseRepository _wareHouse;
private readonly IHw_WareHouse_SubRepository _wareHouse_Sub;
private readonly IHw_FeedReportRepository _feed;
private readonly IHw_BarrelRepository _barrel;
private readonly Ixl_materialRepository _materialRepository;
private readonly Ixl_recipeRepository _recipeRepository;
12 months ago
private readonly ILR_weighRepository _lrWeighRepository;
1 year ago
12 months ago
public Hw_WarehouseServices(IBaseRepository<Hw_Warehouse> dal, IHw_WarehouseRepository wareHouse,
IHw_WareHouse_SubRepository wareHouse_Sub, IHw_FeedReportRepository feed,
IHw_BarrelRepository barrel, Ixl_materialRepository materialRepository,
Ixl_recipeRepository recipeRepository, ILR_weighRepository lrWeighRepository)
1 year ago
{
this._dal = dal;
base.BaseDal = dal;
_wareHouse = wareHouse;
_wareHouse_Sub = wareHouse_Sub;
_feed = feed;
_barrel = barrel;
_materialRepository = materialRepository;
_recipeRepository = recipeRepository;
12 months ago
_lrWeighRepository = lrWeighRepository;
1 year ago
}
12 months ago
#region 扫描桶二维码,获取绑定的小料配方物料
1 year ago
/// <summary>
/// 扫描桶二维码,获取绑定的小料配方
/// </summary>
/// <param name="code">桶条码</param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
12 months ago
public async Task<BarrelView> GetXlInfo(string kettleBarCode, string planId, string code)
1 year ago
{
12 months ago
List<MaterialView> list = new List<MaterialView>();
List<MaterialView> materList = new List<MaterialView>();
1 year ago
try
{
12 months ago
Hw_Barrel hw = await _barrel.FirstAsync(d => d.IsEnable == "是" && d.BarCode == code);
1 year ago
if (hw == null) { return null; }
12 months ago
var recipe = await _recipeRepository.FirstAsync(d => d.Recipe_Verify == 1 && d.Recipe_Code == hw.MaterialID);
if (recipe == null) { return null; }
var materialList = await _lrWeighRepository.QueryAsync(d => d.Plan_ID == hw.PlanId && d.Recipe_ID == recipe.ID);
if (materialList.Count() == 0) { return null; }
foreach (var material in materialList)
{
MaterialView childView = new MaterialView();
childView.MaterialID = material.Material_ID;
var mat = await _materialRepository.FirstAsync(d => d.ID.Equals(material.Material_ID));
if (materialList == null) { continue; }
childView.MaterialBarCode = mat.Material_code;
childView.MaterialName = material.Material_Name;
childView.Weight = material.Real_Weight;
childView.Error = material.Real_Error;
childView.MatchOrNot = 0;
list.Add(childView);
}
var kettle = await _dal.FirstAsync(d => d.BarCode.Equals(kettleBarCode));
var subList = await _wareHouse_Sub.QueryAsync(d => d.MainId == kettle.ID && d.PId.Equals(planId) &&d.MaterialType.Contains("尾料"));
foreach (var check in list)
{
MaterialView childView = new MaterialView();
childView.MaterialID = check.MaterialID;
var mat = await _materialRepository.FirstAsync(d => d.ID.Equals(check.MaterialID));
childView.MaterialBarCode = mat.Material_code;
childView.MaterialName = check.MaterialName;
var pList = subList.Where(d => d.Material_Code.Equals(check.MaterialBarCode));
if (pList == null)
{
childView.MatchOrNot = 0;
materList.Add(childView);
continue;
}
foreach (var item in pList)
{
decimal? maxValue = item.SetWeight + item.SetError;
decimal? minValue = item.SetWeight - item.SetError;
if (minValue <= check.Weight && check.Weight <= maxValue)
{
childView.Weight = check.Weight;
childView.Error = check.Error;
childView.MatchOrNot = 1;
}
else
{
childView.Weight = check.Weight;
childView.Error = check.Error;
childView.MatchOrNot = 0;
}
}
materList.Add(childView);
}
1 year ago
BarrelView view = new BarrelView()
{
BarrelID = hw.BarrelID,
BarrelName = hw.BarrelName,
BarCode = hw.BarCode,
12 months ago
child = materList
1 year ago
};
return view;
}
catch (Exception ex)
{
log.Error(ex.Message);
return null;
}
}
#endregion
12 months ago
#region 根据传入的二维码、计划Id 查询反应釜下所有的物料
1 year ago
/// <summary>
12 months ago
/// 根据传入的二维码、计划Id 查询反应釜下所有的物料
1 year ago
/// </summary>
12 months ago
/// <param name="code">反应釜二维码</param>
/// <param name="planId">计划Id</param>
1 year ago
/// <returns></returns>
1 year ago
public async Task<WarehouseView> QueryByCode(string code, string planId)
1 year ago
{
12 months ago
List<HwWareHouseSubView> list = new List<HwWareHouseSubView>();
1 year ago
try
{
1 year ago
Hw_Warehouse wh = await _wareHouse.FirstAsync(d => d.BarCode == code);
1 year ago
if (wh == null) return null;
12 months ago
var sub = await _wareHouse_Sub.QueryAsync(d => d.MainId == wh.ID&& d.PId== planId&&(d.MaterialType.Contains("整包")|| d.MaterialType.Contains("尾料")));
if (sub.Count == 0) return null;
sub.ForEach(sub =>
{
HwWareHouseSubView view = new HwWareHouseSubView();
view.ID = sub.ID;
view.MainId = sub.MainId;
view.PId = sub.PId;
view.ProductName = sub.ProductName;
view.MaterialID = sub.MaterialID;
view.Material_Code = sub.Material_Code;
view.MaterialName = sub.MaterialName;
view.BinId = sub.BinId;
view.MaterialType = sub.MaterialType;
view.SetWeight = sub.SetWeight;
view.SetError = sub.SetError;
list.Add(view);
});
WarehouseView warehouseView = new WarehouseView()
1 year ago
{
ID = wh.ID,
Name = wh.Name,
BarCode = wh.BarCode,
12 months ago
Children = list
};
1 year ago
return warehouseView;
}
catch (Exception ex)
{
log.Error(ex.Message);
return null;
}
}
#endregion
#region 更新PLC状态
/// <summary>
/// 更新PLC状态
/// </summary>
/// <param name="view">本次验证状态 1开启0关闭</param>
/// <returns></returns>
public async Task<bool> UpdatePlcState(KettleView view)
{
List<Hw_WareHouse_Sub> subList = null;
12 months ago
List<Hw_FeedReport> report = new List<Hw_FeedReport>();
1 year ago
Hw_Warehouse wh = null;
string message = string.Empty;
string name = string.Empty;
#region 反应釜工位信号
//反应釜工位报警
switch (view.Station)
{
case 1:
name = "DB109.DBW4.0";
break;
case 2:
name = "DB109.DBW10.0";
break;
case 3:
name = "DB109.DBW16.0";
break;
case 4:
name = "DB109.DBW22.0";
break;
case 5:
name = "DB109.DBW28.0";
break;
case 6:
name = "DB109.DBW34.0";
break;
case 7:
name = "DB109.DBW40.0";
break;
case 8:
name = "DB109.DBW46.0";
break;
case 9:
name = "DB109.DBW52.0";
break;
case 10:
name = "DB109.DBW58.0";
break;
case 11:
name = "DB109.DBW64.0";
break;
case 12:
name = "DB109.DBW70.0";
break;
case 13:
name = "DB109.DBW76.0";
break;
case 14:
name = "DB109.DBW82.0";
break;
case 15:
name = "DB109.DBW88.0";
break;
default:
break;
}
#endregion
try
{
12 months ago
//log.Info("开始");
var solventPlc = PlcHelper.siemensList.SingleOrDefault(d => d.EquipName.Equals("溶剂PLC"));
log.Info($"PLC状态{solventPlc.plc.IsConnected}");
1 year ago
if (solventPlc.plc.IsConnected)
{
12 months ago
#region 记录数据
1 year ago
//同时记录该扫码到数据库中
12 months ago
wh = await _wareHouse.FirstAsync(d => d.BarCode == view.KCode);
if (wh == null) return false;
subList = await _wareHouse_Sub.QueryAsync(d => d.MainId == wh.ID);
if (subList.Count == 0) return false;
log.Info("开启模式");
1 year ago
if (view.State == 1)
{
message = "开启";
}
if (view.State == 0)
{
message = "关闭";
}
if (string.IsNullOrEmpty(message))
{
log.Error("传递状态为空!");
return false;
}
12 months ago
foreach (var item in view.Child)
1 year ago
{
12 months ago
Hw_FeedReport hw = new Hw_FeedReport()
{
WID = wh.ID,
PlanId = view.PlanId,
WName = wh.Name,
WCode = wh.BarCode,
MaterialId = item.MatCode,
MaterialName = item.MatName,
MCode = item.MatCode,
MType = item.MatType,
IsTrue = message,
CreateTime = DateTime.Now,
WholePackage = item.WholePackage.Trim()
};
report.Add(hw);
}
await _feed.AddAsync(report);
1 year ago
if (string.IsNullOrEmpty(name))
{
log.Error("点位名称为空!");
return false;
}
bool result = solventPlc.plc.WriteInt16(name, view.State.ToString());//反应釜点位
if (result)
{
if (view.State == 1)
{
12 months ago
await _feed.AddAsync(report);
1 year ago
}
return result;
}
else
{
log.Error("写入点位失败!");
return result;
}
12 months ago
#endregion
}
else
{
return false;
1 year ago
}
}
catch (Exception ex)
{
12 months ago
log.Info("异常:"+ex.Message);
1 year ago
log.Error(ex.Message);
return false;
}
}
#endregion
#region 获取所有计划号
/// <summary>
/// 获取所有计划号
/// </summary>
/// <returns></returns>
1 year ago
public async Task<List<string>> GetWarehousePlan(string code)
1 year ago
{
12 months ago
Hw_Warehouse wh = await _wareHouse.FirstAsync(d=>d.BarCode== code.Trim());
1 year ago
if (wh==null) return null;
var list = await _wareHouse_Sub.QueryAsync(d=>d.MainId== wh.ID);
var planList = (from d in list
select d.PId).Distinct();
return planList.ToList();
1 year ago
}
#endregion
}
}