服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2024-11-26 976d3ae1547b896cc5c7875408c09616d5c2ee55
Tiger.Business.MES/Transaction/YadaPacking.cs
@@ -11,6 +11,8 @@
using Tiger.Model;
using Tiger.Model.Entitys.MES.Position;
using Tiger.Business.MES.WorkAction;
using Tiger.Model.MES.Yada;
using System.Data;
namespace Tiger.Business.MES.Transaction
{
@@ -36,6 +38,9 @@
        public string NodeCmd { get; set; }
        public PackingAction TempPkgAction { get; set; }
        public bool IsPrintCustomerLabel { get; set; }
        public string ShippingOrder { get; set; }
        public DateTime curtime = DateTime.Now;
        public DateTime prevtime = DateTime.Now;
        #endregion Propertys & Variables
        #region Functions
@@ -46,23 +51,29 @@
        /// <returns></returns>
        public async Task<ApiAction<SubmitOutput>> Submit(SubmitInput input)
        {
            curtime = prevtime = DateTime.Now; Logger.Console.Info($"{CurSN} : ***** 1 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
            var action = new ApiAction<SubmitOutput>(new SubmitOutput());
            try
            {
                if (input.Qty.ToInt32() <= 0)
                {
                    action.Data.ShortMsg = new("请输入包装数量", ShortMessage.Types.Error);
                    action.Data.OperInfo = new();
                    action.Data.Data = "QtyError";
                    action.IsSuccessed = false;
                    action.LocaleMsg = new($"包装数量[{input.Qty}]不正确,请重新输入数量后继续包装操作");
                    //action.LocaleMsg = new("MES.Transaction.PackingNode.Submit.QtyError", input.Qty);
                    return action;
                }
                //if (input.Qty.ToInt32() <= 0)
                //{
                //    action.Data.ShortMsg = new("请输入包装数量", ShortMessage.Types.Error);
                //    action.Data.OperInfo = new();
                //    action.Data.Data = "QtyError";
                //    action.IsSuccessed = false;
                //    action.LocaleMsg = new($"包装数量[{input.Qty}]不正确,请重新输入数量后继续包装操作");
                //    //action.LocaleMsg = new("MES.Transaction.PackingNode.Submit.QtyError", input.Qty);
                //    return action;
                //}
                if (input.Options.ContainsKey("IsPrintCustomerLabel"))
                {
                    IsPrintCustomerLabel = input.Options["IsPrintCustomerLabel"].ToBoolean();
                }
                //if (input.Options.ContainsKey("ShippingOrder"))
                //{
                //    ShippingOrder = IsPrintCustomerLabel ? input.Options["ShippingOrder"].ToString() : "";
                //}
                //PkgSubmit:手动结束包装,手动保存暂存的包装数据
                if (NodeCmd == "PkgSubmit")
                {
@@ -83,13 +94,13 @@
                        var wosns = Biz.Db.Queryable<BIZ_MES_WO_BATCH, BIZ_MES_WO_SN>((q, s) => new JoinQueryInfos(JoinType.Inner, q.ORDER_NO == s.WORK_ORDER && q.BATCH_NO == s.BATCH_NO))
                                                .ByAuth(input.AuthOption)//.Where((q, s) => s.STATUS < BIZ_MES_WO_SN.STATUSs.Finished.GetValue())
                                                .Where((q, s) => s.SN == input.SN || s.FLOW_SN == input.SN || s.TRAY_SN == input.SN).Select((q, s) => new { Batch = q, SN = s }).ToList();
                        curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 2 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                        //先判断当前工单不为空且当前岗位在当前工单是不是首站,如果是则不允许变更当前工单,尝试把条码绑定到当前工单
                        if (!CurBatch.IsNullOrEmpty() && CurBatch.IsFirstNode(PostCode))
                        {
                            //更新CurBatch
                            CurBatch?.Update();
                            curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 3 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                            var wosn = wosns.FirstOrDefault(q => q.Batch.ORDER_NO == CurBatch.WO.ORDER_NO && (q.SN.STATUS == BIZ_MES_WO_SN.STATUSs.Finished.GetValue() || q.SN.STATUS == BIZ_MES_WO_SN.STATUSs.Scrap.GetValue()));
                            if (!wosn.IsNullOrEmpty())
                            {
@@ -103,6 +114,7 @@
                            {
                                //提交数据
                                action = NodeSubmit(action, input);
                                curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 4 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                                //更新工序信息
                                if (!action.IsSuccessed)
                                {
@@ -223,20 +235,23 @@
                    {
                        //更新CurBatch
                        CurBatch?.Update();
                        curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 5 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                        var submitStep = Steps.Where(q => q.ID == input.CurStepID && !q.IsFinished).FirstOrDefault();
                        //如果客户端返回的当前工步ID找不到未完成的行为工步,则直接开始下一个行为工步
                        if (submitStep.IsNullOrEmpty())
                        {
                            action = BeginNextActionStep(input);
                            curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 6 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                        }
                        else
                        {
                            action = submitStep.Submit(input);
                            curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 7 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                            //如果当前工步已完成,开始执行下一工步
                            if (action.IsSuccessed && submitStep.IsFinished)
                            {
                                action = BeginNextActionStep(input);
                                curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 8 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                            }
                            //如果当前工步未完成
                            else
@@ -281,6 +296,7 @@
                //action.CatchExceptionWithLog(ex, $"包装工序:提交操作数据异常");
                action.CatchExceptionWithLog(ex, Biz.L("MES.Transaction.PackingNode.SubmitException"));
            }
            curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 9 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
            return action;
        }
@@ -292,6 +308,7 @@
        /// <returns></returns>
        public ApiAction<SubmitOutput> NodeSubmit(ApiAction<SubmitOutput> action, SubmitInput input)
        {
            curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 10 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
            var curNode = CurBatch.GetNode(PostCode);
            try
            {
@@ -318,7 +335,7 @@
                        return woStatus;
                    }
                    var wipSNs = Biz.Db.Queryable<MES_WIP_DATA>().IncludesAllFirstLayer().Where(q => q.SN == input.SN || q.FLOW_SN == input.SN || q.TRAY_SN == input.SN).Where(q => q.STATUS < MES_WIP_DATA.STATUSs.Finished.GetValue()).ToList();
                    var wipSNs = Biz.Db.Queryable<MES_WIP_DATA>().IncludesAllFirstLayer().Where(q => q.SN == input.SN || q.FLOW_SN == input.SN || q.TRAY_SN == input.SN).Where(q => q.FINISHED_FLAG != "Y").ToList();
                    if (wipSNs.IsNullOrEmpty())
                    {
                        var wipSN = new MES_WIP_DATA()
@@ -375,7 +392,46 @@
                    //设置当前指令为正常产品过站
                    NodeCmd = "NodeSubmit";
                    curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 11 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                    //获取出货状态
                    DbClient db = Biz.DataSource["YadaU9C"].Client;
                    var ship = db.Queryable<mes_ShipList>().Where(q => q.MoDoc == CurBatch.WO.ORDER_NO && q.Status == 2 && SqlFunc.DateIsSame(q.BusinessDate, DateTime.Now)).First();
                    int CurShipQty = 0;
                    int WaitShipmentCarton = 0;
                    if (ship.IsNullOrEmpty()) { IsPrintCustomerLabel = false; } else
                    {
                        CurShipQty = ship.ShipQty;
                        SugarParameter[] pars = Biz.Db.Ado.GetParameters(new { BATCH = CurBatch.Batch.BATCH_NO, NodeIdOrName = curNode.NODE_NAME });
                        var list = Biz.Db.Ado.UseStoredProcedure().GetDataTable("SP_MES_GET_NODE_BATCH_COUNT", pars).AsEnumerable().ToList();
                        if (!list.IsNullOrEmpty())
                        {
                            var batchCount = list.Where(q => q["ProdDate"].ToString() == "Whole").FirstOrDefault();
                            WaitShipmentCarton = batchCount["WaitShipment"].ToInt32();
                            IsPrintCustomerLabel = batchCount.IsNullOrEmpty() ? false : batchCount["WaitShipment"].ToInt32() >= ship.ShipQty ? false : true;
                        }
                    }
                    ShippingOrder = IsPrintCustomerLabel ? ship?.ShipDoc ?? "" : "";
                    //缓存出货数量
                    if (Context.ContainsKey("CurShipQty"))
                    {
                        Context["CurShipQty"] = CurShipQty;
                    }
                    else
                    {
                        Context.Add("CurShipQty", CurShipQty);
                    }
                    //缓存出货箱数
                    if (Context.ContainsKey("CurWaitShipmentCarton"))
                    {
                        Context["CurWaitShipmentCarton"] = WaitShipmentCarton;
                    }
                    else
                    {
                        Context.Add("CurWaitShipmentCarton", WaitShipmentCarton);
                    }
                    curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 12 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                    //绑定条码到工单
                    foreach (var wipSN in wipSNs)
                    {
@@ -418,6 +474,8 @@
                        wipSN.OPER_CODE = curNode.OPER_CODE;
                        wipSN.SEGMENT = curNode.SEGMENT;
                        wipSN.OPERATION_TIME = DateTime.Now;
                        wipSN.SHIPPING_ORDER = ShippingOrder;
                        wipSN.UNBIND_FLAG = "N";
                        var curShiftPeriod = GetShiftPeriodForNow();
                        if (!curShiftPeriod.IsNullOrEmpty())
                        {
@@ -438,7 +496,7 @@
                        {
                            var woSN = woSNs.First(q => q.WIP_ID == wipSN.ID);
                            woSN.STATUS = BIZ_MES_WO_SN.STATUSs.Finished.GetValue();
                            wipSN.STATUS = MES_WIP_DATA.STATUSs.Finished.GetValue();
                            wipSN.STATUS = MES_WIP_DATA.STATUSs.Output.GetValue();
                            wipSN.OUTLINE_DATE = DateTime.Now;
                        }
                        //如果有提交不良
@@ -517,6 +575,7 @@
                    var _curNodeSetting = curNodeSetting.Clone();
                    var _Batch = CurBatch.Batch.Clone();
                    curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 13 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
                    //初始化工步列表
                    Steps.Clear();
                    var curStep = new WorkStep(IWorkStep.NodeTypes.Node, this)
@@ -640,6 +699,7 @@
                //action.CatchExceptionWithLog(ex, $"{curNode.NODE_NAME}:工序节点工步提交数据异常,请检查工序节点设置");
                action.CatchExceptionWithLog(ex, Biz.L("MES.Transaction.PackingNode.NodeSubmitException", curNode.NODE_NAME));
            }
            curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 13.1 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
            return action;
        }
@@ -653,6 +713,7 @@
            var curPkg = Context.ContainsKey("CurPackage") ? Context["CurPackage"] as WipPkg : null;
            var operInfo = SetOperNodeInfo(CurOperInfo(locale));
            Action endAction = null;
            curtime = DateTime.Now;  Logger.Console.Info($"{CurSN} : ***** 14 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
            //如果当前条码已经走到流程终点则记录条码完工
            if (operInfo.IsReachedEndNode)
@@ -672,7 +733,7 @@
                //条码完工
                foreach (var wipSN in CurWipSNs)
                {
                    wipSN.STATUS = MES_WIP_DATA.STATUSs.Finished.GetValue();
                    wipSN.FINISHED_FLAG = "Y";
                    wipSN.NODE_ID = curNode.ID;
                    wipSN.NODE_NAME = curNode.NODE_NAME;
                    wipSN.OPER_CODE = curNode.OPER_CODE;
@@ -692,7 +753,8 @@
                var _woSns = woSNs.Clone();
                var _wipSns = CurWipSNs.Clone();
                var _wipHiss = wipHiss.Clone();
                var _pkgList = curPkg?.IsFinished == true ? GetPackageList(curPkg.Item) : new();
                var _pkgList = curPkg?.IsFinished == true ? GetPackageList(curPkg.Item).Clone() : new();
                var _wipIDs = curPkg?.IsFinished == true ? curPkg.Item.GetWipSnList().Select(q => q.Key).ToList().Clone() : new();
                //保存数据
                endAction = () =>
                {
@@ -704,14 +766,14 @@
                    db.Storageable(_wipHiss, UserCode).ExecuteCommand();
                    if (_pkgList.Any())
                    {
                        var wipIDs = _pkgList.Where(q => !q.WIP_ID.IsNullOrEmpty()).Select(q => q.WIP_ID).ToList();
                        //var wipIDs = _pkgList.Where(q => !q.WIP_ID.IsNullOrEmpty()).Select(q => q.WIP_ID).ToList();
                        var wo = _pkgList.First().WORK_ORDER;
                        //包装信息保存到生产过程记录
                        db.Updateable<MES_WIP_HIS>(UserCode)
                            .SetColumns(q => q.TRAY_SN == curPkg.Item.Package.SN)
                            .SetColumns(q => q.INNER_SN == q.CARTON_SN)
                            .SetColumns(q => q.CARTON_SN == curPkg.Item.Package.SN)
                            .Where(q => q.WORK_ORDER == wo && q.OPER_CODE == "EndNode" && wipIDs.Contains(q.WIP_ID))
                            .Where(q => q.WORK_ORDER == wo && q.OPER_CODE == "EndNode" && _wipIDs.Contains(q.WIP_ID))
                            .ExecuteCommand();
                    }
                };
@@ -719,7 +781,7 @@
            //保存数据库
            SaveStepsCommitActionToDB(endAction);
            //保存成功,返回过站消息
            operInfo.InputQty += CurWipSNs.Count;
            action.Data.Data = new PackingActionOutput() { PkgInfo = curPkg };
@@ -736,7 +798,7 @@
            //重置工序
            ResetNode();
            curtime = DateTime.Now; Logger.Console.Info($"{CurSN} : ***** 15 : {(curtime - prevtime).TotalSeconds}s", ConsoleColor.Green); prevtime = curtime;
            return action;
        }
@@ -828,7 +890,7 @@
                        TempPkgAction.Init(this);
                        int shipQty = input.Data.ToInt32();
                        int curQty = CurPkg.Item.TotalQty;
                        double curQty = CurPkg.Item.TotalQty;
                        int ruleQty = TempPkgAction.PkgRule.GetTotalQty();
                        //有填出货数量则验证当前包装数量必须等于(出货数量/包装规则数量)的余数
                        if (shipQty > 0 && curQty != shipQty % ruleQty)
@@ -872,6 +934,52 @@
            return action;
        }
        /// <summary>
        /// 打印测试标签
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiAction<PackingActionOutput>> TestPrint()
        {
            var action = new ApiAction<PackingActionOutput>();
            try
            {
                var CurPkg = Context.ContainsKey("CurPackage") ? Context["CurPackage"] as WipPkg : null;
                var LabelPV = Biz.Db.Queryable<BAS_LABEL_PV>().ToList();
                var label = CurPkg.Item.LABEL_CODE.IsNullOrEmpty() ? null : Biz.Db.Queryable<BAS_LABEL_TEMP>().Where(q => q.LABEL_CODE == CurPkg.Item.LABEL_CODE).IncludesAllFirstLayer().First();
                var printLb =  SetLabelVariables(LabelPV, label, new PackingAction(), CurPkg);
                action.Data = new PackingActionOutput()
                {
                    ExecCode = "PrintTest",
                    PkgInfo = CurPkg,
                    PrintLable = printLb,
                };
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"包装工序:打印测试标签异常");
            }
            return action;
        }
        /// <summary>
        /// 获取是否打印客户标签还是入库标签
        /// </summary>
        /// <returns></returns>
        public async Task<ApiAction<ShipingInfo>> GetIsCustomerLabelAndShipQty()
        {
            var action = new ApiAction<ShipingInfo>();
            try
            {
                action.Data = new ShipingInfo() { IsPrintCustomerLabel= IsPrintCustomerLabel,ShipQty = Context["CurShipQty"].ToInt32() };
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"包装工序:获取是否打印客户标签还是入库标签异常");
            }
            return action;
        }
        #endregion Functions
        /// <summary>