xiaojiao
昨天 9376389dbf19d202f766a4fe47cf80dfe4db4f42
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.Log;
using WIDESEAWCS_BasicInfoRepository;
using WIDESEAWCS_Common.APIEnum;
using WIDESEAWCS_Common.StationEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.Agv;
using WIDESEAWCS_DTO.PDA;
using WIDESEAWCS_IBasicInfoRepository;
using WIDESEAWCS_IBasicInfoService;
using WIDESEAWCS_Model.Models;
 
namespace WIDESEAWCS_BasicInfoService
{
    public class Dt_MaterialInfoService : ServiceBase<Dt_MaterialInfo, IDt_MaterialInfoRepository>, IDt_MaterialInfoService
    {
        private readonly IDt_ContainerInfoRepository _ContainerInfoRepository;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IDt_MaterialInfo_HtyRepository _MaterialInfo_HtyRepository;
        private readonly IMapper _mapper;
        private readonly IDt_StationManagerRepository _stationManagerRepository;
        private readonly ApiInfoRepository _apiInfoRepository;
        public Dt_MaterialInfoService(IDt_MaterialInfoRepository BaseDal, IDt_ContainerInfoRepository dt_ContainerInfoRepository, IUnitOfWorkManage unitOfWorkManage, IDt_MaterialInfo_HtyRepository materialInfo_HtyRepository,IMapper mapper, IDt_StationManagerRepository stationManagerRepository, ApiInfoRepository apiInfoRepository) : base(BaseDal)
        {
            _ContainerInfoRepository = dt_ContainerInfoRepository;
            _unitOfWorkManage = unitOfWorkManage;
            _MaterialInfo_HtyRepository = materialInfo_HtyRepository;
            _mapper = mapper;
            _stationManagerRepository = stationManagerRepository;
            _apiInfoRepository = apiInfoRepository;
        }
        /// <summary>
        /// 新增组盘信息
        /// </summary>
        /// <param name="containerbindingDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<WebResponseContent>  ContainerbindingAsync([FromBody] ContainerbindingDTO containerbindingDTO)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                _unitOfWorkManage.BeginTran();
 
                Dt_MaterialInfo dt_MaterialInfo = await BaseDal.QueryFirstAsync(x => x.ContainerCode == containerbindingDTO.VehicleNumber);
                if (dt_MaterialInfo != null) return content.Error("当前容器已绑定 请勿重复提交");
 
                Dt_StationManager dt_StationManager = _stationManagerRepository.QueryFirst(
                x => x.StationLocation == containerbindingDTO.Position 
                && x.StationStatus == ((int)StationEnum.Enable).ToString());
 
                Dt_ContainerInfo containerInfo = await _ContainerInfoRepository.QueryFirstAsync(x => x.ContainerCode == containerbindingDTO.VehicleNumber);
 
                if (dt_StationManager == null) return content.Error("当前站台有任务 或已停用");
 
                ContainerInDTO containerInDTO = new ContainerInDTO(); // 容器入场DTO
                containerInDTO.requestId = Guid.NewGuid().ToString().Replace("-", "");
                containerInDTO.containerCode = containerbindingDTO.VehicleNumber;
                containerInDTO.position = containerbindingDTO.Position;
                if (containerInfo == null)
                {
                    containerInDTO.isNew = true;
                }
                WebResponseContent contentIn = AgvSendContainerIn(containerInDTO, APIEnum.AgvcontainerIn);
                if (!content.Status)
                    throw new Exception(content.Message);
 
 
 
 
                dt_MaterialInfo = new Dt_MaterialInfo();
                dt_MaterialInfo.MaterialName = containerbindingDTO.materSn;
                dt_MaterialInfo.ContainerCode = containerbindingDTO.VehicleNumber;
                dt_MaterialInfo.Position = containerbindingDTO.Position;
                dt_MaterialInfo.Carmodel = containerbindingDTO.Carmodel;
                dt_MaterialInfo.Region = dt_StationManager.StationArea;
 
                // 2. 执行一定会报错的代码:除以零
                //int a = 1;
                //int b = 0;
                //int result = a / b;  // 这里会抛出 DivideByZeroException
                
                if (containerInfo == null)
                {
                    containerInfo = new Dt_ContainerInfo();
                    containerInfo.RequestId = Guid.NewGuid().ToString().Replace("-", "");
                    containerInfo.ContainerCode = containerbindingDTO.VehicleNumber;
                    dt_MaterialInfo.IsNew = true;
                    await _ContainerInfoRepository.AddDataAsync(containerInfo);
                }
                dt_StationManager.StationStatus = ((int)StationEnum.Thereisatask).ToString();
 
                await _stationManagerRepository.UpdateDataAsync(dt_StationManager);
                await BaseDal.AddDataAsync(dt_MaterialInfo);
                _unitOfWorkManage.CommitTran();
                return content.OK(message:"物料绑定成功");
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content.Error(ex.Message);
                throw;
            }
 
        }
        /// <summary>
        /// 容器出场
        /// </summary>
        /// <param name="PalletCode"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<WebResponseContent> DeleteGroupPlateAsync(string PalletCode)
        {
            WebResponseContent content = new WebResponseContent();
 
            ContainerOutDTO containerOutDTO = new ContainerOutDTO();
            containerOutDTO.containerCode = PalletCode;
            containerOutDTO.requestId = Guid.NewGuid().ToString().Replace("-", "");
 
            WebResponseContent contentIn = AgvSendContainerOutDTO(containerOutDTO, APIEnum.AgvcontainerOut);
            if (!content.Status)
                throw new Exception(content.Message);
 
            return content.OK(message: "物料解绑成功");
        }
        // 创建一个使用小驼峰命名法的序列化设置
        JsonSerializerSettings settings = new JsonSerializerSettings
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver()
        };
        /// <summary>
        /// AGV容器入场
        /// </summary>
        /// <returns></returns>
        public WebResponseContent AgvSendContainerIn(ContainerInDTO containerInDTO, APIEnum Container = APIEnum.AgvcontainerIn)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                string? apiAddress = _apiInfoRepository.QueryFirst(x => x.ApiCode == Container.ToString())?.ApiAddress;
                if (string.IsNullOrEmpty(apiAddress)) throw new Exception($"未找到容器入场接口,请检查接口配置");
                string request = JsonConvert.SerializeObject(containerInDTO, settings);
                string response = HttpHelper.Post(apiAddress, request);
                WriteLog.Write_Log("AGV容器入场", "AGV容器入场下发接口", "请求信息", $"请求:{request},回传:{response}");
                AgvResponseContent agvContent = response.DeserializeObject<AgvResponseContent>() ?? throw new Exception("AGV容器入场未返回结果");
                if (agvContent.Success)
                {
                    content.OK();
                }
                else
                {
                    content.Error(agvContent.Message);
                }
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// AGV容器出场
        /// </summary>
        /// <returns></returns>
        public WebResponseContent AgvSendContainerOutDTO(ContainerOutDTO containerOutDTO, APIEnum Container = APIEnum.AgvcontainerOut)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                string? apiAddress = _apiInfoRepository.QueryFirst(x => x.ApiCode == Container.ToString())?.ApiAddress;
                if (string.IsNullOrEmpty(apiAddress)) throw new Exception($"未找到容器入场接口,请检查接口配置");
                string request = JsonConvert.SerializeObject(containerOutDTO, settings);
                string response = HttpHelper.Post(apiAddress, request);
                WriteLog.Write_Log("AGV容器出场", "AGV容器出场下发接口", "请求信息", $"请求:{request},回传:{response}");
                AgvResponseContent agvContent = response.DeserializeObject<AgvResponseContent>() ?? throw new Exception("AGV容器出场未返回结果");
                if (agvContent.Success)
                {
                    content.OK();
                }
                else
                {
                    content.Error(agvContent.Message);
                }
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
    }
}