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.

283 lines
10 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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;
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;
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)
{
this._dal = dal;
base.BaseDal = dal;
_wareHouse = wareHouse;
_wareHouse_Sub = wareHouse_Sub;
_feed = feed;
_barrel = barrel;
_materialRepository = materialRepository;
_recipeRepository = recipeRepository;
}
#region 扫描桶二维码,获取绑定的小料配方
/// <summary>
/// 扫描桶二维码,获取绑定的小料配方
/// </summary>
/// <param name="code">桶条码</param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task<BarrelView> GetXlInfo(string code)
{
try
{
Hw_Barrel hw = (await _barrel.QueryAsync(d => d.IsEnable == "是")).FirstOrDefault(d => d.BarCode == code);
if (hw == null) { return null; }
var recipe = (await _recipeRepository.QueryAsync(d => d.Recipe_Verify == 1)).FirstOrDefault(d => d.Recipe_Code == hw.MaterialID);
BarrelView view = new BarrelView()
{
BarrelID = hw.BarrelID,
BarrelName = hw.BarrelName,
BarCode = hw.BarCode,
MaterialID = recipe.Recipe_Code,
MaterialName = recipe.Recipe_Name,
Weight = hw.Weight
};
return view;
}
catch (Exception ex)
{
log.Error(ex.Message);
return null;
}
}
#endregion
#region 根据传入的code 查询投料釜下所有的物料
/// <summary>
/// 根据传入的code 查询投料釜下所有的物料
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public async Task<WarehouseView> QueryByCode(string code, string planId)
{
try
{
Hw_Warehouse wh = await _wareHouse.FirstAsync(d => d.BarCode == code);
if (wh == null) return null;
var sub = await _wareHouse_Sub.QueryAsync(d => d.MainId == wh.ID&& d.PId== planId);
if (sub == null) return null;
WarehouseView warehouseView = new WarehouseView()
{
ID = wh.ID,
PlanId = wh.PlanId,
Name = wh.Name,
BarCode = wh.BarCode,
CreateTime = wh.CreateTime,
UpdateTime = wh.UpdateTime,
Children = sub
};
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;
List<Hw_Barrel> hbList = null;
Hw_WareHouse_Sub sub = null;
Hw_Warehouse wh = null;
Hw_Barrel hwBarrel = 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
{
var solventPlc = PlcHelper.siemensList.SingleOrDefault(d=>d.EquipName.Equals("小料PLC"));
if (solventPlc.plc.IsConnected)
{
//同时记录该扫码到数据库中
List<Hw_Warehouse> list = await _wareHouse.QueryAsync();
if (list != null)
{
wh = list.FirstOrDefault(d => d.BarCode == view.KCode);
subList = await _wareHouse_Sub.QueryAsync(d => d.MainId == wh.ID);
if (subList.Count > 0)
{
sub = subList.FirstOrDefault(d => d.MaterialID == view.MatCode.Trim());
}
}
if (view.State == 1)
{
message = "开启";
}
if (view.State == 0)
{
message = "关闭";
}
if (string.IsNullOrEmpty(message))
{
log.Error("传递状态为空!");
return false;
}
Hw_FeedReport hw = new Hw_FeedReport()
{
WID = wh.ID,
PlanId = view.PlanId,
WName = wh.Name,
WCode = wh.BarCode,
MaterialId = sub.MaterialID,
MaterialName = sub.MaterialName,
MCode = sub.Material_Code,
MType = view.MatType,
IsTrue = message,
CreateTime = DateTime.Now,
WholePackage = view.WholePackage.Trim()
};
//解绑 桶绑定
//hbList = await _barrel.QueryAsync();
//if (hbList != null)
//{
// hwBarrel = hbList.FirstOrDefault(d => d.BarCode == view.BarrelCode);
// if (hwBarrel != null)
// {
// hwBarrel.MaterialID = null;
// hwBarrel.MaterialName = null;
// await _feed.UpdateAsync(hw);
// }
//}
//保存记录
// int i = await _feed.AddAsync(hw);
if (string.IsNullOrEmpty(name))
{
log.Error("点位名称为空!");
return false;
}
bool result = solventPlc.plc.WriteInt16(name, view.State.ToString());//反应釜点位
if (result)
{
if (view.State == 1)
{
await _feed.AddAsync(hw);
}
return result;
}
else
{
log.Error("写入点位失败!");
return result;
}
}
return false;
}
catch (Exception ex)
{
log.Error(ex.Message);
return false;
}
}
#endregion
#region 获取所有计划号
/// <summary>
/// 获取所有计划号
/// </summary>
/// <returns></returns>
public async Task<List<string>> GetWarehousePlan(string code)
{
Hw_Warehouse wh = await _wareHouse.FirstAsync(d=>d.BarCode== code);
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();
}
#endregion
}
}