222 lines
8.1 KiB
C#
222 lines
8.1 KiB
C#
using System;
|
||
using SqlSugar;
|
||
using Infrastructure.Attribute;
|
||
using Infrastructure.Extensions;
|
||
using DOAN.Model;
|
||
using DOAN.Model.Dto;
|
||
using DOAN.Model.MES.base_;
|
||
using DOAN.Model.MES.base_.Dto;
|
||
using DOAN.Repository;
|
||
using DOAN.Service.MES.base_.IService;
|
||
|
||
using System.Linq;
|
||
using Microsoft.IdentityModel.Tokens;
|
||
using Mapster;
|
||
using System.Xml.Linq;
|
||
using static Org.BouncyCastle.Crypto.Engines.SM2Engine;
|
||
|
||
|
||
namespace DOAN.Service.MES.base_
|
||
{
|
||
/// <summary>
|
||
/// 物料清单Service业务层处理
|
||
/// </summary>
|
||
[AppService(ServiceType = typeof(IBaseMaterialBOMService), ServiceLifetime = LifeTime.Transient)]
|
||
public class BaseMaterialBOMService : BaseService<BaseMaterialList>, IBaseMaterialBOMService
|
||
{
|
||
/// <summary>
|
||
/// 查询物料清单列表
|
||
/// </summary>
|
||
/// <param name="parm"></param>
|
||
/// <returns></returns>
|
||
public PagedInfo<BaseMaterialListDto> GetList(BaseMaterialListQueryDto2 parm)
|
||
{
|
||
var predicate = Expressionable.Create<BaseMaterialList>();
|
||
|
||
var response = Queryable()
|
||
.Where(predicate.ToExpression())
|
||
.ToPage<BaseMaterialList, BaseMaterialListDto>(parm);
|
||
|
||
return response;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取详情
|
||
/// </summary>
|
||
/// <param name="Id"></param>
|
||
/// <returns></returns>
|
||
public BaseMaterialList GetInfo(string Id)
|
||
{
|
||
var response = Queryable()
|
||
.Where(x => x.Id == Id)
|
||
.First();
|
||
|
||
return response;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加物料清单
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public BaseMaterialList AddBaseMaterialList(BaseMaterialList model)
|
||
{
|
||
return Context.Insertable(model).ExecuteReturnEntity();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 修改物料清单
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public int UpdateBaseMaterialList(BaseMaterialList model)
|
||
{
|
||
//var response = Update(w => w.Id == model.Id, it => new BaseMaterialList()
|
||
//{
|
||
// Name = model.Name,
|
||
// Code = model.Code,
|
||
// Customer code = model.Customer code,
|
||
// Color = model.Color,
|
||
// Specification = model.Specification,
|
||
// Unit = model.Unit,
|
||
// Description = model.Description,
|
||
// ExpirationUnit = model.ExpirationUnit,
|
||
// ExpirationDate = model.ExpirationDate,
|
||
// ShelfLifeWarningDays = model.ShelfLifeWarningDays,
|
||
// IsShelfLife = model.IsShelfLife,
|
||
// StartTime = model.StartTime,
|
||
// StopTime = model.StopTime,
|
||
// BarCode = model.BarCode,
|
||
// IsBatch = model.IsBatch,
|
||
// CreatedBy = model.CreatedBy,
|
||
// CreatedTime = model.CreatedTime,
|
||
// UpdatedBy = model.UpdatedBy,
|
||
// UpdatedTime = model.UpdatedTime,
|
||
//});
|
||
//return response;
|
||
return Update(model, true);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取bom结构
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public List<BaseMaterialListDto> Achieve_BOM(string id)
|
||
{
|
||
var query1 = Context.Queryable<BaseMaterialBom>().Where(it => it.FkParentId == id);
|
||
return Context.Queryable(query1).LeftJoin<BaseMaterialList>((q, m) => q.FkId == m.Id)
|
||
.Select((q, m) => m)
|
||
.Adapt<List<BaseMaterialListDto>>()
|
||
;
|
||
|
||
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除绑定关系
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public int DeleteBOMBind(string id, string parent_id)
|
||
{
|
||
return Context.Deleteable<BaseMaterialBom>().Where(it => it.FkId == id)
|
||
.Where(it => it.FkParentId == parent_id)
|
||
.ExecuteCommand();
|
||
}
|
||
/// <summary>
|
||
/// 增加绑定关系
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public int ADDBOMBind(string id, string parent_id)
|
||
{
|
||
BaseMaterialBom bom = new BaseMaterialBom();
|
||
bom.FkId = id;
|
||
bom.FkParentId = parent_id;
|
||
bom.CreatedTime = DateTime.Now;
|
||
return Context.Insertable(bom).ExecuteCommand();
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取未绑定的BOM结构
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public List<BaseMaterialListDto> Achieve_BOM_no_bind(BaseMaterialListQueryDto3 query)
|
||
{
|
||
// 成品id 的Bom
|
||
string[] binded_array = Context.Queryable<BaseMaterialBom>().Where(it => it.FkParentId == query.Id).Select(it => it.FkId).ToArray();
|
||
|
||
//在原材料和半成品中 选择
|
||
List<BaseMaterialType> typeList = Context.Queryable<BaseMaterialType>().ToList();
|
||
int[] leaf = FindLeafNodes(typeList, 1).Select(it => it.Id).ToArray();
|
||
int[] leaf2 = FindLeafNodes(typeList, 5).Select(it => it.Id).ToArray();
|
||
int[] leaf3 = leaf.Concat(leaf2).ToArray();
|
||
var predicate = Expressionable.Create<BaseMaterialList>()
|
||
.AndIF(!string.IsNullOrEmpty(query.Name), it => it.Name.Contains(query.Name))
|
||
.AndIF(!string.IsNullOrEmpty(query.Code), it => it.Name.Contains(query.Code))
|
||
.AndIF(!string.IsNullOrEmpty(query.CustomerCode), it => it.Name.Contains(query.CustomerCode))
|
||
;
|
||
|
||
return Context.Queryable<BaseMaterialList>().Where(it => !binded_array.Contains(it.Id))
|
||
.Where(predicate.ToExpression())
|
||
.Where(it => leaf3.Contains(it.FkTypeId.Value)).ToList().Adapt<List<BaseMaterialListDto>>();
|
||
|
||
|
||
}
|
||
|
||
public static List<BaseMaterialType> FindLeafNodes(List<BaseMaterialType> nodes, int ancestorId)
|
||
{
|
||
List<BaseMaterialType> leaves = new List<BaseMaterialType>();
|
||
|
||
bool IsLeaf(BaseMaterialType node, Dictionary<int, BaseMaterialType> nodeMap)
|
||
{
|
||
return !nodeMap.Values.Any(child => child.ParentId == node.Id);
|
||
}
|
||
|
||
void TraverseForLeaves(BaseMaterialType node, int targetAncestorId, Dictionary<int, BaseMaterialType> nodeMap, List<BaseMaterialType> currentPath)
|
||
{
|
||
currentPath.Add(node);
|
||
if (node.Id == targetAncestorId)
|
||
{
|
||
// 如果当前节点是目标祖先节点,开始检查路径上的节点是否为叶子
|
||
foreach (var pathNode in currentPath)
|
||
{
|
||
if (IsLeaf(pathNode, nodeMap))
|
||
{
|
||
leaves.Add(pathNode);
|
||
}
|
||
}
|
||
}
|
||
else if (nodeMap.ContainsKey(node.Id))
|
||
{
|
||
// 继续递归遍历子节点
|
||
foreach (var child in nodeMap.Values.Where(child => child.ParentId == node.Id))
|
||
{
|
||
TraverseForLeaves(child, targetAncestorId, nodeMap, new List<BaseMaterialType>(currentPath));
|
||
}
|
||
}
|
||
}
|
||
|
||
// 构建节点ID到节点对象的映射,便于查找
|
||
Dictionary<int, BaseMaterialType> nodeMap = new Dictionary<int, BaseMaterialType>();
|
||
foreach (var node in nodes)
|
||
{
|
||
nodeMap[node.Id] = node;
|
||
}
|
||
|
||
// 从根节点开始遍历
|
||
foreach (var root in nodeMap.Values.Where(n => n.ParentId == 0)) // 假设根节点的ParentId为0
|
||
{
|
||
TraverseForLeaves(root, ancestorId, nodeMap, new List<BaseMaterialType>());
|
||
}
|
||
|
||
return leaves;
|
||
}
|
||
}
|
||
}
|