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 ICSSoft.APIUnApproveCompRpt; using ICSSoft.APIDeleteCompRpt; namespace ICSSoft.APICloseCompRpt { public class CloseCompRpt { 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 bool Close(List infos) { bool ResultFlag = false; object context; string OrgID = ""; UFIDAU9ISVMOIRcvCompleteRpt4ExternalSrvClient client = new UFIDAU9ISVMOIRcvCompleteRpt4ExternalSrvClient(); UFIDAU9ISVMOCompRptKeyDTOData[] _result = new UFIDAU9ISVMOCompRptKeyDTOData[] { }; MessageBase[] returnMsg; try { if (infos == null || infos.Count == 0) { throw new Exception("传入数据不能为空!"); } 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("传入组织编码错误!"); } UFIDAU9ISVMOCompRptKeyDTOData[] compRptKeyDTOs = new UFIDAU9ISVMOCompRptKeyDTOData[] { }; List compRptKeyList = new List(); foreach (ICSManufactureReceiveDoc Info in infos) { UFIDAU9ISVMOCompRptKeyDTOData model = new UFIDAU9ISVMOCompRptKeyDTOData(); model.m_docNo = Info.MRCVCode; model.m_iD = GetTypeID(Info.MRCVCode, OrgID); model.m_operateResult = true; model.m_operateType = true; compRptKeyList.Add(model); } compRptKeyDTOs = compRptKeyList.ToArray(); context = CreateContextObj(OrgID); //服务调用 (client.Endpoint.Binding as BasicHttpBinding).MaxReceivedMessageSize = int.MaxValue; (client.Endpoint.Binding as BasicHttpBinding).MaxBufferSize = int.MaxValue; _result = client.Do(out returnMsg, context, compRptKeyDTOs, true); bool ISSuccess = true; foreach (UFIDAU9ISVMOCompRptKeyDTOData info in _result) { if (info.m_operateResult == false) { ISSuccess = false; } } if (ISSuccess == true) { ResultFlag = true; } else { #region 存在完工报告关闭失败时先取消入库已入库的完工报告 UFIDAU9ISVMOCompRptKeyDTOData[] opencompRptKeyDTOs = new UFIDAU9ISVMOCompRptKeyDTOData[] { }; List opencompRptKeyList = new List(); foreach (UFIDAU9ISVMOCompRptKeyDTOData info in _result) { if (info.m_operateResult == true) { UFIDAU9ISVMOCompRptKeyDTOData model = new UFIDAU9ISVMOCompRptKeyDTOData(); model.m_docNo = info.m_docNo; model.m_iD = GetTypeID(info.m_docNo, OrgID); model.m_operateResult = true; model.m_operateType = false;//true:完工报告入库 false:完工报告取消入库 opencompRptKeyList.Add(model); } } opencompRptKeyDTOs = opencompRptKeyList.ToArray(); client.Do(out returnMsg, context, opencompRptKeyDTOs, true); #endregion #region 弃审完工报告 List DocIDList = new List(); foreach (ICSManufactureReceiveDoc Info in infos) { long docid = GetTypeID(Info.MRCVCode, OrgID); DocIDList.Add(docid.ToString()); } UnApproveCompRpt unapprove = new UnApproveCompRpt(); unapprove.UnApprove(DocIDList, OrgID); #endregion #region 删除完工报告 List docNoList = new List(); foreach (ICSManufactureReceiveDoc Info in infos) { docNoList.Add(Info.MRCVCode); } DeleteCompRpt del = new DeleteCompRpt(); del.Delete(docNoList, OrgID); #endregion } } catch (Exception ex) { #region 存在完工报告关闭失败时先取消入库已入库的完工报告 context = CreateContextObj(OrgID); UFIDAU9ISVMOCompRptKeyDTOData[] opencompRptKeyDTOs = new UFIDAU9ISVMOCompRptKeyDTOData[] { }; List opencompRptKeyList = new List(); foreach (UFIDAU9ISVMOCompRptKeyDTOData info in _result) { if (info.m_operateResult == true) { UFIDAU9ISVMOCompRptKeyDTOData model = new UFIDAU9ISVMOCompRptKeyDTOData(); model.m_docNo = info.m_docNo; model.m_iD = GetTypeID(info.m_docNo, OrgID); model.m_operateResult = true; model.m_operateType = false;//true:完工报告入库 false:完工报告取消入库 opencompRptKeyList.Add(model); } } opencompRptKeyDTOs = opencompRptKeyList.ToArray(); client.Do(out returnMsg, context, opencompRptKeyDTOs, true); #endregion #region 弃审完工报告 List DocIDList = new List(); foreach (ICSManufactureReceiveDoc Info in infos) { long docid = GetTypeID(Info.MRCVCode, OrgID); DocIDList.Add(docid.ToString()); } UnApproveCompRpt unapprove = new UnApproveCompRpt(); unapprove.UnApprove(DocIDList, OrgID); #endregion #region 删除完工报告 List docNoList = new List(); foreach (ICSManufactureReceiveDoc Info in infos) { docNoList.Add(Info.MRCVCode); } DeleteCompRpt del = new DeleteCompRpt(); del.Delete(docNoList, OrgID); #endregion log.Error(ex.Message); throw new Exception(ex.Message); } return ResultFlag; } /// /// 关闭完工报告(接口内部调用,如:创建退料单后调用审核单据) /// /// /// /// public bool Close(List DocNOList, string OrgCode) { bool ResultFlag = false; object context; string OrgID = ""; UFIDAU9ISVMOIRcvCompleteRpt4ExternalSrvClient client = new UFIDAU9ISVMOIRcvCompleteRpt4ExternalSrvClient(); UFIDAU9ISVMOCompRptKeyDTOData[] _result = new UFIDAU9ISVMOCompRptKeyDTOData[] { }; MessageBase[] returnMsg; try { if (DocNOList == null || DocNOList.Count == 0) { throw new Exception("传入数据不能为空!"); } string orgsql = @"select ID from Base_Organization where Code='{0}'"; orgsql = string.Format(orgsql, OrgCode); DataTable orgdt = DBHelper.Query(orgsql, connString); if (orgdt.Rows.Count != 0) { OrgID = orgdt.Rows[0]["ID"].ToString(); } else { throw new Exception("传入组织编码错误!"); } UFIDAU9ISVMOCompRptKeyDTOData[] compRptKeyDTOs = new UFIDAU9ISVMOCompRptKeyDTOData[] { }; List compRptKeyList = new List(); foreach (string DocNO in DocNOList) { UFIDAU9ISVMOCompRptKeyDTOData model = new UFIDAU9ISVMOCompRptKeyDTOData(); model.m_docNo = DocNO; model.m_iD = GetTypeID(DocNO, OrgID); model.m_operateResult = true; model.m_operateType = true; compRptKeyList.Add(model); } compRptKeyDTOs = compRptKeyList.ToArray(); context = CreateContextObj(OrgID); //服务调用 (client.Endpoint.Binding as BasicHttpBinding).MaxReceivedMessageSize = int.MaxValue; (client.Endpoint.Binding as BasicHttpBinding).MaxBufferSize = int.MaxValue; _result = client.Do(out returnMsg, context, compRptKeyDTOs, true); bool ISSuccess = true; foreach (UFIDAU9ISVMOCompRptKeyDTOData info in _result) { if (info.m_operateResult == false) { ISSuccess = false; } } if (ISSuccess == true) { ResultFlag = true; } else { #region 存在完工报告关闭失败时先取消入库已入库的完工报告 UFIDAU9ISVMOCompRptKeyDTOData[] opencompRptKeyDTOs = new UFIDAU9ISVMOCompRptKeyDTOData[] { }; List opencompRptKeyList = new List(); foreach (UFIDAU9ISVMOCompRptKeyDTOData info in _result) { if (info.m_operateResult == true) { UFIDAU9ISVMOCompRptKeyDTOData model = new UFIDAU9ISVMOCompRptKeyDTOData(); model.m_docNo = info.m_docNo; model.m_iD = GetTypeID(info.m_docNo, OrgID); model.m_operateResult = true; model.m_operateType = false;//true:完工报告入库 false:完工报告取消入库 opencompRptKeyList.Add(model); } } opencompRptKeyDTOs = opencompRptKeyList.ToArray(); client.Do(out returnMsg, context, opencompRptKeyDTOs, true); #endregion #region 弃审完工报告 List DocIDList = new List(); foreach (string DocNO in DocNOList) { long docid = GetTypeID(DocNO, OrgID); DocIDList.Add(docid.ToString()); } UnApproveCompRpt unapprove = new UnApproveCompRpt(); unapprove.UnApprove(DocIDList, OrgID); #endregion #region 删除完工报告 DeleteCompRpt del = new DeleteCompRpt(); del.Delete(DocNOList, OrgID); #endregion } } catch (Exception ex) { #region 存在完工报告关闭失败时先取消入库已入库的完工报告 context = CreateContextObj(OrgID); UFIDAU9ISVMOCompRptKeyDTOData[] opencompRptKeyDTOs = new UFIDAU9ISVMOCompRptKeyDTOData[] { }; List opencompRptKeyList = new List(); foreach (UFIDAU9ISVMOCompRptKeyDTOData info in _result) { if (info.m_operateResult == true) { UFIDAU9ISVMOCompRptKeyDTOData model = new UFIDAU9ISVMOCompRptKeyDTOData(); model.m_docNo = info.m_docNo; model.m_iD = GetTypeID(info.m_docNo, OrgID); model.m_operateResult = true; model.m_operateType = false;//true:完工报告入库 false:完工报告取消入库 opencompRptKeyList.Add(model); } } opencompRptKeyDTOs = opencompRptKeyList.ToArray(); client.Do(out returnMsg, context, opencompRptKeyDTOs, true); #endregion #region 弃审完工报告 List DocIDList = new List(); foreach (string DocNO in DocNOList) { long docid = GetTypeID(DocNO, OrgID); DocIDList.Add(docid.ToString()); } UnApproveCompRpt unapprove = new UnApproveCompRpt(); unapprove.UnApprove(DocIDList, OrgID); #endregion #region 删除完工报告 DeleteCompRpt del = new DeleteCompRpt(); del.Delete(DocNOList, OrgID); #endregion log.Error(ex.Message); throw new Exception(ex.Message); } return ResultFlag; } 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 } }