public static bool SaveRptDesign(RptDesign rpt) { try { if (rpt == null) { return(false); } var flag = 0L; using (var conn = new MySqlConnection(CommonConst.ConStr)) { var d = conn.Query <RptDesign> ("select * from rpt_design where RptName=@RptName", new { RptName = rpt.RptName }).FirstOrDefault(); if (d == null) { rpt.Id = Guid.NewGuid().ToString("N"); flag = conn.Insert(rpt); return(flag >= 0); } else { var oldRptName = d.RptName; d.RptName = rpt.RptName; //d.UploadPerson=rpt.UploadPerson; var dirPath = GetRptDirectoryById(d.DirId); var result = conn.Update(d); if (result) { rpt.Id = d.Id; } if (result && !string.IsNullOrEmpty(dirPath.DirPath)) { var path = Path.Combine(ProjectPathClass.UserCustomPath, dirPath.DirPath.TrimStart(Path.DirectorySeparatorChar)); var file = Path.Combine(path, string.Format("{0}.rpt", oldRptName)); var target = Path.Combine(path, string.Format("{0}.rpt", d.RptName)); if (File.Exists(file)) { File.Move(file, target); } } return(result); } } } catch (Exception ex) { log.Error(string.Format("message:{0},StackTrace:{1}", ex.Message, ex.StackTrace)); return(false); } }
/// <summary> /// 扫描ProjectPathClass.UserCustomPath 目录 ,将未在数据库中的文件夹,放入数据库 /// </summary> public static void DealWithDirs() { try { var isSyncWhenStart = ConfigurationManager.AppSettings.Get("isSyncWhenStart"); if (isSyncWhenStart != "1") { return; } var userCustomPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UserCustom"); var dirs = Directory.GetDirectories(userCustomPath, "*", SearchOption.AllDirectories); var tempDirs = new List <string> (); //文件夹 与数据库同步 if (dirs != null) { foreach (var d in dirs) { var relativePath = d.Replace(userCustomPath, ""); tempDirs.Add(relativePath); var subDirs = relativePath.Split(new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); if (subDirs == null || subDirs.Length == 0) { continue; } List <string> subs = new List <string> (subDirs); for (var i = 0; i < subDirs.Length; i++) { var p = Path.Combine(subs.ToArray()); if (!p.StartsWith(Path.DirectorySeparatorChar.ToString())) { p = Path.Combine(Path.DirectorySeparatorChar.ToString(), p); } //var time = DateTime.Now.ToString ("yyyyMMddHHmmss"); RptDb.InsertDir(new RptDirectory() { DirName = subs [subs.Count - 1], DirPath = p, //CreateTime = time, //LastModifyTime = time, LastModifyUser = "******" // }); subs.RemoveAt(subs.Count - 1); } } var dbDirs = RptDb.GetAllRptDirectory(); if (dbDirs != null) { foreach (var dbDir in dbDirs) { if (dbDir.DirName == "$") { continue; } var dir = tempDirs.FirstOrDefault((d) => d == dbDir.DirPath); if (dir == null) { RptDb.DeleteRptDirectory(dbDir); } } } } // 根目录 var rootList = RptDb.GetRptDirectoryByName("$"); if (rootList == null || rootList.Count == 0) { RptDb.SaveDir(new RptDirectory() { DirName = "$", DirPath = Path.DirectorySeparatorChar.ToString(), //CreateTime = time, //LastModifyTime = time, LastModifyUser = "******" // }); } var files = Directory.GetFiles(userCustomPath, "*.rpt", SearchOption.AllDirectories); var dbfiles = RptDb.GetAllRptDesign(); if (files != null) { foreach (var file in files) { var fileName = Path.GetFileNameWithoutExtension(file); if (string.IsNullOrEmpty(fileName)) { continue; } var design = RptDb.GetRptDesignByName(fileName); if (design == null) { var fileDir = Path.GetDirectoryName(file); var dir = fileDir.Replace(userCustomPath, "").Replace(Path.DirectorySeparatorChar, '$'); if (string.IsNullOrEmpty(dir)) { dir = "$"; } var rptDirs = RptDb.GetRptDirectoryByName(dir); var rptDir = rptDirs == null ? null : rptDirs.FirstOrDefault(); var rtpDesign = new RptDesign() { DirId = rptDir == null ? string.Empty : rptDir.Id, RptName = fileName, UploadPerson = "system", //文件是复制过来的 UploadTime = DateTime.Now.ToString("yyyyMMddHHmmss") }; var flag = RptDb.SaveRptDesign(rtpDesign); if (flag) { RptDb.SaveRptRuntime(new RptRuntime() { RptId = rtpDesign.Id }); } } else { } } var fileIds = new List <string> (); if (dbfiles != null) { foreach (var dbfile in dbfiles) { var file = files.FirstOrDefault(f => Path.GetFileNameWithoutExtension(f) == dbfile.RptName); if (file == null) { fileIds.Add(dbfile.Id); } } if (fileIds.Count > 0) { RptDb.DeleteRptDesign(fileIds); } } } log.Info("DealWithDirs Ok.."); } catch (IOException ex) { log.Error(string.Format("message:{0},StackTrace:{1}", ex.Message, ex.StackTrace)); //return false; } }
public RptManageModule() { CurrentPrjInfo.CurrentEnvironment = MESEnvironment.MESReportServer; ProjectPathClass.ProjectPath = AppDomain.CurrentDomain.BaseDirectory; Post ["/dirs"] = _ => { try { if (this.Request.Body.Length <= 0) { return(string.Empty); } byte[] bs = new byte[this.Request.Body.Length]; this.Request.Body.Read(bs, 0, bs.Length); var json = System.Text.Encoding.Default.GetString(bs); log.InfoFormat("/dirs--request:json:{0}", json); var obj = JsonConvert.DeserializeObject <Dictionary <string, string> > (json); if (obj == null) { string str = GetResult(1, "请求参数为空"); return(this.Response.AsText(str)); } var id = string.Empty; var dirName = string.Empty; var parent = string.Empty; var flag = string.Empty; if (!obj.TryGetValue("id", out id)) { id = string.Empty; } if (!obj.TryGetValue("name", out dirName)) { dirName = string.Empty; } if (!obj.TryGetValue("parent", out parent)) { parent = "$"; } if (!obj.TryGetValue("flag", out flag)) { flag = "0"; } if (!string.IsNullOrEmpty(parent)) { parent = parent.Replace('$', Path.DirectorySeparatorChar); } var dirPath = Path.Combine(parent, dirName); if (flag == null) { string str = GetResult(1, "请求标志flag为空"); return(this.Response.AsText(str)); } List <dynamic> files = null; dynamic res = new ExpandoObject(); if (flag == "0") //string isDir = this.Request.Query.isdir; { string dir = string.Empty; //log.InfoFormat ("dirs-json:{0}", json); if (dirPath == null || string.IsNullOrEmpty(dirPath) || string.IsNullOrWhiteSpace(dirPath) || dirPath == "$") { dir = ProjectPathClass.UserCustomPath; } else { dir = Path.Combine(ProjectPathClass.UserCustomPath, dirPath.TrimStart(Path.DirectorySeparatorChar)); } files = GetDirs(dir); var dbDirs = RptDb.GetAllRptDirectory(); if (files != null && files.Count > 0 && dbDirs != null && dbDirs.Count > 0) { foreach (var d in files) { d.Id = string.Empty; d.CreateTime = string.Empty; var dbdir = dbDirs.FirstOrDefault(dd => { //var dPath = Path.Combine (ProjectPathClass.UserCustomPath, dd.DirPath.TrimStart (Path.DirectorySeparatorChar)); //log.DebugFormat("==dPath:{0},d.path:{1},DirName:{2},d.Name:{3}", dd.DirPath, d.Path, dd.DirName, d.Name); return(dd.DirName == d.Name && dd.DirPath == d.Path); }); if (dbdir != null) { d.Id = dbdir.Id; d.CreateTime = dbdir.CreateTime; } } List <dynamic> results = new List <dynamic> (); if (dir == ProjectPathClass.UserCustomPath) { var root = dbDirs.FirstOrDefault((direct) => direct.DirName == "$"); if (root != null) { dynamic rptRoot = new ExpandoObject(); rptRoot.Name = root.DirName; rptRoot.HasChild = 1; rptRoot.Id = root.Id; results.Add(rptRoot); } } files.Sort((x, y) => string.Compare(x.CreateTime, y.CreateTime)); foreach (var file in files) { dynamic rptPath = new ExpandoObject(); rptPath.Name = file.Name; rptPath.HasChild = file.HasChild; rptPath.Id = file.Id; results.Add(rptPath); } res.list = results; } } else if (flag == "1") // 设计时 { if (string.IsNullOrEmpty(id)) { string str = GetResult(1, "请求文件id为空"); return(this.Response.AsText(str)); } var list = RptDb.GetRptDesignByDirId(id); res.list = list; } else if (flag == "2") // 运行时 { if (string.IsNullOrEmpty(id)) { string str = GetResult(1, "请求文件id为空"); return(this.Response.AsText(str)); } var designList = RptDb.GetRptDesignByDirId(id); var list = RptDb.GetRptRuntimeByDirId(id); if (designList == null || designList.Count == 0 || list == null || list.Count == 0) { string str = GetResult(0, res); return(this.Response.AsText(str)); } List <dynamic> runList = new List <dynamic> (); foreach (var run in list) { dynamic runtime = new ExpandoObject(); runtime.Id = run.Id; runtime.QueryCount = run.QueryCount; runtime.ExportCount = run.ExportCount; runtime.LastQeuryPerson = run.LastQeuryPerson; DateTime dt = DateTime.MinValue; if (DateTime.TryParseExact(run.LastQueryTime, "yyyyMMddHHmmss", null, System.Globalization.DateTimeStyles.AssumeLocal, out dt)) { runtime.LastQueryTime = dt.ToString("yyyy-MM-dd HH:mm:ss"); } runtime.RptId = run.Id; var design = designList.FirstOrDefault(d => d.Id == run.RptId); if (design != null) { runtime.RptName = design.RptName; } runList.Add(runtime); } res.list = runList; } string result = GetResult(0, res); return(this.Response.AsText(result)); } catch (Exception ex) { Message.Info(string.Format("message:{0},StackTrace:{1}", ex.Message, ex.StackTrace)); string str = GetResult(1, ex.Message); return(this.Response.AsText(str)); } }; // Get ["/rptruntime/dirid"] = _ => { // // }; // // Get ["/rptdesign/dirid"] = _ => { // // }; Post ["/rptdesign/del"] = _ => { try { if (this.Request.Body.Length <= 0) { return(string.Empty); } byte[] bs = new byte[this.Request.Body.Length]; this.Request.Body.Read(bs, 0, bs.Length); var json = System.Text.Encoding.Default.GetString(bs); log.InfoFormat("/rptdesign/del--request:json:{0}", json); var obj = JsonConvert.DeserializeObject <Dictionary <string, List <string> > > (json); if (obj == null) { string str = GetResult(1, "请求参数为空"); return(this.Response.AsText(str)); } List <string> ids = null; if (!obj.TryGetValue("ids", out ids)) { string str = GetResult(1, "没有要删除的rptid."); return(this.Response.AsText(str)); } var designs = RptDb.GetRptDesignByIds(ids); var flag = RptDb.DeleteRptDesign(ids); if (flag == true) { foreach (var design in designs) { var dir = RptDb.GetRptDirectoryById(design.DirId); if (dir == null) { continue; } var file = Path.Combine(ProjectPathClass.UserCustomPath, dir.DirPath.TrimStart(Path.DirectorySeparatorChar), string.Format("{0}.rpt", design.RptName)); if (File.Exists(file)) { File.Delete(file); } } string str = GetResult(0, "删除成功"); return(this.Response.AsText(str)); } else { string str = GetResult(1, "删除失败"); return(this.Response.AsText(str)); } } catch (Exception ex) { Message.Info(string.Format("message:{0},StackTrace:{1}", ex.Message, ex.StackTrace)); string str = GetResult(1, ex.Message); return(this.Response.AsText(str)); } }; Post ["/dir/create"] = _ => { try { if (this.Request.Body.Length <= 0) { return(string.Empty); } byte[] bs = new byte[this.Request.Body.Length]; this.Request.Body.Read(bs, 0, bs.Length); var json = System.Text.Encoding.Default.GetString(bs); log.InfoFormat("/dir/create--request:json:{0}", json); var obj = JsonConvert.DeserializeObject <Dictionary <string, string> > (json); if (obj == null) { string str = GetResult(1, "请求参数为空"); return(this.Response.AsText(str)); } var dir = new RptDirectory(); //dir.Id = Guid.NewGuid ().ToString ("N"); var name = string.Empty; if (obj.TryGetValue("name", out name)) { dir.DirName = name; } //dir.DirName = obj ["name"]; var parent = string.Empty; if (!obj.TryGetValue("parent", out parent)) { parent = "$"; } if (!string.IsNullOrEmpty(parent)) { parent = parent.Replace('$', Path.DirectorySeparatorChar); } dir.DirPath = Path.Combine(parent, dir.DirName); string user = string.Empty; if (obj.TryGetValue("user", out user)) { dir.LastModifyUser = user; } //dir.LastModifyUser = obj ["user"]; var dirPath = Path.Combine(ProjectPathClass.UserCustomPath, dir.DirPath.TrimStart(Path.DirectorySeparatorChar)); var flag = RptDb.SaveDir(dir); dynamic res = new ExpandoObject(); if (flag == true) { if (Directory.Exists(dirPath)) { Directory.Delete(dirPath); } Directory.CreateDirectory(dirPath); res.id = dir.Id; res.msg = "创建成功"; string str = GetResult(0, res); return(this.Response.AsText(str)); } else { res.id = string.Empty; res.msg = "创建失败"; if (Directory.Exists(dirPath)) { Directory.Delete(dirPath); } string str = GetResult(1, res); return(this.Response.AsText(str)); } } catch (Exception ex) { Message.Info(string.Format("/dir/create--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace)); string str = GetResult(1, ex.Message); return(this.Response.AsText(str)); } }; Post ["/dir/edit"] = _ => { try { if (this.Request.Body.Length <= 0) { return(string.Empty); } byte[] bs = new byte[this.Request.Body.Length]; this.Request.Body.Read(bs, 0, bs.Length); var json = System.Text.Encoding.Default.GetString(bs); log.InfoFormat("/dir/edit--request:json:{0}", json); var obj = JsonConvert.DeserializeObject <Dictionary <string, string> > (json); if (obj == null) { string str = GetResult(1, "请求参数为空"); return(this.Response.AsText(str)); } var dir = new RptDirectory(); string id = string.Empty; if (obj.TryGetValue("id", out id)) { dir.Id = id; } string name = string.Empty; if (obj.TryGetValue("name", out name)) { dir.DirName = name; } var parent = string.Empty; if (!obj.TryGetValue("parent", out parent)) { parent = "$"; } if (!string.IsNullOrEmpty(parent)) { parent = parent.Replace('$', Path.DirectorySeparatorChar); } dir.DirPath = Path.Combine(parent, dir.DirName); string user = string.Empty; if (obj.TryGetValue("user", out user)) { dir.LastModifyUser = user; } var rptDir = RptDb.GetRptDirectoryById(dir.Id); if (rptDir == null) { string str = GetResult(1, "没有"); return(this.Response.AsText(str)); } var tempDirPath = dir.DirPath; var flag = RptDb.SaveDir(dir); if (flag == true) { var dirPath = Path.Combine(ProjectPathClass.UserCustomPath, rptDir.DirPath.TrimStart(Path.DirectorySeparatorChar)); var targetPath = Path.Combine(ProjectPathClass.UserCustomPath, tempDirPath.TrimStart(Path.DirectorySeparatorChar)); if (Directory.Exists(dirPath)) { Directory.Move(dirPath, targetPath); } string str = GetResult(0, "修改成功"); return(this.Response.AsText(str)); } else { string str = GetResult(1, "修改失败"); return(this.Response.AsText(str)); } } catch (Exception ex) { Message.Info(string.Format("/dir/edit--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace)); string str = GetResult(1, ex.Message); return(this.Response.AsText(str)); } }; Post ["/dir/del"] = _ => { try { if (this.Request.Body.Length <= 0) { return(string.Empty); } byte[] bs = new byte[this.Request.Body.Length]; this.Request.Body.Read(bs, 0, bs.Length); var json = System.Text.Encoding.Default.GetString(bs); log.InfoFormat("/dir/del--request:json:{0}", json); var obj = JsonConvert.DeserializeObject <Dictionary <string, string> > (json); if (obj == null) { string str = GetResult(1, "请求参数为空"); return(this.Response.AsText(str)); } string id = string.Empty; if (!obj.TryGetValue("id", out id)) { string str = GetResult(1, "要删除的文件夹id为空"); return(this.Response.AsText(str)); } var dir = RptDb.GetRptDirectoryById(id); if (dir == null) { string str = GetResult(1, "没有要删除的文件夹"); return(this.Response.AsText(str)); } if (!string.IsNullOrEmpty(dir.DirPath)) { dir.DirPath = dir.DirPath.Replace('$', Path.DirectorySeparatorChar); } var flag = RptDb.DeleteRptDirectory(dir); if (flag == true) { var dirPath = Path.Combine(ProjectPathClass.UserCustomPath, dir.DirPath.TrimStart(Path.DirectorySeparatorChar)); if (Directory.Exists(dirPath)) { Directory.Delete(dirPath, true); } string str = GetResult(0, "删除成功"); return(this.Response.AsText(str)); } else { string str = GetResult(1, "删除失败"); return(this.Response.AsText(str)); } } catch (Exception ex) { Message.Info(string.Format("/dir/del--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace)); string str = GetResult(1, ex.Message); return(this.Response.AsText(str)); } }; Post ["/upload"] = _ => { try { string user = this.Request.Form.user; string path = this.Request.Form.path; string dirId = this.Request.Form.dirId; log.InfoFormat("/upload--request:Form-user:{0},path:{1},dirId:{2}", user ?? string.Empty, path ?? string.Empty, dirId ?? string.Empty); if (string.IsNullOrEmpty(dirId)) { string str = GetResult(1, "文件夹Id为空"); return(this.Response.AsText(str)); } if (!string.IsNullOrEmpty(path)) { path = path.Replace('$', Path.DirectorySeparatorChar); } if (string.IsNullOrEmpty(path)) { path = ProjectPathClass.UserCustomPath; } else { path = Path.Combine(ProjectPathClass.UserCustomPath, path.TrimStart(Path.DirectorySeparatorChar)); } if (this.Request.Files.Count() == 0) { string ss = GetResult(0, "没有要上传的文件"); return(this.Response.AsText(ss)); } List <string> existsFiles = new List <string> (); foreach (var file in this.Request.Files) { var filename = Path.Combine(path, file.Name); using (FileStream fileStream = new FileStream(filename, FileMode.Create)) { var design = new RptDesign() { RptName = Path.GetFileNameWithoutExtension(filename), UploadPerson = user, UploadTime = DateTime.Now.ToString("yyyyMMddHHmmss"), DirId = dirId }; if (RptDb.ExistsRptDesignByName(design.RptName)) { existsFiles.Add(file.Name); continue; } file.Value.CopyTo(fileStream); var flag = RptDb.SaveRptDesign(design); if (flag) { RptDb.SaveRptRuntime(new RptRuntime() { RptId = design.Id, QueryCount = 0, ExportCount = 0 }); } } } if (existsFiles.Count == 0) { string ss = GetResult(0, "上传成功"); return(this.Response.AsText(ss)); } else { dynamic result = new ExpandoObject(); result.msg = "上传失败:要上传的文件在服务器上已经有同名文件,请修改后再上传"; result.list = existsFiles; string ss = GetResult(1, result); return(this.Response.AsText(ss)); } } catch (Exception ex) { Message.Info(string.Format("/upload--message:{0},StackTrace:{1}", ex.Message, ex.StackTrace)); string str = GetResult(1, ex.Message); return(this.Response.AsText(str)); } }; }