2025-06-25 16:06:49 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-07-01 14:20:07 +08:00
|
|
|
|
using Seyounth.Auto.Hs.Runtime.Printer;
|
2025-06-25 16:06:49 +08:00
|
|
|
|
using Seyounth.Auto.Hs.Runtime.Scanner;
|
2025-07-01 14:20:07 +08:00
|
|
|
|
using Syc.Abp.Application.Contracts;
|
2025-06-25 16:06:49 +08:00
|
|
|
|
using Syc.Basic.Web.WMS.Entitys;
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 默认的扫码枪扫码触发事件处理
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class DefaultScannerEventHandle : IScannerEventHandle
|
|
|
|
|
{
|
2025-07-01 14:20:07 +08:00
|
|
|
|
private readonly IRepository<Produce> produceRepository;
|
|
|
|
|
private readonly IPrinterService printerService;
|
|
|
|
|
private readonly IRepository<Box> boxRepository;
|
2025-06-25 16:06:49 +08:00
|
|
|
|
private readonly IRepository<Silk> silkRepository;
|
|
|
|
|
private readonly IUnitOfWorkManager uowm;
|
2025-07-01 14:20:07 +08:00
|
|
|
|
private readonly ILogger<DefaultScannerEventHandle> logger;
|
2025-06-25 16:06:49 +08:00
|
|
|
|
|
2025-07-01 14:20:07 +08:00
|
|
|
|
public DefaultScannerEventHandle(IRepository<Produce> produceRepository,IPrinterService printerService,IRepository<Box> boxRepository,IRepository<Silk> silkRepository,IUnitOfWorkManager unitOfWork,ILogger<DefaultScannerEventHandle> logger)
|
2025-06-25 16:06:49 +08:00
|
|
|
|
{
|
2025-07-01 14:20:07 +08:00
|
|
|
|
this.produceRepository = produceRepository;
|
|
|
|
|
this.printerService = printerService;
|
|
|
|
|
this.boxRepository = boxRepository;
|
2025-06-25 16:06:49 +08:00
|
|
|
|
this.silkRepository = silkRepository;
|
|
|
|
|
this.uowm = unitOfWork;
|
2025-07-01 14:20:07 +08:00
|
|
|
|
this.logger = logger;
|
2025-06-25 16:06:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 扫码枪
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="code"></param>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task ExecAsync(string code, int id)
|
|
|
|
|
{
|
|
|
|
|
using (var uow = uowm.Reserve(UnitOfWork.UnitOfWorkReservationName))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-07-01 14:20:07 +08:00
|
|
|
|
if (id == 1)
|
|
|
|
|
await Yarn(code);
|
|
|
|
|
else
|
|
|
|
|
await Box(code);
|
2025-06-25 16:06:49 +08:00
|
|
|
|
await uow.CompleteAsync();
|
|
|
|
|
}
|
2025-07-01 14:20:07 +08:00
|
|
|
|
catch (Exception ex) when (ex is FriendlyException friendlyException)
|
|
|
|
|
{
|
|
|
|
|
logger.LogError(ex.GetBaseException(),"扫码报错");
|
|
|
|
|
await WebSocketManager.SocketManager.BroadcastAsync(friendlyException.Message);
|
|
|
|
|
await uow.RollbackAsync();
|
|
|
|
|
}
|
2025-06-25 16:06:49 +08:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-07-01 14:20:07 +08:00
|
|
|
|
logger.LogError(ex.GetBaseException(), "扫码报错");
|
2025-06-25 16:06:49 +08:00
|
|
|
|
await uow.RollbackAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-01 14:20:07 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 整箱扫码
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="code"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task Box(string code)
|
|
|
|
|
{
|
|
|
|
|
//if (!QueueManage.BoxQueue.Contains(code))
|
|
|
|
|
//{
|
|
|
|
|
// logger.LogInformation($"{code} 加入box集合");
|
|
|
|
|
// QueueManage.BoxQueue.Add(code);
|
|
|
|
|
//}
|
|
|
|
|
//else
|
|
|
|
|
//{
|
|
|
|
|
// logger.LogWarning($"{code}已在box集合中无需重复扫码");
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 丝锭扫码
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="code"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task Yarn(string code)
|
|
|
|
|
{
|
|
|
|
|
//throw Oops.Oh("异常返回前端测试");
|
|
|
|
|
logger.LogInformation($"接收到丝锭条码:{code}");
|
|
|
|
|
var produce = await produceRepository.FirstOrDefaultAsync(e => e.IfUse == 1 && e.IsDelete == 0);
|
|
|
|
|
var b = await silkRepository.AnyAsync(e => e.Code == code && e.IsDelete == 0);
|
|
|
|
|
if (produce is null)
|
|
|
|
|
{
|
|
|
|
|
throw Oops.Oh("无生产信息,请先设置生产信息再扫码");
|
|
|
|
|
}
|
|
|
|
|
if (b)
|
|
|
|
|
{
|
|
|
|
|
throw Oops.Oh("条码已存在");
|
|
|
|
|
}
|
|
|
|
|
logger.LogInformation($"当前生产信息:{produce.Name}|{produce.Spec}|{produce.Lot_No}|{produce.BoxSpec}|{produce.Qty}");
|
|
|
|
|
|
|
|
|
|
Silk silk = new Silk()
|
|
|
|
|
{
|
|
|
|
|
Code = code,
|
|
|
|
|
Length = produce.Length,
|
|
|
|
|
Lot_No = produce.Lot_No,
|
|
|
|
|
Name = produce.Name,
|
|
|
|
|
Date = DateTime.Now,
|
|
|
|
|
Type = produce.Type,
|
|
|
|
|
Status = 0,
|
|
|
|
|
Status_Details = "已扫码待称重",
|
|
|
|
|
Net_Weight = 0,
|
|
|
|
|
Createtime = DateTime.Now,
|
|
|
|
|
};
|
|
|
|
|
silk = await silkRepository.InsertAsync(silk);
|
|
|
|
|
logger.LogInformation($"丝锭:{code} 已添加到数据库");
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-25 16:06:49 +08:00
|
|
|
|
public async Task ExecAsync2(string code, int id)
|
|
|
|
|
{
|
|
|
|
|
using (var uow = uowm.Reserve(UnitOfWork.UnitOfWorkReservationName))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* 如果有其他的处理写在这里,或者另外建一个类实现IScannerEventHandle接口,
|
|
|
|
|
*/
|
2025-07-01 14:20:07 +08:00
|
|
|
|
var msg = new DeviceMessage(id, "扫码枪", code);
|
2025-06-25 16:06:49 +08:00
|
|
|
|
if (!code.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
var result = await silkRepository.AnyAsync(x => x.Code == code);
|
|
|
|
|
if (result)
|
|
|
|
|
msg.Value = $"存在重复编号({code})";
|
|
|
|
|
}
|
|
|
|
|
await WebSocketManager.SocketManager.BroadcastAsync(msg.ToJsonString());
|
|
|
|
|
await uow.CompleteAsync();
|
|
|
|
|
}
|
2025-07-01 14:20:07 +08:00
|
|
|
|
catch (Exception ex) when (ex is FriendlyException friendlyException)
|
|
|
|
|
{
|
|
|
|
|
await WebSocketManager.SocketManager.BroadcastAsync(friendlyException.Message);
|
|
|
|
|
await uow.RollbackAsync();
|
|
|
|
|
}
|
2025-06-25 16:06:49 +08:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
await uow.RollbackAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|