纽威
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.

283 lines
12 KiB

  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.APIApproveTransferIN
  17. {
  18. public class ApproveTransferIN
  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. /// <summary>
  24. /// 审核一步式调入单(外部通过API调用)
  25. /// </summary>
  26. /// <param name="infos"></param>
  27. /// <returns></returns>
  28. public bool Approve(List<ICSTransferDoc> infos)
  29. {
  30. bool ResultFlag = false;
  31. UFIDAU9ISVTransferInISVITransferInBatchApproveSRVClient client = new UFIDAU9ISVTransferInISVITransferInBatchApproveSRVClient();
  32. MessageBase[] returnMsg;
  33. //上下文信息
  34. object context;
  35. //传入 参数
  36. try
  37. {
  38. if (infos == null || infos.Count == 0)
  39. {
  40. throw new Exception("传入数据不能为空!");
  41. }
  42. string OrgID = "";
  43. string orgsql = @"select ID from Base_Organization
  44. where Code='{0}'";
  45. orgsql = string.Format(orgsql, infos[0].WorkPoint);
  46. DataTable orgdt = DBHelper.Query(orgsql, connString);
  47. if (orgdt.Rows.Count != 0)
  48. {
  49. OrgID = orgdt.Rows[0]["ID"].ToString();
  50. }
  51. else
  52. {
  53. throw new Exception("传入组织编码错误!");
  54. //给上下文信息赋值
  55. context = CreateContextObj(OrgID);
  56. //给传入参数赋值
  57. //服务调用
  58. (client.Endpoint.Binding as BasicHttpBinding).MaxReceivedMessageSize = int.MaxValue;
  59. (client.Endpoint.Binding as BasicHttpBinding).MaxBufferSize = int.MaxValue;
  60. UFIDAU9CBOPubControllerCommonArchiveDataDTOData[] queryArr = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData[] { };
  61. List<UFIDAU9CBOPubControllerCommonArchiveDataDTOData> queryList = new List<UFIDAU9CBOPubControllerCommonArchiveDataDTOData>();
  62. UFIDAU9CBOPubControllerCommonArchiveDataDTOData q = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  63. foreach (ICSTransferDoc Info in infos)
  64. {
  65. string DocNo = "";
  66. string sql = @"SELECT DocNO FROM InvDoc_TransferIn a WHERE a.ID='" + Info.ID + "' AND Org='" + OrgID + "'";
  67. DataTable dt = DBHelper.Query(sql, connString);
  68. if (dt != null && dt.Rows.Count > 0)
  69. {
  70. string a = dt.Rows[0][0].ToString();
  71. if (string.IsNullOrWhiteSpace(a))
  72. {
  73. throw new Exception("传入单据ID有误,请确认!");
  74. }
  75. else
  76. {
  77. DocNo = a;
  78. }
  79. }
  80. else
  81. {
  82. throw new Exception("传入单据ID有误,请确认!");
  83. }
  84. q.m_iD = long.Parse(Info.ID);
  85. q.m_code = DocNo;
  86. queryList.Add(q);
  87. }
  88. queryArr = queryList.ToArray();
  89. returnMsg = client.Do(context, queryArr, infos[0].User, DateTime.Now);
  90. if (returnMsg != null && returnMsg.Length > 0)
  91. {
  92. foreach (var item in returnMsg)
  93. {
  94. throw new Exception(item.localMessage);
  95. }
  96. }
  97. else
  98. {
  99. ResultFlag = true;
  100. }
  101. }
  102. }
  103. catch (Exception ex)
  104. {
  105. log.Error(GetExceptionMessage(ex));
  106. throw new Exception(GetExceptionMessage(ex));
  107. }
  108. return ResultFlag;
  109. }
  110. /// <summary>
  111. /// 审核一步式调入单(接口内部调用,如:创建退料单后调用审核单据)
  112. /// </summary>
  113. /// <param name="DocNOList"></param>
  114. /// <param name="OrgCode"></param>
  115. /// <returns></returns>
  116. public bool Approve(List<string> DocIDList, string OrgCode,string UserCode)
  117. {
  118. bool ResultFlag = false;
  119. UFIDAU9ISVTransferInISVITransferInBatchApproveSRVClient client = new UFIDAU9ISVTransferInISVITransferInBatchApproveSRVClient();
  120. MessageBase[] returnMsg;
  121. //上下文信息
  122. object context;
  123. //传入 参数
  124. try
  125. {
  126. if (DocIDList == null || DocIDList.Count == 0)
  127. {
  128. throw new Exception("传入数据不能为空!");
  129. }
  130. string OrgID = "";
  131. string orgsql = @"select ID from Base_Organization
  132. where Code='{0}'";
  133. orgsql = string.Format(orgsql, OrgCode);
  134. DataTable orgdt = DBHelper.Query(orgsql, connString);
  135. if (orgdt.Rows.Count != 0)
  136. {
  137. OrgID = orgdt.Rows[0]["ID"].ToString();
  138. }
  139. else
  140. {
  141. throw new Exception("传入组织编码错误!");
  142. //给上下文信息赋值
  143. context = CreateContextObj(OrgID);
  144. //给传入参数赋值
  145. //服务调用
  146. (client.Endpoint.Binding as BasicHttpBinding).MaxReceivedMessageSize = int.MaxValue;
  147. (client.Endpoint.Binding as BasicHttpBinding).MaxBufferSize = int.MaxValue;
  148. UFIDAU9CBOPubControllerCommonArchiveDataDTOData[] queryArr = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData[] { };
  149. List<UFIDAU9CBOPubControllerCommonArchiveDataDTOData> queryList = new List<UFIDAU9CBOPubControllerCommonArchiveDataDTOData>();
  150. UFIDAU9CBOPubControllerCommonArchiveDataDTOData q = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  151. foreach (string ID in DocIDList)
  152. {
  153. string DocNo = "";
  154. string sql = @"SELECT DocNO FROM InvDoc_TransferIn a WHERE a.ID='" + ID + "' AND Org='" + OrgID + "'";
  155. DataTable dt = DBHelper.Query(sql, connString);
  156. if (dt != null && dt.Rows.Count > 0)
  157. {
  158. string a = dt.Rows[0][0].ToString();
  159. if (string.IsNullOrWhiteSpace(a))
  160. {
  161. throw new Exception("传入单据ID有误,请确认!");
  162. }
  163. else
  164. {
  165. DocNo = a;
  166. }
  167. }
  168. else
  169. {
  170. throw new Exception("传入单据ID有误,请确认!");
  171. }
  172. q.m_iD = long.Parse(ID);
  173. q.m_code = DocNo;
  174. queryList.Add(q);
  175. }
  176. queryArr = queryList.ToArray();
  177. returnMsg = client.Do(context, queryArr, UserCode, DateTime.Now);
  178. if (returnMsg != null && returnMsg.Length > 0)
  179. {
  180. foreach (var item in returnMsg)
  181. {
  182. throw new Exception(item.localMessage);
  183. }
  184. }
  185. else
  186. {
  187. ResultFlag = true;
  188. }
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. log.Error(GetExceptionMessage(ex));
  194. throw new Exception(GetExceptionMessage(ex));
  195. }
  196. return ResultFlag;
  197. }
  198. #region 给上下文信息赋值
  199. /// <summary>
  200. /// 给上下文信息赋值
  201. /// </summary>
  202. /// <returns></returns>
  203. private static ThreadContext CreateContextObj(string OrgID)
  204. {
  205. // 实例化应用上下文对象
  206. ThreadContext thContext = new ThreadContext();
  207. System.Collections.Generic.Dictionary<object, object> ns = new Dictionary<object, object>();
  208. ns.Add("OrgID", OrgID);
  209. ns.Add("UserID", ConfigurationManager.AppSettings["userID"].ToString());
  210. ns.Add("UserCode", ConfigurationManager.AppSettings["userCode"].ToString());
  211. ns.Add("Datetime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  212. ns.Add("CultureName", ConfigurationManager.AppSettings["cultureName"].ToString());
  213. ns.Add("EnterpriseID", ConfigurationManager.AppSettings["enterpriseID"].ToString());
  214. ns.Add("DefaultCultureName", ConfigurationManager.AppSettings["cultureName"].ToString());
  215. thContext.nameValueHas = ns;
  216. return thContext;
  217. }
  218. #endregion
  219. #region 提取异常信息
  220. /// <summary>
  221. /// 提取异常信息
  222. /// </summary>
  223. /// <param name="ex"></param>
  224. private string GetExceptionMessage(Exception ex)
  225. {
  226. string faultMessage = "未知错误,请查看ERP日志!";
  227. System.TimeoutException timeoutEx = ex as System.TimeoutException;
  228. if (timeoutEx != null)
  229. {
  230. faultMessage = "访问服务超时,请修改配置信息!";
  231. }
  232. else
  233. {
  234. FaultException<ServiceException> faultEx = ex as FaultException<ServiceException>;
  235. if (faultEx == null)
  236. {
  237. faultMessage = ex.Message;
  238. }
  239. else
  240. {
  241. ServiceException serviceEx = faultEx.Detail;
  242. if (serviceEx != null && !string.IsNullOrEmpty(serviceEx.Message)
  243. && !serviceEx.Message.Equals("fault", StringComparison.OrdinalIgnoreCase))
  244. {
  245. // 错误信息在faultEx.Message中,请提取,
  246. // 格式为"Fault:料品不能为空,请录入\n 在....."
  247. int startIndex = serviceEx.Message.IndexOf(":");
  248. int endIndex = serviceEx.Message.IndexOf("\n");
  249. if (endIndex == -1)
  250. endIndex = serviceEx.Message.Length;
  251. if (endIndex > 0 && endIndex > startIndex + 1)
  252. {
  253. faultMessage = serviceEx.Message.Substring(startIndex + 1, endIndex - startIndex - 1);
  254. }
  255. else
  256. {
  257. faultMessage = serviceEx.Message;
  258. }
  259. }
  260. }
  261. }
  262. return faultMessage;
  263. }
  264. #endregion
  265. }
  266. }