WMS/HHWMS.Web/Resposity/UnitWork.cs
zhougf 42c26b2800 feat(入库流程): 实现完整的入库流程逻辑
1. 在IUnitWork接口和实现中添加Save方法用于保存更改
2. 完善WCS任务状态处理逻辑,区分线边库和成品库的不同处理流程
3. 实现入库控制器SubmitReceipt方法,包含库位分配、任务创建、库存记录等完整流程
4. 修改LocationTypeEnum枚举值定义,将L改为C表示成品库位
5. 优化库位查询逻辑,增加高度和类型过滤条件
6. 修改容器类型从枚举改为静态类定义
7. 修复LocationController中库位编码生成格式
8. 在WcsRequest中添加LocationType字段用于库位类型区分
2025-06-20 22:20:35 +08:00

258 lines
8.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using HHWMS.Web.Models;
using Microsoft.EntityFrameworkCore;
using NPOI.POIFS.FileSystem;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Z.EntityFramework.Plus;
namespace HHWMS.Web
{
public class UnitWork : EmptyContext, IUnitWork
{
private IDataContext _data;
private LoginUserInfo _info;
public UnitWork(WTMContext context)
{
_data = context.DC;
_info = context.LoginUserInfo;
}
#region find
public T FindSingle<T>(Expression<Func<T, bool>> where) where T : class
{
return _data.Set<T>().AsNoTracking().FirstOrDefault(where);
}
public T FindById<T>(Guid id) where T : class
{
return _data.Set<T>().Find(id);
}
public T FindById<T>(int id) where T : class
{
return _data.Set<T>().Find(id);
}
public IQueryable<T> Find<T>(Expression<Func<T, bool>> where) where T : class
{
return _data.Set<T>().Where(where);
}
public bool IsExit<T>(Expression<Func<T, bool>> where) where T : class
{
return _data.Set<T>().Any(where);
}
public int GetCount<T>(Expression<Func<T, bool>> where) where T : class
{
return _data.Set<T>().Count(where);
}
#endregion find
#region insert
public new int Add<T>(T entity) where T : class
{
if (typeof(T).IsSubclassOf(typeof(BasePoco)))
{
if ((entity as BasePoco).CreateTime == null)
{
(entity as BasePoco).CreateTime = DateTime.Now;
}
if (string.IsNullOrEmpty((entity as BasePoco).CreateBy))
{
(entity as BasePoco).CreateBy = _info?.ITCode;
}
}
_data.Set<T>().Add(entity);
return _data.SaveChanges();
}
public int BatchAdd<T>(IEnumerable<T> entities) where T : class
{
foreach (T entity in entities)
{
//自动设定添加日期和添加人
if (typeof(T).IsSubclassOf(typeof(BasePoco)))
{
if ((entity as BasePoco).CreateTime == null)
{
(entity as BasePoco).CreateTime = DateTime.Now;
}
if (string.IsNullOrEmpty((entity as BasePoco).CreateBy))
{
(entity as BasePoco).CreateBy = _info?.ITCode;
}
}
}
_data.Set<T>().AddRange(entities);
return _data.SaveChanges();
}
public int AddToHistory<T>(string name, Expression<Func<T, bool>> where, Expression<Func<T, object>> entity) where T : class
{
return _data.Set<T>().Where(where).InsertFromQuery(name,entity);
}
#endregion insert
#region update
public new int Update<T>(T entity) where T : class
{
if (typeof(T).IsSubclassOf(typeof(BasePoco)))
{
(entity as BasePoco).UpdateTime = DateTime.Now;
(entity as BasePoco).UpdateBy = _info?.ITCode;
}
_data.Set<T>().Update(entity);
return _data.SaveChanges();
}
public int BatchUpdate<T>(IEnumerable<T> entities) where T : class
{
foreach (T entity in entities)
{
//自动设定添加日期和添加人
if (typeof(T).IsSubclassOf(typeof(BasePoco)))
{
if ((entity as BasePoco).CreateTime == null)
{
(entity as BasePoco).CreateTime = DateTime.Now;
}
if (string.IsNullOrEmpty((entity as BasePoco).CreateBy))
{
(entity as BasePoco).CreateBy = _info?.ITCode;
}
}
}
_data.Set<T>().UpdateRange(entities);
return _data.SaveChanges();
}
public int BatchUpdate<T>(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity) where T : class
{
return _data.Set<T>().Where(where).Update(entity);
}
#endregion update
#region delete
public int Delete<T>(T entity) where T : class
{
_data.Set<T>().Remove(entity);
return _data.SaveChanges();
}
public int BatchDelete<T>(IEnumerable<T> entities) where T : class
{
if (_data.DBType.Equals(DBTypeEnum.SqlServer))
{
return _data.Set<T>().DeleteRangeByKey(entities);
}
else
{
_data.Set<T>().RemoveRange(entities);
return _data.SaveChanges();
}
}
public int Save()
{
return _data.SaveChanges();
}
public int BatchDelete<T>(Expression<Func<T, bool>> where) where T : class
{
return _data.Set<T>().Where(where).Delete();
}
/// <summary>
/// 测试 不支持mysql
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entity"></param>
/// <returns></returns>
public int DeleteById<T>(T entity) where T : class
{
return _data.Set<T>().DeleteByKey((entity as BasePoco).ID);
}
/// <summary>
/// 测试 不支持mysql
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="id"></param>
/// <returns></returns>
public int DeleteById<T>(int id) where T : class
{
return _data.Set<T>().DeleteByKey(id);
}
#endregion delete
public string GetSerialNumber(string prefix, int length = 4)
{
int seq = 0;
using (var tran = _data.BeginTransaction())
{
try
{
var syscount = FindSingle<SysCount>(x => x.Prefix == prefix);
if (syscount == null)
{
syscount = new SysCount
{
Prefix = prefix,
Value = 1,
UpdateTime = DateTime.Now
};
Add(syscount);
seq = 1;
}
else
{
// 判断日期是否是当天,否则从1开始
if (DateTime.Now.Day == syscount.UpdateTime.Value.Day)
{
_data.Set<SysCount>().Where(x => x.Prefix == prefix)
.Update(x => new SysCount
{
Value = x.Value + 1,
UpdateTime = DateTime.Now
});
seq = FindSingle<SysCount>(x => x.Prefix == prefix).Value;
}
else
{
_data.Set<SysCount>().Where(x => x.Prefix == prefix)
.Update(x => new SysCount
{
Value = 1,
UpdateTime = DateTime.Now
});
seq = 1;
}
}
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
throw;
}
}
return prefix + DateTime.Now.ToString("yyyyMMdd") + seq.ToString().PadLeft(length, '0');
}
}
}