zhuangpei-mesbackend/DOAN.Service/MES/base/BaseMaterialBOMService.cs
2024-07-16 08:42:07 +08:00

193 lines
6.5 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 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;
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(string id)
{
string[] binded_array= Context.Queryable<BaseMaterialBom>().Where(it => it.FkId == id).Select(it=>it.FkId).ToArray();
if (binded_array != null && binded_array.Count() > 0)
{
//在原材料和半成品中 选择
List<BaseMaterialType> typeList= Context.Queryable<BaseMaterialType>().Where(it => it.Id == 5).Where(it => it.Id == 1).ToList();
int[] leaf= FindLeafNodes(typeList).Select(it=>it.Id).ToArray();
return Context.Queryable<BaseMaterialList>().Where(it => !binded_array.Contains(it.Id))
.Where(it => leaf.Contains(it.FkTypeId.Value)).ToList().Adapt<List<BaseMaterialListDto>>();
}
return null;
}
// 假设nodes是一个列表包含了所有的Node对象
public static List<BaseMaterialType> FindLeafNodes(List<BaseMaterialType> nodes, int parentId = 0)
{
List<BaseMaterialType> leafNodes = new List<BaseMaterialType>();
// 查找当前parentId下的所有子节点
var children = nodes.FindAll(node => node.ParentId == parentId);
// 如果没有子节点,说明是叶子节点
if (children.Count == 0)
return leafNodes;
// 对每个子节点递归查找其下的叶子节点
foreach (var child in children)
{
var subLeafNodes = FindLeafNodes(nodes, child.Id);
leafNodes.AddRange(subLeafNodes);
}
// 如果这个节点没有子节点(即所有子节点都是叶子节点),则它本身也是叶子节点
if (leafNodes.Count == children.Count)
leafNodes.Add(nodes.FirstOrDefault(n => n.Id == parentId));
return leafNodes;
}
}
}