服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2024-10-24 81cbea9827b32d3a2fbfbd305e1d392276a04ce7
Tiger.Business.MES/BIZ/BIZ_MES_WO.cs
@@ -16,6 +16,7 @@
using Tiger.Model.Entitys.MES.Position;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Drawing.Printing;
using System.Net.NetworkInformation;
namespace Tiger.Business.MES
{
@@ -108,12 +109,18 @@
            {
                //保存前的判断,并生成批次号
                var _wo = await Biz.Db.Queryable<BIZ_MES_WO>().Where(x => x.ORDER_NO == input.Wo.ORDER_NO).FirstAsync();
                //if (_wo.STATUS > (int)BIZ_MES_WO.STATUSs.Init)
                //{
                //    result.IsSuccessed = false;
                //    result.LocaleMsg = new($"工单不是初始化状态,不能下发!");
                //    return result;
                //}
                if (_wo.IsNullOrEmpty())
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单不存在,不能下发!");
                    return result;
                }
                if (_wo.ROUTE_STATUS != BIZ_MES_WO.ROUTE_STATUSs.Finish.GetValue())
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单工艺路线未配置,不能下发!");
                    return result;
                }
                if (_wo.STATUS > (int)BIZ_MES_WO.STATUSs.Working)
                {
                    result.IsSuccessed = false;
@@ -121,19 +128,23 @@
                    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)
                if ((batchs.Count > 0 && 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 (Biz.Db.Queryable<BIZ_MES_WO_BATCH>().Any(x => x.ORDER_NO == input.Wo.ORDER_NO && x.ACT_LINE == input.WoBatch.ACT_LINE && x.STATUS> BIZ_MES_WO.STATUSs.Imported.GetValue() && x.STATUS< BIZ_MES_WO.STATUSs.Closed.GetValue()))
                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.WoBatch.ACT_LINE}]中状态为[{input.WoBatch.STATUS.GetEnumDesc<BIZ_MES_WO.STATUSs>()}],不能下发!");
                    result.LocaleMsg = new($"工单[{input.Wo.ORDER_NO}]已经存在工单批次[{_batch.BATCH_NO}]在线体[{input.WoBatch.ACT_LINE}]中状态为[{_batch.STATUS.GetEnumDesc<BIZ_MES_WO.STATUSs>()}],不能下发!");
                    return result;
                }
                WoContext.RemoveBatch(input.WoBatch.BATCH_NO);
                //WoContext.RemoveBatch(input.WoBatch.BATCH_NO);
                input.Wo.ACT_LINE = input.WoBatch.ACT_LINE;
                input.Wo.RELEASE_TIME = DateTime.Now;
                input.Wo.RELEASE_USER = input.WoBatch.UPDATE_USER;
                var _batchWos = await Biz.Db.Queryable<V_MES_WO_BATCH>().Where(x => x.ORDER_NO == input.WoBatch.ORDER_NO).ToListAsync();
                if (_batchWos.Count > 0)
@@ -160,6 +171,33 @@
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"保存批次工单异常");
                }
                //如果成功,根据条件异步保存工艺路线到产品的客户
                if (result.IsSuccessed && input.IfToCust)
                {
                    Work.DoAsync(() =>
                    {
                        try
                        {
                            SugarParameter[] pars = Biz.Db.Ado.GetParameters(new { WO = input.Wo.ORDER_NO, ERR_CODE = 0, ERR_MSG = "" });
                            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()}");
                            }
                            else
                            {
                                Logger.Default.Error($"工单[{input.Wo.ORDER_NO}]下发完成后保存工艺路线数据到产品[{input.Wo.ITEM_CODE}]的客户[{input.Wo.CUST_CODE ?? ""}]的工艺路线出错,{pars[2].Value.ToString()}");
                            }
                        }
                        catch (Exception ex)
                        {
                            result.CatchExceptionWithLog(ex, "保存批次工单异常");
                        }
                    });
                }
            }
            catch (Exception ex)
@@ -197,7 +235,7 @@
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    db.Updateable(_wo, input.UserId).UpdateColumns(x => new { x.STATUS }).ExecuteCommand();
                    db.Updateable(_wo, input.UserId).UpdateColumns(x => new { x.STATUS, x.ROUTE_STATUS, x.UPDATE_USER, x.UPDATE_TIME }).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
@@ -224,12 +262,27 @@
            try
            {
                //保存前的判断
                var _wo = await Biz.Db.Queryable<BIZ_MES_WO>().Where(x => x.ORDER_NO == input.WorkOrder).FirstAsync();
                var _woBatch = await Biz.Db.Queryable<BIZ_MES_WO_BATCH>().Where(x => x.BATCH_NO == input.WoBatch).FirstAsync();
                if (_woBatch != null)
                {
                    _woBatch.STATUS = input.Status < 0 ? _woBatch.STATUS : input.Status;
                    if (input.Status == BIZ_MES_WO_BATCH.STATUSs.Paused.GetValue()) {
                        WoContext.RemoveBatch(input.WoBatch);
                    if (input.Status == BIZ_MES_WO_BATCH.STATUSs.Paused.GetValue())
                    {
                        //WoContext.RemoveBatch(input.WoBatch);
                    }
                    //如果是下发
                    if (input.Status == BIZ_MES_WO_BATCH.STATUSs.Release.GetValue())
                    {
                        if (Biz.Db.Queryable<BIZ_MES_WO_BATCH>().Any(x => x.ORDER_NO == input.WorkOrder && x.BATCH_NO != input.WoBatch && x.ACT_LINE == input.ActLine && BIZ_MES_WO.STATUSs.Imported.GetValue() < x.STATUS && x.STATUS < BIZ_MES_WO.STATUSs.Closed.GetValue()))
                        {
                            result.IsSuccessed = false;
                            result.LocaleMsg = new($"工单存在未完成的工单批次,不能下发");
                            return result;
                        }
                        _woBatch.RELEASE_TIME = DateTime.Now;
                        _woBatch.RELEASE_USER = input.UserId;
                        _woBatch.ACT_LINE = input.ActLine;
                    }
                }
                else
@@ -242,7 +295,15 @@
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    db.Updateable(_woBatch, input.UserId).UpdateColumns(x=> new { x.STATUS}).ExecuteCommand();
                    //如果是下发
                    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();
                    }
                    else
                    {
                        db.Updateable(_woBatch, input.UserId).UpdateColumns(x => new { x.STATUS, x.UPDATE_USER, x.UPDATE_TIME }).ExecuteCommand();
                    }
                });
                if (!dbTran.IsSuccess)
                {
@@ -268,21 +329,42 @@
            try
            {
                //保存前的判断
                if (input.WORK_ORDER.IsNullOrEmpty())
                if (input.WORK_ORDER.IsNullOrEmpty() && input.PROD_CODE.IsNullOrEmpty())
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单号不能为空!");
                    result.LocaleMsg = new($"工单号或者产品编码不能为空!");
                    return result;
                }
                List<BAS_LABEL_VAR_WO> varWoList = new();
                string imageFileName = Path.GetFileNameWithoutExtension(input.LABEL_VIEW_PATH);
                string path = @$"{BizConfig.Configuration["UploadAddress"]}//Upload/Template//Temp//{imageFileName}.png";
                if (File.Exists(path))
                {
                    varWoList = await Biz.Db.Queryable<BAS_LABEL_VAR_WO>()
                        .Where(x => x.LABEL_ID == input.LABEL_ID)
                        .WhereIF(!input.WORK_ORDER.IsNullOrEmpty(), x => x.WORK_ORDER == input.WORK_ORDER)
                        .WhereIF(!input.PROD_CODE.IsNullOrEmpty(), x => x.PROD_CODE == input.PROD_CODE)
                        .ToListAsync();
                    foreach (var item in varWoList)
                    {
                        item.LABEL_VIEW_PATH = $"{BizConfig.Configuration["DownloadAddress"]}//Template//Temp//{imageFileName}.png";
                    }
                }
                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 } :
                    !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(t => new { t.LABEL_ID, t.WORK_ORDER, t.VAR_NAME, t.GHOST_ROW })
                       .WhereColumns(predicate)
                       .ToStorage();
                    y.AsInsertable.ExecuteCommand();
                    y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                    if (varWoList.Count > 0)
                    {
                        db.Updateable(varWoList).UpdateColumns(q => q.LABEL_VIEW_PATH).ExecuteCommand();
                    }
                });
                if (!dbTran.IsSuccess)
                {
@@ -294,7 +376,7 @@
            {
                result.CatchExceptionWithLog(ex, "更新工单模板变量异常");
            }
            return await Task.FromResult(result);
            return result;
        }
        /// <summary>
@@ -308,25 +390,37 @@
            try
            {
                QueryAble<BAS_LABEL_VAR_WO> query = new();
                if (input.WorkOrder.IsNullOrEmpty())
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单号不能为空!");
                    return result;
                }
                //if (input.WorkOrder.IsNullOrEmpty())
                //{
                //    result.IsSuccessed = false;
                //    result.LocaleMsg = new($"工单号不能为空!");
                //    return result;
                //}
                var labeltemp = Biz.Db.Queryable<BAS_LABEL_TEMP>().Where(q => q.LABEL_CODE.Equals(input.LabelId) || q.ID.Equals(input.LabelId)).First();
                if (labeltemp == null) {
                if (labeltemp == null)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"模板不存在!");
                    return result;
                }
                var lableVars = await Biz.Db.Queryable<BAS_LABEL_VAR_WO>().Where(q => SqlFunc.IsNullOrEmpty(q.WORK_ORDER) && q.LABEL_ID.Equals(labeltemp.ID)).ToListAsync();
                var queryable = Biz.Db.Queryable<BAS_LABEL_VAR_WO>().Where(q => q.WORK_ORDER.Equals(input.WorkOrder) && q.LABEL_ID.Equals(labeltemp.ID));
                query.page = await queryable.ToPageAsync(input.pageIndex, input.pageSize);
                var lableVars = await Biz.Db.Queryable<BAS_LABEL_VAR_WO>().Where(q => SqlFunc.IsNullOrEmpty(q.WORK_ORDER)
                && SqlFunc.IsNullOrEmpty(q.PROD_CODE)
                && SqlFunc.IsNullOrEmpty(q.CUST_CODE)
                && q.LABEL_ID.Equals(labeltemp.ID)).ToListAsync();
                var queryable = Biz.Db.Queryable<BAS_LABEL_VAR_WO>()
                    .Where(q => q.LABEL_ID.Equals(labeltemp.ID))
                    .WhereIF(!input.WorkOrder.IsNullOrEmpty(), q => q.WORK_ORDER.Equals(input.WorkOrder))
                    .WhereIF(!input.ProdCode.IsNullOrEmpty(), q => q.PROD_CODE.Equals(input.ProdCode))
                    .WhereIF(!input.CustCode.IsNullOrEmpty(), q => q.PROD_CODE.Equals(input.CustCode));
                query.page = await queryable.ToPageAsync(input.page, input.pageSize);
                //如果不存在,则返回初始数据
                if (query.page.totals == 0)
                {
                    query.page = await Biz.Db.Queryable<BAS_LABEL_VAR_WO>().Where(q => SqlFunc.IsNullOrEmpty(q.WORK_ORDER) && q.LABEL_ID.Equals(labeltemp.ID)).ToPageAsync(input.pageIndex, input.pageSize);
                    query.page = await Biz.Db.Queryable<BAS_LABEL_VAR_WO>().Where(q => SqlFunc.IsNullOrEmpty(q.WORK_ORDER)
                     && SqlFunc.IsNullOrEmpty(q.PROD_CODE)
                     && SqlFunc.IsNullOrEmpty(q.CUST_CODE)
                     && q.LABEL_ID.Equals(labeltemp.ID))
                     .ToPageAsync(input.page, input.pageSize);
                    query.Items = query.page.data;
                }
                //如果初始变量数大于工单设置的变量数
@@ -336,11 +430,15 @@
                    {
                        if (queryable.ToList().Any(q => q.VAR_NAME == item.VAR_NAME))
                        {
                            var list = queryable.ToList().Where(q => q.VAR_NAME == item.VAR_NAME).First();
                            item.ID = list?.ID;
                            item.WORK_ORDER = input.WorkOrder;
                            item.VAR_VALUE = queryable.ToList().Where(q => q.VAR_NAME == item.VAR_NAME).First()?.VAR_VALUE;
                            item.PROD_CODE = input.ProdCode;
                            item.VAR_VALUE = list?.VAR_VALUE;
                            item.LABEL_VIEW_PATH = list?.LABEL_VIEW_PATH;
                        }
                    }
                    query.Items = lableVars.Skip((input.pageIndex - 1) * input.pageSize).Take(input.pageSize).ToList();
                    query.Items = lableVars.Skip((input.page - 1) * input.pageSize).Take(input.pageSize).ToList();
                    query.page.totals = lableVars.Count;
                    query.page.data = query.Items;
                }
@@ -352,5 +450,36 @@
            }
            return result;
        }
        /// <summary>
        /// 获取重打标签信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiAction<LOG_LABEL_PRINT>> GetRePrintInfo(RePrintInput input)
        {
            var result = new ApiAction<LOG_LABEL_PRINT>();
            try
            {
                //先查出工单条码中是否存在
                var woSn = await Biz.Db.Queryable<BIZ_MES_WO_SN>().Where(q => q.SN.Equals(input.Code) || q.FLOW_SN.Equals(input.Code)).FirstAsync();
                if (woSn == null)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"条码不存在!");
                    return result;
                }
                result.Data = await Biz.Db.Queryable<LOG_LABEL_PRINT>()
                    .WhereIF(input.ReqType == 0, q => q.SN.Equals(woSn.SN) || q.SN.Equals(woSn.FLOW_SN)) //白盒标签
                    .WhereIF(input.ReqType == 1, q => q.SN.Equals(woSn.OUTER_SN)) //箱标签
                    .FirstAsync();
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "获取重打标签信息异常");
            }
            return result;
        }
    }
}