using AutoMapper; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; using WIDESEAWCS_Common.StationEnum; using WIDESEAWCS_Core; using WIDESEAWCS_Core.BaseRepository; using WIDESEAWCS_Core.BaseServices; 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; public Dt_MaterialInfoService(IDt_MaterialInfoRepository BaseDal, IDt_ContainerInfoRepository dt_ContainerInfoRepository, IUnitOfWorkManage unitOfWorkManage, IDt_MaterialInfo_HtyRepository materialInfo_HtyRepository,IMapper mapper, IDt_StationManagerRepository stationManagerRepository) : base(BaseDal) { _ContainerInfoRepository = dt_ContainerInfoRepository; _unitOfWorkManage = unitOfWorkManage; _MaterialInfo_HtyRepository = materialInfo_HtyRepository; _mapper = mapper; _stationManagerRepository = stationManagerRepository; } /// /// 新增组盘信息 /// /// /// /// 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()); if (dt_StationManager == null) return content.Error("当前站台未配置 或已停用"); 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 Dt_ContainerInfo containerInfo = await _ContainerInfoRepository.QueryFirstAsync(x => x.ContainerCode == containerbindingDTO.VehicleNumber); 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); } 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(); // 先查询相应的组盘绑定信息 如果没有数据则返回 Dt_MaterialInfo dt_MaterialInfo = await BaseDal.QueryFirstAsync(x => x.ContainerCode == PalletCode); if (dt_MaterialInfo == null) return content.Error("查询不到相关数据 或者已经解绑!"); // 这里面要写物料组盘信息的状态改变例如已解绑 dt_MaterialInfo.IsBind = false; Dt_MaterialInfo_Hty dt_MaterialInfo_Hty = _mapper.Map(dt_MaterialInfo); await _MaterialInfo_HtyRepository.AddDataAsync(dt_MaterialInfo_Hty); await BaseDal.DeleteDataAsync(dt_MaterialInfo); // 删除当前组盘数据添加进入历史记录 return content.OK(message: "物料解绑成功"); } } }