using log4net; using System; using System.Collections.Generic; using System.Reflection; using UFIDA.U8.Framework.Api.ControlObject.Common; using UFIDA.U8.Framework.Api.ControlObject.Entity; using UFIDA.U8.Framework.Api.ControlObject.Json; using UFSoft.U8.Framework.BizBase; using UFSoft.U8.Framework.ContextSrv; using UFSoft.U8.U8M.BFDispatcher; using UFSoft.U8.U8M.Common; using UFSoft.U8.U8M.DOSchema.MO; using UFSoft.U8.U8M.Exceptions; namespace UFIDA.U8.Framework.Api.ControlObject.U8M.MO { /// /// 补料申请单(MO39) /// public class ReplenishApply { //日志组件 private readonly ILog _logInfo = LogManager.GetLogger("logInfo"); /// /// 获取空数据源 /// /// /// /// private ReplenishApplyDs GetEmptyDs(IContextContainer callar, string cCardNum) { try { _logInfo.Debug($"获取空数据集-----{cCardNum}"); var ds = MOMExecutor.BFReplenishApply_LoadData("LoadFirst", 0, "", callar); if (ds == null) return new ReplenishApplyDs(); #region 清除数据集里的数据 _logInfo.Debug("清空数据集-----mom_replenishapplydtl_ExtraDefine"); if (ds.Tables.Contains("mom_replenishapplydtl_ExtraDefine")) { ds.Tables["mom_replenishapplydtl_ExtraDefine"].Clear(); } _logInfo.Debug("清空数据集-----mom_replenishapply_ExtraDefine"); if (ds.Tables.Contains("mom_replenishapply_ExtraDefine")) { ds.Tables["mom_replenishapply_ExtraDefine"].Clear(); } _logInfo.Debug("清空数据集-----ReplenishApplyDTL,ReplenishApply"); ds.ReplenishApplyDTL.Clear(); ds.ReplenishApply.Clear(); #endregion return ds; } catch { throw; } } /// /// 填充DataSet数据 /// /// /// /// /// /// private ReplenishApplyDs GetVoucherDs(IContextContainer callar, string cCardNum, AutoDocNoGenerateParam docNoParam, Dictionary input) { _logInfo.Debug("获取空数据集"); var ds = GetEmptyDs(callar, cCardNum); var head = ds.ReplenishApply.NewReplenishApplyRow(); ds.ReplenishApply.AddReplenishApplyRow(head); //_logInfo.Debug($"获取单号规则-----{cCardNum}-----{docNoParam.Paramaters.ToJson()}"); //var hsTable = U8MCommonExecutor.BFVoucher_GetVoucherIDFormula(cCardNum, callar); //_logInfo.Debug($"获取单号-----{cCardNum}-----{hsTable.ToJson()}"); //var docNo = U8MCommonExecutor.BFVoucher_GetVoucherID(cCardNum, hsTable, false, callar); var docNo = ""; _logInfo.Debug("BE处理中"); var be = new UFSoft.U8.MO.BE.ReplenishApply(ds); be.DocCode = ""; _logInfo.Debug("填充表头参数"); var beHead = new BEHelper(be); beHead.SetValue(input); if (string.IsNullOrWhiteSpace(be.DocCode)) { _logInfo.Debug($"设置单号:{docNo}"); be.DocCode = docNo; } var bodys = input["body"] as List> ?? $"{input["body"]}".ToObject>>(); _logInfo.Debug("填充表体参数"); for (var i = 0; i < bodys.Count; i++) { var body = bodys[i]; var detail = be.ReplenishApplyDTLs[i]; var beBody = new BEHelper(detail, "MO39"); beBody.SetValue(body); beBody.SetPartId(); } _logInfo.Debug($"获取数据集:{be.DataObject.ToJson()}"); ds = (ReplenishApplyDs)be.DataObject; return ds; } public BaseResult Save(BaseLogin baseLogin, string cCardNum, Dictionary input, bool bVerify = false) { try { var callar = new DefaultContextContainer(); var docNoParam = new AutoDocNoGenerateParam(EnumAutoDocNoType.HandAuto, cCardNum, false) { IsCurrentDocNoByHand = false }; var ds = GetVoucherDs(callar, cCardNum, docNoParam, input); if (ds == null) { throw new Exception("未能构建数据集"); } _logInfo.Debug($"单据编码:{cCardNum},开始保存"); var id = MOMExecutor.BFReplenishApply_Save(ds, docNoParam, callar); if (id > 0) { var resDs = MOMExecutor.BFReplenishApply_LoadData("LoadById", id, "", callar); if (bVerify) { try { MOMExecutor.BFReplenishApply_ChangeStatus(id, 1, resDs.ReplenishApply[0].Ufts, callar); } catch (Exception ex) { throw new Exception($"审核时发生异常:{ex.Message}"); } } _logInfo.Debug($"单据编码:{cCardNum},保存完成,id:{id},单号:{resDs.ReplenishApply[0].DocCode}"); return new BaseResult { bSuccess = true, message = "保存成功", result = $"{id}", voucherCode = resDs.ReplenishApply[0].DocCode, voucherId = $"{id}" }; } throw new Exception("保存失败,U8未能返回制单结果"); } catch (U8MReferenceBENotExistException ex) { _logInfo.Error(ex.Message, ex); return new BaseResult { bSuccess = false, message = "保存失败", result = ex.Message }; } catch (TargetInvocationException ex) { if (ex.InnerException is U8MReferenceBENotExistException) { _logInfo.Error(ex.InnerException.Message, ex.InnerException); return new BaseResult { bSuccess = false, message = "保存失败", result = ex.InnerException.Message }; } else { _logInfo.Error(ex.Message, ex); return new BaseResult { bSuccess = false, message = "保存失败", result = ex.Message }; } } catch (Exception ex) { _logInfo.Error(ex.Message, ex); return new BaseResult { bSuccess = false, message = "保存失败", result = ex.Message }; } } public BaseResult Verify(BaseLogin baseLogin, int voucherId) { try { if (voucherId <= 0) throw new Exception("没有需要审核的数据"); var callar = new DefaultContextContainer(); var resDs = MOMExecutor.BFReplenishApply_LoadData("LoadById", voucherId, "", callar); MOMExecutor.BFReplenishApply_ChangeStatus(voucherId, 1, resDs.ReplenishApply[0].Ufts, callar); return new BaseResult { bSuccess = true, message = "审核成功", result = $"{voucherId}" }; } catch (U8MReferenceBENotExistException ex) { _logInfo.Error(ex.Message, ex); return new BaseResult { bSuccess = false, message = "审核失败", result = ex.Message }; } catch (Exception ex) { _logInfo.Error(ex.Message, ex); return new BaseResult { bSuccess = false, message = "审核失败", result = ex.Message }; } } public BaseResult Verify(BaseLogin baseLogin, string voucherCode) { try { if (string.IsNullOrWhiteSpace(voucherCode)) throw new Exception("没有需要审核的数据"); var callar = new DefaultContextContainer(); var voucherId = MOMExecutor.BFReplenishApply_GetIdByCode(voucherCode, callar); var resDs = MOMExecutor.BFReplenishApply_LoadData("LoadById", voucherId, "", callar); MOMExecutor.BFReplenishApply_ChangeStatus(voucherId, 1, resDs.ReplenishApply[0].Ufts, callar); return new BaseResult { bSuccess = true, message = "审核成功", result = $"{voucherId}" }; } catch (U8MReferenceBENotExistException ex) { _logInfo.Error(ex.Message, ex); return new BaseResult { bSuccess = false, message = "审核失败", result = ex.Message }; } catch (Exception ex) { _logInfo.Error(ex.Message, ex); return new BaseResult { bSuccess = false, message = "审核失败", result = ex.Message }; } } public BaseResult UnVerify(BaseLogin baseLogin, int voucherId) { try { if (voucherId <= 0) throw new Exception("没有需要弃审的数据"); var callar = new DefaultContextContainer(); var resDs = MOMExecutor.BFReplenishApply_LoadData("LoadById", voucherId, "", callar); MOMExecutor.BFReplenishApply_ChangeStatus(voucherId, 2, resDs.ReplenishApply[0].Ufts, callar); return new BaseResult { bSuccess = true, message = "弃审成功", result = $"{voucherId}" }; } catch (U8MReferenceBENotExistException ex) { _logInfo.Error(ex.Message, ex); return new BaseResult { bSuccess = false, message = "弃审失败", result = ex.Message }; } catch (Exception ex) { _logInfo.Error(ex.Message, ex); return new BaseResult { bSuccess = false, message = "弃审失败", result = ex.Message }; } } public BaseResult UnVerify(BaseLogin baseLogin, string voucherCode) { try { if (string.IsNullOrWhiteSpace(voucherCode)) throw new Exception("没有需要审核的数据"); var callar = new DefaultContextContainer(); var voucherId = MOMExecutor.BFReplenishApply_GetIdByCode(voucherCode, callar); var resDs = MOMExecutor.BFReplenishApply_LoadData("LoadById", voucherId, "", callar); MOMExecutor.BFReplenishApply_ChangeStatus(voucherId, 2, resDs.ReplenishApply[0].Ufts, callar); return new BaseResult { bSuccess = true, message = "弃审成功", result = $"{voucherId}" }; } catch (U8MReferenceBENotExistException ex) { _logInfo.Error(ex.Message, ex); return new BaseResult { bSuccess = false, message = "弃审失败", result = ex.Message }; } catch (Exception ex) { _logInfo.Error(ex.Message, ex); return new BaseResult { bSuccess = false, message = "弃审失败", result = ex.Message }; } } public BaseResult Delete(BaseLogin baseLogin, string cCardNum, long voucherId) { try { if (voucherId <= 0) throw new Exception("没有需要删除的数据"); var callar = new DefaultContextContainer(); var docNoParam = new AutoDocNoGenerateParam(EnumAutoDocNoType.HandAuto, cCardNum, false) { IsCurrentDocNoByHand = false }; var resDs = MOMExecutor.BFReplenishApply_LoadData("LoadById",(int)voucherId, "", callar); if(resDs == null) throw new Exception($"未查询到单据ID为{voucherId}的单据"); if(resDs.ReplenishApplyDTL!=null && resDs.ReplenishApplyDTL.Count > 0) { for(var i= resDs.ReplenishApplyDTL.Count - 1; i >= 0; i--) { resDs.ReplenishApplyDTL.Rows[i].Delete(); } } resDs.ReplenishApply[0].Delete(); MOMExecutor.BFReplenishApply_Save(resDs, docNoParam, callar); return new BaseResult { bSuccess = true, message = "删除成功", result = $"{voucherId}" }; } catch (U8MReferenceBENotExistException ex) { _logInfo.Error(ex.Message, ex); return new BaseResult { bSuccess = false, message = "删除失败", result = ex.Message }; } catch (Exception ex) { _logInfo.Error(ex.Message, ex); return new BaseResult { bSuccess = false, message = "删除失败", result = ex.Message }; } } public BaseResult Delete(BaseLogin baseLogin, string cCardNum, string voucherCode) { try { if (string.IsNullOrWhiteSpace(voucherCode)) throw new Exception("没有需要删除的数据"); var callar = new DefaultContextContainer(); var docNoParam = new AutoDocNoGenerateParam(EnumAutoDocNoType.HandAuto, cCardNum, false) { IsCurrentDocNoByHand = false }; var voucherId= MOMExecutor.BFReplenishApply_GetIdByCode(voucherCode, callar); var resDs = MOMExecutor.BFReplenishApply_LoadData("LoadById", voucherId, "", callar); if (resDs == null) throw new Exception($"未查询到单据ID为{voucherId}的单据"); if (resDs.ReplenishApplyDTL != null && resDs.ReplenishApplyDTL.Count > 0) { for (var i = resDs.ReplenishApplyDTL.Count - 1; i >= 0; i--) { resDs.ReplenishApplyDTL.Rows[i].Delete(); } } resDs.ReplenishApply[0].Delete(); MOMExecutor.BFReplenishApply_Save(resDs, docNoParam, callar); return new BaseResult { bSuccess = true, message = "删除成功", result = $"{voucherId}" }; } catch (U8MReferenceBENotExistException ex) { _logInfo.Error(ex.Message, ex); return new BaseResult { bSuccess = false, message = "删除失败", result = ex.Message }; } catch (Exception ex) { _logInfo.Error(ex.Message, ex); return new BaseResult { bSuccess = false, message = "删除失败", result = ex.Message }; } } } }