服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2024-10-17 4417ac9e48587bdfcc0bdee5e84dca7627a6e4a4
Tiger.Business/MES/Biz.Route.cs
@@ -273,20 +273,26 @@
                try
                {
                    var _route = Db.Queryable<MES_ROUTE>().Where(x => x.ROT_CODE == route.ROT_CODE).First();
                    if (_route != null && Db.Queryable<MES_ROUTE_NODE>().Where(x => x.ROT_ID == _route.ID).Any())
                    //if (_route != null && Db.Queryable<MES_ROUTE_NODE>().Where(x => x.ROT_ID == _route.ID).Any())
                    //{
                    //    result.IsSuccessed = false;
                    //    result.LocaleMsg = new($"工艺路线已经有设计记录,不能保存!");
                    //    return result;
                    //}
                    if (_route != null && Db.Queryable<MES_PROD_OPER>().Where(x => x.ROT_ID == _route.ID).Any())
                    {
                        result.IsSuccessed = false;
                        result.LocaleMsg = new($"工艺路线已经有设计记录,不能保存!");
                        result.LocaleMsg = new($"工艺路线已经有绑定记录,不能保存!");
                        return result;
                    }
                    var db = Db;
                    var dbTran = db.UseTran(() =>
                    {
                        var y = db.Storageable(route)
                           .WhereColumns(t => new { t.ROT_CODE, t.GHOST_ROW })
                           //.WhereColumns(t => new { t.ROT_CODE, t.GHOST_ROW })
                           .ToStorage();
                        y.AsInsertable.ExecuteCommand();
                        y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                        y.AsUpdateable.ExecuteCommand();
                    });
                    if (!dbTran.IsSuccess)
                    {
@@ -333,6 +339,9 @@
                                ALLOW_DFT_IN = "N",
                                IS_INPUT = "N",
                                IS_OUTPUT = "N",
                                AUTH_ORG = node.AUTH_ORG,
                                AUTH_PROD = node.AUTH_PROD,
                                AUTH_WH = node.AUTH_WH,
                            });
                        }
                    }
@@ -340,7 +349,7 @@
                    List<MES_PROD_ACTION> actList = new List<MES_PROD_ACTION>();
                    foreach (var act in routeData.acts)
                    {
                        if (!Db.Queryable<MES_PROD_ACTION>().Any(q => q.NODE_ID == act.ID && q.ROT_ID == routeData.route.ID))
                        if (!Db.Queryable<MES_PROD_ACTION>().Any(q => q.ACT_ID == act.ID && q.ROT_ID == routeData.route.ID))
                        {
                            actList.Add(new()
                            {
@@ -359,6 +368,9 @@
                                PKG_CODE = "",
                                IS_ACTIVE = "Y",
                                SETUP_FINISH = "N",
                                AUTH_ORG = act.AUTH_ORG,
                                AUTH_PROD = act.AUTH_PROD,
                                AUTH_WH = act.AUTH_WH,
                            });
                        }
                    }
@@ -373,9 +385,11 @@
                            y.AsInsertable.ExecuteCommand();
                            y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                        }
                        db.Deleteable<MES_PROD_OPER>().Where(x => x.PROD_CODE == routeData.route.PROD_CODE && !SqlFunc.ContainsArray(nodeIds, x.NODE_ID)).ExecuteCommand();
                        db.Deleteable<MES_PROD_ACTION>().Where(x => x.PROD_CODE == routeData.route.PROD_CODE && !SqlFunc.ContainsArray(nodeIds, x.NODE_ID)).ExecuteCommand();
                        db.Deleteable<MES_PROD_ACTION>().Where(x => x.PROD_CODE == routeData.route.PROD_CODE && !SqlFunc.ContainsArray(actIds, x.ACT_ID)).ExecuteCommand();
                        if (!routeData.route.PROD_CODE.IsNullOrEmpty())
                        {
                            db.Deleteable<MES_PROD_OPER>().Where(x => x.ROT_ID == routeData.route.ID && x.PROD_CODE == routeData.route.PROD_CODE && !SqlFunc.ContainsArray(nodeIds, x.NODE_ID)).ExecuteCommand();
                            db.Deleteable<MES_PROD_ACTION>().Where(x => x.ROT_ID == routeData.route.ID && x.PROD_CODE == routeData.route.PROD_CODE && !SqlFunc.ContainsArray(actIds, x.ACT_ID)).ExecuteCommand();
                        }
                        db.Deleteable<MES_ROUTE_NODE>(false).Where(x => x.ROT_ID == routeData.route.ID).ExecuteCommand();
                        db.Deleteable<MES_ROUTE_EDGE>(false).Where(x => x.ROT_ID == routeData.route.ID).ExecuteCommand();
                        db.Deleteable<MES_ROUTE_NODE_ACT>(false).Where(x => x.ROT_ID == routeData.route.ID).ExecuteCommand();
@@ -391,13 +405,16 @@
                        {
                            db.Insertable(routeData.acts).ExecuteCommand();
                        }
                        if (operList.Count > 0)
                        if (!routeData.route.PROD_CODE.IsNullOrEmpty())
                        {
                            db.Insertable(operList).ExecuteCommand();
                        }
                        if (actList.Count > 0)
                        {
                            db.Insertable(actList).ExecuteCommand();
                            if (operList.Count > 0)
                            {
                                db.Insertable(operList).ExecuteCommand();
                            }
                            if (actList.Count > 0)
                            {
                                db.Insertable(actList).ExecuteCommand();
                            }
                        }
                    });
                    if (!dbTran.IsSuccess)
@@ -504,11 +521,10 @@
                            expableOper.And(x => x.ROT_ID == item.ID);
                        }
                    }
                    if (!string.IsNullOrEmpty(input.prodCode))
                    {
                        expable.And(x => x.PROD_CODE == input.prodCode);
                        expableOper.And(x => x.PROD_CODE == input.prodCode);
                    }
                    string _custCode = input.custCode ?? "";
                    expable.And(x => x.PROD_CODE == input.prodCode && (x.CUST_CODE == _custCode));
                    expableOper.And(x => x.PROD_CODE == input.prodCode && x.CUST_CODE == _custCode);
                    var exp = expable.ToExpression();
                    var expOper = expableOper.ToExpression();
                    var db = Db;
@@ -614,6 +630,42 @@
                catch (Exception ex)
                {
                    result.CatchExceptionWithLog(ex, "删除工艺路线异常");
                }
                return result;
            }
            /// <summary>
            /// 保存默认工艺路线到产品
            /// </summary>
            /// <param name="input"></param>
            /// <returns></returns>
            public async Task<ApiAction> SetDefaultRoute(ProdRotInput input)
            {
                var result = new ApiAction();
                try
                {
                    var item = await Biz.Db.Queryable<BAS_ITEM>().ByAuth(input.options).Where(x => x.ITEM_CODE == input.prodCode).FirstAsync();
                    if (item == null)
                    {
                        result.IsSuccessed = false;
                        result.LocaleMsg = new($"产品[{input.prodCode}]不存在!");
                        return result;
                    }
                    item.DEFAULT_ROUTE = input.isDefault ? input.rotCode : "";
                    var db = Db;
                    var dbTran = db.UseTran(() =>
                    {
                        db.Updateable(item).UpdateColumns(q => new { q.DEFAULT_ROUTE }).ExecuteCommand();
                    });
                    if (!dbTran.IsSuccess)
                    {
                        result.IsSuccessed = false;
                        result.LocaleMsg = new($"保存默认工艺路线到产品异常");
                    }
                }
                catch (Exception ex)
                {
                    result.CatchExceptionWithLog(ex, "保存默认工艺路线到产品异常");
                }
                return result;
            }
@@ -784,6 +836,9 @@
                                ALLOW_DFT_IN = "N",
                                IS_INPUT = "N",
                                IS_OUTPUT = "N",
                                AUTH_ORG = node.AUTH_ORG,
                                AUTH_PROD = node.AUTH_PROD,
                                AUTH_WH = node.AUTH_WH,
                            });
                        }
                    }
@@ -791,7 +846,7 @@
                    List<MES_WO_ACTION> actList = new List<MES_WO_ACTION>();
                    foreach (var act in routeData.acts)
                    {
                        if (!Db.Queryable<MES_WO_ACTION>().Any(q => q.NODE_ID == act.ID))
                        if (!Db.Queryable<MES_WO_ACTION>().Any(q => q.ACT_ID == act.ID))
                        {
                            actList.Add(new()
                            {
@@ -811,6 +866,9 @@
                                PKG_CODE = "",
                                IS_ACTIVE = "Y",
                                SETUP_FINISH = "N",
                                AUTH_ORG = act.AUTH_ORG,
                                AUTH_PROD = act.AUTH_PROD,
                                AUTH_WH = act.AUTH_WH,
                            });
                        }
                    }
@@ -826,7 +884,6 @@
                        //    y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                        //}
                        db.Deleteable<MES_WO_OPER>().Where(x => x.WORK_ORDER == routeData.route.WORK_ORDER && !SqlFunc.ContainsArray(nodeIds, x.NODE_ID)).ExecuteCommand();
                        db.Deleteable<MES_WO_ACTION>().Where(x => x.WORK_ORDER == routeData.route.WORK_ORDER && !SqlFunc.ContainsArray(nodeIds, x.NODE_ID)).ExecuteCommand();
                        db.Deleteable<MES_WO_ACTION>().Where(x => x.WORK_ORDER == routeData.route.WORK_ORDER && !SqlFunc.ContainsArray(actIds, x.ACT_ID)).ExecuteCommand();
                        db.Deleteable<MES_WO_NODE>(false).Where(x => x.WORK_ORDER == routeData.route.WORK_ORDER).ExecuteCommand();
                        db.Deleteable<MES_WO_EDGE>(false).Where(x => x.WORK_ORDER == routeData.route.WORK_ORDER).ExecuteCommand();
@@ -843,7 +900,7 @@
                        {
                            db.Insertable(routeData.acts).ExecuteCommand();
                        }
                        if (operList.Count>0)
                        if (operList.Count > 0)
                        {
                            db.Insertable(operList).ExecuteCommand();
                        }
@@ -896,6 +953,43 @@
                return await Task.FromResult(result);
            }
            #endregion
            /// <summary>
            /// 获取工艺路线树形结构
            /// </summary>
            /// <param name="prodCode"></param>
            /// <param name="orgCode"></param>
            /// <returns></returns>
            public async Task<List<V_MES_ROUTE_PTREE>> GetRoutePTree(string prodCode, string orgCode)
            {
                var result = new List<V_MES_ROUTE_PTREE>();
                try
                {
                    SugarParameter[] pars = Biz.Db.Ado.GetParameters(new { PROD_CODE = prodCode, ORG_CODE = orgCode });
                    result = Biz.Db.Ado.UseStoredProcedure().SqlQuery<V_MES_ROUTE_PTREE>("SP_MES_GET_ROUTE_PTREE", pars);//返回List
                    var item = await Db.Queryable<BAS_ITEM>().Where(q => q.ITEM_CODE == prodCode).FirstAsync();
                    if (item != null && !item.DEFAULT_ROUTE.IsNullOrEmpty())
                    {
                        var pid = result.Where(q => q.code == $"DefaultRoute_{orgCode}_{prodCode}").Select(q => q.tid).FirstOrDefault();
                        result.ForEach(d =>
                        {
                            if (d.pid == pid && d.code == item.DEFAULT_ROUTE)
                            {
                                d.isDefault = true;
                            }
                            else
                            {
                                d.isDefault = false;
                            }
                        });
                    }
                }
                catch (Exception ex)
                {
                    Logger.Default.Error(ex, "保存默认工艺路线到产品异常");
                }
                return result;
            }
        } //endClass Route
    } //endClass Biz
}