纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
7.7 KiB

2 years ago
  1. using ICSSoft.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using ICSSoft.Entity;
  11. using UFSoft.UBF.Util.Context;
  12. using System.ServiceModel;
  13. using www.ufida.org.EntityData;
  14. using UFSoft.UBF.Exceptions1;
  15. using UFSoft.UBF.Service;
  16. namespace ICSSoft.APICreateLotMaster
  17. {
  18. public class CreateLotMaster
  19. {
  20. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  21. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  22. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  23. public OperationResule Create(string WHCode, string ItemCode, string LotNo, string OrgID, ref string LotID)
  24. {
  25. //U9.PM.PO.POShipLine.CreatePOShipLineDefault();
  26. #region 服务调用框架结构
  27. //实例化代理类 UFIDAU9ISVItemIBatchCreateItemByDTOSRVClient
  28. UFIDAU9ISVCBOLotICommonCreateLotMasterSRVClient client = new UFIDAU9ISVCBOLotICommonCreateLotMasterSRVClient();
  29. //服务返回结果
  30. UFIDAU9BaseDTOsIDCodeNameDTOData[] returnItems;
  31. //返回异常信息,目前还没有使用此属性
  32. UFSoft.UBF.Exceptions1.MessageBase[] returnMsg;
  33. //上下文信息
  34. object context;
  35. //传入 参数
  36. //;
  37. OperationResule result = new OperationResule();
  38. string[] splitBy = new string[] { };
  39. UFIDAU9ISVCBOLotCreateLotMasterDTOData[] createLotMasterDTOList = new UFIDAU9ISVCBOLotCreateLotMasterDTOData[] { };
  40. List<UFIDAU9ISVCBOLotCreateLotMasterDTOData> list = new List<UFIDAU9ISVCBOLotCreateLotMasterDTOData>();
  41. UFIDAU9ISVCBOLotCreateLotMasterDTOData _model = new UFIDAU9ISVCBOLotCreateLotMasterDTOData();
  42. _model.m_lotCode = LotNo;//批号
  43. _model.m_item = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  44. _model.m_item.m_code = ItemCode;//料品
  45. _model.m_usedToOrg = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  46. _model.m_usedToOrg.m_iD = long.Parse(OrgID);
  47. //_model.m_lotDisabledTime = Convert.ToDateTime("9999.12.31");
  48. list.Add(_model);
  49. createLotMasterDTOList = list.ToArray();
  50. try
  51. {
  52. //给上下文信息赋值
  53. context = CreateContextObj(OrgID);
  54. //给传入参数赋值
  55. //pOList = SetrCVDtos( rCVInfoList);
  56. //服务调用
  57. (client.Endpoint.Binding as BasicHttpBinding).MaxReceivedMessageSize = int.MaxValue;
  58. (client.Endpoint.Binding as BasicHttpBinding).MaxBufferSize = int.MaxValue;
  59. returnItems = client.Do(out returnMsg, context, createLotMasterDTOList);
  60. foreach (var item in returnItems)
  61. {
  62. long id = item.m_iD;
  63. string sql = @"SELECT lotcode from Lot_LotMaster WHERE ID={0}";
  64. sql = string.Format(sql, id);
  65. DataTable _dtlotcode = DBHelper.Query(sql, connString);
  66. string lotcode = string.Empty;
  67. if (_dtlotcode != null && _dtlotcode.Rows.Count > 0)
  68. {
  69. lotcode = _dtlotcode.Rows[0][0].ToString();
  70. }
  71. result.DocNo = lotcode;
  72. result.IsSuccess = true;
  73. LotID = id.ToString();
  74. result.Message = "创建库存批号成功";
  75. }
  76. return result;
  77. }
  78. catch (Exception ex)
  79. {
  80. //异常信息捕获
  81. string Message = GetExceptionMessage(ex);
  82. result.DocNo = "";
  83. result.IsSuccess = false;
  84. result.Message = Message;
  85. return result;
  86. }
  87. #endregion
  88. }
  89. #region 给上下文信息赋值
  90. /// <summary>
  91. /// 给上下文信息赋值
  92. /// </summary>
  93. /// <returns></returns>
  94. private static ThreadContext CreateContextObj(string OrgID)
  95. {
  96. // 实例化应用上下文对象
  97. ThreadContext thContext = new ThreadContext();
  98. System.Collections.Generic.Dictionary<object, object> ns = new Dictionary<object, object>();
  99. ns.Add("OrgID", OrgID);
  100. ns.Add("UserID", ConfigurationManager.AppSettings["userID"].ToString());
  101. ns.Add("UserCode", ConfigurationManager.AppSettings["userCode"].ToString());
  102. ns.Add("Datetime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  103. ns.Add("CultureName", ConfigurationManager.AppSettings["cultureName"].ToString());
  104. ns.Add("EnterpriseID", ConfigurationManager.AppSettings["enterpriseID"].ToString());
  105. ns.Add("DefaultCultureName", ConfigurationManager.AppSettings["cultureName"].ToString());
  106. thContext.nameValueHas = ns;
  107. return thContext;
  108. }
  109. #endregion
  110. private static DataTable GetData(string DocCode, string OrgID)
  111. {
  112. string sql = @" select ID,DocNo
  113. from MO_IssueDoc
  114. where DocNo='" + DocCode + "' AND Org='" + OrgID + "'";
  115. return DBHelper.Query(sql, connString);
  116. }
  117. #region 提取异常信息
  118. /// <summary>
  119. /// 提取异常信息
  120. /// </summary>
  121. /// <param name="ex"></param>
  122. private static string GetExceptionMessage(Exception ex)
  123. {
  124. string faultMessage = "未知错误,请查看ERP日志!";
  125. System.TimeoutException timeoutEx = ex as System.TimeoutException;
  126. if (timeoutEx != null)
  127. {
  128. faultMessage = "访问服务超时,请修改配置信息!";
  129. }
  130. else
  131. {
  132. FaultException<ServiceException> faultEx = ex as FaultException<ServiceException>;
  133. if (faultEx == null)
  134. {
  135. faultMessage = ex.Message;
  136. }
  137. else
  138. {
  139. ServiceException serviceEx = faultEx.Detail;
  140. if (serviceEx != null && !string.IsNullOrEmpty(serviceEx.Message)
  141. && !serviceEx.Message.Equals("fault", StringComparison.OrdinalIgnoreCase))
  142. {
  143. // 错误信息在faultEx.Message中,请提取,
  144. // 格式为"Fault:料品不能为空,请录入\n 在....."
  145. int startIndex = serviceEx.Message.IndexOf(":");
  146. int endIndex = serviceEx.Message.IndexOf("\n");
  147. if (endIndex == -1)
  148. endIndex = serviceEx.Message.Length;
  149. if (endIndex > 0 && endIndex > startIndex + 1)
  150. {
  151. faultMessage = serviceEx.Message.Substring(startIndex + 1, endIndex - startIndex - 1);
  152. }
  153. else
  154. {
  155. faultMessage = serviceEx.Message;
  156. }
  157. }
  158. }
  159. }
  160. return faultMessage;
  161. }
  162. #endregion
  163. public class OperationResule
  164. {
  165. public string DocNo { get; set; }
  166. public bool IsSuccess { get; set; }
  167. public string Message { get; set; }
  168. }
  169. }
  170. }