public void AddApplicationBody(ApplicationDoc doc, Func <Application, CurrentUser, bool> privilege) { if (!PdfHelper.Check(doc.ApplicationId, doc.RelativeURL)) { throw new OtherException("申请书正文格式错误"); } //保存到数据库 using (var ctx = new AspodesDB()) { var application = ctx.Applications.FirstOrDefault(a => a.ApplicationId == doc.ApplicationId); if (null == application) { throw new NotFoundException("未找到申请书"); } if (!privilege(application, UserHelper.GetCurrentUser())) { throw new UnauthorizationException(); } var pre = ctx.ApplicationDocs.Where(ad => ad.ApplicationId == doc.ApplicationId && ad.Type == doc.Type); //申请书正文只有一份,附件可以有多份 ctx.ApplicationDocs.Add(doc); ctx.ApplicationDocs.RemoveRange(pre); ctx.SaveChanges(); } }
/// <summary> /// 下载申请书的PDF文档 /// </summary> /// <param name="applicationId"></param> /// <returns></returns> public HttpResponseMessage DownloadApplicationPDFDoc(string applicationId) { ApplicationDoc pdf = null; using (var ctx = new AspodesDB()) { pdf = ctx.ApplicationDocs.FirstOrDefault(ad => ad.ApplicationId == applicationId && ad.Type == ApplicationDocType.PDF); } if (pdf == null) { throw new NotFoundException("未找到需要下载的文档"); } return(FileHelper.Download(HttpContext.Current, pdf.RelativeURL, pdf.Name)); }
/// <summary> /// 根据申请书文档的ID下载文档 /// </summary> /// <param name="applicationDocId">文档ID</param> /// <returns> /// 成功,返回ResponseStatus.success和文件流 /// 未找到文档,返回ResponseStatus.parameter_error /// 失败,返回ResponseStatus.unkown_error,和错误信息 /// </returns> public HttpResponseMessage DownloadApplicationDoc(int?applicationDocId) { ApplicationDoc doc = null; using (var ctx = new AspodesDB()) { doc = ctx.ApplicationDocs.FirstOrDefault(ad => ad.ApplicationDocId == applicationDocId); } if (doc == null) { throw new NotFoundException("未找到需要下载的文档"); } return(FileHelper.Download(HttpContext.Current, doc.RelativeURL, doc.Name)); }
/// <summary> /// 保存申请书,主要操作是生成申请书PDF文档,将申请书的状态修改为ApplicationStatus.NEW /// </summary> /// <param name="applicationId">申请书ID</param> /// <returns> /// 成功,返回ResponseStatus.success, /// 没有找申请书,返回ResponseStatus.parameter_error, /// 出错,返回ResponseStatus.unknown_error和错误信息 /// </returns> public GetApplicationDTO SaveApplication(string applicationId, Func <Application, CurrentUser, bool> privilege) { using (var ctx = new AspodesDB()) { //取出申请书信息 Application application = ctx.Applications.FirstOrDefault(a => a.ApplicationId == applicationId); if (null == application) { throw new NotFoundException("未找到申请书"); } if (!privilege(application, UserHelper.GetCurrentUser())) { throw new UnauthorizationException(); } //生成申请书PDF文档 var body = ctx.ApplicationDocs.FirstOrDefault(ad => ad.ApplicationId == application.ApplicationId && ad.Type == ApplicationDocType.BODY); var oldPdf = ctx.ApplicationDocs.FirstOrDefault(ad => ad.ApplicationId == application.ApplicationId && ad.Type == ApplicationDocType.PDF); if (null != oldPdf) { ApplicationDocRepository.Delete(oldPdf.ApplicationDocId.Value, (a, u) => true); } var pdfName = body.Name.Replace(".docx", "") + "PDF"; bool createPDF = PdfHelper.CreatePdf(application.ApplicationId, body.RelativeURL, pdfName, SystemConfig.ApplicationStartYear); if (!createPDF) { throw new OtherException("PDF生成失败"); } //添加PDF文档记录 ApplicationDoc pdf = new ApplicationDoc() { Name = pdfName + ".pdf", RelativeURL = body.RelativeURL.Replace(body.Name, pdfName + ".pdf"), Type = ApplicationDocType.PDF, ApplicationId = applicationId }; ctx.ApplicationDocs.Add(pdf); //修改申请书状态 application.Status = ApplicationStatus.NEW; ctx.SaveChanges(); return(Mapper.Map <GetApplicationDTO>(application)); } }
public void AddApplicationAttachment(ApplicationDoc doc, Func <Application, CurrentUser, bool> privilege) { using (var ctx = new AspodesDB()) { var application = ctx.Applications.FirstOrDefault(a => a.ApplicationId == doc.ApplicationId); if (null == application) { throw new NotFoundException("未找到申请书"); } if (!privilege(application, UserHelper.GetCurrentUser())) { throw new UnauthorizationException(); } ctx.ApplicationDocs.Add(doc); ctx.SaveChanges(); } }
public int WriteNewApplicationToDB(ApplicationDoc doc) { using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + dbPath)) { connection.Open(); // вставым запись в таблицу var sql = "INSERT INTO " + APPLICATIONS_TABLE.table_name + " (" + APPLICATIONS_TABLE.contragent + "," + APPLICATIONS_TABLE.doc_guid + "," + APPLICATIONS_TABLE.doc_number + "," + APPLICATIONS_TABLE.doc_date + "," + APPLICATIONS_TABLE.uploaded + ") VALUES ('" + doc.contragent + "','" + doc.doc_guid + "','" + doc.doc_number + "','" + doc.doc_date + "', 0)"; using (SQLiteCommand cmd = new SQLiteCommand(sql, connection)) { try { cmd.ExecuteNonQuery(); } catch (Exception) { return(0); } } // прочитаем ее и получим новый id using (SQLiteCommand cmd = new SQLiteCommand(connection)) { cmd.CommandText = "SELECT " + APPLICATIONS_TABLE.id + " FROM " + APPLICATIONS_TABLE.table_name + " WHERE " + APPLICATIONS_TABLE.doc_guid + " = '" + doc.doc_guid + "'"; cmd.CommandType = CommandType.Text; using (SQLiteDataReader r = cmd.ExecuteReader()) { while (r.Read()) { return(Convert.ToInt32(r[APPLICATIONS_TABLE.id])); } } } } return(0); }
public void CreateApplicationPDF(string applicationId) { using (var ctx = new AspodesDB()) { //取出申请书信息 Application application = ctx.Applications.FirstOrDefault(a => a.ApplicationId == applicationId); if (null == application) { throw new NotFoundException("未找到申请书"); } //生成申请书PDF文档 var body = ctx.ApplicationDocs.FirstOrDefault(ad => ad.ApplicationId == application.ApplicationId && ad.Type == ApplicationDocType.BODY); var oldPdf = ctx.ApplicationDocs.FirstOrDefault(ad => ad.ApplicationId == application.ApplicationId && ad.Type == ApplicationDocType.PDF); if (null != oldPdf) { ApplicationDocRepository.Delete(oldPdf.ApplicationDocId.Value, (a, u) => true); } var pdfName = body.Name.Replace(".docx", "") + "PDF"; bool createPDF = PdfHelper.CreatePdf(application.ApplicationId, body.Name, pdfName, SystemConfig.ApplicationStartYear); if (!createPDF) { throw new OtherException("PDF生成失败"); } //添加PDF文档记录 ApplicationDoc pdf = new ApplicationDoc() { Name = pdfName + ".pdf", RelativeURL = body.RelativeURL.Replace(body.Name, pdfName + ".pdf"), Type = ApplicationDocType.PDF, ApplicationId = applicationId }; ctx.ApplicationDocs.Add(pdf); ctx.SaveChanges(); } }