服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2025-01-03 9e70e03376aa1d7b86baef15e2ddfe3641fc0cb8
Tiger.Business.MES/BIZ/BIZ_MES_WO.cs
@@ -19,6 +19,8 @@
using System.Net.NetworkInformation;
using Tiger.Model.MES.Yada;
using static Microsoft.CodeAnalysis.CSharp.SyntaxTokenParser;
using Tiger.Model.Minsun;
using Tiger.Model.Entitys.MES.U9C;
namespace Tiger.Business.MES
{
@@ -130,20 +132,30 @@
                    return result;
                }
                var batchs = await Biz.Db.Queryable<BIZ_MES_WO_BATCH>().Where(x => x.ORDER_NO == input.Wo.ORDER_NO).ToListAsync();
                if ((batchs.Count > 0 && batchs.Sum(x => x.PLAN_QTY) + input.WoBatch.PLAN_QTY > _wo.PLAN_QTY) || input.WoBatch.PLAN_QTY > _wo.PLAN_QTY)
                if (batchs.Count > 0)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单下发的数量超过工单计划数量,不能下发!");
                    return result;
                }
                var _batch = Biz.Db.Queryable<BIZ_MES_WO_BATCH>().Where(x => x.ORDER_NO == input.Wo.ORDER_NO && x.ACT_LINE == input.WoBatch.ACT_LINE && x.STATUS < BIZ_MES_WO.STATUSs.Closed.GetValue()).First();
                if (_batch != null)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单[{input.Wo.ORDER_NO}]已经存在工单批次[{_batch.BATCH_NO}]在线体[{input.WoBatch.ACT_LINE}]中状态为[{_batch.STATUS.GetEnumDesc<BIZ_MES_WO.STATUSs>()}],不能下发!");
                    return result;
                    if ((batchs.Sum(x => x.PLAN_QTY) + input.WoBatch.PLAN_QTY > _wo.PLAN_QTY) || input.WoBatch.PLAN_QTY > _wo.PLAN_QTY)
                    {
                        result.IsSuccessed = false;
                        result.LocaleMsg = new($"工单下发的数量超过工单计划数量,不能下发!");
                        return result;
                    }
                    if (batchs.Any(x => x.ORDER_NO == input.Wo.ORDER_NO && x.ACT_LINE == input.WoBatch.ACT_LINE && x.STATUS < BIZ_MES_WO.STATUSs.Closed.GetValue()))
                    {
                        result.IsSuccessed = false;
                        result.LocaleMsg = new($"工单[{input.Wo.ORDER_NO}]已经有批次存在线体[{input.WoBatch.ACT_LINE}]中,而且状态不为关闭,不能下发!");
                        return result;
                    }
                    if (batchs.Any(x => x.ORDER_NO == input.Wo.ORDER_NO && x.STATUS == BIZ_MES_WO.STATUSs.Init.GetValue() && SqlFunc.DateIsSame(x.PLAN_DATE, DateTime.Now)))
                    {
                        result.IsSuccessed = false;
                        result.LocaleMsg = new($"工单[{input.Wo.ORDER_NO}]已经有批次存在计划日期相同,而且状态为[初始化],请转到工单批次中下发!");
                        return result;
                    }
                }
                //WoContext.RemoveBatch(input.WoBatch.BATCH_NO);
                input.WoBatch.PLAN_DATE = DateTime.Now;
                input.Wo.PLAN_DATE = (input.Wo.PLAN_DATE ?? "") + ((input.Wo.PLAN_DATE ?? "").Contains(input.WoBatch.PLAN_DATE.ToString("yyyy-MM-dd")) ? "" : $",{input.WoBatch.PLAN_DATE.ToString("yyyy-MM-dd")}");
                input.Wo.ACT_LINE = input.WoBatch.ACT_LINE;
                input.Wo.RELEASE_TIME = DateTime.Now;
                input.Wo.RELEASE_USER = input.WoBatch.UPDATE_USER;
@@ -165,7 +177,7 @@
                       .WhereColumns(t => new { t.ORDER_NO, t.GHOST_ROW })
                       .ToStorage();
                    y.AsInsertable.ExecuteCommand();
                    y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                    y.AsUpdateable.IgnoreColumns(x => x.ID).UpdateColumns(q => new { q.STATUS, q.PLAN_DATE, q.ACT_LINE, q.RELEASE_TIME, q.RELEASE_USER, q.UPDATE_TIME, q.UPDATE_USER }).ExecuteCommand();
                    db.Insertable(input.WoBatch).ExecuteCommand();
                });
@@ -185,7 +197,6 @@
                            pars[1].Direction = System.Data.ParameterDirection.Output;
                            pars[2].Direction = System.Data.ParameterDirection.Output;
                            Biz.Db.Ado.UseStoredProcedure().ExecuteCommand("SP_MES_WO2CUST", pars);
                            //response.LocaleMsg = new(pars[2].Value.ToString());
                            if (pars[1].Value.ToInt32() == 0)
                            {
                                Logger.Default.Info($"工单[{input.Wo.ORDER_NO}]下发完成后保存工艺路线数据到产品[{input.Wo.ITEM_CODE}]的客户[{input.Wo.CUST_CODE ?? ""}]的工艺路线中,{pars[2].Value.ToString()}");
@@ -224,6 +235,8 @@
                var _wo = await Biz.Db.Queryable<BIZ_MES_WO>().Where(x => x.ORDER_NO == input.WorkOrder).FirstAsync();
                if (_wo != null)
                {
                    _wo.CHANGE_TIME = _wo.STATUS == BIZ_MES_WO.STATUSs.Paused.GetValue() && input.Status == BIZ_MES_WO.STATUSs.Working.GetValue() ? DateTime.Now : _wo.CHANGE_TIME;
                    _wo.CHANGE_USER = _wo.STATUS == BIZ_MES_WO.STATUSs.Paused.GetValue() && input.Status == BIZ_MES_WO.STATUSs.Working.GetValue() ? input.UserId : _wo.CHANGE_USER;
                    _wo.STATUS = input.Status < 0 ? _wo.STATUS : input.Status;
                    _wo.ROUTE_STATUS = input.RouteStatus;
                }
@@ -268,6 +281,8 @@
                var _woBatch = await Biz.Db.Queryable<BIZ_MES_WO_BATCH>().Where(x => x.BATCH_NO == input.WoBatch).FirstAsync();
                if (_woBatch != null)
                {
                    _woBatch.CHANGE_TIME = _woBatch.STATUS == BIZ_MES_WO_BATCH.STATUSs.Paused.GetValue() && input.Status == BIZ_MES_WO_BATCH.STATUSs.Working.GetValue() ? DateTime.Now : _woBatch.CHANGE_TIME;
                    _woBatch.CHANGE_USER = _woBatch.STATUS == BIZ_MES_WO_BATCH.STATUSs.Paused.GetValue() && input.Status == BIZ_MES_WO_BATCH.STATUSs.Working.GetValue() ? input.UserId : _woBatch.CHANGE_USER;
                    _woBatch.STATUS = input.Status < 0 ? _woBatch.STATUS : input.Status;
                    if (input.Status == BIZ_MES_WO_BATCH.STATUSs.Paused.GetValue())
                    {
@@ -285,6 +300,16 @@
                        _woBatch.RELEASE_TIME = DateTime.Now;
                        _woBatch.RELEASE_USER = input.UserId;
                        _woBatch.ACT_LINE = input.ActLine;
                        _woBatch.CHANGE_USER = input.UserId;
                        _woBatch.CHANGE_TIME = DateTime.Now;
                        _wo.STATUS = BIZ_MES_WO.STATUSs.Release.GetValue();
                    }
                    else
                    {
                        if (input.Status == BIZ_MES_WO_BATCH.STATUSs.Init.GetValue() && !Biz.Db.Queryable<BIZ_MES_WO_BATCH>().Where(x => x.ORDER_NO == input.WorkOrder && x.BATCH_NO != input.WoBatch && x.STATUS > BIZ_MES_WO_BATCH.STATUSs.Init.GetValue()).Any())
                        {
                            _wo.STATUS = BIZ_MES_WO.STATUSs.Init.GetValue();
                        }
                    }
                }
                else
@@ -300,11 +325,15 @@
                    //如果是下发
                    if (input.Status == BIZ_MES_WO_BATCH.STATUSs.Release.GetValue())
                    {
                        db.Updateable(_woBatch, input.UserId).UpdateColumns(x => new { x.STATUS, x.RELEASE_USER, x.RELEASE_TIME, x.ACT_LINE, x.UPDATE_USER, x.UPDATE_TIME }).ExecuteCommand();
                        db.Updateable(_woBatch, input.UserId).UpdateColumns(x => new { x.STATUS, x.RELEASE_USER, x.RELEASE_TIME, x.ACT_LINE, x.UPDATE_USER, x.UPDATE_TIME, x.CHANGE_USER, x.CHANGE_TIME }).ExecuteCommand();
                    }
                    else
                    {
                        db.Updateable(_woBatch, input.UserId).UpdateColumns(x => new { x.STATUS, x.UPDATE_USER, x.UPDATE_TIME }).ExecuteCommand();
                        db.Updateable(_woBatch, input.UserId).UpdateColumns(x => new { x.STATUS, x.UPDATE_USER, x.UPDATE_TIME, x.CHANGE_USER, x.CHANGE_TIME }).ExecuteCommand();
                    }
                    if (!_wo.IsNullOrEmpty())
                    {
                        db.Updateable(_wo, input.UserId).UpdateColumns(x => new { x.STATUS, x.UPDATE_USER, x.UPDATE_TIME }).ExecuteCommand();
                    }
                });
                if (!dbTran.IsSuccess)
@@ -355,7 +384,7 @@
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    Expression<Func<BAS_LABEL_VAR_WO, object>> predicate = !input.WORK_ORDER.IsNullOrEmpty() ? t => new { t.LABEL_ID, t.WORK_ORDER, t.VAR_NAME, t.GHOST_ROW } :
                    Expression<Func<BAS_LABEL_VAR_WO, object>> predicate = !input.WORK_ORDER.IsNullOrEmpty() ? t => new { t.LABEL_ID, t.WORK_ORDER, t.VAR_NAME, t.GHOST_ROW } :
                    !input.PROD_CODE.IsNullOrEmpty() ? t => new { t.LABEL_ID, t.PROD_CODE, t.VAR_NAME, t.GHOST_ROW } : t => new { t.LABEL_ID, t.VAR_NAME, t.GHOST_ROW };
                    var y = db.Storageable(input)
                       .WhereColumns(predicate)
@@ -463,7 +492,7 @@
            var result = new ApiAction();
            try
            {
                var woSn = await Biz.Db.Queryable<BIZ_MES_WO_SN>().Where(q=>q.SN == code || q.FLOW_SN == code || q.OUTER_SN == code).FirstAsync();
                var woSn = await Biz.Db.Queryable<BIZ_MES_WO_SN>().Where(q => q.SN == code || q.FLOW_SN == code || q.OUTER_SN == code).FirstAsync();
                if (woSn == null)
                {
                    result.IsSuccessed = false;
@@ -563,7 +592,7 @@
                    result.LocaleMsg = new($"条码不存在!");
                    return result;
                }
                var woNode = await Biz.Db.Queryable<MES_WO_NODE>().Where(q => q.WORK_ORDER.Equals(woSn.WORK_ORDER) && q.OPER_CODE== "SpecPackingNode").FirstAsync();
                var woNode = await Biz.Db.Queryable<MES_WO_NODE>().Where(q => q.WORK_ORDER.Equals(woSn.WORK_ORDER) && q.OPER_CODE == "SpecPackingNode").FirstAsync();
                if (woNode == null)
                {
                    result.IsSuccessed = false;
@@ -579,7 +608,7 @@
                //如果查不到就去行为日志查
                if (labelPrint.IsNullOrEmpty() && input.ReqType == 1)
                {
                    printParam = Biz.Db.Queryable<MES_WIP_ACT>().Where(q => q.CARTON_SN.Equals(woSn.OUTER_SN) && q.ACT_VALUE_4 == "Customer").First()?.ACT_VALUE_3 ?? "";
                    printParam = Biz.Db.Queryable<MES_WIP_ACT>().Where(q => q.CARTON_SN.Equals(woSn.OUTER_SN) && q.ACT_VALUE_4 == "Customer" && !SqlFunc.IsNullOrEmpty(q.ACT_VALUE_3)).First()?.ACT_VALUE_3 ?? "";
                }
                else if (labelPrint.IsNullOrEmpty() && input.ReqType == 2)
                {
@@ -604,22 +633,46 @@
        /// </summary>
        /// <param name="wo"></param>
        /// <returns></returns>
        public async Task<ApiAction> GetShipList(string wo, string orgCode)
        public async Task<ApiAction<ShippingOutput>> GetShipList(ShippingInput input)
        {
            var result = new ApiAction();
            var result = new ApiAction<ShippingOutput>();
            try
            {
                DbClient db = Biz.DataSource["YadaU9C"].Client;
                var org = await Biz.Db.Queryable<SYS_ORGANIZATION>().Where(q=>q.ORG_CODE == orgCode).FirstAsync();
                var shipList = db.Queryable<mes_ShipList>().Where(q => q.MoDoc.Equals(wo) && SqlFunc.DateIsSame(q.BusinessDate, DateTime.Now) && q.Status == 2 && q.Org == org.ID.ToInt64()).ToList();
                if (shipList.Any())
                var org = await Biz.Db.Queryable<SYS_ORGANIZATION>().Where(q => q.ORG_CODE == input.orgCode).FirstAsync();
                string wo = input.wo;
                if (!input.SN.IsNullOrEmpty() && input.wo.IsNullOrEmpty())
                {
                    result.Data = shipList[0].ShipQty;
                    var woSn = Biz.Db.Queryable<BIZ_MES_WO_SN>().Where(q => q.SN == input.SN || q.FLOW_SN == input.SN).First();
                    if (!woSn.IsNullOrEmpty())
                    {
                        wo = woSn.WORK_ORDER;
                    }
                }
                var ship = db.Queryable<mes_ShipList>().Where(q => q.MoDoc.Equals(wo) && q.Org == org.ID.ToInt64() && q.Status == 2).First();
                if (!ship.IsNullOrEmpty())
                {
                    result.Data = ship.BusinessDate.Date == DateTime.Now.Date ? new()
                    {
                        ShipDoc = ship.ShipDoc,
                        ShippingQty = ship.ShipQty.ToDouble(),
                        Tag = "Customer"
                    } : new()
                    {
                        ShipDoc = ship.ShipDoc,
                        ShippingQty = ship.ShipQty.ToDouble(),
                        Tag = "InStoreLabel"
                    };
                    result.LocaleMsg = new($"出货信息存在!");
                }
                else
                {
                    result.Data = "InStoreLabel";
                    result.Data = new()
                    {
                        ShipDoc = "",
                        ShippingQty = 0,
                        Tag = "InStoreLabel"
                    };
                }
            }
            catch (Exception ex)
@@ -662,5 +715,188 @@
            }
            return result;
        }
        /// <summary>
        /// 生成入库获取条码工单信息及包装信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<ApiAction<InStoreInfo>> GetErpProdInBth(string code)
        {
            var result = new ApiAction<InStoreInfo>();
            try
            {
                //得到工单条码记录
                var cartonSn = code.Split('|')[0];
                var woSns = await Biz.Db.Queryable<BIZ_MES_WO_SN>().Where(q => q.OUTER_SN == cartonSn).ToListAsync();
                if (woSns.Count == 0)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"条码不存在!");
                    return result;
                }
                var wo = await Biz.Db.Queryable<BIZ_MES_WO>().Where(q => q.ORDER_NO == woSns[0].WORK_ORDER).IncludesAllFirstLayer().IncludesAllSecondLayer(q => q._ItemInfos).FirstAsync();
                if (wo == null)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"条码对应的工单不存在");
                    return result;
                }
                //判断工单是否已经全部入库
                InStoreInfo inStoreInfo = new()
                {
                    ErpProdInBth = new()
                    {
                        WORK_ORDER = wo.ORDER_NO,
                        BATCH_NO = woSns[0].BATCH_NO,
                        SALES_ORDER = wo.SALES_ORDER,
                        ITEM_CODE = wo.ITEM_CODE,
                        ITEM_NAME = wo.ItemInfo?.ITEM_DESC,
                        SapCode = wo.ItemInfo?.ExtInfo?.SapCode,
                        SCAN_QTY = woSns.Count,
                        IS_HANDLED = "N" //Y-已生成,N-未生成,F-失败
                    },
                    InStoreScanInfo = new()
                    {
                        SN = cartonSn,
                        SALES_ORDER = wo.SALES_ORDER,
                        WORK_ORDER = wo.ORDER_NO,
                        BATCH_NO = woSns[0].BATCH_NO,
                        SCAN_QTY = woSns.Count
                    }
                };
                result.Data = inStoreInfo;
                result.LocaleMsg = new($"获取条码[{code}]工单信息及包装信息成功");
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, $"获取条码[{code}]工单信息及包装信息异常");
            }
            return result;
        }
        /// <summary>
        /// 获取工单备料信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<ApiAction<List<WoFlowCardPrintJson>>> GetWoPickList(string code)
        {
            var action = new ApiAction<List<WoFlowCardPrintJson>>($"成功", new List<WoFlowCardPrintJson>());
            try
            {
                DbClient db = Biz.DataSource["YadaU9C"].Client;
                var moPickLists = db.Queryable<mes_MOPickList>().Where(q => q.MoDocNo == code).OrderBy(q => q.DocLineNO).ToList();
                List<WoFlowCardPrintJson> woFlowCardPrintJson = new();
                if (moPickLists.Count>0)
                {
                    var ids = moPickLists.Select(x => SqlFunc.ToString(x.ItemMaster)).ToList();
                    var items = Biz.Db.Queryable<BAS_ITEM>().Where(q => ids.Contains(q.ID)).IncludesAllFirstLayer().ToList();
                    var wo = await Biz.Db.Queryable<BIZ_MES_WO>().Where(q => q.ORDER_NO == code).IncludesAllFirstLayer().FirstAsync();
                    SugarParameter[] pars = Biz.Db.Ado.GetParameters(new { WO = code });
                    var routeList = Biz.Db.Ado.UseStoredProcedure().SqlQuery<V_MES_WO_PTREE>("SP_MES_GET_WO_PTREE", pars);//返回List
                    var routeStr = string.Join("->", routeList.Where(q => q.type == "Node" && q.name != "完工").OrderBy(x => x.seq).Select(q => q.name).ToList());
                    //生成打印实体
                    int i = 1;
                    int n = 1;
                    string Model = moPickLists.Where(q => q.IssueStyleName == "推式").FirstOrDefault()?.ItemModel;
                    string MoRemark = moPickLists[0]?.MoRemark;
                    string SoRemark = moPickLists[0]?.SoRemark;
                    string Customer = moPickLists[0]?.Customer;
                    string DemandCode = moPickLists[0]?.DemandCode;
                    WoFlowCardPrintJson printJson = new()
                    {
                        ID = Guid.NewGuid().ToString("N"),
                        OrderNo = code,
                        Status = wo.STATUS.GetEnum<BIZ_MES_WO.STATUSs>().GetDesc(),
                        Model = Model,
                        ItemCode = wo.ITEM_CODE,
                        ItemDesc = wo.ItemInfo?.ITEM_DESC,
                        SapCode = wo.ItemInfo?.ExtInfo?.SapCode,
                        Qty = wo.PLAN_QTY,
                        SoRemark = SoRemark,
                        MoRemark = MoRemark,
                        Customer = Customer,
                        DemandCode = DemandCode,
                        RouteStr = routeStr,
                        Items = new() { }
                    };
                    foreach (var item in moPickLists)
                    {
                        PrintWoPickList list = new()
                        {
                            LineNo = item.DocLineNO,
                            SapCode = item.SapCode,
                            ItemCode = item.Code,
                            ItemDesc = items.Where(q => q.ID == SqlFunc.ToString(item.ItemMaster)).FirstOrDefault()?.ITEM_DESC,
                            PickQty = item.ActualReqQty,
                            LineRemark = item.Remark,
                            WhCode = item.InventoryInfoCode,
                            WhCode07 = item.PictureCode,
                            Method = item.IssueStyleName,
                        };
                        printJson.Items.Add(list);
                        if (i == 8 || n % 11 == 0)
                        {
                            if (i == 8) { n = 0; }
                            woFlowCardPrintJson.Add(printJson);
                            printJson = new()
                            {
                                ID = Guid.NewGuid().ToString("N"),
                                OrderNo = code,
                                ItemCode = wo.ITEM_CODE,
                                ItemDesc = wo.ItemInfo?.ITEM_DESC,
                                SapCode = wo.ItemInfo?.ExtInfo?.SapCode,
                                Qty = wo.PLAN_QTY,
                                Model = Model,
                                MoRemark = MoRemark,
                                SoRemark = SoRemark,
                                Customer = Customer,
                                DemandCode = DemandCode,
                                RouteStr = routeStr,
                                Items = new() { }
                            };
                        }
                        i++;
                        n++;
                    }
                    if (printJson.Items.Count > 0)
                    {
                        woFlowCardPrintJson.Add(printJson);
                    }
                }
                action.Data = woFlowCardPrintJson;
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"获取工单{code}备料信息异常");
            }
            return action;
        }
        #region 提交前通用方法
        /// <summary>
        /// 提交前检查是否岗位、工单和不良代码
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<ApiAction<CheckCodeOutput>> CheckCode(string code)
        {
            var action = new ApiAction<CheckCodeOutput>($"检查条码{code}成功", new CheckCodeOutput());
            try
            {
                action.Data.Position = DbCache.Cache.Position[code];
                action.Data.WorkOrder = await Biz.Db.Queryable<BIZ_MES_WO>().Where(q => q.ORDER_NO == code).FirstAsync();
                action.Data.Defect = DbCache.Cache.Defect[code];
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"检查条码{code}异常");
            }
            return action;
        }
        #endregion
    }
}