zhuangpei-mesbackend/DOAN.Service/MES/mm/line/MmLineInventoryService.cs
2025-03-31 13:36:19 +08:00

341 lines
11 KiB
C#

using System;
using SqlSugar;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using DOAN.Model;
using DOAN.Repository;
using System.Linq;
using DOAN.Model.MES.mm.line;
using DOAN.Model.MES.mm.line.Dto;
using DOAN.Service.MES.mm.line.IService;
using DOAN.Model.MES.base_;
using DOAN.Model.Mobile.Dto;
using Aliyun.OSS;
namespace DOAN.Service.MES.mm.line
{
/// <summary>
/// mm_line_inventoryService业务层处理
/// </summary>
[AppService(ServiceType = typeof(IMmLineInventoryService), ServiceLifetime = LifeTime.Transient)]
public class MmLineInventoryService : BaseService<MmLineInventory>, IMmLineInventoryService
{
/// <summary>
/// 查询mm_line_inventory列表
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public PagedInfo<MmLineInventoryDto> GetList(MmLineInventoryQueryDto parm)
{
var predicate = Expressionable.Create<MmLineInventory>()
.AndIF(!string.IsNullOrEmpty(parm.LineCode), o => o.LineCode == parm.LineCode)
.AndIF(!string.IsNullOrEmpty(parm.MaterialName), o => o.MaterialName.Contains(parm.MaterialName))
.AndIF(!string.IsNullOrEmpty(parm.MaterialCode), o => o.MaterialCode.Contains(parm.MaterialCode))
;
var response = Queryable()
.Where(predicate.ToExpression())
.ToPage<MmLineInventory, MmLineInventoryDto>(parm);
return response;
}
/// <summary>
/// 获取详情
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public MmLineInventory GetInfo(string Id)
{
var response = Queryable()
.Where(x => x.Id == Id)
.First();
return response;
}
/// <summary>
/// 添加mm_line_inventory
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public MmLineInventory AddMmLineInventory(MmLineInventory model)
{
model.Id = XueHua;
return Context.Insertable(model).ExecuteReturnEntity();
}
/// <summary>
/// 修改mm_line_inventory
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public int UpdateMmLineInventory(MmLineInventory model)
{
//var response = Update(w => w.Id == model.Id, it => new MmLineInventory()
//{
// LocationCode = model.LocationCode,
// MaterialCode = model.MaterialCode,
// MaterialName = model.MaterialName,
// Linecode = model.Linecode,
// Quantity = model.Quantity,
// Unit = model.Unit,
// LastStocktakingTime = model.LastStocktakingTime,
// LastStocktakingNum = model.LastStocktakingNum,
// Updatetime = model.Updatetime,
// Updateby = model.Updateby,
// Createtime = model.Createtime,
// Createby = model.Createby,
//});
//return response;
return Update(model, true);
}
/// <summary>
/// 入库
/// </summary>
/// <param name="LineCode"></param>
/// <param name="MaterialCode"></param>
/// <param name="BatchCode"></param>
/// <param name="LocationCode"></param>
/// <param name="Quantity"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public int putInLineMaterial(string LineCode, string MaterialCode, string BatchCode, string LocationCode,int Quantity)
{
int result = 0;
string materialName = "";
if (string.IsNullOrEmpty(LineCode) || string.IsNullOrEmpty(MaterialCode))
{
throw new Exception("线别或物料编号不能为空");
}
BatchCode = "20250331001";
LocationCode = "001";
//根据线别,物料条码, 看库里有没有这条入库数据
MmLineInventory mmLineInventory = Queryable()
.Where(x => x.LineCode == LineCode && x.MaterialCode == MaterialCode).First();
//判断库存表有没有记录
if (mmLineInventory == null)
{
BaseMaterialBom baseMaterialBom = Context.Queryable<BaseMaterialBom>().Where(o=>o.InvCode==MaterialCode).First();
//根据物料编号去bom 表去查一下
if (baseMaterialBom == null)
{
throw new Exception("Bom表找不到这个物料");
}
materialName = baseMaterialBom.InvName;
//新增一条
result=AddMmLineInventory(LocationCode,MaterialCode, materialName,BatchCode,LineCode,Quantity,"",Quantity);
if (result == 1)
{
OperationLogRecording(1,3, MaterialCode, materialName,LineCode,0,Quantity,LocationCode);
}
}
else
{
//修改
int begin = mmLineInventory.Quantity.Value;
mmLineInventory.Quantity= mmLineInventory.Quantity+Quantity;
mmLineInventory.Updatetime=DateTime.Now;
mmLineInventory.Updateby = "系统";
result = Context.Updateable(mmLineInventory).ExecuteCommand();
if (result == 1)
{
OperationLogRecording(2, 3, MaterialCode, materialName, LineCode, begin, mmLineInventory.Quantity.Value, LocationCode);
}
}
return result;
}
/// <summary>
/// 日志记录
/// </summary>
/// <param name="type">1新增 2修改</param>
/// <param name="TransactionType">1出库 3入库 5盘点</param>
/// <param name="MaterialCode"></param>
/// <param name="MaterialName"></param>
/// <param name="LineCode"></param>
/// <param name="BeginQuantity"></param>
/// <param name="AfterQuantity"></param>
/// <param name="LocationCode"></param>
/// <returns></returns>
public int OperationLogRecording(int type,int TransactionType,string MaterialCode,string MaterialName,string LineCode,int BeginQuantity,int AfterQuantity,string LocationCode)
{
MmLineTransactionLog mmLineTransactionLog = new()
{
Id = XueHua,
TransactionType = TransactionType,
MaterialCode = MaterialCode,
MaterialName = MaterialName,
LineCode = LineCode,
Quantity = AfterQuantity,
LocationCode = LocationCode,
Createtime = DateTime.Now,
Createby="系统"
};
string remark = "";
if (type == 1)
{
remark += "新增一条库存记录";
}
else {
remark += "修改一条库存记录";
}
mmLineTransactionLog.Remark = remark+ string.Format(@" ,变更前库存为:{0},变更后库存为:{1}", BeginQuantity, AfterQuantity);
return Context.Insertable(mmLineTransactionLog).ExecuteCommand();
}
/// <summary>
/// 出库
/// </summary>
/// <param name="LineCode"></param>
/// <param name="MaterialCode"></param>
/// <param name="BatchCode"></param>
/// <param name="LocationCode"></param>
/// <param name="Quantity"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public int outboundLineMaterial(string LineCode, string MaterialCode, string BatchCode, string LocationCode, int Quantity)
{
string materialName = "";
int result = 0;
if (string.IsNullOrEmpty(LineCode) || string.IsNullOrEmpty(MaterialCode))
{
throw new Exception("线别或物料编号不能为空");
}
if (Quantity < 0)
{
throw new Exception("出库数量不能小于0");
}
//根据线别,物料条码,批号 看库里有没有这条入库数据
MmLineInventory mmLineInventory = Queryable()
.Where(x => x.LineCode == LineCode && x.MaterialCode == MaterialCode ).First();
//出库时,如果库存表找不到记录,先新增一条,再进行扣除
if (mmLineInventory == null)
{
BaseMaterialBom baseMaterialBom = Context.Queryable<BaseMaterialBom>().Where(o => o.InvCode == MaterialCode).First();
//根据物料编号去bom 表去查一下
if (baseMaterialBom == null)
{
throw new Exception("Bom表找不到这个物料");
}
materialName = baseMaterialBom.InvName;
//新增一条
result= AddMmLineInventory(LocationCode, MaterialCode, materialName, BatchCode,LineCode,0,"",0);
if (result == 1)
{
//记录日志
OperationLogRecording(1, 3, MaterialCode, materialName, LineCode, 0, 0, LocationCode);
}
//查出来再进行扣除
MmLineInventory mmLineInventory1 = Queryable()
.Where(x => x.LineCode == LineCode && x.MaterialCode == MaterialCode).First();
int begin = mmLineInventory1.Quantity.Value;
mmLineInventory1.Quantity = mmLineInventory1.Quantity - Quantity;
mmLineInventory1.Updatetime = DateTime.Now;
mmLineInventory1.Updateby = "系统";
result = Context.Updateable(mmLineInventory1).ExecuteCommand();
if (result == 1)
{
//记录
OperationLogRecording(2, 1, MaterialCode, materialName, LineCode, begin, mmLineInventory1.Quantity.Value, LocationCode);
}
}
else
{
int begin = mmLineInventory.Quantity.Value;
mmLineInventory.Quantity= mmLineInventory.Quantity-Quantity;
mmLineInventory.Updatetime = DateTime.Now;
mmLineInventory.Updateby = "系统";
result = Context.Updateable(mmLineInventory).ExecuteCommand();
if (result == 1)
{
OperationLogRecording(2, 1, MaterialCode, materialName, LineCode, begin, mmLineInventory.Quantity.Value, LocationCode);
}
}
return result;
}
/// <summary>
/// 库存表新增
/// </summary>
/// <param name="LocationCode"></param>
/// <param name="MaterialCode"></param>
/// <param name="MaterialName"></param>
/// <param name="BatchCode"></param>
/// <param name="LineCode"></param>
/// <param name="Quantity"></param>
/// <param name="Unit"></param>
/// <param name="LastStocktakingNum"></param>
/// <returns></returns>
public int AddMmLineInventory(string LocationCode,string MaterialCode,string MaterialName,string BatchCode,string LineCode, int Quantity,string Unit,int LastStocktakingNum)
{
//新增一条
MmLineInventory mmLineInventory1 = new MmLineInventory();
mmLineInventory1.Id = XueHua;
mmLineInventory1.LocationCode = LocationCode;
mmLineInventory1.MaterialCode = MaterialCode;
mmLineInventory1.MaterialName = MaterialName;
mmLineInventory1.BatchCode = BatchCode;
mmLineInventory1.LineCode = LineCode;
mmLineInventory1.Quantity = Quantity;
mmLineInventory1.Unit = Unit;
mmLineInventory1.LastStocktakingNum = Quantity;
mmLineInventory1.LastStocktakingTime = DateTime.Now;
mmLineInventory1.Createtime = DateTime.Now;
mmLineInventory1.Createby = "系统";
return Context.Insertable(mmLineInventory1).ExecuteCommand();
}
/// <summary>
/// 盘点
/// </summary>
/// <param name="Id"></param>
/// <param name="Quantity"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public int stocktakeLineMaterial(string Id, int Quantity)
{
int result = 0;
if (string.IsNullOrEmpty(Id))
{
throw new Exception("Id不能为空");
}
if (Quantity < 0)
{
throw new Exception("盘点数量不能小于0");
}
MmLineInventory mmLineInventory = Queryable().Where(x => x.Id == Id).First();
if (mmLineInventory== null)
{
throw new Exception("查找不到数据");
}
int begin = mmLineInventory.Quantity.Value;
mmLineInventory.Quantity = Quantity;
mmLineInventory.Updatetime = DateTime.Now;
mmLineInventory.Updateby = "系统";
result = Context.Updateable(mmLineInventory).ExecuteCommand();
if (result == 1)
{
OperationLogRecording(2, 5, mmLineInventory.MaterialCode, mmLineInventory.MaterialName, mmLineInventory.LineCode, begin, Quantity, mmLineInventory.LocationCode);
}
return result;
}
}
}