using ICSSoft.Common; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; using ICSSoft.Entity; using UFSoft.UBF.Util.Context; using System.ServiceModel; using www.ufida.org.EntityData; using UFSoft.UBF.Exceptions1; using UFSoft.UBF.Service; using ICSSoft.APIApproveCompRpt; using ICSSoft.APIDeleteCompRpt; using ICSSoft.APICloseCompRpt; using ICSSoft.DataProject; namespace ICSSoft.APICreateCompRpt { public class CreateCompRpt { private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"]; private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"]; /// /// 创建完工报告(外部通过API调用) /// /// /// public string Create(List infos) { string msg = ""; DataSet ds = null; UFIDAU9ISVMOICreateCompRptSrvClient client = new UFIDAU9ISVMOICreateCompRptSrvClient(); //服务返回结果 UFIDAU9ISVMOCompRptKeyDTOData[] returnItem; //返回异常信息,目前还没有使用此属性 UFSoft.UBF.Exceptions.MessageBase[] returnMsg; //上下文信息 object context; //传入 参数 UFIDAU9ISVMOCompRptDTOData[] compRptDTOs; try { if (infos == null || infos.Count == 0) { throw new Exception("传入数据不能为空!"); } string OrgID = ""; string orgsql = @"select ID from Base_Organization where Code='{0}'"; orgsql = string.Format(orgsql, infos[0].WorkPoint); DataTable orgdt = DBHelper.Query(orgsql, connString); if (orgdt.Rows.Count != 0) { OrgID = orgdt.Rows[0]["ID"].ToString(); } else { throw new Exception("传入的组织编码有误!"); } //给上下文信息赋值 context = CreateContextObj(OrgID); //给传入参数赋值 compRptDTOs = GetDataNew(infos, OrgID); //服务调用 (client.Endpoint.Binding as BasicHttpBinding).MaxReceivedMessageSize = int.MaxValue; (client.Endpoint.Binding as BasicHttpBinding).MaxBufferSize = int.MaxValue; returnItem = client.Do(out returnMsg, context, compRptDTOs); List DocNoList = new List(); foreach (UFIDAU9ISVMOCompRptKeyDTOData data in returnItem) { if (data.m_operateResult == false) { throw new Exception(data.m_errorMsg); } DocNoList.Add(data.m_docNo); } bool ApproveResult = false; ApproveCompRpt custApproveComRpt = new ApproveCompRpt(); ApproveResult = custApproveComRpt.Approve(DocNoList, OrgID); if (ApproveResult == false) { bool DeleteResult = false; DeleteCompRpt delete = new DeleteCompRpt(); DeleteResult = delete.Delete(DocNoList, OrgID); } else { bool CloseResult = false; CloseCompRpt CloseSV = new CloseCompRpt(); CloseResult = CloseSV.Close(DocNoList, OrgID); if (CloseResult == true) { string strDocNOList = ""; foreach (string DocNo in DocNoList) { if (strDocNOList == "") { strDocNOList = "'" + DocNo + "'"; } else { strDocNOList += ",'" + DocNo + "'"; } } string sql = @"select DISTINCT a.ID as ID,a.ID as IDs,a.DocNo as MRCVCode,c.Code AS cDepCode,d.Name AS cDepName ,e.Code AS cWhCode,f.Name AS cWhName ,g.DocNo as MOCode ,a.CreatedBy as CreateUser ,a.CreatedOn as CreateDateTime ,a.ApproveBy as Checker ,a.ApproveOn as CheckDateTime from MO_CompleteRpt a left join CBO_Department c on c.ID=a.HandleDept left join CBO_Department_Trl d on d.ID=c.ID left join CBO_Wh e on e.ID=a.RcvWh left join CBO_Wh_Trl f on f.ID=e.ID left join MO_MO g on g.ID=a.MO WHERE A.DocNo in({0}) AND a.Org='{1}'"; sql = string.Format(sql, strDocNOList, OrgID); DataTable mdt = DBHelper.Query(sql, connString); sql = @"select DISTINCT a.ID as IDs, b.ID as DetailID,b.LineNum as Sequence,c.Code as InvCode , b.RcvQtyByWhUOM as Quantity ,0 as Amount,a.MO as MODetailID, isnull(d.Code,'') ProjectCode,isnull(e.Code,'') cBatch,'' version ,'' brand, '' as cFree1, '' as cFree2, '' as cFree3, '' as cFree4, '' as cFree5, '' as cFree6, '' as cFree7, '' as cFree8, '' as cFree9, '' as cFree10 from MO_CompleteRpt a left join MO_CompleteRptRcvLine b on b.CompleteRpt=a.ID left join CBO_ItemMaster c on c.ID=a.Item left join CBO_Project d on d.ID=a.Project left join Lot_LotMaster e on e.ID=a.ProductLotMaster WHERE A.DocNo in({0}) AND a.Org='{1}'"; sql = string.Format(sql, strDocNOList, OrgID); DataTable cdt = DBHelper.Query(sql, connString); DataTable tempdt1 = mdt.Copy(); tempdt1.TableName = "tempdt1"; ds.Tables.Add(tempdt1); DataTable tempdt2 = cdt.Copy(); tempdt2.TableName = "tempdt2"; ds.Tables.Add(tempdt2); } else { throw new Exception("完工报告关闭失败,创建的完工报告已删除!"); } } } catch (Exception ex) { log.Error(GetExceptionMessage(ex)); throw new Exception(GetExceptionMessage(ex)); } msg = JSON.DataSetToJson(ds, "details", "IDs"); return msg; } private static long GetTypeID(string docTypeCode, string OrgID) { string sql = @"select ID from MO_CompleteRpt where DocNo='" + docTypeCode + "' AND Org='" + OrgID + "' "; DataTable dt = DBHelper.Query(sql, connString); if (dt.Rows.Count > 0) { return long.Parse(dt.Rows[0][0].ToString()); } else { return 0; } } #region 给上下文信息赋值 /// /// 给上下文信息赋值 /// /// private static ThreadContext CreateContextObj(string OrgID) { // 实例化应用上下文对象 ThreadContext thContext = new ThreadContext(); System.Collections.Generic.Dictionary ns = new Dictionary(); ns.Add("OrgID", OrgID); ns.Add("UserID", ConfigurationManager.AppSettings["userID"].ToString()); ns.Add("UserCode", ConfigurationManager.AppSettings["userCode"].ToString()); ns.Add("Datetime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); ns.Add("CultureName", ConfigurationManager.AppSettings["cultureName"].ToString()); ns.Add("EnterpriseID", ConfigurationManager.AppSettings["enterpriseID"].ToString()); ns.Add("DefaultCultureName", ConfigurationManager.AppSettings["cultureName"].ToString()); thContext.nameValueHas = ns; return thContext; } #endregion private static UFIDAU9ISVMOCompRptDTOData[] GetDataNew(List CompRptList, string OrgID) { List datas = new List(); foreach (ICSManufactureReceiveDoc item in CompRptList) { foreach (ICSManufactureReceiveDocs line in item.details) { UFIDAU9ISVMOCompRptDTOData data = new UFIDAU9ISVMOCompRptDTOData(); if (line.Quantity != 0) { data.m_completeQty = line.Quantity; } data.m_org = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData(); data.m_org.m_iD = long.Parse(OrgID); data.m_completeDocType = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData(); data.m_completeDocType.m_code = "1"; //data.m_completeDocType.m_iD = GetTypeID(docTypeCode); data.m_mO = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData(); data.m_mO.m_code = line.InvCode; data.m_handlePerson = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData(); data.m_handlePerson.m_code = item.User; data.m_handleDept = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData(); data.m_handleDept.m_code = item.DepCode; data.m_rcvWh = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData(); data.m_rcvWh.m_iD = GetWHID(item.WHCode, OrgID); data.m_completeDate = DateTime.Now; data.m_actualRcvTime = DateTime.Now; datas.Add(data); } } return datas.ToArray(); } public static long GetWHID(string WHCode, string OrgID) { long WHID = 0; string sql = @" SELECT ID FROM CBO_Wh WHERE Code='" + WHCode + "' AND Org='" + OrgID + "' "; sql = string.Format(sql); DataTable _dtWH = DBHelper.Query(sql, connString); string _WHCode = string.Empty; if (_dtWH != null && _dtWH.Rows.Count > 0) { _WHCode = _dtWH.Rows[0][0].ToString(); WHID = long.Parse(_WHCode); } return WHID; } #region 提取异常信息 /// /// 提取异常信息 /// /// private static string GetExceptionMessage(Exception ex) { string faultMessage = "未知错误,请查看ERP日志!"; System.TimeoutException timeoutEx = ex as System.TimeoutException; if (timeoutEx != null) { faultMessage = "访问服务超时,请修改配置信息!"; } else { FaultException faultEx = ex as FaultException; if (faultEx == null) { faultMessage = ex.Message; } else { ServiceException serviceEx = faultEx.Detail; if (serviceEx != null && !string.IsNullOrEmpty(serviceEx.Message) && !serviceEx.Message.Equals("fault", StringComparison.OrdinalIgnoreCase)) { // 错误信息在faultEx.Message中,请提取, // 格式为"Fault:料品不能为空,请录入\n 在....." int startIndex = serviceEx.Message.IndexOf(":"); int endIndex = serviceEx.Message.IndexOf("\n"); if (endIndex == -1) endIndex = serviceEx.Message.Length; if (endIndex > 0 && endIndex > startIndex + 1) { faultMessage = serviceEx.Message.Substring(startIndex + 1, endIndex - startIndex - 1); } else { faultMessage = serviceEx.Message; } } } } return faultMessage; } #endregion } }