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

333 lines
15 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 UFSoft.UBF.Service;
  14. using UFSoft.UBF.Exceptions;
  15. using www.ufida.org.EntityData;
  16. using ICSSoft.APICreateLotMaster;
  17. using ICSSoft.APIApproveIssue;
  18. using ICSSoft.APIDeleteIssue;
  19. using ICSSoft.ASKDataProject;
  20. namespace ICSSoft.APICreateIssue
  21. {
  22. public class CreateIssue
  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. public string Create(List<ICSMOIssueDocNegative> infos)
  28. {
  29. DataSet ds = null;
  30. string msg = "";
  31. ThreadContext context = new ThreadContext();
  32. try
  33. {
  34. string _MOCode = string.Empty;
  35. UFIDAU9ISVMOICreatRecedeIssueDocSVClient client = new UFIDAU9ISVMOICreatRecedeIssueDocSVClient();
  36. MessageBase[] message = null;
  37. List<UFIDAU9ISVMORecedeItemAndSnDTOData> molist = new List<UFIDAU9ISVMORecedeItemAndSnDTOData>();
  38. UFIDAU9ISVMORecedeItemAndSnDTOData mo = new UFIDAU9ISVMORecedeItemAndSnDTOData();
  39. UFIDAU9CBOPubControllerCommonArchiveDataDTOData docType = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  40. string OrgID = "";
  41. string orgsql = @"select ID from Base_Organization
  42. where Code='{0}'";
  43. orgsql = string.Format(orgsql, infos[0].WorkPoint);
  44. DataTable orgdt = DBHelper.Query(orgsql, connString);
  45. if (orgdt.Rows.Count != 0)
  46. {
  47. OrgID = orgdt.Rows[0]["ID"].ToString();
  48. }
  49. else
  50. {
  51. throw new Exception("组织编码:" + infos[0].WorkPoint + "不存在!");
  52. }
  53. foreach (ICSMOIssueDocNegative model in infos)
  54. {
  55. if (!string.IsNullOrWhiteSpace(model.SourceCode))
  56. {
  57. string LLID = "";
  58. string llsql = @"select ID from MO_IssueDoc
  59. where DocNo='{0}' And Org='{1}'";
  60. llsql = string.Format(llsql, model.SourceCode, OrgID);
  61. DataTable lldt = DBHelper.Query(llsql, connString);
  62. if (lldt.Rows.Count > 0)
  63. {
  64. LLID = lldt.Rows[0]["ID"].ToString();
  65. }
  66. else
  67. {
  68. throw new Exception("领料单:" + model.SourceCode + "不存在!");
  69. }
  70. #region 遍历入参LL单行
  71. foreach (var item in model.details)
  72. {
  73. mo = new UFIDAU9ISVMORecedeItemAndSnDTOData();
  74. long lllineID = 0;
  75. llsql = @"select ID,IsIssued from MO_IssueDocLine
  76. where ID='{0}'";
  77. llsql = string.Format(llsql, item.SourceDetailID);
  78. lldt = DBHelper.Query(llsql, connString);
  79. if (lldt.Rows.Count > 0)
  80. {
  81. if (lldt.Rows[0]["IsIssued"].ToString() != "True" && lldt.Rows[0]["IsIssued"].ToString() != "true")
  82. {
  83. throw new Exception("领料单:'" + model.SourceCode + "'对应领料单行:'" + item.Sequence + "'未进行发料确认,不能退料!");
  84. }
  85. else
  86. {
  87. lllineID = long.Parse(lldt.Rows[0]["ID"].ToString());
  88. }
  89. }
  90. else
  91. {
  92. throw new Exception("领料单:'" + model.SourceCode + "'对应领料单行:'" + item.Sequence + "'不存在!");
  93. }
  94. mo.m_issueDocLine = lllineID;
  95. string strCcode = item.InvCode; //子料
  96. string strWhCode = string.Empty; //仓库
  97. string _strWhID = string.Empty;//仓库ID
  98. if (!string.IsNullOrWhiteSpace(strCcode))
  99. {
  100. string sqlWH = @"select ID,Code from CBO_Wh
  101. WHERE Code='" + model.WHCode + "' AND Org='" + OrgID + "'";
  102. sqlWH = string.Format(sqlWH);
  103. DataTable _dtWH = DBHelper.Query(sqlWH, connString);
  104. if (_dtWH.Rows.Count == 0)
  105. {
  106. throw new Exception("没有仓库信:" + model.WHCode + "信息!");
  107. }
  108. else
  109. {
  110. strWhCode = _dtWH.Rows[0]["Code"].ToString();
  111. _strWhID = _dtWH.Rows[0]["ID"].ToString();
  112. mo.m_whCode = strWhCode;
  113. mo.m_wh = long.Parse(_strWhID);
  114. }
  115. }
  116. else
  117. {
  118. throw new Exception("没有子料信息");
  119. }
  120. string sqlINV = @"select ID,Code from CBO_ItemMaster
  121. WHERE Code='" + item.InvCode + "' AND Org='" + OrgID + "'";
  122. sqlINV = string.Format(sqlINV);
  123. DataTable _dtINV = DBHelper.Query(sqlINV, connString);
  124. if (_dtINV.Rows.Count == 0)
  125. {
  126. throw new Exception("料品编码:" + item.InvCode + "不存在!");
  127. }
  128. mo.m_recedeQty = item.Quantity;
  129. mo.m_itemCode = item.InvCode;
  130. mo.m_recedeReason = 3;
  131. #region 创建批号接口调用
  132. string LotID = "";
  133. if (item.BatchCode != "")
  134. {
  135. CreateLotMaster LotMasterCreate = new CreateLotMaster();
  136. string lotWHCode = model.WHCode;
  137. string lotItemCode = item.InvCode;
  138. string LotCode = item.BatchCode;
  139. LotMasterCreate.Create(lotWHCode, lotItemCode, LotCode, OrgID, ref LotID);
  140. if (LotID != "")
  141. {
  142. mo.m_lotMaster = long.Parse(LotID);
  143. mo.m_lotNo = item.BatchCode;
  144. }
  145. }
  146. else
  147. {
  148. mo.m_lotNo = "";
  149. }
  150. #endregion
  151. molist.Add(mo);
  152. }
  153. #endregion
  154. }
  155. else
  156. {
  157. throw new Exception("传入领料单号为空!");
  158. }
  159. docType.m_code = "2";
  160. }
  161. context = CreateContextObj(OrgID);
  162. string[] _result = null;
  163. _result = client.Do(out message, context, molist.ToArray(), false, docType);
  164. List<string> LLCodeList = new List<string>();
  165. string DocNOList = "";
  166. if (_result != null && _result.Count() > 0)
  167. {
  168. for (int i = 0; i < _result.Count(); i++)
  169. {
  170. LLCodeList.Add(_result[i].ToString());
  171. if (DocNOList == "")
  172. {
  173. DocNOList = "'" + _result[i].ToString() + "'";
  174. }
  175. else
  176. {
  177. DocNOList += ",'" + _result[i].ToString() + "'";
  178. }
  179. }
  180. }
  181. if (LLCodeList != null)
  182. {
  183. ApproveIssue sh = new ApproveIssue();
  184. bool approveresult = sh.Approve(LLCodeList, infos[0].WorkPoint);
  185. if (approveresult == true)
  186. {
  187. #region 查询
  188. string sql = @"select DISTINCT a.ID as ID,a.ID as IDs,e.DocNo as SourceCode,
  189. a.DocNo as IssueNEGCode,f.Code AS cDepCode,g.Name AS cDepName,h.Code AS cWhCode
  190. ,i.Name AS cWhName,'' as SourceType,a.CreatedBy as CreateUser,a.CreatedOn as CreateDateTime,
  191. a.ApproveBy as Checker,a.ApproveOn as CheckDateTime
  192. from MO_IssueDoc a
  193. left join MO_IssueDocLine c on c.IssueDoc=a.ID
  194. left join MO_IssueDocLine d on d.ID=c.SourceIssueDocLine
  195. left join MO_IssueDoc e on e.ID=d.IssueDoc
  196. left join CBO_Department f on f.ID=c.IssueDept
  197. left join CBO_Department_Trl g on g.ID=f.ID
  198. left join CBO_Wh h on h.ID=c.Wh
  199. left join CBO_Wh_Trl i on i.ID=h.ID
  200. WHERE a.DocNo in ({0}) AND a.Org='{1}'";
  201. sql = string.Format(sql, DocNOList, OrgID);
  202. DataTable mdt= DBHelper.Query(sql, connString);
  203. sql = @"select DISTINCT a.ID as IDs,b.ID as DetailID,b.LineNum as Sequence,c.Code as InvCode
  204. ,b.IssuedQty as Quantity,b.SourceIssueDocLine as SourceDetailID,0 as Amount
  205. ,isnull(d.Code,'') ProjectCode,isnull(e.Code,'') cBatch,'' version ,'' brand,
  206. '' as cFree1,
  207. '' as cFree2,
  208. '' as cFree3,
  209. '' as cFree4,
  210. '' as cFree5,
  211. '' as cFree6,
  212. '' as cFree7,
  213. '' as cFree8,
  214. '' as cFree9,
  215. '' as cFree10
  216. from MO_IssueDoc a
  217. left join MO_IssueDocLine b on b.IssueDoc=a.ID
  218. left join CBO_ItemMaster c on c.ID=b.Item
  219. left join CBO_Project d on d.ID=b.Project
  220. left join Lot_LotMaster e on e.ID=b.LotMaster
  221. WHERE a.DocNo in ({0}) AND a.Org='{1}'";
  222. sql = string.Format(sql, DocNOList, OrgID);
  223. DataTable cdt = DBHelper.Query(sql, connString);
  224. DataTable tempdt1 = mdt.Copy();
  225. tempdt1.TableName = "tempdt1";
  226. ds.Tables.Add(tempdt1);
  227. DataTable tempdt2 = cdt.Copy();
  228. tempdt2.TableName = "tempdt2";
  229. ds.Tables.Add(tempdt2);
  230. #endregion
  231. }
  232. else
  233. {
  234. DeleteIssue issuedel = new DeleteIssue();
  235. issuedel.Delete(LLCodeList, OrgID);
  236. throw new Exception("退料单审核失败,创建的退料单已删除!");
  237. }
  238. }
  239. }
  240. catch (Exception ex)
  241. {
  242. throw new Exception(GetExceptionMessage(ex));
  243. }
  244. msg = JSON.DataSetToJson(ds, "details", "IDs");
  245. return msg;
  246. }
  247. #region 给上下文信息赋值
  248. /// <summary>
  249. /// 给上下文信息赋值
  250. /// </summary>
  251. /// <returns></returns>
  252. private static ThreadContext CreateContextObj(string OrgID)
  253. {
  254. // 实例化应用上下文对象
  255. ThreadContext thContext = new ThreadContext();
  256. System.Collections.Generic.Dictionary<object, object> ns = new Dictionary<object, object>();
  257. ns.Add("OrgID", OrgID);
  258. ns.Add("UserID", ConfigurationManager.AppSettings["userID"].ToString());
  259. ns.Add("UserCode", ConfigurationManager.AppSettings["userCode"].ToString());
  260. ns.Add("Datetime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  261. ns.Add("CultureName", ConfigurationManager.AppSettings["cultureName"].ToString());
  262. ns.Add("EnterpriseID", ConfigurationManager.AppSettings["enterpriseID"].ToString());
  263. ns.Add("DefaultCultureName", ConfigurationManager.AppSettings["cultureName"].ToString());
  264. thContext.nameValueHas = ns;
  265. return thContext;
  266. }
  267. #endregion
  268. #region 提取异常信息
  269. /// <summary>
  270. /// 提取异常信息
  271. /// </summary>
  272. /// <param name="ex"></param>
  273. private static string GetExceptionMessage(Exception ex)
  274. {
  275. string faultMessage = "未知错误,请查看ERP日志!";
  276. System.TimeoutException timeoutEx = ex as System.TimeoutException;
  277. if (timeoutEx != null)
  278. {
  279. faultMessage = "访问服务超时,请修改配置信息!";
  280. }
  281. else
  282. {
  283. FaultException<ServiceException> faultEx = ex as FaultException<ServiceException>;
  284. if (faultEx == null)
  285. {
  286. faultMessage = ex.Message;
  287. }
  288. else
  289. {
  290. ServiceException serviceEx = faultEx.Detail;
  291. if (serviceEx != null && !string.IsNullOrEmpty(serviceEx.Message)
  292. && !serviceEx.Message.Equals("fault", StringComparison.OrdinalIgnoreCase))
  293. {
  294. // 错误信息在faultEx.Message中,请提取,
  295. // 格式为"Fault:料品不能为空,请录入\n 在....."
  296. int startIndex = serviceEx.Message.IndexOf(":");
  297. int endIndex = serviceEx.Message.IndexOf("\n");
  298. if (endIndex == -1)
  299. endIndex = serviceEx.Message.Length;
  300. if (endIndex > 0 && endIndex > startIndex + 1)
  301. {
  302. faultMessage = serviceEx.Message.Substring(startIndex + 1, endIndex - startIndex - 1);
  303. }
  304. else
  305. {
  306. faultMessage = serviceEx.Message;
  307. }
  308. }
  309. }
  310. }
  311. return faultMessage;
  312. }
  313. #endregion
  314. }
  315. }