pan
2025-11-12 d44b4cfa1bf7858236f928488f3777617b6c7449
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using WIDESEA_DTO.Basic;
using WIDESEA_IBasicService;
 
namespace WIDESEA_BasicService
{
    public class ESSApiService : IESSApiService
    {
        private readonly IHttpClientFactory _httpClientFactory;
        private readonly ILogger<ESSApiService> _logger;
        public ESSApiService(IHttpClientFactory httpClientFactory, ILogger<ESSApiService> logger)
        {
            _httpClientFactory = httpClientFactory;
            _logger = logger;
        }
 
        /// <summary>
        /// 容器流动通知
        /// </summary>
        public async Task<bool> MoveContainerAsync(MoveContainerRequest request)
        {
            try
            {
                var url = "conveyor/moveContainer";
 
                var result = await PostAsync<MoveContainerRequest, ApiResponse<string>>(url, request);
                if (result != null && result.Code == 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                _logger.LogInformation("容器流动失败:  " + ex.Message);
                return false;
            }
        }
 
        /// <summary>
        /// 创建任务
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> CreateTaskAsync(TaskModel request)
        {
            try
            {
                _logger.LogInformation("创建任务Request:  " + JsonConvert.SerializeObject(request));
                var url = "task/create";
 
                var result = await PostAsync<TaskModel, ApiResponse<TasksData>>(url, request);
                if (result != null && result.Code == 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                _logger.LogInformation("创建任务失败:  " + ex.Message);
                return false;
            }
        }
 
 
 
        private async Task<TResponse> PostAsync<TRequest, TResponse>(string url, TRequest request)
        {
 
            string json = JsonConvert.SerializeObject(request, new JsonSerializerSettings
            {
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
            });
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            var _client = _httpClientFactory.CreateClient("ESSUrl");
            _client.DefaultRequestHeaders.Clear();
 
            _client.DefaultRequestHeaders.Add("Accept", "application/json");
 
            using var response = await _client.PostAsync(url, content);
            string body = await response.Content.ReadAsStringAsync();
            _logger.LogInformation($"ESSAPI post :  {_client.BaseAddress}  {url}   {body}");
            if (!response.IsSuccessStatusCode)
            {
                throw new HttpRequestException(body);
            }
 
            return JsonConvert.DeserializeObject<TResponse>(body);
 
        }
    }
}