using System.Text.Json; using Microsoft.AspNetCore.Mvc; using SlnMesnac.Config; using SlnMesnac.Model.domain; using SlnMesnac.Model.dto; using SlnMesnac.Plc; using SlnMesnac.Repository.service; namespace SlnMesnac.Controllers; /// /// CWSS 投料校验 /// [Route("api/[controller]")] [ApiController] public class CwssIngCheckController { private ILogger _logger; private AppConfig _appConfig; private PlcPool _plcPool; private IWmsTaskOutService _wmsTaskOutService; private ICwssBinToMaterService _cwssBinToMaterService; private ICwssMaterialInfoService _cwssMaterialInfoService; private IInputRecordService _inputRecordService; public CwssIngCheckController(ILogger logger, AppConfig appConfig, IWmsTaskOutService wmsTaskOutService, PlcPool plcPool, ICwssBinToMaterService cwssBinToMaterService, ICwssMaterialInfoService cwssMaterialInfoService, IInputRecordService inputRecordService) { _logger = logger; _appConfig = appConfig; _wmsTaskOutService = wmsTaskOutService; _plcPool = plcPool; _cwssBinToMaterService = cwssBinToMaterService; _cwssMaterialInfoService = cwssMaterialInfoService; _inputRecordService = inputRecordService; } /// /// 根据物料条码获取CWSS物料信息 /// /// /// [HttpGet("GetCwssMaterialInfo/{MaterialBarcode}")] public ApiResponse GetCwssMaterialInfo(string MaterialBarcode) { var result = new ApiResponse(); try { do { WmsTaskOut wmsInfo = this.GetWmsTaskOutBySerialNum(MaterialBarcode); if (wmsInfo == null) { result.SetFailure($"物料信息获取失败,根据物料条码获取物料编号为空,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); break; } var info = _cwssMaterialInfoService.GetCwssMaterialInfoByMatCode(wmsInfo.MaterNo); if(info == null) { result.SetFailure($"物料信息获取失败,根据物料编号获取CWSS物料信息为空,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } else { string matStr = JsonSerializer.Serialize(info); result.SetSuccess(matStr); _logger.LogInformation($"获取物料信息成功:{matStr},时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } } while (false); } catch(Exception e) { result.SetException(e, "CWSS物料信息获取异常"); _logger.LogError($"CWSS物料信息获取异常:{e.Message}"); } return result; } /// /// 小料投料校验 /// /// /// [HttpPost("SmallMaterial")] public ApiResponse SmallMaterialCheck(IngredientInfo? ingredientInfo) { var result = new ApiResponse(); try { do { if (ingredientInfo == null) { result.SetSuccess($"小料校验失败,缺少必要参数,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); break; } _logger.LogInformation($"小料验证参数:{JsonSerializer.Serialize(ingredientInfo)}"); CwssBinToMater cwssInfo = _cwssBinToMaterService.GetCwssBinToMaterByBinNo(ingredientInfo.EquipId, ingredientInfo.CanNumber); if (cwssInfo == null) { result.SetFailure($"小料校验失败,根据料罐获取对应信息为空,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); break; } WmsTaskOut wmsInfo = this.GetWmsTaskOutBySerialNum(ingredientInfo.MaterialBarcode); if (wmsInfo == null) { result.SetFailure($"小料校验失败,根据物料条码获取物料信息为空,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); break; } if (cwssInfo.MaterialId.Contains(wmsInfo.MaterNo)) { result.SetSuccess($"小料校验成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } else { result.SetFailure($"小料校验失败,物料不匹配,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); break; } if (ingredientInfo.IsOpen == 1) { var openFlag = SendCwssOpenInstruct((int)cwssInfo.BinSerial); if (openFlag) { _logger.LogInformation($"小料开仓指令下发成功"); result.SetSuccess($"小料校验成功,开仓指令下发成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } else { _logger.LogInformation("小料开仓指令下发失败"); result.SetSuccess($"小料校验成功,开仓指令下发失败,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); break; } } //保存数据 ingredientInfo.EquipId = Convert.ToInt32(cwssInfo.EquipCode); ingredientInfo.CanNumber = cwssInfo.BinSerial.ToString(); SaveInputRecord(ingredientInfo, 1, ""); } while (false); } catch (Exception e) { result.SetException(e,"小料投料校验异常"); _logger.LogError($"小料投料校验异常:{e.Message}"); } return result; } /// /// 获取Cwss投料记录 /// /// /// [HttpPost("GetCwssInputRecord")] public ApiResponse GetCwssInputRecord(ApiRequestParam? apiRequestParam) { var result = new ApiResponse(); try { do { var cwssInputRecordList = _inputRecordService.GetCwssInputRecordList(); if (cwssInputRecordList != null) { if (!string.IsNullOrEmpty(apiRequestParam.MaterialBarcode)) cwssInputRecordList = cwssInputRecordList.Where(x => x.MaterialBarcode == apiRequestParam.MaterialBarcode) .ToList(); if (!string.IsNullOrEmpty(apiRequestParam.CanNumber)) cwssInputRecordList = cwssInputRecordList.Where(x => x.CanNumber == apiRequestParam.CanNumber).ToList(); if (!string.IsNullOrEmpty(apiRequestParam.beginTime)) cwssInputRecordList = cwssInputRecordList.Where(x => x.RecordTime >= Convert.ToDateTime(apiRequestParam.beginTime)).ToList(); if (!string.IsNullOrEmpty(apiRequestParam.endTime)) cwssInputRecordList = cwssInputRecordList.Where(x => x.RecordTime <= Convert.ToDateTime(apiRequestParam.endTime)).ToList(); } string matStr = JsonSerializer.Serialize(cwssInputRecordList); result.SetSuccess(matStr); _logger.LogInformation($"获取Cwss投料记录成功:{matStr},时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } while (false); } catch(Exception e) { result.SetException(e, "获取Cwss投料记录异常"); _logger.LogError($"获取Cwss投料记录异常:{e.Message}"); } return result; } /// /// 下发Cwss开仓指令 /// /// /// /// private bool SendCwssOpenInstruct(int binNo) { bool result = false; var plcInfo = _plcPool.GetPlcByKey("cwss"); int allOtherNumbersAreZero = plcInfo.readInt32ByAddress(_appConfig.cwssBinStatusAddr); if (allOtherNumbersAreZero != 1) { throw new ArgumentException($"开仓指令下发异常,存在门锁未关到位的料仓,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } result = plcInfo.writeValueByAddress(2, "D3850"); if (result) { result = plcInfo.writeValueByAddress(binNo, _appConfig.cwssOpenAddr); } if (!result) { throw new ArgumentException($"开仓指令下发失败,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } return result; } /// /// 根据物料条码获取WMS出库记录反馈信息 /// /// /// private WmsTaskOut GetWmsTaskOutBySerialNum(string MaterialBarcode) { WmsTaskOut? result = null; try { result = _wmsTaskOutService.GetWmsTaskOutBySerialNum(MaterialBarcode); _logger.LogInformation($"根据物料条码:{MaterialBarcode};获取WMS出库记录:{JsonSerializer.Serialize(result)},时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } catch (Exception ex) { _logger.LogError($"根据物料条码:{MaterialBarcode};获取WMS出库记录异常:{ex.Message}"); } return result; } /// /// 保存投料记录 /// private void SaveInputRecord(IngredientInfo info,int isFlag,string remark) { try { CwssInputRecord cwssInputRecord = new CwssInputRecord() { MaterialBarcode = info.MaterialBarcode, CanNumber = info.CanNumber, EquipId = info.EquipId, InputQuantity = info.InputQuantity, InputWeight = info.InputWeight, IsRemainder = info.IsRemainder, RemainderQuantity = info.RemainderQuantity, RemainderWeight = info.RemainderWeight, IsOpen = info.IsOpen, IsFlag = isFlag, Remark = remark, RecordTime = DateTime.Now }; bool result = _inputRecordService.SaveCwssInputRecord(cwssInputRecord); if (result) { _logger.LogInformation($"投料记录:{JsonSerializer.Serialize(cwssInputRecord)}保存成功"); } else { _logger.LogInformation($"投料记录:{JsonSerializer.Serialize(cwssInputRecord)}保存失败"); } } catch (Exception ex) { _logger.LogError($"投料记录保存异常:{ex.Message}"); } } }