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.Business/Services/Base/InterfaceServiceNew.cs |  104 +++++++++++++++++++++++++++++++---------------------
 1 files changed, 62 insertions(+), 42 deletions(-)

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);
                     }

--
Gitblit v1.9.3