shgx_tz_mom/ZR.Service/mes/wms/WmBlankRecordService.cs

405 lines
15 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 Infrastructure.Attribute;
using SqlSugar;
using System;
using ZR.Model;
using ZR.Model.MES.pro;
using ZR.Model.MES.wms;
using ZR.Model.MES.wms.Dto;
using ZR.Repository;
using ZR.Service.mes.wms.IService;
namespace ZR.Service.mes.wms
{
/// <summary>
/// 毛坯库存库存变动记录表Service业务层处理
/// </summary>
[AppService(ServiceType = typeof(IWmBlankRecordService), ServiceLifetime = LifeTime.Transient)]
public class WmBlankRecordService : BaseService<WmBlankRecord>, IWmBlankRecordService
{
/// <summary>
/// 查询毛坯库存库存变动记录表列表
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public PagedInfo<WmBlankRecordDto> GetList(WmBlankRecordQueryDto parm)
{
DateTime startTime = parm.CreatedTimeStart ?? new DateTime(1999, 1, 1, 0, 0, 0);
DateTime endTime = parm.CreatedTimeEnd ?? new DateTime(1999, 1, 1, 0, 0, 0);
var predicate = Expressionable
.Create<WmBlankRecord>()
.AndIF(
!string.IsNullOrEmpty(parm.FkBlankInventoryId),
it => it.FkBlankInventoryId == parm.FkBlankInventoryId
)
.AndIF(
!string.IsNullOrEmpty(parm.BlankNum),
it => it.BlankNum.Contains(parm.BlankNum)
)
// .AndIF(parm.Status > -1, it => it.Status == parm.Status)
.AndIF(parm.Type > 0, it => it.Type == parm.Type)
.AndIF(
parm.CreatedTimeStart > new DateTime(1999, 1, 1, 0, 0, 0),
it => it.CreatedTime >= startTime.ToLocalTime()
)
.AndIF(
parm.CreatedTimeEnd > new DateTime(1999, 1, 1, 0, 0, 0),
it => it.CreatedTime <= endTime.ToLocalTime()
);
var response = Queryable()
.Where(predicate.ToExpression())
.OrderByDescending(it => it.CreatedTime)
.ToPage<WmBlankRecord, WmBlankRecordDto>(parm);
if (response.Result.Count > 0)
{
foreach (WmBlankRecordDto item in response.Result)
{
WmMaterial material = Context
.Queryable<WmMaterial>()
.Where(it => it.BlankNum == item.BlankNum)
.Where(it => it.Type == 2)
.First();
if (material == null)
{
item.Description = "此毛坯号不在物料清单内!";
continue;
}
item.Color = material.Color;
item.Unit = material.Unit;
item.Version = material.Version;
item.Specification = material.Specification;
item.Description = !string.IsNullOrEmpty(material.Description)
? material.Description
: material.ProductName;
}
}
return response;
}
/// <summary>
/// 获取详情
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public WmBlankRecord GetInfo(string Id)
{
var response = Queryable().Where(x => x.Id == Id).First();
return response;
}
/// <summary>
/// 添加毛坯库存库存变动记录表
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public WmBlankRecord AddWmBlankRecord(WmBlankRecord model)
{
model.Id = SnowFlakeSingle.Instance.NextId().ToString();
return Context.Insertable(model).ExecuteReturnEntity();
}
/// <summary>
/// 修改毛坯库存库存变动记录表
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public int UpdateWmBlankRecord(WmBlankRecord model)
{
//var response = Update(w => w.Id == model.Id, it => new WmBlankRecord()
//{
// BlankNum = model.BlankNum,
// ChangeQuantity = model.ChangeQuantity,
// Type = model.Type,
// Status = model.Status,
// Remark = model.Remark,
// CreatedBy = model.CreatedBy,
// CreatedTime = model.CreatedTime,
// UpdatedBy = model.UpdatedBy,
// UpdatedTime = model.UpdatedTime,
//});
//return response;
return Update(model, true);
}
/// <summary>
/// 增加库存
/// </summary>
/// <param name="id"></param>
/// <param name="fk_blank_inventory_id"></param>
/// <param name="change_quantity"></param>
/// <returns></returns>
public int AddInventory(
string fkBlankInventoryId,
string blankNum,
int changeQuantity,
string username,
string remark,
DateTime? actionTime
)
{
bool hasRecord = Context
.Queryable<WmBlankInventory>()
.Where(it => it.Id == fkBlankInventoryId)
.Any();
if (!hasRecord)
{
throw new Exception("无此毛坯号记录!");
}
// 增加库存
int result = Context
.Updateable<WmBlankInventory>()
.SetColumns(it => it.Quantity == it.Quantity + changeQuantity)
.Where(it => it.Id == fkBlankInventoryId)
.ExecuteCommand();
if (result == 1)
{
WmBlankRecord res = new WmBlankRecord();
res.Id = SnowFlakeSingle.Instance.NextId().ToString();
res.FkBlankInventoryId = fkBlankInventoryId;
res.BlankNum = blankNum;
res.ChangeQuantity = changeQuantity;
res.Type = 1;
res.Status = 1;
res.ActionTime = actionTime;
res.Remark = remark;
res.CreatedBy = username;
res.CreatedTime = DateTime.Now;
//填写库存记录
Context.Insertable(res).ExecuteCommand();
}
return result;
}
/// <summary>
/// 删除库存
/// </summary>
/// <param name="id"></param>
/// <param name="fk_blank_inventory_id"></param>
/// <param name="change_quantity"></param>
/// <returns></returns>
public int DeleteInventory(
string fkBlankInventoryId,
string blankNum,
int changeQuantity,
string username,
string remark,
DateTime? actionTime
)
{
int resut = 0;
bool tranReuslt = UseTran2(() =>
{
WmBlankInventory record = Context
.Queryable<WmBlankInventory>()
.Where(it => it.Id == fkBlankInventoryId)
.Where(it => it.BlankNum == blankNum)
.First();
if (record != null && record.Quantity < changeQuantity)
{
resut = 0 - changeQuantity;
throw new Exception("所填变动数字大于库存,无法减少!");
}
else if (record == null)
{
resut = -1;
throw new Exception("无此毛坯号记录!");
}
else
{
resut = Context
.Updateable<WmBlankInventory>()
.SetColumns(it => it.Quantity == it.Quantity - changeQuantity)
.Where(it => it.Id == fkBlankInventoryId)
.ExecuteCommand();
if (resut == 1)
{
WmBlankRecord res = new WmBlankRecord();
res.Id = SnowFlakeSingle.Instance.NextId().ToString();
res.FkBlankInventoryId = fkBlankInventoryId;
res.BlankNum = blankNum;
res.ChangeQuantity = changeQuantity;
res.Type = 2;
res.Status = 1;
res.Remark = remark;
res.ActionTime = actionTime;
res.CreatedBy = username;
res.CreatedTime = DateTime.Now;
//填写库存记录
Context.Insertable(res).ExecuteCommand();
}
}
});
if (!tranReuslt)
{
throw new Exception("操作执行失败,事务失败!");
}
return resut;
}
/// <summary>
/// 盘点库存
/// </summary>
/// <param name="fkBlankInventoryId"></param>
/// <param name="blankNum"></param>
/// <param name="changeQuantity"></param>
/// <param name="username"></param>
/// <param name="remark"></param>
/// <param name="actionTime"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public int DoStocktakingBlankInventory(
string fkBlankInventoryId,
string blankNum,
int changeQuantity,
string username,
string remark,
DateTime? actionTime
)
{
try
{
Context.Ado.BeginTran();
DateTime nowTime = DateTime.Now.ToLocalTime();
// 原始数据记录
WmBlankInventory oldInfo = Context
.Queryable<WmBlankInventory>()
.Where(it => it.Id == fkBlankInventoryId)
.First();
if (oldInfo == null)
{
throw new Exception("毛坯记录不存在!");
}
WmBlankInventory updateInfo =
new()
{
Id = fkBlankInventoryId,
Quantity = changeQuantity,
UpdatedBy = username,
UpdatedTime = nowTime
};
int res1 = Context
.Updateable(updateInfo)
.IgnoreColumns(ignoreAllNullColumns: true)
.ExecuteCommand();
WmBlankRecord res =
new()
{
Id = SnowFlakeSingle.Instance.NextId().ToString(),
FkBlankInventoryId = fkBlankInventoryId,
BlankNum = blankNum,
ChangeQuantity = changeQuantity,
Type = 4,
Status = 1,
ActionTime = actionTime,
Remark =
remark + "\n自动添加备注盘点原箱数" + oldInfo.Quantity + ",新箱数" + changeQuantity,
CreatedBy = username,
CreatedTime = nowTime
};
//填写库存记录
Context.Insertable(res).ExecuteCommand();
Context.Ado.CommitTran();
return res1;
}
catch (Exception e)
{
Context.Ado.RollbackTran();
throw new Exception(e.Message);
}
}
public int DoOutboundByWorkOrderId(string workOrderId, int changeQuantity, string username)
{
try
{
Context.Ado.BeginTran();
int type = 1;
DateTime nowTime = DateTime.Now.ToLocalTime();
// 提出工单
ProWorkorder_v2 workOrderInfo = Context
.Queryable<ProWorkorder_v2>()
.Where(it => it.ClientWorkorder == workOrderId)
.First();
if (workOrderInfo == null)
{
Context.Ado.RollbackTran();
throw new Exception("工单记录不存在!" + workOrderId);
}
//TODO 20250325 remark1 存在不扣,则不扣除
if (workOrderInfo.Remark1 != null)
{
if (workOrderInfo.Remark1.Contains("不扣"))
{
Context.Ado.RollbackTran();
return 0;
}
if (workOrderInfo.Remark1.Contains("返工"))
{
type = 2;
}
}
// 根据工单查看毛坯库存数据记录
WmBlankInventory blankInventory = Context
.Queryable<WmBlankInventory>()
.Where(it => it.BlankNum == workOrderInfo.BlankNumber)
.Where(it => it.Status == 1)
.Where(it => it.Type == type)
.First();
if (blankInventory == null)
{
Context.Ado.RollbackTran();
throw new Exception("毛坯记录不存在!请检查毛坯仓库,毛坯号:" + workOrderInfo.BlankNumber);
}
WmBlankInventory updateInfo =
new()
{
Id = blankInventory.Id,
Quantity = blankInventory.Quantity - workOrderInfo.PreviousNumber,
UpdatedBy = username,
UpdatedTime = nowTime
};
int res1 = Context
.Updateable(updateInfo)
.IgnoreColumns(ignoreAllNullColumns: true)
.ExecuteCommand();
WmBlankRecord res =
new()
{
Id = SnowFlakeSingle.Instance.NextId().ToString(),
FkBlankInventoryId = blankInventory.Id,
BlankNum = blankInventory.BlankNum,
ChangeQuantity = workOrderInfo.PreviousNumber,
Type = 2,
Status = 1,
ActionTime = nowTime,
Remark =
"自动添加备注:工单号"
+ workOrderId
+ ",上件数"
+ workOrderInfo.PreviousNumber
+ "包装完成自动出库。",
CreatedBy = username,
CreatedTime = nowTime
};
//添加库存记录
Context.Insertable(res).ExecuteCommand();
Context.Ado.CommitTran();
return res1;
}
catch (Exception e)
{
Context.Ado.RollbackTran();
throw new Exception(e.Message);
}
}
}
}