using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Business.Models;
using Microsoft.AspNetCore.Authorization;
using Business.Permissions;
using XCZ;
using Business.ResourceExamineManagement.Dto;
using Bussiness.Model.MES.IC;
using AutoMapper.Internal.Mappers;
using Bussiness.Model.Tech;
using Bussiness.Model.Production;
using Business.Core.MongoDBHelper;
using Business.Core.Utilities;
using Hangfire.Storage.Monitoring;
using Business.BookManagement.Dto;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Application.Services;
using ZstdSharp.Unsafe;
using System.Transactions;
using NUglify.JavaScript.Syntax;
using System.Linq.Expressions;
using XCZ.Extensions;
using System.ComponentModel;
using System.Reflection.Emit;
using NUglify.Helpers;
using Microsoft.AspNetCore.SignalR.Protocol;
using XCZ.Extensions;
using System.ComponentModel.Design;
namespace Business.ResourceExamineManagement
{
///
/// 资源检查
///
//[Authorize(BusinessPermissions.ResourceExamine.Default)]
public class ResourceExamineAppService : ApplicationService, IResourceExamineAppService
{
#region 服务
///
/// 工艺路径
///
private readonly IMongoDB _mes_technique;
///
/// 工艺关联工序
///
private readonly IMongoDB _mes_tech_process;
///
/// 工作日历
///
private readonly IMongoDB _mes_work_calendar;
///
/// 工作日历明细
///
private readonly IMongoDB _mes_work_calendar_list;
///
/// 工艺工序关联工位
///
private readonly IMongoDB _mes_tech_proc_workshop;
///
/// 排程占用记录
///
private readonly IMongoDB _mes_schedule_occupy;
///
/// 物料详情
///
private readonly IMongoDB _ic_item;
private IBasicRepository _mysql_ic_item;
///
/// 物料BOM
///
private readonly IMongoDB _ic_bom;
private IBasicRepository _mysql_ic_bom;
///
/// 物料BOM明细
///
private readonly IMongoDB _ic_bom_child;
private IBasicRepository _mysql_ic_bom_child;
///
/// 物料库存表
///
private readonly IMongoDB _ic_item_stock;
///
/// 物料质检表
///
private readonly IMongoDB _ic_check;
///
/// 替代群组
///
private readonly IMongoDB _ic_substitute;
///
/// 替代群组
///
private readonly IMongoDB _ic_substitute_all;
///
/// 替代群组
///
private readonly IMongoDB _ic_substitute_all_dtl;
///
/// 生产工单主表
///
private readonly IMongoDB _mes_morder;
#endregion
///
/// 构造函数
///
///
///
public ResourceExamineAppService(
IMongoDB mes_technique,
IMongoDB ic_item,
IMongoDB ic_bom,
IMongoDB ic_bom_child,
IMongoDB ic_item_stock,
IMongoDB ic_check,
IMongoDB ic_substitute,
IMongoDB ic_substitute_all,
IMongoDB ic_substitute_all_dtl,
IMongoDB mes_morder,
IBasicRepository mysql_ic_item,
IBasicRepository mysql_ic_bom,
IBasicRepository mysql_ic_bom_child
)
{
_mes_technique = mes_technique;
_ic_item = ic_item;
_ic_bom = ic_bom;
_ic_bom_child = ic_bom_child;
_ic_item_stock = ic_item_stock;
_ic_check = ic_check;
_ic_substitute = ic_substitute;
_ic_substitute_all = ic_substitute_all;
_ic_substitute_all_dtl = ic_substitute_all_dtl;
_mes_morder = mes_morder;
_mysql_ic_item = mysql_ic_item;
_mysql_ic_bom = mysql_ic_bom;
_mysql_ic_bom_child = mysql_ic_bom_child;
}
///
/// mongoDB示例方法,后期删除
///
///
public async Task test()
{
//多条插入
List infos = new List();
mes_technique info;
for (int i = 0; i < 3; i++)
{
info = new mes_technique();
info.GenerateNewId();
info.tech_name = "多条" + i;
info.level = i;
infos.Add(info);
}
await _mes_technique.InsertMany(infos);
var info2 = _mes_technique.GetAll().Result;
var a = new PschedDto();
a.count = info2.Count;
//获取数据
var info1 = await _mes_technique.GetOneByID((long)1732029975067480064);
//更新数据
info1.tech_name = "更新***";
var rlt = await _mes_technique.UpdateOne(info1, info1.Id);
//根据条件查询数据
Expression> filter = x => x.Id == (long)1732376973889097728 && x.tech_name == "多条0";
var info3 = await _mes_technique.GetManyByCondition(filter);
}
///
/// 资源检查
///
///
///
///
public async Task ReceiveResult(SeorderentryDto input)
{
return null;
throw new NotImplementedException();
}
///
/// 产能计算
///
/// Bom主键
/// 需要数量
public async Task ProductiveExamine(long? BomId, int Quantity)
{
}
///
/// 检查在制工单
///
/// Bom编码
/// 需要数量
/// 交付日期
///
public async Task> CheckMorder(string bomNumber, decimal? Quantity, DateTime DeliverDate)
{
if (string.IsNullOrEmpty(bomNumber) || Quantity != null)
{
//TODO:入参异常;
throw new NotImplementedException("BOM编码或需求数量不能为空!");
}
//根据Bom编码查询出对应工单并且状态不为完成、关闭,非委外工单。
//TODO:工单类型;
Expression> filter = x => x.bom_number == bomNumber && (x.morder_state != "完成" || x.morder_state != "关闭"
&& x.morder_icitem_type != "相关委外工单") && x.IsDeleted == false;
var morderList = await _mes_morder.GetManyByCondition(filter);
//首先满足需求数量工单其次判断是否满足交付日期、当数量不满足时继续查找最早交付日期订单 工单数量累加。
//当前工单计划日期-1天 小于交付日期 && 计算生产数量-入库数据并且大于需求产品数量。
var morderDataList = morderList.Where(x => x.planner_end_date.Value.AddDays(-1) < DeliverDate &&
(x.morder_production_number - x.inventory_number) > Quantity).ToList();
if (morderDataList.Count > 0)
{
//存在此数据满足当前BOM交付找到最早日期工单,则返回无需后续继续检查。
var morder = morderDataList.OrderByDescending(x => x.planner_end_date).FirstOrDefault();
//生成mes_mooccupy工单占用表数据,代表此工单被某个销售订单已占用。
}
else
{
// 寻找最早日期工单 && 计算生产数量-入库数据并且大于需求产品数量后累加直到满足需求产品数量
var morderListData = morderList.Where(x => x.planner_end_date.Value.AddDays(-1) < DeliverDate).OrderByDescending(x => x.planner_end_date).ToList();
if (morderListData.Count == 0)
{
//TODO:后期处理无在制工单返回内容
throw new NotImplementedException("无可用在制工单!");
}
List mes_Morders = new List();
decimal? number = Quantity;
foreach (var item in morderListData)
{
number = number - (item.morder_production_number - item.inventory_number);
if (number < 0)
{
mes_Morders.Add(item);
}
else
{
break;
}
}
//生成mes_mooccupy工单占用表数据,代表多个工单被某个销售订单已占用。
}
return morderList;
#region 注释
/* var morderList = _mes_morder.GetAll().Result;
//查询生产工单类型为计划工单并且工单类型为备库工单 并且BOM编码一致数据,订单状态为 初始或下达工单
var morderTypeList = morderList.Where(x => x.work_order_type == "备库工单" && x.morder_type == "计划工单" &&
x.bom_number == bomNumber && (x.morder_state == "初始" || x.morder_state == "下达")).ToList();
//计划数量-需求数量=剩余可用数量 -->使用计划结束时间排序取第一条满足条件工单
// List =>数量满足Quantity && 生成工单结束日期最早
var SatisfyQuantityList = morderTypeList.Where(x => (x.morder_production_number.Value - x.need_number.Value) >= Quantity).
ToList().OrderBy(x => x.moentry_etime).FirstOrDefault();
if (SatisfyQuantityList == null)
{
}
else
{
}*/
#endregion
}
///
/// BOM预处理
///
///
///
///
public void BomPretreatment(long? orderid, long? BomId, int Quantity)
{
if (orderid == null)
{
//throw new bu
}
if (BomId == null)
{
//throw new bu
}
//var query = (await _ic_bom.GetQueryableAsync()).WhereIf(true, a => a.bom_id == BomId).ToList();
var help = new SnowFlake();
var bomlist = _ic_bom.GetAll().Result;
var bomchildlist = _ic_bom_child.GetAll().Result;
var icitemlist = _ic_item.GetAll().Result;
List returnlist = new List();
var dto = new BomChildExamineDto();
dto.bom_id = BomId.Value;
dto.level = 1;
dto.id = help.NextId();
dto.parent_id = help.NextId();
dto.qty = 1;
dto.num = "1";
dto.isbom = 1;
dto.is_replace = 0;
dto.haveicsubs = 0;
dto.substitute_code = "";
dto.icitem_ids = "";
GetBomList(bomlist, bomchildlist, icitemlist, dto, returnlist);
}
///
/// BOM预处理层级组装
///
///
///
///
///
///
public void GetBomList(List bomlist, List bomchildlist, List icitemlist, BomChildExamineDto dto, List returnlist)
{
int level = dto.level++;//初始化定义level层级
var help = new SnowFlake();
var bom = bomlist.WhereIf(true, s => s.Id == dto.bom_id).FirstOrDefault();
var item = icitemlist.WhereIf(true, a => a.Id == bom.icitem_id).FirstOrDefault();
if (bom == null || item == null)
{
}
//var dto = new BomChildExamineDto();
//dto.level = level;
//dto.bom_id = bomId;
//dto.id = help.NextId();
//dto.parent_id = parent_id;
dto.item_id = bom.icitem_id;
dto.item_name = bom.item_name;
dto.item_code = bom.item_number;
dto.model = item.model;
dto.unit = bom.unit;
dto.erp_cls = item.erp_cls;
dto.erp_cls_name = item.erp_cls_name;
//var bdto = ObjectMapper.Map(bom);
returnlist.Add(dto);
var childlist = bomchildlist.WhereIf(true, a => a.bom_id == bom.Id).ToList();
int idx = 1;
foreach (var c in childlist)
{
string childNum = dto.num + "." + idx.ToString();
var icitem = icitemlist.WhereIf(true, a => a.Id == c.icitem_id).FirstOrDefault();
//如果此明细查的到BOM信息,则代表此child是一个子BOM。
if (c.is_bom == 1)
{
var childBom = bomlist.WhereIf(true, a => a.icitem_id == c.icitem_id).FirstOrDefault();
if (childBom != null)
{
var cdto = new BomChildExamineDto();
cdto.id = help.NextId();
cdto.level = level;
cdto.parent_id = dto.id;
cdto.qty = c.qty.Value;
cdto.backflush = c.backflush;
cdto.num = childNum;
cdto.isbom = 1;
cdto.is_replace = c.is_replace;
cdto.haveicsubs = c.haveicsubs;
cdto.substitute_code = c.substitute_code;
cdto.icitem_ids = c.icitem_ids;
GetBomList(bomlist, bomchildlist, icitemlist, cdto, returnlist);
}
}
else
{
if (icitem != null)
{
var childDto = new BomChildExamineDto();
childDto.level = level++;
childDto.bom_id = dto.bom_id;
childDto.id = help.NextId();
childDto.parent_id = dto.id;
childDto.item_id = icitem.Id;
childDto.item_name = icitem.name;
childDto.item_code = icitem.number;
childDto.num = childNum;
childDto.model = icitem.model;
childDto.unit = c.unit;
childDto.erp_cls = item.erp_cls;
childDto.erp_cls_name = item.erp_cls_name;
childDto.backflush = c.backflush;
childDto.qty = c.qty.Value;
childDto.isbom = 0;
childDto.is_replace = c.is_replace;
childDto.haveicsubs = c.haveicsubs;
childDto.substitute_code = c.substitute_code;
childDto.icitem_ids = c.icitem_ids;
returnlist.Add(childDto);
}
}
idx++;
}
}
///
/// BOM替代关系预处理
///
public void BomSubstitute(List returnlist, List icitemlist)
{
var sublist = _ic_substitute.GetManyByCondition(s => s.substitute_code.IsIn(returnlist.Select(c => c.substitute_code))).Result.ToList();
var suballlist =_ic_substitute_all.GetManyByCondition(s => s.substitute_id.IsIn(sublist.Select(c => c.Id))).Result.ToList();
var subdtllist = _ic_substitute_all_dtl.GetManyByCondition(s => s.substitute_allid.IsIn(suballlist.Select(c => c.Id))).Result.ToList();
List dicIds = new List();
var help = new SnowFlake();
//除顶级外,其他层级关系全带出来。生成平铺
foreach (var item in returnlist)
{
//最顶级、虚拟件
if (item.level != 1 && item.erp_cls!=4)
{
//有替代关系
if (item.haveicsubs == 1)
{
/*//群组替代
if (item.is_replace == 1)
{
if (!string.IsNullOrEmpty(item.icitem_ids))
{
var ids = item.icitem_ids.Split(',');
foreach (var i in ids)
{
long id = 0;
if (long.TryParse(i, out id))
{
dicIds.Add(id);
}
}
}
}*/
var sl = sublist.Find(s => s.substitute_code == item.substitute_code);
if (sl != null)
{
var sall = suballlist.Where(s => s.substitute_id == sl.Id).ToList();
foreach (var sal in sall)
{
var sadl = subdtllist.Where(s => s.substitute_allid== sal.Id).ToList();
foreach (var dtl in sadl)
{
SubstitutePretreatment(sal, dtl, item, returnlist, icitemlist);
//var sitem = icitemlist.Find(s => s.Id == dtl.icitem_id);
//if (sitem != null)
//{
// var childDto = new BomChildExamineDto();
// childDto.level = item.level;
// childDto.bom_id = item.bom_id;
// childDto.id = help.NextId();
// childDto.parent_id = item.parent_id;
// childDto.item_id = sitem.Id;
// childDto.item_name = sitem.name;
// childDto.item_code = sitem.number;
// childDto.num = item.num;
// childDto.model = sitem.model;
// childDto.unit = sitem.unit;
// childDto.erp_cls = sitem.erp_cls;
// childDto.erp_cls_name = sitem.erp_cls_name;
// childDto.backflush = item.backflush;
// childDto.replace_amount = dtl.use_amount.Value;
// childDto.replace_amount = dtl.replace_amount.Value;
// childDto.isbom = 0;
// childDto.is_replace = c.is_replace;
// childDto.haveicsubs = c.haveicsubs;
// childDto.substitute_code = c.substitute_code;
// childDto.icitem_ids = c.icitem_ids;
// returnlist.Add(childDto);
//}
}
}
}
}
}
}
}
public void SubstitutePretreatment(ic_substitute_all sal, ic_substitute_all_dtl dtl,BomChildExamineDto toDto,List returnlist, List icitemlist)
{
//生成sal层级dto,是toDto的替代关系,类似R6-K50的BOM的层级1.1.1
//循环生成dtl层级dto,明细
//如果dtl对应的icitem是BOM,还需要向下继续展开。
}
///
/// 计算库存量
///
///
public void BomStock(List returnlist,long factoryid)
{
returnlist = returnlist.OrderBy(s => s.num).ToList();
//取物料库存记录
var stocklist = _ic_item_stock.GetManyByCondition(s => s.icitem_id.IsIn(returnlist.Select(c => c.item_id)) && s.factory_id == factoryid).Result;
//取当前物料质检记录
//var checklist = _ic_check.GetManyByCondition(s => s.icitem_id.IsIn(returnlist.Select(c => c.item_id)) && s.istest.GetValueOrDefault() != 1).Result.ToList();
List idlist = new List();
foreach (var item in returnlist)
{
if (item.erp_cls != 4)//虚拟件不计算
{
var sl = stocklist.Where(s => s.icitem_id == item.item_id).ToList();
//库存=物料库存+VMI库存+质检未入库。
item.sqty = sl.Sum(s => s.sqty).GetValueOrDefault();
//质检记录没加入,暂时不考虑。
//VMI仓储信息
}
}
}
}
}