Exemplo n.º 1
0
        /// <summary>
        /// 设置,如果存在则更新,否则插入
        /// </summary>
        /// <param name="jobTask">作业任务</param>
        /// <param name="connectionId">连接ID</param>
        /// <returns>影响行数</returns>
        public int Set(JobTaskInfo jobTask, string connectionId = null)
        {
            if (list == null)
            {
                list = new List <JobTaskInfo>();
                jobTask.InitCreateTime();
                jobTask.InitModifyTime();
                list.Add(jobTask);
                jsonFile.WriteJsonFile(list);
                return(1);
            }
            var exists = list.Where(p => p.JtName == jobTask.JtName && p.JtGroup == jobTask.JtGroup).FirstOrDefault();

            if (exists == null)
            {
                list.Add(jobTask);
            }
            else
            {
                jobTask.SyncAssign <JobTaskInfo, JobTaskInfo>(exists, "Id", "Name", "Group");
            }
            jsonFile.WriteJsonFile(list);

            return(1);
        }
 /// <summary>
 /// 模型到数据库填充其他属性
 /// </summary>
 /// <param name="jobTask">作业任务</param>
 private void ModelToDbFullOtherProps(JobTaskInfo jobTask)
 {
     if (jobTask == null)
     {
         return;
     }
     jobTask.JobParamsJsonString     = jobTask.JobParams.ToJsonString();
     jobTask.TriggerParamsJsonString = jobTask.TriggerParams.ToJsonString();
 }
 /// <summary>
 /// 数据库到模型填充其他属性
 /// </summary>
 /// <param name="jobTask">作业任务</param>
 private void DbToModelFullOtherProps(JobTaskInfo jobTask)
 {
     if (jobTask == null)
     {
         return;
     }
     jobTask.JobParams     = jobTask.JobParamsJsonString.ToJsonObject <IDictionary <string, string> >();
     jobTask.TriggerParams = jobTask.TriggerParamsJsonString.ToJsonObject <IDictionary <string, string> >();
 }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="connectionId">连接ID</param>
        /// <returns>作业任务</returns>
        public JobTaskInfo Find(int id, string connectionId = null)
        {
            var         sql    = $"{BasicSelectSql()} WHERE {PfxEscapeChar}{GetFieldByProp("Id")}{SufxEscapeChar}=@Id";
            JobTaskInfo result = null;

            DbConnectionManager.BrainpowerExecute(connectionId, this, (connId, dbConn) =>
            {
                result = ExecRecordSqlLog <JobTaskInfo>(sql, () =>
                {
                    return(dbConn.QueryFirstOrDefault <JobTaskInfo>(sql, new
                    {
                        Id = id,
                    }, GetDbTransaction(connId, AccessMode.SLAVE)));
                }, tag: "Find");
            }, AccessMode.SLAVE);
            DbToModelFullOtherProps(result);

            return(result);
        }
        /// <summary>
        /// 设置,如果存在则更新,否则插入
        /// </summary>
        /// <param name="jobTask">作业任务</param>
        /// <param name="connectionId">连接ID</param>
        /// <returns>影响行数</returns>
        public int Set(JobTaskInfo jobTask, string connectionId = null)
        {
            int result = 0;

            ModelToDbFullOtherProps(jobTask);
            DbConnectionManager.BrainpowerExecute(connectionId, this, (connId, dbConn) =>
            {
                if (Exists(jobTask.JtName, jobTask.JtGroup, connId))
                {
                    result = UpdateCron(jobTask.TriggerCron, jobTask.JtName, jobTask.JtGroup, connId);
                }
                else
                {
                    jobTask.CreateTime = jobTask.ModifyTime = DateTime.Now;
                    result             = Insert(jobTask, connectionId: connId);
                }
            });

            return(result);
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="group">分组</param>
        /// <param name="connectionId">连接ID</param>
        /// <returns>作业任务</returns>
        public JobTaskInfo Find(string name, string group = null, string connectionId = null)
        {
            var         sql    = $"{BasicSelectSql()} WHERE {GetNameAndGroupSql()}";
            JobTaskInfo result = null;

            DbConnectionManager.BrainpowerExecute(connectionId, this, (connId, dbConn) =>
            {
                result = ExecRecordSqlLog <JobTaskInfo>(sql, () =>
                {
                    return(dbConn.QueryFirstOrDefault <JobTaskInfo>(sql, new
                    {
                        Name = name,
                        Group = group
                    }, GetDbTransaction(connId, AccessMode.SLAVE)));
                }, tag: "Find");
            }, AccessMode.SLAVE);
            DbToModelFullOtherProps(result);

            return(result);
        }