From bf84ef23de798e6509394a1424cc9f6cc0307c65 Mon Sep 17 00:00:00 2001
From: Rodney Chen <rodney.chen@hotmail.com>
Date: 星期三, 20 十一月 2024 15:09:18 +0800
Subject: [PATCH] Merge branch 'master' of http://47.115.28.255:8110/r/TigerClouds-Tech/Server/TigerApi6_2024

---
 Tiger.Business/Services/Base/InterfaceServiceNew.cs |  156 +++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 113 insertions(+), 43 deletions(-)

diff --git a/Tiger.Business/Services/Base/InterfaceServiceNew.cs b/Tiger.Business/Services/Base/InterfaceServiceNew.cs
index 01edbce..bec31f0 100644
--- a/Tiger.Business/Services/Base/InterfaceServiceNew.cs
+++ b/Tiger.Business/Services/Base/InterfaceServiceNew.cs
@@ -1,10 +1,12 @@
 锘縰sing FluentScheduler;
 using Newtonsoft.Json;
 using Rhea.Common;
+using Sundial;
 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
+using System.Reflection;
 using System.Security.Cryptography;
 using System.Text;
 using System.Threading;
@@ -277,7 +279,8 @@
             SaveRunningStatus(); //淇濆瓨鐘舵��
         }
 
-        private void SaveRunningStatus() {
+        private void SaveRunningStatus()
+        {
             List<TSK_TRIG> listTrig = new List<TSK_TRIG>();
             foreach (var schedule in JobManager.RunningSchedules)
             {
@@ -295,7 +298,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 +316,67 @@
         /// <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");
+                    if (Biz.Db.Queryable<TSK_JOB>().Any(x => 
+                    (x.JobName != jobParam.JobName && x.JobType == jobParam.DataType) || 
+                    (x.JobName == jobParam.JobName && x.JobType != jobParam.DataType)))
                     {
-                        db.Insertable(job, "system").ExecuteCommand();
+                        apiAction.IsSuccessed = false;
+                        apiAction.LocaleMsg = new($"鐩稿悓鐨勭被鍚嶄笉鑳芥湁涓嶅悓鐨勪换鍔″悕鎴栬�呯浉鍚岀殑浠诲姟鍚嶄笉鑳芥湁涓嶅悓鐨勭被鍚�");
+                        return apiAction;
                     }
-                    if (trigger != null)
+                    TSK_JOB job = Biz.Db.Queryable<TSK_JOB>().Where(x => x.JobName == jobParam.JobName && x.JobType == jobParam.DataType).First() ?? new()
                     {
-                        db.Insertable(trigger, "system").ExecuteCommand();
+                        ID = guid,
+                    };
+                    job.Remark = jobParam.Remark;
+                    job.JobType = jobParam.DataType;
+                    job.AssemblyName = jobParam.AssemblyName;
+                    job.JobName = jobParam.JobName;
+                    var trigger = Biz.Db.Queryable<TSK_TRIG>().Where(x => x.JobId == job.ID).First() ?? new TSK_TRIG()
+                    {
+                        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.JobType, 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 +386,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);
         }
 
@@ -389,7 +418,7 @@
                 var jobs = Biz.Db.Queryable<TSK_JOB>().IncludesAllFirstLayer().ToList();
                 foreach (var job in jobs)
                 {
-                    if (job.JobName == "fU9C_MES_GetWo" || job.JobName == "fTest")
+                    //if (job.JobName == "fU9C_MES_GetWo" || job.JobName == "fTest")
                     {
                         Type type = Type.GetType($"{job.AssemblyName}.{job.JobType},{job.AssemblyName}", throwOnError: true);
                         dynamic _type = Activator.CreateInstance(type);
@@ -417,7 +446,7 @@
         /// <returns></returns>
         private Schedule Schedule<TJob>(TJob entity, string JobName, Registry registry) where TJob : class, ITJob
         {
-            return registry.Schedule<TJob>().WithName(typeof(TJob).Name);
+            return registry.Schedule<TJob>().WithName(JobName);
         }
 
         /// <summary>
@@ -489,7 +518,7 @@
                     }
                     break;
                 case TrigArgs.EveryType.Weekdays:
-                    if (args.Hours > 0 )
+                    if (args.Hours > 0)
                     {
                         timeUnit.Weekdays().At(args.Hours, args.Minutes);
                     }
@@ -499,7 +528,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);
                     }
@@ -512,6 +541,47 @@
         }
 
         /// <summary>
+        /// 绔嬪嵆杩愯
+        /// </summary>
+        /// <param name="jobname"></param>
+        public ApiAction ImmediateRun<TJob>(TJob newEntity, string jobname) where TJob : class, ITJob
+        {
+            ApiAction apiAction = new();
+            try
+            {
+                if (JobManager.AllSchedules.Any(q => q.Name == jobname))
+                {
+                    JobManager.RemoveJob(jobname);
+                    string guid = Guid.NewGuid().ToString("N");
+                    TSK_JOB job = Biz.Db.Queryable<TSK_JOB>().Where(x => x.JobName == jobname).IncludesAllFirstLayer().First();
+                    var trigger = job.Triggers[0];
+                    TrigArgs args = JsonConvert.DeserializeObject<TrigArgs>(job.Triggers[0].Args);
+                    args.runType = RunType.ToRunNow;
+                    JobManager.AddJob<TJob>((s) => GenerateSchedule(args, s.WithName(jobname)));
+                    trigger.Status = TSK_TRIG.Statuss.Running.GetValue();
+                    trigger.Args = JsonConvert.SerializeObject(args);
+
+                    var db = Biz.Db;
+                    //淇濆瓨鍒版暟鎹簱
+                    var dbTran = db.UseTran(() =>
+                    {
+                        db.Updateable(trigger, "system_ImmediateRun_job").UpdateColumns(q => new { q.Status, q.Args, q.UPDATE_TIME, q.UPDATE_USER }).ExecuteCommand();
+                    });
+                    if (!dbTran.IsSuccess)
+                    {
+                        Logger.Scheduler.Fatal(dbTran.ErrorException, $"绔嬪嵆杩愯宸ヤ綔浠诲姟鏃朵繚瀛樼姸鎬佸埌鏁版嵁搴撳紓甯�");
+                    }
+                    apiAction.LocaleMsg = new($"绔嬪嵆杩愯宸ヤ綔浠诲姟鎴愬姛");
+                }
+            }
+            catch (System.Exception ex)
+            {
+                apiAction.CatchExceptionWithLog(ex);
+            }
+            return apiAction;
+        }
+
+        /// <summary>
         /// 鍋滄鏈嶅姟
         /// </summary>
         public void Stop()

--
Gitblit v1.9.3