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

434 lines
17 KiB
C#

using System;
using System.Linq;
using Infrastructure.Attribute;
using SqlSugar;
using ZR.Model;
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(IWmGoodsNowProductionService),
ServiceLifetime = LifeTime.Transient
)]
public class WmGoodsNowProductionService
: BaseService<WmGoodsNowProduction>,
IWmGoodsNowProductionService
{
/// <summary>
/// 查询成品库当前货物表列表
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public PagedInfo<WmGoodsNowProductionDto> GetList(WmGoodsNowProductionQueryDto parm)
{
List<string> partnumberByDescription = new();
if (parm != null && !string.IsNullOrEmpty(parm.Description))
{
partnumberByDescription = Context
.Queryable<WmMaterial>()
.Where(it => it.Description.Contains(parm.Description))
.Select(it => it.Partnumber)
.ToList();
}
var predicate = Expressionable
.Create<WmGoodsNowProduction>()
.AndIF(
partnumberByDescription.Count > 0,
it => partnumberByDescription.Contains(it.Partnumber)
)
.AndIF(
!string.IsNullOrEmpty(parm.Partnumber),
it => it.Partnumber.Contains(parm.Partnumber)
)
.AndIF(
!string.IsNullOrEmpty(parm.PackageCodeClient),
it => it.PackageCodeClient.Contains(parm.PackageCodeClient)
)
.AndIF(
parm.EntryWarehouseTimeStart != null,
it => it.EntryWarehouseTime > parm.EntryWarehouseTimeStart
)
.AndIF(
parm.EntryWarehouseTimeEnd != null,
it => it.EntryWarehouseTime < parm.EntryWarehouseTimeEnd
)
.AndIF(
!string.IsNullOrEmpty(parm.PackageCodeClient),
it => it.PackageCodeClient.Contains(parm.PackageCodeClient)
)
.AndIF(
!string.IsNullOrEmpty(parm.LocationCode),
it => it.LocationCode.Contains(parm.LocationCode)
);
var response = Context
.Queryable<WmGoodsNowProduction>()
.Where(predicate.ToExpression())
.OrderByDescending(x => x.EntryWarehouseTime)
.ToPage<WmGoodsNowProduction, WmGoodsNowProductionDto>(parm);
foreach (WmGoodsNowProductionDto item in response.Result)
{
WmMaterial material = Context
.Queryable<WmMaterial>()
.Where(it => it.Partnumber == item.Partnumber)
.Where(it => it.Type == 1)
.First();
if (material == null)
{
item.Description = "此零件号不在物料清单内!";
continue;
}
item.Description = !string.IsNullOrEmpty(material.Description)
? material.Description
: material.ProductName;
}
return response;
}
/// <summary>
/// 批量查看
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public (List<WmGoods_nodeDto>, int) QuerypatchsearchList(WmGoodsNowProductionQueryDto parm)
{
List<WmGoods_nodeDto> list = null;
int total = 0;
var predicate = Expressionable
.Create<WmGoodsNowProduction>()
.AndIF(
!string.IsNullOrEmpty(parm.Partnumber),
it => it.Partnumber.Contains(parm.Partnumber)
)
.AndIF(
!string.IsNullOrEmpty(parm.PackageCodeClient),
it => it.PackageCodeClient.Contains(parm.PackageCodeClient)
);
List<WmGoodsNowProduction> rawdatas = Queryable()
.Where(predicate.ToExpression())
.ToPageList(parm.PageNum, parm.PageSize, ref total);
if (rawdatas != null && rawdatas.Count > 0)
{
//todo 对字段进行拆分
List<WmGoods_nodeDto> WmGoods_nodeDto_list = rawdatas
.Select(p => new WmGoods_nodeDto()
{
Id = p.Id,
PackageCode = p.PackageCode,
PackageCodeClient_short_parent = p.PackageCodeClient.Split("_")[0],
PackageCodeClient_son = p.PackageCodeClient,
PackageCodeOriginal = p.PackageCodeClient,
LocationCode = p.LocationCode,
Partnumber = p.Partnumber,
Pack_num = 1,
GoodsNumLogic = p.GoodsNumLogic,
GoodsNumAction = p.GoodsNumAction,
EntryWarehouseTime = p.EntryWarehouseTime,
Remark = p.Remark,
})
.ToList();
//todo 分组 聚合 生成父节点
List<WmGoods_nodeDto> WmGoods_nodeDto_list_parent = WmGoods_nodeDto_list
.GroupBy(p => p.PackageCodeClient_short_parent)
.Select(group => new WmGoods_nodeDto()
{
PackageCodeClient_short_parent = "",
PackageCodeClient_son = group.Key,
GoodsNumLogic = group.Sum(group => group.GoodsNumLogic),
GoodsNumAction = group.Sum(group => group.GoodsNumAction),
Partnumber = group.Max(group => group.Partnumber),
Pack_num = group.Count()
})
.ToList();
//todo 合并
list = WmGoods_nodeDto_list.Concat(WmGoods_nodeDto_list_parent).ToList();
}
return (list, total);
}
/// <summary>
/// 移动端 货物查看 根据Query零件号与批次号查看信息
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public List<WmGoodShortPackageCodeDto> QueryshortPatch(CommonQueryDto parm)
{
// 结果集
List<WmGoodShortPackageCodeDto> resultList = new();
// 全数据处理
var predicate = Expressionable
.Create<WmGoodsNowProduction>()
.OrIF(!string.IsNullOrEmpty(parm.Query), it => it.Partnumber.Contains(parm.Query))
.OrIF(
!string.IsNullOrEmpty(parm.Query),
it => it.PackageCodeClient.Contains(parm.Query)
);
List<WmGoodsNowProduction> wmGoodsNowsList = Context
.Queryable<WmGoodsNowProduction>()
.Where(predicate.ToExpression())
.OrderByDescending(it => it.PackageCodeClient)
.ToList();
// 聚合数据
resultList = wmGoodsNowsList
.GroupBy(it => it.PackageCodeClient.Split('_')[0])
.Select(group => new WmGoodShortPackageCodeDto
{
ShortPackageCode = group.Key,
Partnumber = group.Max(item => item.Partnumber),
EntryWarehouseTime = group.Max(item => item.EntryWarehouseTime),
PackageNumber = group.Count(),
PartnumberNumber = group.Sum(item => item.GoodsNumAction)
})
.ToList();
// 结果数据处理
//每页多少条
int rows = parm.PageSize;
//第几页
int page = parm.PageNum;
//每一页开始下标
int startIndex = (page - 1) * rows;
//数据总数
int sum = resultList.Count;
if (startIndex + rows > sum)
{
resultList = resultList.Skip(startIndex).Take(sum).ToList();
}
else
{
resultList = resultList.Skip(startIndex).Take(startIndex + rows).ToList();
}
return resultList;
}
/// <summary>
/// 移动端 短批次号详情
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public List<WmGoodsNowProductionDto> Patchsearchdetail(WmGoodsNowProductionQueryDto parm)
{
var predicate = Expressionable
.Create<WmGoodsNowProduction>()
.AndIF(
!string.IsNullOrEmpty(parm.PackageCodeClient),
it => it.PackageCodeClient.Contains(parm.PackageCodeClient)
);
var response = Queryable()
.Where(predicate.ToExpression())
.OrderBy(it => it.PackageCodeClient)
.Select(it => new WmGoodsNowProductionDto
{
PackageCodeClient = it.PackageCodeClient,
LocationCode = it.LocationCode,
Partnumber = it.Partnumber,
GoodsNumAction = it.GoodsNumAction,
EntryWarehouseTime = it.EntryWarehouseTime,
Remark = (!string.IsNullOrEmpty(it.Remark) ? it.Remark : "无备注"),
})
.ToList();
foreach (WmGoodsNowProductionDto item in response)
{
WmMaterial material = Context
.Queryable<WmMaterial>()
.Where(it => it.Partnumber == item.Partnumber)
.First();
if (material == null)
{
item.Description = "此零件号不在物料清单内!";
continue;
}
item.Description = !string.IsNullOrEmpty(material.Description)
? material.Description
: material.ProductName;
}
return response;
}
/// <summary>
/// 获取详情
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public WmGoodsNowProduction GetInfo(string Id)
{
var response = Queryable().Where(x => x.Id == Id).First();
return response;
}
/// <summary>
/// 添加成品库当前货物表
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public WmGoodsNowProduction AddWmGoodsNowProduction(WmGoodsNowProduction model)
{
if (string.IsNullOrEmpty(model.Id))
{
model.Id = SnowFlakeSingle.Instance.NextId().ToString(); //也可以在程序中直接获取ID
}
if (string.IsNullOrEmpty(model.PackageCode))
{
model.PackageCode = model.PackageCodeClient;
}
if (string.IsNullOrEmpty(model.PackageCodeOriginal))
{
model.PackageCodeOriginal = model.PackageCodeClient;
}
bool hasRecord = Context
.Queryable<WmGoodsNowProduction>()
.Where(it => it.PackageCodeClient == model.PackageCodeClient)
.Any();
if (hasRecord)
{
throw new Exception("成品仓库已有相同批次号记录");
}
return Context.Insertable(model).ExecuteReturnEntity();
}
/// <summary>
/// 修改成品库当前货物表
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public int UpdateWmGoodsNowProduction(WmGoodsNowProduction model)
{
//var response = Update(w => w.Id == model.Id, it => new WmGoodsNowProduction()
//{
// PackageCode = model.PackageCode,
// PackageCodeClient = model.PackageCodeClient,
// PackageCodeOriginal = model.PackageCodeOriginal,
// LocationCode = model.LocationCode,
// Partnumber = model.Partnumber,
// GoodsNumLogic = model.GoodsNumLogic,
// GoodsNumAction = model.GoodsNumAction,
// EntryWarehouseTime = model.EntryWarehouseTime,
// Remark = model.Remark,
// UpdatedBy = model.UpdatedBy,
// UpdatedTime = model.UpdatedTime,
// CreatedBy = model.CreatedBy,
// CreatedTime = model.CreatedTime,
//});
//return response;
return Update(model, true);
}
/// <summary>
/// 修改实际库存
/// </summary>
/// <param name="id"></param>
/// <param name="stack_num"></param>
/// <returns></returns>
public int ModifyInventoryQuantity(string id, int stack_num)
{
return Context
.Updateable<WmGoodsNowProduction>()
.SetColumns(it => it.GoodsNumAction == stack_num)
.Where(it => it.Id == id)
.ExecuteCommand();
}
public PagedInfo<WmGoodsNowProductionDictDto> GetDictData(WmGoodsNowProductionDictDto parm)
{
var predicate = Expressionable
.Create<WmGoodsNowProduction>()
.AndIF(!string.IsNullOrEmpty(parm.Query), it => it.Partnumber.Contains(parm.Query))
.Or(it => it.PackageCodeClient.Contains(parm.Query))
// .Or(it => it.LocationCode.Contains(parm.Query))
;
var response = Queryable()
.Where(predicate.ToExpression())
.Select(it => new WmGoodsNowProductionDictDto
{
Id = it.Id,
PackageCodeClient = it.PackageCodeClient,
LocationCode = it.LocationCode,
Partnumber = it.Partnumber,
Description = string.Empty,
GoodsNumAction = it.GoodsNumAction,
GoodsNumLogic = it.GoodsNumLogic,
Label = string.Empty,
})
.ToPage(parm);
foreach (WmGoodsNowProductionDictDto item in response.Result)
{
WmMaterial material = Context
.Queryable<WmMaterial>()
.Where(it => it.Partnumber == item.Partnumber)
.First();
if (material == null)
{
item.Description = "此零件号不在物料清单内!";
}
else
{
item.Description = !string.IsNullOrEmpty(material.Description)
? material.Description
: material.ProductName;
}
string label =
"批次号:"
+ item.PackageCodeClient
+ " 零件号:"
+ item.Partnumber
+ " 描述:"
+ item.Description;
var value = new
{
Id = item.Id,
PackageCodeClient = item.PackageCodeClient,
LocationCode = item.LocationCode,
Partnumber = item.Partnumber,
Description = item.Description,
GoodsNumAction = item.GoodsNumAction,
GoodsNumLogic = item.GoodsNumLogic,
};
item.Value = value;
item.Label = label;
}
return response;
}
public int BatchUpdateLocationCode(BatchUpdateLocationCodeDto parm)
{
try
{
List<WmGoodsNowProduction> updateList = new List<WmGoodsNowProduction>();
foreach (string id in parm.Ids)
{
WmGoodsNowProduction nowProduction = new()
{
Id = id,
LocationCode = parm.LocationCode,
UpdatedBy = parm.UpdatedBy,
UpdatedTime = parm.UpdatedTime,
};
updateList.Add(nowProduction);
}
return Context.Updateable(updateList).IgnoreColumns(ignoreAllNullColumns:true).ExecuteCommand();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
}
}