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;
using Infrastructure.Model;
using Aliyun.OSS;
namespace DOAN.Service.MES.base_
{
///
/// 物料清单Service业务层处理
///
[AppService(ServiceType = typeof(IBaseMaterialBOMService), ServiceLifetime = LifeTime.Transient)]
public class BaseMaterialBOMService : BaseService, IBaseMaterialBOMService
{
///
/// 查询物料清单列表
///
///
///
public PagedInfo GetList(BaseMaterialListQueryDto2 parm)
{
int[] nodes = null;
if (parm.Type == 1)
{
nodes = FindLeafNodes(Context.Queryable().ToList(), 6).Select(it => it.Id).ToArray();
}
else if (parm.Type == 2)
{
nodes = FindLeafNodes(Context.Queryable().ToList(), 5).Select(it => it.Id).ToArray();
}
var predicate = Expressionable.Create();
var response = Queryable()
.Where(it => nodes.Contains(it.FkTypeId.Value))
.Where(predicate.ToExpression())
.ToPage(parm);
return response;
}
///
/// 获取详情
///
///
///
public BaseMaterialList GetInfo(string Id)
{
var response = Queryable()
.Where(x => x.Id == Id)
.First();
return response;
}
///
/// 添加物料清单
///
///
///
public BaseMaterialList AddBaseMaterialList(BaseMaterialList model)
{
return Context.Insertable(model).ExecuteReturnEntity();
}
///
/// 修改物料清单
///
///
///
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);
}
///
/// 获取bom结构
///
///
///
public List Achieve_BOM(string id)
{
var query1 = Context.Queryable().Where(it => it.FkParentId == id);
return Context.Queryable(query1).LeftJoin((q, m) => q.FkId == m.Id)
.InnerJoin((q, m,t)=>m.FkTypeId==t.Id)
.Select((q, m,t) => new BaseMaterialListDto2() {
Type_Name = t.Name,
Type_Code = t.Code,
},true)
.ToList()
.Adapt>()
;
}
///
/// 删除绑定关系
///
///
///
public int DeleteBOMBind(string id, string parent_id)
{
return Context.Deleteable().Where(it => it.FkId == id)
.Where(it => it.FkParentId == parent_id)
.ExecuteCommand();
}
///
/// 增加绑定关系
///
///
///
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();
}
///
/// 获取未绑定的BOM结构
///
///
///
public PagedInfo Achieve_BOM_no_bind(BaseMaterialListQueryDto3 query)
{
// 成品id 的Bom结构
string[] binded_array = Context.Queryable().Where(it => it.FkParentId == query.Id).Select(it => it.FkId).ToArray();
//在原材料和半成品中 选择
List typeList = Context.Queryable().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()
.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))
;
var qua = Context.Queryable().Where(it => !binded_array.Contains(it.Id))
.Where(predicate.ToExpression())
.Where(it => leaf3.Contains(it.FkTypeId.Value));
return Context.Queryable(qua).InnerJoin((q, t) => q.FkTypeId == t.Id)
.Select((q, t) => new BaseMaterialListDto2()
{
Type_Name = t.Name,
Type_Code = t.Code,
}, true).ToPage(query);
}
public static List FindLeafNodes( List nodes,int ancestorId)
{
List leafNodes = new List();
void FindLeaves(int currentId)
{
// 查找当前ID的直接子节点
var children = nodes.Where(n => n.ParentId == currentId).ToList();
// 如果没有子节点,说明是叶子节点
if (!children.Any())
{
leafNodes.Add(nodes.FirstOrDefault(n => n.Id == currentId));
}
else
{
// 对每个子节点递归调用FindLeaves
foreach (var child in children)
{
FindLeaves(child.Id);
}
}
}
// 从指定的祖先节点开始查找
FindLeaves(ancestorId);
return leafNodes;
}
///
/// bom视图
///
///
///
public BaseParseNode ShowBOMView(string id)
{
//TODO 获取成品id的一级子集 bom
return null;
}
}
}