using Rhea.Common;
using Tiger.IBusiness;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tiger.Model;
using static Tiger.Business.Biz;
using Org.BouncyCastle.Ocsp;
using Tiger.Model.Entitys.MES.Position;
using MailKit.Search;
namespace Tiger.Business.MES.Transaction
{
///
/// MES岗位事务
///
public class Position : MESTransactionBase, IPosition
{
public IPosition Init(string id, string apiHost, string userCode, string postCode)
{
TransID = id;
UserCode = userCode;
ApiHost = apiHost;
PostCode = postCode;
CurPosition = Biz.Db.Queryable().Where(q => q.POST_CODE == postCode).First();
//if (CurPosition.IsNullOrEmpty()) throw new InvalidDataException($"MES.Transaction.Position.PositionNotExistsException", new Exception($"{postCode}|"));
if (CurPosition.IsNullOrEmpty()) throw new InvalidDataException($"岗位[{postCode}]不存在,请提交正确的岗位代码", new Exception($"{postCode}|"));
CurLine = Biz.Db.Queryable().Where(q => q.LINE_CODE == CurPosition.LINE_CODE).First();
//if (CurLine.IsNullOrEmpty()) throw new InvalidDataException($"MES.Transaction.Position.LineNotExistsException", new Exception($"{postCode}|{CurPosition.LINE_CODE}"));
if (CurLine.IsNullOrEmpty()) throw new InvalidDataException($"岗位[{postCode}]所属的产线[{CurPosition.LINE_CODE}]不存在,请先设置所属产线", new Exception($"{postCode}|{CurPosition.LINE_CODE}"));
CurWorkshop = Biz.Db.Queryable().Where(q => q.WS_CODE == CurLine.WS_CODE).First();
//if (CurWorkshop.IsNullOrEmpty()) throw new InvalidDataException($"MES.Transaction.Position.WorkshopNotExistsException", new Exception($"{postCode}|{CurLine.WS_CODE}"));
if (CurWorkshop.IsNullOrEmpty()) throw new InvalidDataException($"岗位[{postCode}]所属的车间[{CurLine.WS_CODE}]不存在,请先设置所属车间", new Exception($"{postCode}|{CurLine.WS_CODE}"));
CurFactory = Biz.Db.Queryable().Where(q => q.FTY_CODE == CurWorkshop.FTY_CODE).First();
//if (CurFactory.IsNullOrEmpty()) throw new InvalidDataException($"MES.Transaction.Position.FactoryNotExistsException", new Exception($"{postCode}|{CurWorkshop.FTY_CODE}"));
if (CurFactory.IsNullOrEmpty()) throw new InvalidDataException($"岗位[{postCode}]所属的工厂[{CurWorkshop.FTY_CODE}]不存在,请先设置所属工厂", new Exception($"{postCode}|{CurWorkshop.FTY_CODE}"));
return this;
}
#region Propertys & Variables
public string UserCode { get; set; }
public string PostCode { get; set; }
public MES_FACTORY CurFactory { get; set; }
public MES_WORKSHOP CurWorkshop { get; set; }
public MES_LINE CurLine { get; set; }
public MES_POSITION CurPosition { get; set; }
public WorkBatch CurBatch { get; set; }
public IWorkBatch WorkBatch => CurBatch;
public MES_WIP_DATA CurWipSN { get; set; }
public MES_WIP_HIS CurWipSNHis { get; set; }
public List Steps { get; set; } = new();
public bool IsFinishAllSteps => Steps.Any() && !Steps.Any(q => !q.IsFinished);
public int CurStep => Steps.Where(q => !q.IsFinished).OrderBy(q => q.Sequence).FirstOrDefault()?.Sequence ?? 0;
private DbClient CommitDB;
#endregion Propertys & Variables
#region Functions
///
/// 获取提交数据的DbClient对象
///
///
public DbClient GetCommitDB()
{
return CommitDB ??= Biz.Db;
}
///
/// 选择工单
///
///
///
public async Task SelectOrder(WoInput input)
{
var action = new ApiAction();
try
{
if (!WoContext.ExistsBatch(input.OrderNo, CurLine.LINE_CODE))
{
var wo = await Biz.Db.Queryable().ByAuth(input.AuthOption).Where(q => q.ORDER_NO == input.OrderNo).FirstAsync();
//验证明细是否正确
if (wo.IsNullOrEmpty())
{
action.IsSuccessed = false;
//action.LocaleMsg = new($"工单[{input.OrderNo}]不存在", input.OrderNo);
action.LocaleMsg = new("MES.Transaction.Position.SelectOrder.EmptyException", input.OrderNo);
return action;
}
if (wo.STATUS != BIZ_MES_WO.STATUSs.Release.GetValue() && wo.STATUS != BIZ_MES_WO.STATUSs.Working.GetValue())
{
action.IsSuccessed = false;
//action.LocaleMsg = new($"工单[{input.OrderNo}]状态[{wo.STATUS.GetEnum().GetName()}]不能生产");
action.LocaleMsg = new("MES.Transaction.Position.SelectOrder.StatusException", input.OrderNo, wo.STATUS.GetEnum().GetName());
return action;
}
var batch = await Biz.Db.Queryable().ByAuth(input.AuthOption).Where(q => q.ORDER_NO == input.OrderNo && q.ACT_LINE == CurLine.LINE_CODE).FirstAsync();
if (batch.IsNullOrEmpty())
{
action.IsSuccessed = false;
//action.LocaleMsg = new($"工单[{input.OrderNo}]没有下发到产线状态[{CurLine.LINE_CODE}]");
action.LocaleMsg = new("MES.Transaction.Position.SelectOrder.LineException", input.OrderNo, CurLine.LINE_CODE);
return action;
}
var wb = new WorkBatch(input.OrderNo).Init(CurLine.LINE_CODE);
WoContext.WoBatchDic.Add(wb.Batch.BATCH_NO, wb);
}
CurBatch = WoContext.GetBatch(input.OrderNo, CurLine.LINE_CODE);
action.Data = new { WorkOrder = CurBatch.WO, Bacth = CurBatch.Batch };
}
catch (Exception ex)
{
action.CatchExceptionWithLog(ex, $"采集工序:选择工单异常");
}
return action;
}
///
/// 重置当前工步
///
public void ResetSteps()
{
Steps.Clear();
CurWipSN = null;
}
#endregion Functions
public override bool Close(bool needSaveHistoryLog = false)
{
//needSaveHistoryLog = true;
//保存操作日志
this.IsFinished = true;
return IsFinished ? base.Close(needSaveHistoryLog) : IsFinished;
}
}//endClass
}