圣珀
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.

402 lines
18 KiB

2 years ago
2 years ago
  1. using ICS.WCF.Base;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Linq;
  7. using System.ServiceModel;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using UFIDA.ISV.BFIssueApprove;
  11. using UFIDA.ISV.BFIssueDelete;
  12. using UFIDA.ISV.CreateLotMaster;
  13. using UFSoft.UBF.Exceptions;
  14. using UFSoft.UBF.Service;
  15. using UFSoft.UBF.Util.Context;
  16. using www.ufida.org.EntityData;
  17. namespace UFIDA.ISV.CreateIssue
  18. {
  19. public class CustCreateIssue
  20. {
  21. /// <summary>
  22. /// 创建退料单
  23. /// </summary>
  24. /// <returns></returns>
  25. ///
  26. public OperationResult CreateIssue(List<IssueData> modellist)
  27. {
  28. OperationResult result = new OperationResult();
  29. ThreadContext context = new ThreadContext();
  30. try
  31. {
  32. StringBuilder beginstrlog = new StringBuilder();
  33. beginstrlog.AppendLine("创建退料单(传入数据开始)");
  34. beginstrlog.AppendLine("*****************************************************");
  35. foreach (var model in modellist)
  36. {
  37. beginstrlog.AppendLine("生产领料单:" + model.LLCode);
  38. beginstrlog.AppendLine("备注:" + model.Memo);
  39. beginstrlog.AppendLine("组织编码:" + model.OrgCode);
  40. beginstrlog.AppendLine("用户编码:" + model.UserCode);
  41. foreach (var item in model.linelist)
  42. {
  43. bool isLotCheck = Appconfig.WHLotCheckState(item.WHCode, model.OrgCode);
  44. if (isLotCheck == false)
  45. {
  46. item.LotNO = "";
  47. }
  48. beginstrlog.AppendLine("领料单行号:" + item.LLLineNO);
  49. beginstrlog.AppendLine("料号:" + item.ItemCode);
  50. beginstrlog.AppendLine("数量:" + item.ItemQty);
  51. beginstrlog.AppendLine("仓库:" + item.WHCode);
  52. beginstrlog.AppendLine("批号:" + item.LotNO);
  53. }
  54. beginstrlog.AppendLine("--------------------------------------------------");
  55. }
  56. beginstrlog.AppendLine("*****************************************************");
  57. beginstrlog.AppendLine("创建退料单(传入数据结束)");
  58. Appconfig.WriteLogFile(beginstrlog.ToString(), "创建退料单");
  59. string _MOCode = string.Empty;
  60. UFIDAU9ISVMOICreatRecedeIssueDocSVClient client = new UFIDAU9ISVMOICreatRecedeIssueDocSVClient();
  61. MessageBase[] message = null;
  62. List<UFIDAU9ISVMORecedeItemAndSnDTOData> molist = new List<UFIDAU9ISVMORecedeItemAndSnDTOData>();
  63. UFIDAU9ISVMORecedeItemAndSnDTOData mo = new UFIDAU9ISVMORecedeItemAndSnDTOData();
  64. UFIDAU9CBOPubControllerCommonArchiveDataDTOData docType = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  65. StringBuilder str = new StringBuilder();
  66. str.AppendLine("************领料单日志记录开始**************");
  67. str.AppendLine("组织编码:" + modellist[0].OrgCode);
  68. string OrgID = "";
  69. string orgsql = @"select ID from Base_Organization
  70. where Code='{0}'";
  71. orgsql = string.Format(orgsql, modellist[0].OrgCode);
  72. DataTable orgdt = DBhlper.Query(orgsql, Appconfig.GetU9ConnStr());
  73. if (orgdt.Rows.Count != 0)
  74. {
  75. OrgID = orgdt.Rows[0]["ID"].ToString();
  76. }
  77. else
  78. {
  79. result.MESSAGE = "组织编码:" + modellist[0].OrgCode + "不存在!";
  80. result.IsSuccess = false;
  81. return result;
  82. }
  83. #region 获取业务员名称
  84. string CreateBy = string.Empty;
  85. string usersql = @"select B.Name from CBO_Operators A
  86. LEFT JOIN CBO_Operators_Trl B
  87. ON B.ID=A.ID
  88. where A.Code='{0}' and A.Org='{1}'";
  89. usersql = string.Format(usersql, modellist[0].UserCode, OrgID);
  90. DataTable userdt = DBhlper.Query(usersql, Appconfig.GetU9ConnStr());
  91. #endregion
  92. if (userdt.Rows.Count != 0)
  93. {
  94. CreateBy = userdt.Rows[0]["Name"].ToString();
  95. }
  96. else
  97. {
  98. }
  99. foreach (IssueData model in modellist)
  100. {
  101. if (!string.IsNullOrWhiteSpace(model.LLCode))
  102. {
  103. str.AppendLine("单号:" + model.LLCode);
  104. string LLID = "";
  105. string llsql = @"select ID from MO_IssueDoc
  106. where DocNo='{0}' And Org='{1}'";
  107. llsql = string.Format(llsql, model.LLCode, OrgID);
  108. DataTable lldt = DBhlper.Query(llsql, Appconfig.GetU9ConnStr());
  109. if (lldt.Rows.Count > 0)
  110. {
  111. LLID = lldt.Rows[0]["ID"].ToString();
  112. str.AppendLine("主键ID:" + LLID);
  113. }
  114. else
  115. {
  116. result.MESSAGE = "领料单:" + model.LLCode + "不存在!";
  117. result.IsSuccess = false;
  118. return result;
  119. }
  120. #region 遍历入参LL单行
  121. foreach (var item in model.linelist)
  122. {
  123. mo = new UFIDAU9ISVMORecedeItemAndSnDTOData();
  124. long lllineID = 0;
  125. llsql = @"select ID,IsIssued from MO_IssueDocLine
  126. where IssueDoc='{0}'
  127. AND LineNum={1}";
  128. llsql = string.Format(llsql, LLID, item.LLLineNO);
  129. lldt = DBhlper.Query(llsql, Appconfig.GetU9ConnStr());
  130. if (lldt.Rows.Count > 0)
  131. {
  132. if (lldt.Rows[0]["IsIssued"].ToString() != "True" && lldt.Rows[0]["IsIssued"].ToString() != "true")
  133. {
  134. result.MESSAGE = "领料单:'" + model.LLCode + "'对应领料单行:'" + item.LLLineNO + "'未进行发料确认,不能退料!";
  135. result.IsSuccess = false;
  136. return result;
  137. }
  138. else
  139. {
  140. lllineID = long.Parse(lldt.Rows[0]["ID"].ToString());
  141. str.AppendLine("子表主键ID:" + lllineID);
  142. }
  143. }
  144. else
  145. {
  146. result.MESSAGE = "领料单:'" + model.LLCode + "'对应领料单行:'" + item.LLLineNO + "'不存在!";
  147. result.IsSuccess = false;
  148. return result;
  149. }
  150. mo.m_issueDocLine = lllineID;
  151. string strCcode = item.ItemCode; //子料
  152. string strWhCode = string.Empty; //仓库
  153. string _strWhID = string.Empty;//仓库ID
  154. if (!string.IsNullOrWhiteSpace(strCcode))
  155. {
  156. string sqlWH = @"select ID,Code from CBO_Wh
  157. WHERE Code='" + item.WHCode + "' AND Org='" + OrgID + "'";
  158. sqlWH = string.Format(sqlWH);
  159. DataTable _dtWH = DBhlper.Query(sqlWH, Appconfig.GetU9ConnStr());
  160. if (_dtWH.Rows.Count == 0)
  161. {
  162. result.MESSAGE = strCcode + "没有仓库信息 ";
  163. result.IsSuccess = false;
  164. return result;
  165. }
  166. else
  167. {
  168. strWhCode = _dtWH.Rows[0]["Code"].ToString();
  169. _strWhID = _dtWH.Rows[0]["ID"].ToString();
  170. mo.m_whCode = strWhCode;
  171. mo.m_wh = long.Parse(_strWhID);
  172. str.AppendLine("仓库:" + mo.m_whCode);
  173. }
  174. }
  175. else
  176. {
  177. result.MESSAGE = strCcode + "没有子料信息 ";
  178. result.IsSuccess = false;
  179. return result;
  180. }
  181. int i = Appconfig.IsExistItem(strCcode);
  182. if (i < 1)
  183. {
  184. result.MESSAGE = "料品编码不存在!";
  185. result.IsSuccess = false;
  186. return result;
  187. }
  188. str.AppendLine("数量:" + item.ItemQty);
  189. str.AppendLine("物料编码:" + item.ItemCode);
  190. str.AppendLine("批号:" + item.LotNO);
  191. mo.m_recedeQty = item.ItemQty;
  192. mo.m_itemCode = item.ItemCode;
  193. mo.m_recedeReason = 3;
  194. #region 创建批号接口调用
  195. string LotID = "";
  196. if (item.LotNO != "")
  197. {
  198. CustCreateLotMaster LotMasterCreate = new CustCreateLotMaster();
  199. string lotWHCode = item.WHCode;
  200. string lotItemCode = item.ItemCode;
  201. string LotCode = item.LotNO;
  202. LotMasterCreate.CustCreateRCVLotMaster(lotWHCode, lotItemCode, LotCode, OrgID, ref LotID);
  203. if (LotID != "")
  204. {
  205. mo.m_lotMaster = long.Parse(LotID);
  206. mo.m_lotNo = item.LotNO;
  207. }
  208. }
  209. #endregion
  210. str.AppendLine("批号ID:" + LotID);
  211. molist.Add(mo);
  212. }
  213. #endregion
  214. }
  215. else
  216. {
  217. result.MESSAGE = "存在领料单号为空";
  218. result.IsSuccess = false;
  219. return result;
  220. }
  221. docType.m_code = "21";
  222. docType.m_iD = 1002201100110115;
  223. docType.m_name = "退料补料(补料)";
  224. }
  225. str.AppendLine("////////////molist记录开始/////////////");
  226. foreach (var item in molist)
  227. {
  228. str.AppendLine("item.m_issueDocLine:" + item.m_issueDocLine);
  229. str.AppendLine("item.m_recedeQty:" + item.m_recedeQty);
  230. str.AppendLine("item.m_itemCode:" + item.m_itemCode);
  231. str.AppendLine("item.m_whCode:" + item.m_whCode);
  232. str.AppendLine("item.m_wh:" + item.m_wh);
  233. }
  234. str.AppendLine("////////////molist记录结束/////////////");
  235. str.AppendLine("**************日志结束******************");
  236. Appconfig.WriteLogFile(str.ToString(), "创建退料单");
  237. context = CreateContextObj(OrgID);
  238. string[] _result = null;
  239. _result = client.Do(out message, context, molist.ToArray(), false, docType);
  240. List<string> LLCodeList = new List<string>();
  241. StringBuilder endstr = new StringBuilder();
  242. endstr.AppendLine("**************创建退料单返回日志******************");
  243. if (_result != null && _result.Count() > 0)
  244. {
  245. for (int i = 0; i < _result.Count(); i++)
  246. {
  247. LLCodeList.Add(_result[i].ToString());
  248. endstr.AppendLine("退料单号" + _result[i].ToString());
  249. }
  250. }
  251. endstr.AppendLine("**************创建退料单返回日志结束******************");
  252. Appconfig.WriteLogFile(endstr.ToString(), "创建退料单");
  253. if (LLCodeList != null)
  254. {
  255. CustApproveIssue.OperationResult shResult = new CustApproveIssue.OperationResult();
  256. CustApproveIssue sh = new CustApproveIssue();
  257. shResult = sh.CustBFApproveIssue(LLCodeList, modellist[0].OrgCode);
  258. if (shResult.IsSuccess == true)
  259. {
  260. result.IsSuccess = true;
  261. result.LLCode = _result[0].ToString();
  262. result.MESSAGE = "退料单创建成功!";
  263. }
  264. else
  265. {
  266. CustBFIssueDelete issueDelete = new CustBFIssueDelete();
  267. issueDelete.CustDeleteIssue(LLCodeList, OrgID);
  268. result.IsSuccess = false;
  269. result.MESSAGE = shResult.MESSAGE;
  270. }
  271. }
  272. }
  273. catch (Exception ex)
  274. {
  275. string Message = GetExceptionMessage(ex);
  276. result.IsSuccess = false;
  277. result.MESSAGE += Message;
  278. StringBuilder str = new StringBuilder();
  279. str.AppendLine("创建生产领料单(回仓):");
  280. //for (int i = 0; i < modellist.Count(); i++)
  281. //{
  282. // str.AppendLine("单号:" + modellist[i]);
  283. //}
  284. str.AppendLine("返回错误信息:" + result.MESSAGE);
  285. Appconfig.WriteLogFile(str.ToString(), "创建退料单");
  286. }
  287. //"必需设置请指定IssueDTO.BusinessType属性,生产线日计划:51,标准生产:47,返工生产:48,报废生产:49"
  288. return result;
  289. }
  290. # region 辅助方法
  291. /// <summary>
  292. /// 创建上下文
  293. /// </summary>
  294. /// <returns></returns>
  295. private static ThreadContext CreateContextObj(string OrgID)
  296. {
  297. // 实例化应用上下文对象
  298. ThreadContext thContext = new ThreadContext();
  299. System.Collections.Generic.Dictionary<object, object> ns = new Dictionary<object, object>();
  300. ns.Add("OrgID", OrgID);
  301. ns.Add("UserID", ConfigurationManager.AppSettings["userID"].ToString());
  302. ns.Add("UserCode", ConfigurationManager.AppSettings["userCode"].ToString());
  303. ns.Add("Datetime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  304. ns.Add("CultureName", ConfigurationManager.AppSettings["cultureName"].ToString());
  305. ns.Add("EnterpriseID", ConfigurationManager.AppSettings["enterpriseID"].ToString());
  306. ns.Add("DefaultCultureName", ConfigurationManager.AppSettings["cultureName"].ToString());
  307. thContext.nameValueHas = ns;
  308. return thContext;
  309. }
  310. # endregion
  311. #region 提取异常信息
  312. /// <summary>
  313. /// 提取异常信息
  314. /// </summary>
  315. /// <param name="ex"></param>
  316. private static string GetExceptionMessage(Exception ex)
  317. {
  318. string faultMessage = "未知错误,请查看ERP日志!";
  319. System.TimeoutException timeoutEx = ex as System.TimeoutException;
  320. if (timeoutEx != null)
  321. {
  322. faultMessage = "访问服务超时,请修改配置信息!";
  323. }
  324. else
  325. {
  326. FaultException<ServiceException> faultEx = ex as FaultException<ServiceException>;
  327. if (faultEx == null)
  328. {
  329. faultMessage = ex.Message;
  330. }
  331. else
  332. {
  333. ServiceException serviceEx = faultEx.Detail;
  334. if (serviceEx != null && !string.IsNullOrEmpty(serviceEx.Message)
  335. && !serviceEx.Message.Equals("fault", StringComparison.OrdinalIgnoreCase))
  336. {
  337. // 错误信息在faultEx.Message中,请提取,
  338. // 格式为"Fault:料品不能为空,请录入\n 在....."
  339. int startIndex = serviceEx.Message.IndexOf(":");
  340. int endIndex = serviceEx.Message.IndexOf("\n");
  341. if (endIndex == -1)
  342. endIndex = serviceEx.Message.Length;
  343. if (endIndex > 0 && endIndex > startIndex + 1)
  344. {
  345. faultMessage = serviceEx.Message.Substring(startIndex + 1, endIndex - startIndex - 1);
  346. }
  347. else
  348. {
  349. faultMessage = serviceEx.Message;
  350. }
  351. }
  352. }
  353. }
  354. return faultMessage;
  355. }
  356. #endregion
  357. public class OperationResult
  358. {
  359. /// <summary>
  360. /// 结果状态,默认值false
  361. /// </summary>
  362. public bool IsSuccess { get; set; }
  363. /// <summary>
  364. ///处理消息
  365. /// </summary>
  366. public string MESSAGE { get; set; }
  367. /// <summary>
  368. /// 输出领料订单
  369. /// </summary>
  370. public string LLCode { get; set; }
  371. }
  372. }
  373. }