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

357 lines
15 KiB

2 years ago
2 years ago
2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.ServiceModel;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using ICS.WCF.Base;
  10. using UFSoft.UBF.Exceptions;
  11. using UFSoft.UBF.Exceptions1;
  12. using UFSoft.UBF.Service;
  13. using www.ufida.org.EntityData;
  14. namespace ICS.CreateIssueIN
  15. {
  16. public class CreateOIssue
  17. {
  18. private static string ORGCode = ConfigurationManager.AppSettings["orgCode"].ToString();
  19. public OperationResult CreateIssue(List<IssueData> modellist)
  20. {
  21. OperationResult result = new OperationResult();
  22. object context = new object();
  23. try
  24. {
  25. StringBuilder beginstrlog = new StringBuilder();
  26. beginstrlog.AppendLine("创建委外备料单(传入数据开始)");
  27. beginstrlog.AppendLine("*****************************************************");
  28. foreach (var model in modellist)
  29. {
  30. beginstrlog.AppendLine("生产订单:" + model.MOCODE);
  31. foreach (var item in model.linelist)
  32. {
  33. beginstrlog.AppendLine("行号:" + item.DOCLINENO);
  34. beginstrlog.AppendLine("料号:" + item.ITEM);
  35. beginstrlog.AppendLine("实际需求数量:" + item.ActualReqQty);
  36. beginstrlog.AppendLine("标准用量:" + item.STDReqQty);
  37. }
  38. beginstrlog.AppendLine("--------------------------------------------------");
  39. }
  40. beginstrlog.AppendLine("*****************************************************");
  41. beginstrlog.AppendLine("创建委外备料单(传入数据结束)");
  42. Appconfig.WriteLogFile(beginstrlog.ToString(), "创建委外备料单");
  43. string _MOCode = string.Empty;
  44. UFIDAU9ISVMOICreateSubContractIssueDocExternalClient client = new UFIDAU9ISVMOICreateSubContractIssueDocExternalClient();
  45. MessageBase[] message = null;
  46. StringBuilder str = new StringBuilder();
  47. string OrgID = "";
  48. OrgID = Appconfig.IsExistsCode(ORGCode, "Base_Organization");
  49. if (string.IsNullOrEmpty(OrgID))
  50. {
  51. throw new Exception("组织编码:" + ORGCode + "在U9中不存在。");
  52. }
  53. UFIDAU9ISVMOIssueDTOData[] molist = createiss(modellist, OrgID);
  54. context = CreateContextObj(OrgID);
  55. UFIDAU9ISVMOIssueKeyDTOData[] _result = null;
  56. _result = client.Do(out message, context, molist);
  57. List<string> LLCodeList = new List<string>();
  58. StringBuilder endstr = new StringBuilder();
  59. endstr.AppendLine("**************创建委外备料单返回日志******************");
  60. if (_result != null && _result.Count() > 0)
  61. {
  62. for (int i = 0; i < _result.Count(); i++)
  63. {
  64. LLCodeList.Add(_result[i].ToString());
  65. endstr.AppendLine("退料单号" + _result[i].ToString());
  66. }
  67. }
  68. endstr.AppendLine("**************创建委外备料单返回日志结束******************");
  69. Appconfig.WriteLogFile(endstr.ToString(), "创建委外备料单");
  70. }
  71. catch (Exception ex)
  72. {
  73. string Message = GetExceptionMessage(ex);
  74. result.IsSuccess = false;
  75. result.MESSAGE += Message;
  76. StringBuilder str = new StringBuilder();
  77. str.AppendLine("创建委外备料单:");
  78. //for (int i = 0; i < modellist.Count(); i++)
  79. //{
  80. // str.AppendLine("单号:" + modellist[i]);
  81. //}
  82. str.AppendLine("返回错误信息:" + result.MESSAGE);
  83. Appconfig.WriteLogFile(str.ToString(), "创建委外备料单");
  84. }
  85. return result;
  86. }
  87. private UFIDAU9ISVMOIssueDTOData[] createiss(List<IssueData> modellist,string OrgID)
  88. {
  89. List<UFIDAU9ISVMOIssueDTOData> lsslist = new List<UFIDAU9ISVMOIssueDTOData>();
  90. foreach (IssueData model in modellist)
  91. {
  92. string moid = "";
  93. UFIDAU9ISVMOIssueDTOData lss = new UFIDAU9ISVMOIssueDTOData();
  94. if (!string.IsNullOrWhiteSpace(model.MOCODE))
  95. {
  96. string orgsql = @"select ID from MO_MO
  97. where DocNo='{0}'";
  98. orgsql = string.Format(orgsql, model.MOCODE);
  99. DataTable orgdt = DBhlper.Query(orgsql, Appconfig.GetU9ConnStr());
  100. if (orgdt.Rows.Count != 0)
  101. {
  102. moid = orgdt.Rows[0]["ID"].ToString();
  103. }
  104. else
  105. throw new Exception("工单:" + model.MOCODE + "不存在!");
  106. List<UFIDAU9CBOPubControllerCommonArchiveDataDTOData> molst = new List<UFIDAU9CBOPubControllerCommonArchiveDataDTOData>();
  107. UFIDAU9CBOPubControllerCommonArchiveDataDTOData mo1 = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  108. mo1.m_iD = Convert.ToInt64(moid);
  109. mo1.m_code = model.MOCODE;
  110. molst.Add(mo1);
  111. lss.m_mOs = molst.ToArray();
  112. UFIDAU9CBOPubControllerCommonArchiveDataDTOData d = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  113. d.m_iD = Convert.ToInt64(OrgID);
  114. d.m_code = ORGCode;
  115. lss.m_issueOrg = d;
  116. List<UFIDAU9ISVMOPickListDTOData> picklst = new List<UFIDAU9ISVMOPickListDTOData>();
  117. UFIDAU9ISVMOPickListDTOData pick = new UFIDAU9ISVMOPickListDTOData();
  118. foreach (IssueDatas line in model.linelist)
  119. {
  120. string ORGID = Appconfig.IsExistsCode(line.ORGCODE, "Base_Organization");
  121. if (string.IsNullOrEmpty(ORGID))
  122. {
  123. throw new Exception("组织编码:" + line.ORGCODE + "在U9中不存在。");
  124. }
  125. pick.m_aCDType = 0;
  126. //无备料领料
  127. pick.m_isNoPickLine = true;
  128. //生产订单
  129. UFIDAU9CBOPubControllerCommonArchiveDataDTOData mo = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  130. mo.m_iD = Convert.ToInt64(moid);
  131. mo.m_code = model.MOCODE;
  132. pick.m_mO = mo;
  133. // 料品
  134. string itemid = "";
  135. orgsql = @"select id from CBO_ItemMaster where code=='{0}'";
  136. orgsql = string.Format(orgsql, line.ITEM);
  137. orgdt = DBhlper.Query(orgsql, Appconfig.GetU9ConnStr());
  138. if (orgdt.Rows.Count != 0)
  139. itemid = orgdt.Rows[0]["ID"].ToString();
  140. else
  141. throw new Exception("料品:" + line.ITEM + "不存在!");
  142. UFIDAU9CBOPubControllerCommonArchiveDataDTOData item = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  143. mo.m_iD = Convert.ToInt64(itemid);
  144. mo.m_code = line.ITEM;
  145. pick.m_item = item;
  146. //领料单位
  147. if (Appconfig.uomid(line.UOM) != "")
  148. {
  149. UFIDAU9CBOPubControllerCommonArchiveDataDTOData uom = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  150. mo.m_iD = Convert.ToInt64(Appconfig.uomid(line.UOM));
  151. mo.m_code = line.UOM;
  152. pick.m_issueUOM = uom;
  153. }
  154. //发料数量
  155. pick.m_issuedQty = Convert.ToDecimal(line.ActualReqQty);
  156. // 应发数量
  157. pick.m_issueQty = Convert.ToDecimal(line.ActualReqQty);
  158. //存储地点
  159. if (string.IsNullOrWhiteSpace(line.WH))
  160. {
  161. throw new Exception("第 "+line.DOCLINENO+" 行,请传入仓库 ");
  162. }
  163. if (Appconfig.GetWhCodeByBinid(line.WH, ORGID) != "")
  164. {
  165. UFIDAU9CBOPubControllerCommonArchiveDataDTOData ww = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  166. mo.m_iD = Convert.ToInt64(Appconfig.GetWhCodeByBinid(line.WH, ORGID));
  167. mo.m_code = line.WH;
  168. pick.m_issueWh = ww;
  169. }
  170. // 批号
  171. //弹性栏位:
  172. //pickListDTO.DescFlexField = new DescFlexSegmentsData();
  173. //pickListDTO.DescFlexField.PrivateDescSeg1 = dd.ItemRLineID;//弹性栏位1存料品需求单行ID
  174. // 备料
  175. //pickListDTO.MOPickList = moPickList.ID;
  176. // 货主组织
  177. UFIDAU9CBOPubControllerCommonArchiveDataDTOData org = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  178. mo.m_iD = Convert.ToInt64(ORGID);
  179. mo.m_code = line.ORGCODE;
  180. pick.m_ownerOrg = org;
  181. //库位
  182. //子料项目号
  183. if (!string.IsNullOrEmpty(line.Project))
  184. {
  185. if (Appconfig.GetProjectid(line.Project, ORGID) != "")
  186. {
  187. UFIDAU9CBOPubControllerCommonArchiveDataDTOData project = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
  188. mo.m_iD = Convert.ToInt64(Appconfig.GetProjectid(line.Project, ORGID));
  189. mo.m_code = line.Project;
  190. pick.m_project = project;
  191. }
  192. }
  193. pick.m_issueDocLineID = 1;
  194. picklst.Add(pick);
  195. }
  196. lss.m_pickListDTOs = picklst.ToArray();
  197. lsslist.Add(lss);
  198. }
  199. else
  200. {
  201. throw new Exception("存在生产订单号为空!");
  202. }
  203. }
  204. return lsslist.ToArray();
  205. }
  206. # region 辅助方法
  207. /// <summary>
  208. /// 创建上下文
  209. /// </summary>
  210. /// <returns></returns>
  211. private static object CreateContextObj(string OrgID)
  212. {
  213. // 实例化应用上下文对象
  214. object thContext = new object();
  215. System.Collections.Generic.Dictionary<object, object> ns = new Dictionary<object, object>();
  216. ns.Add("OrgID", OrgID);
  217. ns.Add("UserID", ConfigurationManager.AppSettings["userID"].ToString());
  218. ns.Add("UserCode", ConfigurationManager.AppSettings["userCode"].ToString());
  219. ns.Add("Datetime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  220. ns.Add("CultureName", ConfigurationManager.AppSettings["cultureName"].ToString());
  221. ns.Add("EnterpriseID", ConfigurationManager.AppSettings["enterpriseID"].ToString());
  222. ns.Add("DefaultCultureName", ConfigurationManager.AppSettings["cultureName"].ToString());
  223. thContext = ns;
  224. return thContext;
  225. }
  226. # endregion
  227. #region 提取异常信息
  228. /// <summary>
  229. /// 提取异常信息
  230. /// </summary>
  231. /// <param name="ex"></param>
  232. private static string GetExceptionMessage(Exception ex)
  233. {
  234. string faultMessage = "未知错误,请查看ERP日志!";
  235. System.TimeoutException timeoutEx = ex as System.TimeoutException;
  236. if (timeoutEx != null)
  237. {
  238. faultMessage = "访问服务超时,请修改配置信息!";
  239. }
  240. else
  241. {
  242. FaultException<ServiceException> faultEx = ex as FaultException<ServiceException>;
  243. if (faultEx == null)
  244. {
  245. faultMessage = ex.Message;
  246. }
  247. else
  248. {
  249. ServiceException serviceEx = faultEx.Detail;
  250. if (serviceEx != null && !string.IsNullOrEmpty(serviceEx.Message)
  251. && !serviceEx.Message.Equals("fault", StringComparison.OrdinalIgnoreCase))
  252. {
  253. // 错误信息在faultEx.Message中,请提取,
  254. // 格式为"Fault:料品不能为空,请录入\n 在....."
  255. int startIndex = serviceEx.Message.IndexOf(":");
  256. int endIndex = serviceEx.Message.IndexOf("\n");
  257. if (endIndex == -1)
  258. endIndex = serviceEx.Message.Length;
  259. if (endIndex > 0 && endIndex > startIndex + 1)
  260. {
  261. faultMessage = serviceEx.Message.Substring(startIndex + 1, endIndex - startIndex - 1);
  262. }
  263. else
  264. {
  265. faultMessage = serviceEx.Message;
  266. }
  267. }
  268. }
  269. }
  270. return faultMessage;
  271. }
  272. #endregion
  273. public class OperationResult
  274. {
  275. /// <summary>
  276. /// 结果状态,默认值false
  277. /// </summary>
  278. public bool IsSuccess { get; set; }
  279. /// <summary>
  280. ///处理消息
  281. /// </summary>
  282. public string MESSAGE { get; set; }
  283. /// <summary>
  284. /// 输出领料订单
  285. /// </summary>
  286. public string LLCode { get; set; }
  287. }
  288. public class IssueData
  289. {
  290. public string MOCODE { get; set; }
  291. public string DOCNO { get; set; }
  292. public List<IssueDatas> linelist { get; set; }
  293. }
  294. public class IssueDatas
  295. {
  296. public string ORGCODE { get; set; }
  297. public string DOCLINENO { get; set; }
  298. public string ITEM { get; set; }
  299. public string UOM { get; set; }
  300. public string Project { get; set; }
  301. public string WH { get; set; }
  302. public decimal ActualReqQty { get; set; }
  303. public decimal STDReqQty { get; set; }
  304. }
  305. }
  306. }