public ProjectFileViewModel(ProjectFilePath path, ShqContext db) { this.Name = System.IO.Path.GetFileName(path.Path); this.Path = path.Path; FileInfo fi = new FileInfo(path.FullPath); this.Size = fi.Length; this.CreatedTime = fi.CreationTime.ToString(); this.LastModfiedTime = fi.LastWriteTime.ToString(); Id = ShqConstants.GetPathId(path.Path); if (Id > 0) { this.Name = System.IO.Path.GetFileNameWithoutExtension(this.Name); var f = db.ProjectFiles.FirstOrDefault(item => item.Id == Id); if (f == null) { this.Name = ""; } else { this.Privilege = f.Privilege; CreatedBy = new ShqUserRequestViewModel(db.ShqUsers.Where(u => u.IdentityUser.Id == db.ProjectFiles.FirstOrDefault(item => item.Id == Id).CreatedById).FirstOrDefault(), db); CreatedTime = f.CreatedTime.ToString(); LastModifiedBy = new ShqUserRequestViewModel(db.ShqUsers.Where(u => u.IdentityUser.Id == db.ProjectFiles.FirstOrDefault(item => item.Id == Id).LastModifiedById).FirstOrDefault(), db); LastModfiedTime = f.LastModfiedTime.ToString(); this.Level = f.Level; } } }
private bool CheckIfParentExistInDb(string parentPath) { if (parentPath.EndsWith("Root") == true) { return(true); } int id = ShqConstants.GetPathId(parentPath); if (id > 0) { return(db.ProjectFiles.FirstOrDefault(item => item.Id == id) != null); } return(false); }
public AuditViewModel GetAudits(int auditsListMaxCount = 30, int auditProjectsMaxcount = 14) { AuditViewModel avm = new AuditViewModel(); DateTime last30Days = DateTime.Now - (new TimeSpan(30, 0, 0, 0)); //var all = db.Audits.Where(item => item.CreatedTime > last30Days); var all = db.Audits.OrderByDescending(item => item.CreatedTime).Take(auditsListMaxCount); List <dynamic> auditsList = new List <dynamic>(); string dbContent = ""; var logins = from user in all where user.Operation == "login" orderby user.CreatedTime descending select user.OperationDetails + "," + user.CreatedTime; //StringBuilder //sb = new StringBuilder(); foreach (var u in logins) { string[] us = u.Split(','); dbContent = string.Format("<strong style='color:blue;'>{0}</strong>用户在<strong style='color:blue;'>{1}</strong>时间登陆了系统<br>", us[0], us[1]); //sb.Append(dbContent); auditsList.Add(new { content = dbContent, date = us[1] }); } //avm.LoginActivities = sb.ToString(); var logouts = from user in all where user.Operation == "logout" orderby user.CreatedTime descending select user.OperationDetails + "," + user.CreatedTime; ////sb = new StringBuilder(); foreach (var u in logouts) { string[] us = u.Split(','); dbContent = string.Format("<strong style='color:blue;'>{0}</strong>用户在<strong style='color:blue;'>{1}</strong>时间登出了系统<br>", us[0], us[1]); //sb.Append(dbContent); auditsList.Add(new { content = dbContent, date = us[1] }); } //avm.LogoutActivities = sb.ToString(); var addUsers = from user in all where user.Operation == "api/ShqUsers/Add" orderby user.CreatedTime descending select user.UserName + "," + user.OperationDetails + "," + user.CreatedTime; //sb = new StringBuilder(); foreach (var u in addUsers) { string[] us = u.Split(','); dbContent = string.Format("<strong style='color:blue;'>{0}</strong>用户在<strong style='color:blue;'>{1}</strong>时间创建了新的用户账号<strong style='color:blue;'>{2}</strong><br>", us[0], us[2], us[1]); //sb.Append(dbContent); auditsList.Add(new { content = dbContent, date = us[2] }); } //avm.UserAddActivities = sb.ToString(); var updateUsers = from user in all where user.Operation == "api/ShqUsers/Update" || user.Operation == "api/ShqUsers/ChangePassword" || user.Operation == "api/ShqUsers/Disable" orderby user.CreatedTime descending select user.UserName + "," + user.OperationDetails + "," + user.CreatedTime; //sb = new StringBuilder(); foreach (var u in updateUsers) { string[] us = u.Split(','); dbContent = string.Format("<strong style='color:blue;'>{0}</strong>用户在<strong style='color:blue;'>{1}</strong>时间修改了<strong style='color:blue;'>{2}</strong><br>", us[0], us[2], us[1]); //sb.Append(dbContent); auditsList.Add(new { content = dbContent, date = us[2] }); } //avm.UserUpdatectivities = sb.ToString(); var addProjects = from user in all where user.Operation == "api/Projects/Add" orderby user.CreatedTime descending select user.UserName + "`" + user.OperationDetails + "`" + user.CreatedTime; //sb = new StringBuilder(); foreach (var u in addProjects) { string[] us = u.Split('`'); var p = JsonConvert.DeserializeObject <ProjectViewModel>(us[1]); dbContent = string.Format("<strong style='color:blue;'>{0}</strong>用户在<strong style='color:blue;'>{1}</strong>时间新建了<strong style='color:blue;'>{2}</strong>类型项目<strong style='color:blue;'>{3}</strong><br>", us[0], us[2], ShqConstants.GetProjectType(p.Type), p.Name); //sb.Append(dbContent); auditsList.Add(new { content = dbContent, date = us[2] }); } //avm.ProjectAddActivities = sb.ToString(); var updateProjects = from user in all where user.Operation == "api/Projects/Update" orderby user.CreatedTime descending select user.UserName + "`" + user.OperationDetails + "`" + user.CreatedTime; //sb = new StringBuilder(); foreach (var u in updateProjects) { string[] us = u.Split('`'); var p = JsonConvert.DeserializeObject <ProjectViewModel>(us[1]); dbContent = string.Format("<strong style='color:blue;'>{0}</strong>用户在<strong style='color:blue;'>{1}</strong>时间编辑了<strong style='color:blue;'>{2}</strong>类型项目<strong style='color:blue;'>{3}</strong><br>", us[0], us[2], ShqConstants.GetProjectType(p.Type), p.Name); //sb.Append(dbContent); auditsList.Add(new { content = dbContent, date = us[2] }); } //avm.ProjectUpdateActivities = sb.ToString(); var updateProjectTrees = from user in all where user.Operation == "api/FTAProjects/AddTree" || user.Operation == "api/FMEAProjects/AddTree" orderby user.CreatedTime descending select user.UserName + "`" + user.OperationDetails + "`" + user.CreatedTime; //sb = new StringBuilder(); foreach (var u in updateProjectTrees) { string[] us = u.Split('`'); var p = JsonConvert.DeserializeObject <ProjectViewModel>(us[1]); dbContent = string.Format("<strong style='color:blue;'>{0}</strong>用户在<strong style='color:blue;'>{1}</strong>时间修改了<strong style='color:blue;'>{2}</strong>类型项目<strong style='color:blue;'>{3}</strong><br>", us[0], us[2], ShqConstants.GetProjectType(p.Type), p.Name); //sb.Append(dbContent); auditsList.Add(new { content = dbContent, date = us[2] }); } //avm.ProjectTreeUpdateActivities = sb.ToString(); var analyzeProjectTrees = from user in all where user.Operation == "api/FTAProjects/AnalyzeTree" orderby user.CreatedTime descending select user.UserName + "`" + user.OperationDetails + "`" + user.CreatedTime; //sb = new StringBuilder(); foreach (var u in analyzeProjectTrees) { string[] us = u.Split('`'); var p = JsonConvert.DeserializeObject <ProjectViewModel>(us[1]); dbContent = string.Format("<strong style='color:blue;'>{0}</strong>用户在<strong style='color:blue;'>{1}</strong>时间对<strong style='color:blue;'>{2}</strong>类型项目<strong style='color:blue;'>{3}</strong>运行了FTA分析<br>", us[0], us[2], ShqConstants.GetProjectType(p.Type), p.Name); //sb.Append(dbContent); auditsList.Add(new { content = dbContent, date = us[2] }); } //avm.ProjectTreeAnalysisActivities = sb.ToString(); avm.AuditsList = String.Join(" ", auditsList.OrderByDescending(item => item.date).Select(x => x.content).Take(auditsListMaxCount).ToList()); //avm.ActivitiesCount = new List<object>(); //using (var con = new MySqlConnection(ConfigurationManager.ConnectionStrings["ShqContext"].ConnectionString)) //{ // con.Open(); // var cmd = con.CreateCommand(); // cmd.CommandText = string.Format("SELECT DATE(audits.CreatedTime),audits.Operation,count(*) as num FROM shqdb.audits " + // "group by DATE(audits.CreatedTime), audits.Operation " + // "order by DATE(audits.CreatedTime); "); // using (var rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection)) // { // while (rdr.Read()) // { // var data = new { date = rdr.GetDateTime(0), operation = rdr.GetString(1), count = rdr.GetDouble(2) }; // avm.ActivitiesCount.Add(data); // } // } //} var shqUser = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault(); avm.ProjectsCreateCount = db.Projects.Where(item => item.CreatedById == shqUser.IdentityUserId && item.Status == ShqConstants.ProjectActive).Count(); avm.ProjectsInvoleCount = db.ProjectShqUsers.Where(item => item.ShqUserId == shqUser.IdentityUserId && item.Privilege > 0 && item.Project.CreatedById != shqUser.IdentityUserId).Count(); avm.ProjectsInvoleCount += avm.ProjectsCreateCount; var projectsAccess = from user in all where user.Operation.Contains("Projects") == true orderby user.CreatedTime descending select user.UserName + "`" + user.OperationDetails + "`" + user.CreatedTime; int total = 3; string lastProjectName = ""; avm.LatestEditProjects = new List <dynamic>(); foreach (var u in projectsAccess) { string[] us = u.Split('`'); var p = JsonConvert.DeserializeObject <ProjectViewModel>(us[1]); if (lastProjectName != p.Name && total > 0) { avm.LatestEditProjects.Add(new { content = string.Format("<strong style='color:blue;'>{0}</strong><br>", p.Name), projectId = p.Id, projectType = p.Type }); total--; } } avm.ProjectsActivitiesCount = new List <AuditProjectData>(); DateTime now = DateTime.Now; int maxDay = 1 - 1 * auditProjectsMaxcount; for (int i = maxDay; i <= 0; i++) { avm.ProjectsActivitiesCount.Add(new AuditProjectData { date = now.AddDays(i).Date.ToString("yyyy-MM-dd"), ftaCount = 0, fmeaCount = 0, workProjectCount = 0 }); } using (var con = new MySqlConnection(ConfigurationManager.ConnectionStrings["ShqContext"].ConnectionString)) { con.Open(); var cmd = con.CreateCommand(); cmd.CommandText = string.Format("(select * from ((SELECT date(temp1.CreatedTime) as CreatedTime,'FMEA' as projectType, (select count(*) from fmeaprojects as temp2 where temp2.CreatedTime<=temp1.CreatedTime) as num FROM fmeaprojects as temp1 group by date(temp1.CreatedTime) order by date(temp1.CreatedTime) desc) " + "union all " + "(SELECT date(temp1.CreatedTime) as CreatedTime, 'FTA' as projectType, (select count(*) from ftaprojects as temp2 where temp2.CreatedTime <= temp1.CreatedTime) as num FROM ftaprojects as temp1 group by date(temp1.CreatedTime) order by date(temp1.CreatedTime) desc) " + "union all " + "(SELECT date(temp1.CreatedTime) as CreatedTime,'Workproject' as projectType, (select count(*) from workprojects as temp2 where temp2.CreatedTime <= temp1.CreatedTime) as num FROM workprojects as temp1 group by date(temp1.CreatedTime) order by date(temp1.CreatedTime) desc)) as groupTemp " + "where groupTemp.CreatedTime >= '" + now.AddDays(maxDay).Date.ToString("yyyy-MM-dd") + "') " + "union all " + "(SELECT date(temp1.CreatedTime) as CreatedTime,'FMEA' as projectType, (select count(*) from fmeaprojects as temp2 where temp2.CreatedTime<=temp1.CreatedTime) as num FROM fmeaprojects as temp1 group by date(temp1.CreatedTime) order by date(temp1.CreatedTime) desc limit 1) " + "union all " + "(SELECT date(temp1.CreatedTime) as CreatedTime, 'FTA' as projectType, (select count(*) from ftaprojects as temp2 where temp2.CreatedTime <= temp1.CreatedTime) as num FROM ftaprojects as temp1 group by date(temp1.CreatedTime) order by date(temp1.CreatedTime) desc limit 1) " + "union all " + "(SELECT date(temp1.CreatedTime) as CreatedTime,'Workproject' as projectType, (select count(*) from workprojects as temp2 where temp2.CreatedTime <= temp1.CreatedTime) as num FROM workprojects as temp1 group by date(temp1.CreatedTime) order by date(temp1.CreatedTime) desc limit 1)" ); using (var rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection)) { while (rdr.Read()) { var data = new { date = rdr.GetDateTime(0).ToString("yyyy-MM-dd"), projectType = rdr.GetString(1), count = rdr.GetInt32(2) }; var element = avm.ProjectsActivitiesCount.FirstOrDefault(item => item.date == data.date); if (element == null) { element = new AuditProjectData { date = data.date, ftaCount = 0, fmeaCount = 0, workProjectCount = 0 }; avm.ProjectsActivitiesCount.Add(element); } switch (data.projectType) { case "FTA": element.ftaCount = data.count; break; case "FMEA": element.fmeaCount = data.count; break; case "Workproject": element.workProjectCount = data.count; break; } } } } avm.ProjectsActivitiesCount = avm.ProjectsActivitiesCount.OrderBy(item => DateTime.Parse(item.date)).ToList(); for (int i = avm.ProjectsActivitiesCount.Count - 1; i > 0; i--) { if (avm.ProjectsActivitiesCount[i].fmeaCount == 0) { var last = avm.ProjectsActivitiesCount.Take(i).LastOrDefault(item => item.fmeaCount != 0); if (last != null) { avm.ProjectsActivitiesCount[i].fmeaCount = last.fmeaCount; } } if (avm.ProjectsActivitiesCount[i].ftaCount == 0) { var last = avm.ProjectsActivitiesCount.Take(i).LastOrDefault(item => item.ftaCount != 0); if (last != null) { avm.ProjectsActivitiesCount[i].ftaCount = last.ftaCount; } } if (avm.ProjectsActivitiesCount[i].workProjectCount == 0) { var last = avm.ProjectsActivitiesCount.Take(i).LastOrDefault(item => item.workProjectCount != 0); if (last != null) { avm.ProjectsActivitiesCount[i].workProjectCount = last.workProjectCount; } } } if (avm.ProjectsActivitiesCount.Count > auditProjectsMaxcount) { avm.ProjectsActivitiesCount = avm.ProjectsActivitiesCount.GetRange(avm.ProjectsActivitiesCount.Count - auditProjectsMaxcount, auditProjectsMaxcount); } return(avm); }
public async Task <IHttpActionResult> SyncProjectFiles(Guid projectId) { var pro = db.Projects.FirstOrDefault(item => item.Id == projectId); if (pro != null) { if (ProjectHelper.HasUpdateAccess(pro) == false) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "No Access")); } } else { return(NotFound()); } string path = ShqConstants.ProjectRootFolder + "\\" + projectId; if (Directory.Exists(path) == false) { Directory.CreateDirectory(path); } path = path + "\\Root"; if (Directory.Exists(path) == false) { Directory.CreateDirectory(path); } var wp = db.WorkProjects.Include("WorkProjectTemplate").FirstOrDefault(item => item.ProjectId == projectId); var sourceFiles = wp.WorkProjectTemplate.ProjectFiles.Where(item => item.Status == (int)ShqConstants.FileStatus.Ready && (item.Level & wp.Level) == wp.Level).ToList(); wp.FilesToCopyNum = sourceFiles.Count(); wp.FilesCopiedNum = 0; string sourceRootFolder = Path.Combine(ShqConstants.TemplateRootFolder, wp.WorkProjectTemplate.Id.ToString()) + "\\Root"; int start = sourceRootFolder.Split('\\').Length; foreach (var file in sourceFiles) { wp.FilesCopiedNum++; string[] folers = file.Path.Split('\\'); string tempPath = path; int to = file.IsFolder == true ? folers.Length : folers.Length - 1; for (int i = start; i < to; i++) { int id = ShqConstants.GetPathId(folers[i]); var tf = db.ProjectFiles.FirstOrDefault(item => item.Id == id).FileId; var f = db.ProjectFiles.FirstOrDefault(item => item.FileId == tf && item.WorkProjectTemplateId == wp.WorkProjectTemplateId && item.WorkProjectId == wp.Id); if (f == null) { var projectFile = db.ProjectFiles.Add(new ProjectFile { FileId = file.FileId, Name = file.Name, Level = file.Level, IsFolder = file.IsFolder, Path = tempPath, Status = (int)ShqConstants.FileStatus.Copying, WorkProjectTemplateId = wp.WorkProjectTemplateId, WorkProjectId = wp.Id, CreatedById = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId, LastModifiedById = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId }); wp.ProjectFiles.Add(projectFile); db.SaveChanges(); tempPath = Path.Combine(tempPath, file.Name + "." + projectFile.Id.ToString()); if (Directory.Exists(tempPath) == false) { Directory.CreateDirectory(tempPath); projectFile.Path = tempPath; projectFile.Status = (int)ShqConstants.FileStatus.Ready; } } else { if (f.Status != (int)ShqConstants.FileStatus.Deleted) { tempPath = Path.Combine(tempPath, f.Name + "." + f.Id.ToString()); if (Directory.Exists(tempPath) == false) { Directory.CreateDirectory(tempPath); f.Status = (int)ShqConstants.FileStatus.Ready; f.LastModifiedById = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId; f.LastModfiedTime = DateTime.Now; } } } } if (file.IsFolder == false) { var f = db.ProjectFiles.FirstOrDefault(item => item.FileId == file.FileId && item.WorkProjectTemplateId == wp.WorkProjectTemplateId && item.WorkProjectId == wp.Id); if (f == null) { var projectFile = db.ProjectFiles.Add(new ProjectFile { FileId = file.FileId, Name = file.Name, Level = file.Level, IsFolder = file.IsFolder, Path = tempPath, WorkProjectTemplateId = wp.WorkProjectTemplateId, WorkProjectId = wp.Id, Status = (int)ShqConstants.FileStatus.Copying, CreatedById = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId, LastModifiedById = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId }); wp.ProjectFiles.Add(projectFile); db.SaveChanges(); string targetPath = tempPath + "\\" + projectFile.Name + "." + projectFile.Id; if (File.Exists(targetPath) == false) { File.Copy(file.Path, targetPath); projectFile.Status = (int)ShqConstants.FileStatus.Ready; projectFile.Path = targetPath; } } else { if (f.Status != (int)ShqConstants.FileStatus.Deleted) { string targetPath = tempPath + "\\" + f.Name + "." + f.Id; if (File.Exists(targetPath) == false) { File.Copy(file.Path, targetPath); f.Status = (int)ShqConstants.FileStatus.Ready; f.Path = targetPath; f.LastModifiedById = db.ShqUsers.Where(u => u.IdentityUser.UserName == HttpContext.Current.User.Identity.Name).FirstOrDefault().IdentityUserId; f.LastModfiedTime = DateTime.Now; } } } } } await db.SaveChangesAsync(); return(Ok()); }
public ProjectFolderViewModel(ProjectFilePath path, SearchOption searchOption, ShqContext db, int folderLevel = 1) { this.Path = path.Path; this.Name = System.IO.Path.GetFileName(path.Path); Id = ShqConstants.GetPathId(path.Path); if (Id > 0) { this.Name = System.IO.Path.GetFileNameWithoutExtension(this.Name); var f = db.ProjectFiles.FirstOrDefault(item => item.Id == Id); if (f == null) { this.Name = ""; } else { CreatedBy = new ShqUserRequestViewModel(db.ShqUsers.Where(u => u.IdentityUser.Id == db.ProjectFiles.FirstOrDefault(item => item.Id == Id).CreatedById).FirstOrDefault(), db); CreatedTime = f.CreatedTime.ToString(); LastModifiedBy = new ShqUserRequestViewModel(db.ShqUsers.Where(u => u.IdentityUser.Id == db.ProjectFiles.FirstOrDefault(item => item.Id == Id).LastModifiedById).FirstOrDefault(), db); LastModfiedTime = f.LastModfiedTime.ToString(); this.Privilege = f.Privilege; this.Level = f.Level; } } if (this.Name == "") { return; } if ((searchOption == SearchOption.TopDirectoryOnly && folderLevel == 1) || searchOption == SearchOption.AllDirectories) { string[] directory, files; directory = Directory.GetDirectories(path.FullPath); files = Directory.GetFiles(path.FullPath); if (directory != null && directory.Length > 0) { foreach (var d in directory) { var pf = new ProjectFolderViewModel(new ProjectFilePath { FullPath = d, Path = System.IO.Path.Combine(path.Path, System.IO.Path.GetFileName(d)) }, searchOption, db, folderLevel); if (pf.Name != "") { this.SubFolders.Add(pf); } } if (this.SubFolders != null && this.SubFolders.Count > 0) { this.SubFolders.Sort((x, y) => x.Name.CompareTo(y.Name)); } } if (files != null && files.Length > 0) { foreach (var f in files) { var ff = new ProjectFileViewModel(new ProjectFilePath { FullPath = f, Path = System.IO.Path.Combine(path.Path, System.IO.Path.GetFileName(f)) }, db); if (ff.Name != "") { this.Files.Add(ff); } } if (this.Files != null && this.Files.Count > 0) { this.Files.Sort((x, y) => x.Name.CompareTo(y.Name)); } } folderLevel--; } }