sunpengfei
2025-08-11 29767916b8e523a425970b3e21397f39803ac2b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using Azure.Core;
using Furion.DatabaseAccessor;
using Furion.FriendlyException;
using Furion.Schedule;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace FlexJobApi.Core
{
    public class ResourceHttpJob(
            IRepository<Resource> rep,
            ResourceHttpUtils resourceHttpUtils
        ) : IJob
    {
        private readonly IRepository<Resource> rep = rep;
        private readonly ResourceHttpUtils resourceHttpUtils = resourceHttpUtils;
 
        public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
        {
            var jobDetail = context.JobDetail;
 
            // 解析 HTTP 请求参数,键名称为类名
            var command = Schedular.Deserialize<SaveScheduleJobDetailCommand>(jobDetail.GetProperty<string>(nameof(ResourceHttpJob)));
 
            var resource = await rep
                .Where(it => it.Id == command.ResourceId)
                .Select(it => new Resource
                {
                    ServiceName = it.ServiceName,
                    Route = it.Route,
                    Method = it.Method
                })
                .FirstOrDefaultAsync();
            if (resource == null) throw Oops.Oh(EnumErrorCodeType.s404, "该资源");
 
            context.Result = await resourceHttpUtils.SendHttpAsync(resource, command.Body, new SystemUserResourceHttpProvider());
        }
    }
}