201 lines
9.7 KiB
C#
201 lines
9.7 KiB
C#
|
using Microsoft.Extensions.Logging;
|
|||
|
using NUglify.Helpers;
|
|||
|
using Seyounth.Auto.Hs.Runtime.Balances;
|
|||
|
using Seyounth.Auto.Hs.Runtime.Plc;
|
|||
|
using Seyounth.Auto.Hs.Runtime.Printer;
|
|||
|
using Syc.Abp.Application.Contracts;
|
|||
|
using Syc.Basic.Web.WMS.Entitys;
|
|||
|
using Syc.Basic.Web.WMS.Service;
|
|||
|
using Syc.Basic.Web.WMS.WebSocket;
|
|||
|
using Syc.Core.Tools;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Volo.Abp.Domain.Repositories;
|
|||
|
using Volo.Abp.Uow;
|
|||
|
|
|||
|
namespace Syc.Basic.Web.WMS.DeviceEventHandle
|
|||
|
{
|
|||
|
public class DefaultBalanceEventHandle : IBalanceEventHandle
|
|||
|
{
|
|||
|
private readonly IRepository<Silk> silkRepository;
|
|||
|
private readonly IPlcService plcService;
|
|||
|
private readonly IRepository<Produce> produceRepository;
|
|||
|
private readonly IRepository<Box> boxRepository;
|
|||
|
private readonly IPrinterService printerService;
|
|||
|
private readonly IUnitOfWorkManager unitOfWork;
|
|||
|
private readonly ILogger<DefaultBalanceEventHandle> logger;
|
|||
|
private readonly static object _lock = new object();
|
|||
|
public DefaultBalanceEventHandle(IRepository<Silk> silkRepository,IPlcService plcService,IRepository<Produce> produceRepository,IRepository<Box> boxRepository,IPrinterService printerService,IUnitOfWorkManager unitOfWork, ILogger<DefaultBalanceEventHandle> logger)
|
|||
|
{
|
|||
|
this.silkRepository = silkRepository;
|
|||
|
this.plcService = plcService;
|
|||
|
this.produceRepository = produceRepository;
|
|||
|
this.boxRepository = boxRepository;
|
|||
|
this.printerService = printerService;
|
|||
|
this.unitOfWork = unitOfWork;
|
|||
|
this.logger = logger;
|
|||
|
}
|
|||
|
|
|||
|
public async Task ExecAsync(decimal weight, int id)
|
|||
|
{
|
|||
|
|
|||
|
using (var uow = unitOfWork.Reserve(UnitOfWork.UnitOfWorkReservationName))
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
|
|||
|
if (weight <= 0)
|
|||
|
return;
|
|||
|
|
|||
|
//logger.LogInformation($"重量稳定:{weight}");
|
|||
|
|
|||
|
if (id == 1)
|
|||
|
await Yanr(weight);
|
|||
|
else
|
|||
|
await Box(weight);
|
|||
|
await uow.CompleteAsync();
|
|||
|
}
|
|||
|
catch (Exception ex) when (ex is FriendlyException friendlyException)
|
|||
|
{
|
|||
|
logger.LogError(ex.GetBaseException(), "称重报错");
|
|||
|
await WebSocketManager.SocketManager.BroadcastAsync(friendlyException.Message);
|
|||
|
await uow.RollbackAsync();
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
logger.LogError(ex.GetBaseException(), "称重报错");
|
|||
|
await uow.RollbackAsync();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 丝锭称重
|
|||
|
/// </summary>
|
|||
|
/// <param name="weight"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public async Task Yanr(decimal weight)
|
|||
|
{
|
|||
|
if (await silkRepository.AnyAsync(e => e.Status == 0 && e.IsDelete == 0))
|
|||
|
{
|
|||
|
var silk = await silkRepository.FirstOrDefaultAsync(e => e.Status == 0 && e.IsDelete == 0);//QueueManage.YarnBalanceQueue.Dequeue();
|
|||
|
silk.Status = 1;
|
|||
|
silk.Status_Details = "已称重待装箱";
|
|||
|
silk.Net_Weight = (double)weight;
|
|||
|
await silkRepository.UpdateAsync(silk);
|
|||
|
string content = "#!A1" +
|
|||
|
"\r\n#N13" +
|
|||
|
"\r\n#PC1017/0" +
|
|||
|
"\r\n#IMR44/46" +
|
|||
|
//"\r\n#HV50" +
|
|||
|
"\r\n#PR6//" +
|
|||
|
"\r\n#PO0" +
|
|||
|
"\r\n#ERNC/1//0.00" +
|
|||
|
"\r\n#R0/0" +
|
|||
|
"\r\n#T3.64 #J43.18 #FD/0/L #SS100/BVUN/21X21/0 #VW/L/\"YuLinHengShenXinCaiLiaoYouXianGongSi\"#G" +
|
|||
|
"\r\n#T11.85 #J40.47 #FD/0/L #SS100/BVUN/19X19/0 #VW/L/\"Yulin Hengshen COLtd\"#G" +
|
|||
|
"\r\n#T2.28 #J25.31 #FD/0/L #SS100/BVUN/20X20/0 #VW/L/\"Name " + silk.Name +"\"#G" +
|
|||
|
"\r\n#T2.28 #J20.91 #FD/0/L #SS100/BVUN/20X20/0 #VW/L/\"Type " +silk.Type+ "\"#G" +
|
|||
|
"\r\n#T2.28 #J16.51 #FD/0/L #SS100/BVUN/20X20/0 #VW/L/\"Net Weight "+silk.Net_Weight+ "\"#G" +
|
|||
|
"\r\n#T2.28 #J12.61 #FD/0/L #SS100/BVUN/20X20/0 #VW/L/\"Length "+silk.Length+ "\"#G" +
|
|||
|
"\r\n#T2.28 #J7.95 #FD/0/L #SS100/BVUN/20X20/0 #VW/L/\"Date "+silk.Date.ToString()+ "\"#G" +
|
|||
|
"\r\n#T2.28 #J3.89 #FD/0/L #SS100/BVUN/20X20/0 #VW/L/\"Lot No "+silk.Lot_No+ "\"#G" +
|
|||
|
"\r\n#T11.51 #J29.54 #FD/0/L #SB13/ONKP2.0/8.80/2 #VW/L/\"" + silk.Code+"\"#G" +
|
|||
|
"\r\n#Q1#G" +
|
|||
|
"\r\n#!P1" +
|
|||
|
"\r\n";
|
|||
|
await printerService.PrintAsync(1, content);
|
|||
|
|
|||
|
logger.LogInformation($"Silk({silk.Id}|{silk.Code})状态更改为【已称重待装箱】,同时向打印机发送打印标签指令");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 整箱称重
|
|||
|
/// </summary>
|
|||
|
/// <param name="weight"></param>
|
|||
|
public async Task Box(decimal weight)
|
|||
|
{
|
|||
|
/* PLC未给顶升到位信号则忽略此次事件处理 */
|
|||
|
if (!await plcService.IsTop()) return;
|
|||
|
|
|||
|
|
|||
|
if (await silkRepository.AnyAsync(e => e.Status == 1 && e.IsDelete == 0))
|
|||
|
{
|
|||
|
var query = await silkRepository.GetQueryableAsync();
|
|||
|
query =query.Where(x => x.Status == 1 && x.IsDelete == 0);
|
|||
|
var count = query.Count();
|
|||
|
var produce = await produceRepository.FirstOrDefaultAsync(e => e.IfUse == 1);
|
|||
|
if (produce is null)
|
|||
|
throw Oops.Oh("无生产信息,请先设置生产信息再扫码");
|
|||
|
if (produce.Qty.Value > count)
|
|||
|
{
|
|||
|
throw Oops.Oh("数量未满箱,无法打印标签");
|
|||
|
}
|
|||
|
logger.LogInformation($"当前生产信息:{produce.Name}|{produce.Spec}|{produce.Lot_No}|{produce.BoxSpec}|{produce.Qty}");
|
|||
|
//var netWeight = query.FirstOrDefault(e => e.Code == QueueManage.BoxQueue.FirstOrDefault()).Net_Weight * produce.Qty.Value;
|
|||
|
var takeQuery = query.Take(produce.Qty.Value);
|
|||
|
var netWeight = takeQuery.Sum(e => e.Net_Weight);
|
|||
|
var box = new Box()
|
|||
|
{
|
|||
|
Dom_Time = DateTime.Now,
|
|||
|
Type = produce.Type,
|
|||
|
Exp_Time = produce.Exp_Time,
|
|||
|
Qty = produce.Qty,
|
|||
|
Length = produce.Length,
|
|||
|
Lot_No = produce.Lot_No,
|
|||
|
Code = Guid.NewGuid().ToString().Substring(9, 18),
|
|||
|
Net_Weight = netWeight,
|
|||
|
Spec = produce.BoxSpec,
|
|||
|
Gross_Weight = (double)weight,
|
|||
|
IsUse = true,
|
|||
|
IsDelete = 0
|
|||
|
};
|
|||
|
string content = "#!A1" +
|
|||
|
"\r\n#N13" +
|
|||
|
"\r\n#PC1017/0" +
|
|||
|
"\r\n#IMR104/130" +
|
|||
|
//"\r\n#HV50" +
|
|||
|
"\r\n#PR6//" +
|
|||
|
"\r\n#PO0" +
|
|||
|
"\r\n#ERNC/1//0.00" +
|
|||
|
"\r\n#R0/0" +
|
|||
|
"\r\n#T7.28 #J11.17 #FD/1/L #SS100/BVUN/51X68/0 #VW/L/\"YuLinHengShenXinCaiLiaoYouXianGongSi\"#G" +
|
|||
|
"\r\n#T13.37 #J40.30 #FD/1/L #SS100/BVUN/46X62/0 #VW/L/\"Yulin Hengshen COLtd\"#G" +
|
|||
|
"\r\n#T52.15 #J9.14 #FD/1/L #SS100/BVUN/47X63/0 #VW/L/\"TYPE " + box.Type + "\"#G" +
|
|||
|
"\r\n#T67.39 #J9.14 #FD/1/L #SS100/BVUN/47X63/0 #VW/L/\"QTY/PCS " + box.Qty + "\"#G" +
|
|||
|
"\r\n#T82.63 #J9.14 #FD/1/L #SS100/BVUN/47X63/0 #VW/L/\"LOT NO. " + box.Lot_No + "\"#G" +
|
|||
|
"\r\n#T98.97 #J9.14 #FD/1/L #SS100/BVUN/47X63/0 #VW/L/\"DOM. " + box.Dom_Time.ToString() + "\"#G" +
|
|||
|
"\r\n#T52.15 #J69.25 #FD/1/L #SS100/BVUN/47X63/0 #VW/L/\"SPEC " + box.Spec + "\"#G" +
|
|||
|
"\r\n#T67.39 #J69.25 #FD/1/L #SS100/BVUN/47X63/0 #VW/L/\"CTY./NO. " + box.Net_Weight + "\"#G" +
|
|||
|
"\r\n#T81.19 #J69.25 #FD/1/L #SS100/BVUN/47X63/0 #VW/L/\"LENGTH " + box.Length + "\"#G" +
|
|||
|
"\r\n#T98.21 #J69.25 #FD/1/L #SS100/BVUN/47X63/0 #VW/L/\"EXP. " + box.Exp_Time.ToString() + "\"#G" +
|
|||
|
"\r\n#T41.06 #J6.68 #FD/1/L #SB13/ONKP2.0/20.82/5 #VW/L/\"" + box.Code + "\"#G" +
|
|||
|
"\r\n#Q1#G\r\n#!P1\r\n";
|
|||
|
|
|||
|
var entity = await boxRepository.InsertAsync(box, true);
|
|||
|
var silks = takeQuery.ToList();
|
|||
|
var codes = silks.Select(e => e.Code);
|
|||
|
logger.LogInformation($"从数据库中待装箱编号:{string.Join(";", codes)}");
|
|||
|
|
|||
|
silks.ForEach(e =>
|
|||
|
{
|
|||
|
logger.LogInformation($"{e.Code}已装箱");
|
|||
|
e.BoxId = entity.Id;
|
|||
|
e.Status = 2;
|
|||
|
e.Status_Details = "已装箱";
|
|||
|
});
|
|||
|
await silkRepository.UpdateManyAsync(silks);
|
|||
|
await printerService.PrintAsync(2, content);
|
|||
|
await plcService.ClearTop();
|
|||
|
await plcService.BoxTagPrintDoneAsync((short)1);
|
|||
|
logger.LogInformation($"称重完成,已将数据({box.Id}|{box.Code})插入到box表,同时向打印机发送出标指令");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|