From 6f3b05c06125b457d0c78e259f3befe4910881cf Mon Sep 17 00:00:00 2001
From: Ben Lin <maobin001@msn.com>
Date: 星期日, 27 十月 2024 16:54:29 +0800
Subject: [PATCH] 计划任务更新

---
 Tiger.IBusiness/Service/IInterfaceService.cs                   |   15 ++-
 Tiger.Business/Services/Base/InterfaceService.cs               |   10 ++
 Tiger.Business/Services/Base/InterfaceServiceNew.cs            |  104 +++++++++++++++----------
 Tiger.Controllers.System/Controllers/TSK/TskController.Base.cs |   67 +++++++++++-----
 Tiger.Model.Net/Entitys/TSK/TSK_JOB.cs                         |    1 
 Tiger.IBusiness/Service/ITigerJob.cs                           |    2 
 Tiger.Business.MES/Common/WoContext.cs                         |    2 
 7 files changed, 130 insertions(+), 71 deletions(-)

diff --git a/Tiger.Business.MES/Common/WoContext.cs b/Tiger.Business.MES/Common/WoContext.cs
index 9804f8d..fda4192 100644
--- a/Tiger.Business.MES/Common/WoContext.cs
+++ b/Tiger.Business.MES/Common/WoContext.cs
@@ -50,7 +50,7 @@
         /// <returns></returns>
         public static bool ExistsBatch(string workorder, string lineCode, string batchNo = "", bool canDoWork = false)
         {
-            return WoBatchDic.WhereIF(canDoWork, q => q.Value.Batch.STATUS == BIZ_MES_WO_BATCH.STATUSs.Release.GetValue() || q.Value.Batch.STATUS == BIZ_MES_WO_BATCH.STATUSs.Working.GetValue())
+            return WoBatchDic.WhereIF(canDoWork, q => !q.Value.Batch.IsNullOrEmpty() && (q.Value.Batch.STATUS == BIZ_MES_WO_BATCH.STATUSs.Release.GetValue() || q.Value.Batch.STATUS == BIZ_MES_WO_BATCH.STATUSs.Working.GetValue()))
                 .Any(q => q.Value.Batch.ORDER_NO == workorder && q.Value.Batch.ACT_LINE == lineCode && (batchNo.IsNullOrEmpty() || q.Value.Batch.BATCH_NO == batchNo));
         }
 
diff --git a/Tiger.Business/Services/Base/InterfaceService.cs b/Tiger.Business/Services/Base/InterfaceService.cs
index 8dc4a88..0dfcd20 100644
--- a/Tiger.Business/Services/Base/InterfaceService.cs
+++ b/Tiger.Business/Services/Base/InterfaceService.cs
@@ -175,5 +175,15 @@
         {
             JobManager.Initialize();
         }
+
+        ApiAction IInterfaceService.AddTskJob<TJob>(TJob newEntity, FluentJobParam jobParam)
+        {
+            throw new NotImplementedException();
+        }
+
+        public void RemovefJob(string jobname)
+        {
+            throw new NotImplementedException();
+        }
     }
 }
diff --git a/Tiger.Business/Services/Base/InterfaceServiceNew.cs b/Tiger.Business/Services/Base/InterfaceServiceNew.cs
index 01edbce..93e4aea 100644
--- a/Tiger.Business/Services/Base/InterfaceServiceNew.cs
+++ b/Tiger.Business/Services/Base/InterfaceServiceNew.cs
@@ -1,6 +1,7 @@
 锘縰sing FluentScheduler;
 using Newtonsoft.Json;
 using Rhea.Common;
+using Sundial;
 using System;
 using System.Collections;
 using System.Collections.Generic;
@@ -277,7 +278,8 @@
             SaveRunningStatus(); //淇濆瓨鐘舵��
         }
 
-        private void SaveRunningStatus() {
+        private void SaveRunningStatus()
+        {
             List<TSK_TRIG> listTrig = new List<TSK_TRIG>();
             foreach (var schedule in JobManager.RunningSchedules)
             {
@@ -295,7 +297,7 @@
             //淇濆瓨鍒版暟鎹簱
             var dbTran = db.UseTran(() =>
             {
-                if (listTrig.Count>0)
+                if (listTrig.Count > 0)
                 {
                     db.Updateable(listTrig, "system").UpdateColumns(q => new { q.Status, q.UPDATE_USER, q.UPDATE_TIME }).ExecuteCommand();
                 }
@@ -313,58 +315,59 @@
         /// <param name="newEntity"></param>
         /// <param name="jobParam"></param>
         /// <returns></returns>
-        public ApiAction AddJob<TJob>(TJob newEntity, FluentJobParam jobParam) where TJob : class, ITJob
+        public ApiAction AddTskJob<TJob>(TJob newEntity, FluentJobParam jobParam) where TJob : class, ITJob
         {
             ApiAction apiAction = new();
             try
             {
-                JobManager.AddJob<TJob>((s) => GenerateSchedule(jobParam.Args, s.WithName(typeof(TJob).Name)));
-                string guid = Guid.NewGuid().ToString("N");
-                var trigger = new TSK_TRIG()
+                if (!JobManager.AllSchedules.Any(q => q.Name == jobParam.JobName))
                 {
-                    JobId = guid,
-                    Args = JsonConvert.SerializeObject(jobParam.Args),
-                    Status = TSK_TRIG.Statuss.Running.GetValue(),
-                    StartTime = DateTime.Now,
-                    NumberOfRuns = 1,
-                };
-                TSK_JOB job = new()
-                {
-                    ID = guid,
-                    Remark = jobParam.Remark,
-                    JobType = typeof(TJob).Name,
-                    AssemblyName = jobParam.AssemblyName,
-                    JobName = jobParam.JobName,
-                };
-                if (Biz.Db.Queryable<TSK_JOB>().Any(q=>q.JobName == typeof(TJob).Name)) {
-                    apiAction.IsSuccessed = false;
-                    apiAction.LocaleMsg = new($"浠诲姟宸茬粡瀛樺湪");
-                    return apiAction;
-                }
-                var db = Biz.Db;
-                //淇濆瓨鍒版暟鎹簱
-                var dbTran = db.UseTran(() =>
-                {
-                    if (job != null)
+                    JobManager.AddJob<TJob>((s) => GenerateSchedule(jobParam.Args, s.WithName(jobParam.JobName)));
+                    string guid = Guid.NewGuid().ToString("N");
+                    TSK_JOB job = Biz.Db.Queryable<TSK_JOB>().Where(x => x.JobName == jobParam.JobName).First() ?? new()
                     {
-                        db.Insertable(job, "system").ExecuteCommand();
-                    }
-                    if (trigger != null)
+                        ID = guid,
+                        Remark = jobParam.Remark,
+                        JobType = jobParam.JobName,
+                        AssemblyName = jobParam.AssemblyName,
+                        JobName = jobParam.JobName,
+                    };
+                    var trigger = Biz.Db.Queryable<TSK_TRIG>().Where(x => x.JobId == job.ID).First() ?? new TSK_TRIG()
                     {
-                        db.Insertable(trigger, "system").ExecuteCommand();
+                        JobId = guid,
+                        StartTime = DateTime.Now,
+                        NumberOfRuns = 1,
+                    };
+                    trigger.Args = JsonConvert.SerializeObject(jobParam.Args);
+                    trigger.Status = TSK_TRIG.Statuss.Running.GetValue();
+                    var db = Biz.Db;
+                    //淇濆瓨鍒版暟鎹簱
+                    var dbTran = db.UseTran(() =>
+                    {
+                        var y = db.Storageable(job)
+                           .WhereColumns(t => new { t.JobName, t.GHOST_ROW })
+                           .ToStorage();
+                        y.AsInsertable.ExecuteCommand();
+                        y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
+
+                        var z = db.Storageable(trigger)
+                           .WhereColumns(t => new { t.JobId, t.GHOST_ROW })
+                           .ToStorage();
+                        z.AsInsertable.ExecuteCommand();
+                        z.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
+                    });
+                    if (!dbTran.IsSuccess)
+                    {
+                        apiAction.CatchExceptionWithLog(dbTran.ErrorException, $"娣诲姞宸ヤ綔浠诲姟鏃朵繚瀛樺埌鏁版嵁搴撳紓甯�");
                     }
-                });
-                if (!dbTran.IsSuccess)
-                {
-                    Logger.Scheduler.Trace(dbTran.ErrorException, $"娣诲姞宸ヤ綔浠诲姟鏃朵繚瀛樺埌鏁版嵁搴撳紓甯�");
                 }
-                //apiAction.Data = entity;
 
             }
             catch (System.Exception ex)
             {
-                Logger.Console.Fatal(ex, "Add Job From DB Exception");
+                apiAction.CatchExceptionWithLog(ex);
             }
+            if (!apiAction.IsSuccessed) { JobManager.RemoveJob(jobParam.JobName); }
             return apiAction;
         }
 
@@ -374,6 +377,23 @@
         /// <param name="jobname"></param>
         public void RemovefJob(string jobname)
         {
+            if (JobManager.AllSchedules.Any(q => q.Name == jobname))
+            {
+                string guid = Guid.NewGuid().ToString("N");
+                TSK_JOB job = Biz.Db.Queryable<TSK_JOB>().Where(x => x.JobName == jobname).First();
+                var trigger = Biz.Db.Queryable<TSK_TRIG>().Where(x => x.JobId == job.ID).First();
+                trigger.Status = TSK_TRIG.Statuss.Stop.GetValue();
+                var db = Biz.Db;
+                //淇濆瓨鍒版暟鎹簱
+                var dbTran = db.UseTran(() =>
+                {
+                    db.Updateable(trigger, "system_job").UpdateColumns(q=>new { q.Status,q.UPDATE_TIME,q.UPDATE_USER}).ExecuteCommand();
+                });
+                if (!dbTran.IsSuccess)
+                {
+                    Logger.Scheduler.Fatal(dbTran.ErrorException, $"娣诲姞宸ヤ綔浠诲姟鏃朵繚瀛樺埌鏁版嵁搴撳紓甯�");
+                }
+            }
             JobManager.RemoveJob(jobname);
         }
 
@@ -489,7 +509,7 @@
                     }
                     break;
                 case TrigArgs.EveryType.Weekdays:
-                    if (args.Hours > 0 )
+                    if (args.Hours > 0)
                     {
                         timeUnit.Weekdays().At(args.Hours, args.Minutes);
                     }
@@ -499,7 +519,7 @@
                     }
                     break;
                 case TrigArgs.EveryType.Months:
-                    if (args.Days>0 && args.Hours > 0)
+                    if (args.Days > 0 && args.Hours > 0)
                     {
                         timeUnit.Months().On(args.Days).At(args.Hours, args.Minutes);
                     }
diff --git a/Tiger.Controllers.System/Controllers/TSK/TskController.Base.cs b/Tiger.Controllers.System/Controllers/TSK/TskController.Base.cs
index e4c17e3..358e6d6 100644
--- a/Tiger.Controllers.System/Controllers/TSK/TskController.Base.cs
+++ b/Tiger.Controllers.System/Controllers/TSK/TskController.Base.cs
@@ -29,27 +29,6 @@
             return Ok(response);
         }
 
-        /// <summary>
-        /// 鏍规嵁Job瀹炰綋鍚嶇О鍜孞ob鍚嶇О娣诲姞浠诲姟
-        /// </summary>
-        /// <param name="action"></param>
-        /// <returns></returns>
-        [HttpPost]
-        [Route("api/[controller]/[action]")]
-        public async Task<IActionResult> AddJob(ApiAction<FluentJobParam> action)
-        {
-            ApiAction response = new();
-            try
-            {
-                response = response.GetResponse(DI.Resolve<IBizContext>().GetInterfaceService()?.AddJob(action.NewDataEntity(), action.Data) ?? new ApiAction($"浠诲姟鏈惎鐢�", false));
-            }
-            catch (System.Exception ex)
-            {
-                response = response.GetResponse().CatchExceptionWithLog(ex);
-            }
-            return Ok(response);
-        }
-
         [HttpPost]
         [Route("api/[controller]/[action]")]
         public async Task<IActionResult> UpdateJob(ApiAction<TSK_JOB> action)
@@ -189,5 +168,51 @@
             }
             return Ok(response);
         }
+
+        #region 鏂扮増璁″垝浠诲姟
+
+
+        /// <summary>
+        /// 鏍规嵁Job瀹炰綋鍚嶇О鍜孞ob鍚嶇О娣诲姞浠诲姟
+        /// </summary>
+        /// <param name="action"></param>
+        /// <returns></returns>
+        [HttpPost]
+        [Route("api/[controller]/[action]")]
+        public async Task<IActionResult> AddTskJob(ApiAction<FluentJobParam> action)
+        {
+            ApiAction response = new();
+            try
+            {
+                response = response.GetResponse(DI.Resolve<IBizContext>().GetInterfaceService()?.AddTskJob(action.NewDataEntity(), action.Data) ?? new ApiAction($"浠诲姟鏈惎鐢�", false));
+            }
+            catch (System.Exception ex)
+            {
+                response = response.GetResponse().CatchExceptionWithLog(ex);
+            }
+            return Ok(response);
+        }
+
+        /// <summary>
+        /// 鍒犻櫎浣滀笟
+        /// </summary>
+        /// <param name="action"></param>
+        /// <returns></returns>
+        [HttpPost]
+        [Route("api/[controller]/[action]")]
+        public async Task<IActionResult> RemoveTskJob(ApiAction action)
+        {
+            ApiAction response = new();
+            try
+            {
+                DI.Resolve<IBizContext>().GetInterfaceService()?.RemovefJob(action.Data?.ToString());
+            }
+            catch (System.Exception ex)
+            {
+                response = response.GetResponse().CatchExceptionWithLog(ex);
+            }
+            return Ok(response);
+        }
+        #endregion
     }
 }
diff --git a/Tiger.IBusiness/Service/IInterfaceService.cs b/Tiger.IBusiness/Service/IInterfaceService.cs
index 778e283..52e71df 100644
--- a/Tiger.IBusiness/Service/IInterfaceService.cs
+++ b/Tiger.IBusiness/Service/IInterfaceService.cs
@@ -1,4 +1,5 @@
-锘縰sing Rhea.Common;
+锘縰sing FluentScheduler;
+using Rhea.Common;
 using Sundial;
 using System;
 using System.Collections.Generic;
@@ -11,16 +12,18 @@
 {
     public interface IInterfaceService:IDependency
     {
-        public void SetSchedulerFactory(ISchedulerFactory schedulerFactory);
-        public ApiAction AddJob<TJob>(TJob newEntity, TskJobParam jobParam) where TJob : class, IJob;
-        public ApiAction AddJob<TJob>(string jobname, params TriggerBuilder[] triggerBuilders) where TJob : class, IJob;
+        public void SetSchedulerFactory(Sundial.ISchedulerFactory schedulerFactory);
+        public ApiAction AddJob<TJob>(TJob newEntity, TskJobParam jobParam) where TJob : class, Sundial.IJob;
+        public ApiAction AddTskJob<TJob>(TJob newEntity, FluentJobParam jobParam) where TJob : class, ITJob;
+        public ApiAction AddJob<TJob>(string jobname, params Sundial.TriggerBuilder[] triggerBuilders) where TJob : class, Sundial.IJob;
         public ApiAction UpdateJob(TSK_JOB job);
         public void StartAllJob();
         public void StartJob(string jobname);
-        public IScheduler GetJob(string jobname);
-        public List<IScheduler> GetJobs();
+        public Sundial.IScheduler GetJob(string jobname);
+        public List<Sundial.IScheduler> GetJobs();
         public void PauseJob(string jobname);
         public void RemoveJob(string jobname);
+        public void RemovefJob(string jobname);
         public void AddJobFromDB();
         public void JobInitialize();
     }
diff --git a/Tiger.Business/Services/Base/ITigerJob.cs b/Tiger.IBusiness/Service/ITigerJob.cs
similarity index 95%
rename from Tiger.Business/Services/Base/ITigerJob.cs
rename to Tiger.IBusiness/Service/ITigerJob.cs
index efc33f0..5284c34 100644
--- a/Tiger.Business/Services/Base/ITigerJob.cs
+++ b/Tiger.IBusiness/Service/ITigerJob.cs
@@ -6,7 +6,7 @@
 using System.Threading;
 using System.Threading.Tasks;
 
-namespace Tiger.Business
+namespace Tiger.IBusiness
 {
     /// <summary>
     /// Sundial浠诲姟鍩虹被
diff --git a/Tiger.Model.Net/Entitys/TSK/TSK_JOB.cs b/Tiger.Model.Net/Entitys/TSK/TSK_JOB.cs
index b01b857..d18d68d 100644
--- a/Tiger.Model.Net/Entitys/TSK/TSK_JOB.cs
+++ b/Tiger.Model.Net/Entitys/TSK/TSK_JOB.cs
@@ -161,6 +161,7 @@
     public class FluentJobParam: TskJobParam
     {
         public string AssemblyName { get; set; }
+        public string DataType { get; set; }
 
         /// <summary>
         /// 鍙傛暟

--
Gitblit v1.9.3