1
heshaofeng
2025-12-24 6e756de136755787168a4b62a70e77b98c69a54f
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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_Core.Helper;
using WIDESEA_Core.LogHelper;
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";
                _logger.LogInformation("ESSApiService MoveContainerAsync Request:  " + JsonConvert.SerializeObject(request));
                var result = await PostAsync<MoveContainerRequest, ApiResponse<string>>(url, request);
                if (result != null && result.Code == 0)
                {
                    //{"code":0,"msg":"success","data":{"107":"TASK_ALREADY_EXIST"}}
                    if (result.Data.Contains("TASK_ALREADY_EXIST"))
                    {
                        return false;
                    }
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                _logger.LogInformation("ESSApiService 容器流动失败:  " + ex.Message);
                return false;
            }
        }
 
        /// <summary>
        /// 创建任务
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> CreateTaskAsync(TaskModel request)
        {
            try
            {
                _logger.LogInformation("ESSApiService 创建任务Request:  " + JsonConvert.SerializeObject(request));
                var url = "task/create";
 
                var result = await PostAsync<TaskModel, ApiResponse<TasksData>>(url, request);
                if (result != null && result.Code == 0)
                {
                    _logger.LogInformation(result.Serialize());
 
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                _logger.LogInformation("ESSApiService 创建任务失败:  " + ex.Message);
                return false;
            }
        }
 
 
 
        private async Task<TResponse> PostAsync<TRequest, TResponse>(string url, TRequest request)
        {
            TResponse response1 = (TResponse)Activator.CreateInstance(typeof(TResponse));
            try
            {
                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);
                }
 
                response1 = JsonConvert.DeserializeObject<TResponse>(body);
 
                return response1;
            }
            catch (Exception ex)
            {
                Logger.Add(request == null ? "" : JsonConvert.SerializeObject(request), response1 == null ? ex.ToString() : JsonConvert.SerializeObject(response1));
                throw new Exception(ex.Message);
            }
            finally
            {
                Logger.Add(request == null ? "" : JsonConvert.SerializeObject(request), response1 == null ? "" : JsonConvert.SerializeObject(response1));
            }
 
        }
    }
}