using ICSSoft.Common; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; using ICSSoft.Entity; using UFSoft.UBF.Util.Context; using System.ServiceModel; using UFSoft.UBF.Service; using UFSoft.UBF.Exceptions; using www.ufida.org.EntityData; using ICSSoft.APICreateLotMaster; using ICSSoft.APIApproveIssue; using ICSSoft.APIDeleteIssue; using ICSSoft.ASKDataProject; namespace ICSSoft.APICreateIssue { public class CreateIssue { private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"]; private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"]; public string Create(List infos) { DataSet ds = null; string msg = ""; ThreadContext context = new ThreadContext(); try { string _MOCode = string.Empty; UFIDAU9ISVMOICreatRecedeIssueDocSVClient client = new UFIDAU9ISVMOICreatRecedeIssueDocSVClient(); MessageBase[] message = null; List molist = new List(); UFIDAU9ISVMORecedeItemAndSnDTOData mo = new UFIDAU9ISVMORecedeItemAndSnDTOData(); UFIDAU9CBOPubControllerCommonArchiveDataDTOData docType = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData(); string OrgID = ""; string orgsql = @"select ID from Base_Organization where Code='{0}'"; orgsql = string.Format(orgsql, infos[0].WorkPoint); DataTable orgdt = DBHelper.Query(orgsql, connString); if (orgdt.Rows.Count != 0) { OrgID = orgdt.Rows[0]["ID"].ToString(); } else { throw new Exception("组织编码:" + infos[0].WorkPoint + "不存在!"); } foreach (ICSMOIssueDocNegative model in infos) { if (!string.IsNullOrWhiteSpace(model.SourceCode)) { string LLID = ""; string llsql = @"select ID from MO_IssueDoc where DocNo='{0}' And Org='{1}'"; llsql = string.Format(llsql, model.SourceCode, OrgID); DataTable lldt = DBHelper.Query(llsql, connString); if (lldt.Rows.Count > 0) { LLID = lldt.Rows[0]["ID"].ToString(); } else { throw new Exception("领料单:" + model.SourceCode + "不存在!"); } #region 遍历入参LL单行 foreach (var item in model.details) { mo = new UFIDAU9ISVMORecedeItemAndSnDTOData(); long lllineID = 0; llsql = @"select ID,IsIssued from MO_IssueDocLine where ID='{0}'"; llsql = string.Format(llsql, item.SourceDetailID); lldt = DBHelper.Query(llsql, connString); if (lldt.Rows.Count > 0) { if (lldt.Rows[0]["IsIssued"].ToString() != "True" && lldt.Rows[0]["IsIssued"].ToString() != "true") { throw new Exception("领料单:'" + model.SourceCode + "'对应领料单行:'" + item.Sequence + "'未进行发料确认,不能退料!"); } else { lllineID = long.Parse(lldt.Rows[0]["ID"].ToString()); } } else { throw new Exception("领料单:'" + model.SourceCode + "'对应领料单行:'" + item.Sequence + "'不存在!"); } mo.m_issueDocLine = lllineID; string strCcode = item.InvCode; //子料 string strWhCode = string.Empty; //仓库 string _strWhID = string.Empty;//仓库ID if (!string.IsNullOrWhiteSpace(strCcode)) { string sqlWH = @"select ID,Code from CBO_Wh WHERE Code='" + model.WHCode + "' AND Org='" + OrgID + "'"; sqlWH = string.Format(sqlWH); DataTable _dtWH = DBHelper.Query(sqlWH, connString); if (_dtWH.Rows.Count == 0) { throw new Exception("没有仓库信:" + model.WHCode + "信息!"); } else { strWhCode = _dtWH.Rows[0]["Code"].ToString(); _strWhID = _dtWH.Rows[0]["ID"].ToString(); mo.m_whCode = strWhCode; mo.m_wh = long.Parse(_strWhID); } } else { throw new Exception("没有子料信息"); } string sqlINV = @"select ID,Code from CBO_ItemMaster WHERE Code='" + item.InvCode + "' AND Org='" + OrgID + "'"; sqlINV = string.Format(sqlINV); DataTable _dtINV = DBHelper.Query(sqlINV, connString); if (_dtINV.Rows.Count == 0) { throw new Exception("料品编码:" + item.InvCode + "不存在!"); } mo.m_recedeQty = item.Quantity; mo.m_itemCode = item.InvCode; mo.m_recedeReason = 3; #region 创建批号接口调用 string LotID = ""; if (item.BatchCode != "") { CreateLotMaster LotMasterCreate = new CreateLotMaster(); string lotWHCode = model.WHCode; string lotItemCode = item.InvCode; string LotCode = item.BatchCode; LotMasterCreate.Create(lotWHCode, lotItemCode, LotCode, OrgID, ref LotID); if (LotID != "") { mo.m_lotMaster = long.Parse(LotID); mo.m_lotNo = item.BatchCode; } } else { mo.m_lotNo = ""; } #endregion molist.Add(mo); } #endregion } else { throw new Exception("传入领料单号为空!"); } docType.m_code = "2"; } context = CreateContextObj(OrgID); string[] _result = null; _result = client.Do(out message, context, molist.ToArray(), false, docType); List LLCodeList = new List(); string DocNOList = ""; if (_result != null && _result.Count() > 0) { for (int i = 0; i < _result.Count(); i++) { LLCodeList.Add(_result[i].ToString()); if (DocNOList == "") { DocNOList = "'" + _result[i].ToString() + "'"; } else { DocNOList += ",'" + _result[i].ToString() + "'"; } } } if (LLCodeList != null) { ApproveIssue sh = new ApproveIssue(); bool approveresult = sh.Approve(LLCodeList, infos[0].WorkPoint); if (approveresult == true) { #region 查询 string sql = @"select DISTINCT a.ID as ID,a.ID as IDs,e.DocNo as SourceCode, a.DocNo as IssueNEGCode,f.Code AS cDepCode,g.Name AS cDepName,h.Code AS cWhCode ,i.Name AS cWhName,'' as SourceType,a.CreatedBy as CreateUser,a.CreatedOn as CreateDateTime, a.ApproveBy as Checker,a.ApproveOn as CheckDateTime from MO_IssueDoc a left join MO_IssueDocLine c on c.IssueDoc=a.ID left join MO_IssueDocLine d on d.ID=c.SourceIssueDocLine left join MO_IssueDoc e on e.ID=d.IssueDoc left join CBO_Department f on f.ID=c.IssueDept left join CBO_Department_Trl g on g.ID=f.ID left join CBO_Wh h on h.ID=c.Wh left join CBO_Wh_Trl i on i.ID=h.ID WHERE a.DocNo in ({0}) AND a.Org='{1}'"; sql = string.Format(sql, DocNOList, OrgID); DataTable mdt= DBHelper.Query(sql, connString); sql = @"select DISTINCT a.ID as IDs,b.ID as DetailID,b.LineNum as Sequence,c.Code as InvCode ,b.IssuedQty as Quantity,b.SourceIssueDocLine as SourceDetailID,0 as Amount ,isnull(d.Code,'') ProjectCode,isnull(e.Code,'') cBatch,'' version ,'' brand, '' as cFree1, '' as cFree2, '' as cFree3, '' as cFree4, '' as cFree5, '' as cFree6, '' as cFree7, '' as cFree8, '' as cFree9, '' as cFree10 from MO_IssueDoc a left join MO_IssueDocLine b on b.IssueDoc=a.ID left join CBO_ItemMaster c on c.ID=b.Item left join CBO_Project d on d.ID=b.Project left join Lot_LotMaster e on e.ID=b.LotMaster WHERE a.DocNo in ({0}) AND a.Org='{1}'"; sql = string.Format(sql, DocNOList, OrgID); DataTable cdt = DBHelper.Query(sql, connString); DataTable tempdt1 = mdt.Copy(); tempdt1.TableName = "tempdt1"; ds.Tables.Add(tempdt1); DataTable tempdt2 = cdt.Copy(); tempdt2.TableName = "tempdt2"; ds.Tables.Add(tempdt2); #endregion } else { DeleteIssue issuedel = new DeleteIssue(); issuedel.Delete(LLCodeList, OrgID); throw new Exception("退料单审核失败,创建的退料单已删除!"); } } } catch (Exception ex) { throw new Exception(GetExceptionMessage(ex)); } msg = JSON.DataSetToJson(ds, "details", "IDs"); return msg; } #region 给上下文信息赋值 /// /// 给上下文信息赋值 /// /// private static ThreadContext CreateContextObj(string OrgID) { // 实例化应用上下文对象 ThreadContext thContext = new ThreadContext(); System.Collections.Generic.Dictionary ns = new Dictionary(); ns.Add("OrgID", OrgID); ns.Add("UserID", ConfigurationManager.AppSettings["userID"].ToString()); ns.Add("UserCode", ConfigurationManager.AppSettings["userCode"].ToString()); ns.Add("Datetime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); ns.Add("CultureName", ConfigurationManager.AppSettings["cultureName"].ToString()); ns.Add("EnterpriseID", ConfigurationManager.AppSettings["enterpriseID"].ToString()); ns.Add("DefaultCultureName", ConfigurationManager.AppSettings["cultureName"].ToString()); thContext.nameValueHas = ns; return thContext; } #endregion #region 提取异常信息 /// /// 提取异常信息 /// /// private static string GetExceptionMessage(Exception ex) { string faultMessage = "未知错误,请查看ERP日志!"; System.TimeoutException timeoutEx = ex as System.TimeoutException; if (timeoutEx != null) { faultMessage = "访问服务超时,请修改配置信息!"; } else { FaultException faultEx = ex as FaultException; if (faultEx == null) { faultMessage = ex.Message; } else { ServiceException serviceEx = faultEx.Detail; if (serviceEx != null && !string.IsNullOrEmpty(serviceEx.Message) && !serviceEx.Message.Equals("fault", StringComparison.OrdinalIgnoreCase)) { // 错误信息在faultEx.Message中,请提取, // 格式为"Fault:料品不能为空,请录入\n 在....." int startIndex = serviceEx.Message.IndexOf(":"); int endIndex = serviceEx.Message.IndexOf("\n"); if (endIndex == -1) endIndex = serviceEx.Message.Length; if (endIndex > 0 && endIndex > startIndex + 1) { faultMessage = serviceEx.Message.Substring(startIndex + 1, endIndex - startIndex - 1); } else { faultMessage = serviceEx.Message; } } } } return faultMessage; } #endregion } }