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, 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; } /// /// 新增组盘信息 /// /// /// /// public async Task 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; } } /// /// 容器出场 /// /// /// /// public async Task 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() }; /// /// AGV容器入场 /// /// 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() ?? throw new Exception("AGV容器入场未返回结果"); if (agvContent.Success) { content.OK(); } else { content.Error(agvContent.Message); } } catch (Exception ex) { content.Error(ex.Message); } return content; } /// /// AGV容器出场 /// /// 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() ?? throw new Exception("AGV容器出场未返回结果"); if (agvContent.Success) { content.OK(); } else { content.Error(agvContent.Message); } } catch (Exception ex) { content.Error(ex.Message); } return content; } } }