xiaojiao
2 天以前 9376389dbf19d202f766a4fe47cf80dfe4db4f42
ÏîÄ¿´úÂë/WIDESEA_WCSServer/WIDESEAWCS_BasicInfoService/Dt_MaterialInfoService.cs
@@ -1,15 +1,22 @@
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;
@@ -24,13 +31,15 @@
        private readonly IDt_MaterialInfo_HtyRepository _MaterialInfo_HtyRepository;
        private readonly IMapper _mapper;
        private readonly IDt_StationManagerRepository _stationManagerRepository;
        public Dt_MaterialInfoService(IDt_MaterialInfoRepository BaseDal, IDt_ContainerInfoRepository dt_ContainerInfoRepository, IUnitOfWorkManage unitOfWorkManage, IDt_MaterialInfo_HtyRepository materialInfo_HtyRepository,IMapper mapper, IDt_StationManagerRepository stationManagerRepository) : base(BaseDal)
        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>
        /// æ–°å¢žç»„盘信息
@@ -52,7 +61,24 @@
                x => x.StationLocation == containerbindingDTO.Position 
                && x.StationStatus == ((int)StationEnum.Enable).ToString());
                if (dt_StationManager == null) return content.Error("当前站台未配置 æˆ–已停用");
                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;
@@ -65,7 +91,7 @@
                //int a = 1;
                //int b = 0;
                //int result = a / b;  // è¿™é‡Œä¼šæŠ›å‡º DivideByZeroException
                Dt_ContainerInfo containerInfo = await _ContainerInfoRepository.QueryFirstAsync(x => x.ContainerCode == containerbindingDTO.VehicleNumber);
                if (containerInfo == null)
                {
                    containerInfo = new Dt_ContainerInfo();
@@ -74,6 +100,9 @@
                    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:"物料绑定成功");
@@ -86,23 +115,91 @@
            }
        }
        /// <summary>
        /// å®¹å™¨å‡ºåœº
        /// </summary>
        /// <param name="PalletCode"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<WebResponseContent> DeleteGroupPlateAsync(string PalletCode)
        {
            WebResponseContent content = new WebResponseContent();
            // å…ˆæŸ¥è¯¢ç›¸åº”的组盘绑定信息 å¦‚果没有数据则返回
            Dt_MaterialInfo dt_MaterialInfo = await BaseDal.QueryFirstAsync(x => x.ContainerCode == PalletCode);
            if (dt_MaterialInfo == null) return content.Error("查询不到相关数据 æˆ–者已经解绑!");
            // è¿™é‡Œé¢è¦å†™ç‰©æ–™ç»„盘信息的状态改变例如已解绑
            dt_MaterialInfo.IsBind = false;
            ContainerOutDTO containerOutDTO = new ContainerOutDTO();
            containerOutDTO.containerCode = PalletCode;
            containerOutDTO.requestId = Guid.NewGuid().ToString().Replace("-", "");
            Dt_MaterialInfo_Hty dt_MaterialInfo_Hty = _mapper.Map<Dt_MaterialInfo_Hty>(dt_MaterialInfo);
            WebResponseContent contentIn = AgvSendContainerOutDTO(containerOutDTO, APIEnum.AgvcontainerOut);
            if (!content.Status)
                throw new Exception(content.Message);
            await _MaterialInfo_HtyRepository.AddDataAsync(dt_MaterialInfo_Hty);
            await BaseDal.DeleteDataAsync(dt_MaterialInfo);
            // åˆ é™¤å½“前组盘数据添加进入历史记录
            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;
        }
    }
}