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

312 lines
13 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. using ICSSoft.APIApproveCompRpt;
  17. using ICSSoft.APIDeleteCompRpt;
  18. using ICSSoft.APICloseCompRpt;
  19. using ICSSoft.DataProject;
  20. namespace ICSSoft.APICreateCompRpt
  21. {
  22. public class CreateCompRpt
  23. {
  24. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  25. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  26. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  27. /// <summary>
  28. /// 创建完工报告(外部通过API调用)
  29. /// </summary>
  30. /// <param name="infos"></param>
  31. /// <returns></returns>
  32. public string Create(List<ICSManufactureReceiveDoc> infos)
  33. {
  34. string msg = "";
  35. DataSet ds = null;
  36. UFIDAU9ISVMOICreateCompRptSrvClient client = new UFIDAU9ISVMOICreateCompRptSrvClient();
  37. //服务返回结果
  38. UFIDAU9ISVMOCompRptKeyDTOData[] returnItem;
  39. //返回异常信息,目前还没有使用此属性
  40. UFSoft.UBF.Exceptions.MessageBase[] returnMsg;
  41. //上下文信息
  42. object context;
  43. //传入 参数
  44. UFIDAU9ISVMOCompRptDTOData[] compRptDTOs;
  45. try
  46. {
  47. if (infos == null || infos.Count == 0)
  48. {
  49. throw new Exception("传入数据不能为空!");
  50. }
  51. string OrgID = "";
  52. string orgsql = @"select ID from Base_Organization
  53. where Code='{0}'";
  54. orgsql = string.Format(orgsql, infos[0].WorkPoint);
  55. DataTable orgdt = DBHelper.Query(orgsql, connString);
  56. if (orgdt.Rows.Count != 0)
  57. {
  58. OrgID = orgdt.Rows[0]["ID"].ToString();
  59. }
  60. else
  61. {
  62. throw new Exception("传入的组织编码有误!");
  63. }
  64. //给上下文信息赋值
  65. context = CreateContextObj(OrgID);
  66. //给传入参数赋值
  67. compRptDTOs = GetDataNew(infos, OrgID);
  68. //服务调用
  69. (client.Endpoint.Binding as BasicHttpBinding).MaxReceivedMessageSize = int.MaxValue;
  70. (client.Endpoint.Binding as BasicHttpBinding).MaxBufferSize = int.MaxValue;
  71. returnItem = client.Do(out returnMsg, context, compRptDTOs);
  72. List<string> DocNoList = new List<string>();
  73. foreach (UFIDAU9ISVMOCompRptKeyDTOData data in returnItem)
  74. {
  75. if (data.m_operateResult == false)
  76. {
  77. throw new Exception(data.m_errorMsg);
  78. }
  79. DocNoList.Add(data.m_docNo);
  80. }
  81. bool ApproveResult = false;
  82. ApproveCompRpt custApproveComRpt = new ApproveCompRpt();
  83. ApproveResult = custApproveComRpt.Approve(DocNoList, OrgID);
  84. if (ApproveResult == false)
  85. {
  86. bool DeleteResult = false;
  87. DeleteCompRpt delete = new DeleteCompRpt();
  88. DeleteResult = delete.Delete(DocNoList, OrgID);
  89. }
  90. else
  91. {
  92. bool CloseResult = false;
  93. CloseCompRpt CloseSV = new CloseCompRpt();
  94. CloseResult = CloseSV.Close(DocNoList, OrgID);
  95. if (CloseResult == true)
  96. {
  97. string strDocNOList = "";
  98. foreach (string DocNo in DocNoList)
  99. {
  100. if (strDocNOList == "")
  101. {
  102. strDocNOList = "'" + DocNo + "'";
  103. }
  104. else
  105. {
  106. strDocNOList += ",'" + DocNo + "'";
  107. }
  108. }
  109. string sql = @"select DISTINCT a.ID as ID,a.ID as IDs,a.DocNo as MRCVCode,c.Code AS cDepCode,d.Name AS cDepName
  110. ,e.Code AS cWhCode,f.Name AS cWhName
  111. ,g.DocNo as MOCode ,a.CreatedBy as CreateUser ,a.CreatedOn as CreateDateTime
  112. ,a.ApproveBy as Checker ,a.ApproveOn as CheckDateTime
  113. from MO_CompleteRpt a
  114. left join CBO_Department c on c.ID=a.HandleDept
  115. left join CBO_Department_Trl d on d.ID=c.ID
  116. left join CBO_Wh e on e.ID=a.RcvWh
  117. left join CBO_Wh_Trl f on f.ID=e.ID
  118. left join MO_MO g on g.ID=a.MO
  119. WHERE A.DocNo in({0}) AND a.Org='{1}'";
  120. sql = string.Format(sql, strDocNOList, OrgID);
  121. DataTable mdt = DBHelper.Query(sql, connString);
  122. sql = @"select DISTINCT a.ID as IDs, b.ID as DetailID,b.LineNum as Sequence,c.Code as InvCode ,
  123. b.RcvQtyByWhUOM as Quantity ,0 as Amount,a.MO as MODetailID,
  124. isnull(d.Code,'') ProjectCode,isnull(e.Code,'') cBatch,'' version ,'' brand,
  125. '' as cFree1,
  126. '' as cFree2,
  127. '' as cFree3,
  128. '' as cFree4,
  129. '' as cFree5,
  130. '' as cFree6,
  131. '' as cFree7,
  132. '' as cFree8,
  133. '' as cFree9,
  134. '' as cFree10
  135. from MO_CompleteRpt a
  136. left join MO_CompleteRptRcvLine b on b.CompleteRpt=a.ID
  137. left join CBO_ItemMaster c on c.ID=a.Item
  138. left join CBO_Project d on d.ID=a.Project
  139. left join Lot_LotMaster e on e.ID=a.ProductLotMaster
  140. WHERE A.DocNo in({0}) AND a.Org='{1}'";
  141. sql = string.Format(sql, strDocNOList, OrgID);
  142. DataTable cdt = DBHelper.Query(sql, connString);
  143. DataTable tempdt1 = mdt.Copy();
  144. tempdt1.TableName = "tempdt1";
  145. ds.Tables.Add(tempdt1);
  146. DataTable tempdt2 = cdt.Copy();
  147. tempdt2.TableName = "tempdt2";
  148. ds.Tables.Add(tempdt2);
  149. }
  150. else
  151. {
  152. throw new Exception("完工报告关闭失败,创建的完工报告已删除!");
  153. }
  154. }
  155. }
  156. catch (Exception ex)
  157. {
  158. log.Error(GetExceptionMessage(ex));
  159. throw new Exception(GetExceptionMessage(ex));
  160. }
  161. msg = JSON.DataSetToJson(ds, "details", "IDs");
  162. return msg;
  163. }
  164. private static long GetTypeID(string docTypeCode, string OrgID)
  165. {
  166. string sql = @"select ID from MO_CompleteRpt where DocNo='" + docTypeCode + "' AND Org='" + OrgID + "' ";
  167. DataTable dt = DBHelper.Query(sql, connString);
  168. if (dt.Rows.Count > 0)
  169. {
  170. return long.Parse(dt.Rows[0][0].ToString());
  171. }
  172. else
  173. {
  174. return 0;
  175. }
  176. }
  177. #region 给上下文信息赋值
  178. /// <summary>
  179. /// 给上下文信息赋值
  180. /// </summary>
  181. /// <returns></returns>
  182. private static ThreadContext CreateContextObj(string OrgID)
  183. {
  184. // 实例化应用上下文对象
  185. ThreadContext thContext = new ThreadContext();
  186. System.Collections.Generic.Dictionary<object, object> ns = new Dictionary<object, object>();
  187. ns.Add("OrgID", OrgID);
  188. ns.Add("UserID", ConfigurationManager.AppSettings["userID"].ToString());
  189. ns.Add("UserCode", ConfigurationManager.AppSettings["userCode"].ToString());
  190. ns.Add("Datetime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  191. ns.Add("CultureName", ConfigurationManager.AppSettings["cultureName"].ToString());
  192. ns.Add("EnterpriseID", ConfigurationManager.AppSettings["enterpriseID"].ToString());
  193. ns.Add("DefaultCultureName", ConfigurationManager.AppSettings["cultureName"].ToString());
  194. thContext.nameValueHas = ns;
  195. return thContext;
  196. }
  197. #endregion
  198. private static UFIDAU9ISVMOCompRptDTOData[] GetDataNew(List<ICSManufactureReceiveDoc> CompRptList, string OrgID)
  199. {
  200. List<UFIDAU9ISVMOCompRptDTOData> datas = new List<UFIDAU9ISVMOCompRptDTOData>();
  201. foreach (ICSManufactureReceiveDoc item in CompRptList)
  202. {
  203. foreach (ICSManufactureReceiveDocs line in item.details)
  204. {
  205. UFIDAU9ISVMOCompRptDTOData data = new UFIDAU9ISVMOCompRptDTOData();
  206. if (line.Quantity != 0)
  207. {
  208. data.m_completeQty = line.Quantity;
  209. }
  210. data.m_org = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  211. data.m_org.m_iD = long.Parse(OrgID);
  212. data.m_completeDocType = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  213. data.m_completeDocType.m_code = "1";
  214. //data.m_completeDocType.m_iD = GetTypeID(docTypeCode);
  215. data.m_mO = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  216. data.m_mO.m_code = line.InvCode;
  217. data.m_handlePerson = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  218. data.m_handlePerson.m_code = item.User;
  219. data.m_handleDept = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  220. data.m_handleDept.m_code = item.DepCode;
  221. data.m_rcvWh = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  222. data.m_rcvWh.m_iD = GetWHID(item.WHCode, OrgID);
  223. data.m_completeDate = DateTime.Now;
  224. data.m_actualRcvTime = DateTime.Now;
  225. datas.Add(data);
  226. }
  227. }
  228. return datas.ToArray();
  229. }
  230. public static long GetWHID(string WHCode, string OrgID)
  231. {
  232. long WHID = 0;
  233. string sql = @" SELECT ID FROM CBO_Wh WHERE Code='" + WHCode + "' AND Org='" + OrgID + "' ";
  234. sql = string.Format(sql);
  235. DataTable _dtWH = DBHelper.Query(sql, connString);
  236. string _WHCode = string.Empty;
  237. if (_dtWH != null && _dtWH.Rows.Count > 0)
  238. {
  239. _WHCode = _dtWH.Rows[0][0].ToString();
  240. WHID = long.Parse(_WHCode);
  241. }
  242. return WHID;
  243. }
  244. #region 提取异常信息
  245. /// <summary>
  246. /// 提取异常信息
  247. /// </summary>
  248. /// <param name="ex"></param>
  249. private static string GetExceptionMessage(Exception ex)
  250. {
  251. string faultMessage = "未知错误,请查看ERP日志!";
  252. System.TimeoutException timeoutEx = ex as System.TimeoutException;
  253. if (timeoutEx != null)
  254. {
  255. faultMessage = "访问服务超时,请修改配置信息!";
  256. }
  257. else
  258. {
  259. FaultException<ServiceException> faultEx = ex as FaultException<ServiceException>;
  260. if (faultEx == null)
  261. {
  262. faultMessage = ex.Message;
  263. }
  264. else
  265. {
  266. ServiceException serviceEx = faultEx.Detail;
  267. if (serviceEx != null && !string.IsNullOrEmpty(serviceEx.Message)
  268. && !serviceEx.Message.Equals("fault", StringComparison.OrdinalIgnoreCase))
  269. {
  270. // 错误信息在faultEx.Message中,请提取,
  271. // 格式为"Fault:料品不能为空,请录入\n 在....."
  272. int startIndex = serviceEx.Message.IndexOf(":");
  273. int endIndex = serviceEx.Message.IndexOf("\n");
  274. if (endIndex == -1)
  275. endIndex = serviceEx.Message.Length;
  276. if (endIndex > 0 && endIndex > startIndex + 1)
  277. {
  278. faultMessage = serviceEx.Message.Substring(startIndex + 1, endIndex - startIndex - 1);
  279. }
  280. else
  281. {
  282. faultMessage = serviceEx.Message;
  283. }
  284. }
  285. }
  286. }
  287. return faultMessage;
  288. }
  289. #endregion
  290. }
  291. }