public int GenerateTasks(int projectId, List <TemplateTask> templateTasks, List <TemplateTime> templateTimes) { var onceTimeDict = new Dictionary <string, DateTime>(); var loopTimeDict = new Dictionary <string, List <DateTime> >(); foreach (var templateTime in templateTimes) { var dateList = DateUtils.GenerateDateList( templateTime.BeginTime, templateTime.EndTime, templateTime.TimeSpan, templateTime.TimeSpanUnit, templateTime.TemplateTimeType, templateTime.SearchDirection == TemplateTimeSearchDirection.Forward, templateTime.HandleReduplicate == TemplateTimeHandleReduplicate.Ignore ); loopTimeDict[templateTime.TemplateTimeName] = dateList; if (dateList.Count == 1) { onceTimeDict[templateTime.TemplateTimeName] = dateList[0]; } } var container = new TemplateTaskContainer(templateTasks); var sortedTask = container.SortByDependency(); int moduleId = 9527; //Dictionary<TemplateTaskId, List<Tuple<NewTaskId, WorkCode>>> var taskDependencyDict = new Dictionary <int, List <Tuple <int, string> > >(); int totalTaskCount = 0; foreach (var templateTask in sortedTask) { taskDependencyDict[templateTask.TemplateTaskId] = new List <Tuple <int, string> >(); var newTaskInfo = taskDependencyDict[templateTask.TemplateTaskId]; DateTime temp; bool isDateContainsLetter = !DateTime.TryParse(templateTask.TriggerDate, out temp); if (isDateContainsLetter) { var newTasks = GenerateLoopTask(loopTimeDict, projectId, moduleId, templateTask, taskDependencyDict); newTaskInfo.AddRange(newTasks.ConvertAll(x => new Tuple <int, string>(x.TaskId, x.ShortCode))); totalTaskCount += newTasks.Count; } else { var newTask = GenerateOnceTask(onceTimeDict, projectId, moduleId, templateTask, taskDependencyDict, -1); newTaskInfo.Add(new Tuple <int, string>(newTask.TaskId, newTask.ShortCode)); totalTaskCount += 1; } } return(totalTaskCount); }
public ActionResult UploadTemplateTaskFile(string templateGuid) { return(ActionUtils.Json(() => { CommUtils.Assert(Request.Files.Count > 0, "请选择文件"); var file = Request.Files[0]; CommUtils.Assert(file.ContentLength > 0, "请选择文件"); CommUtils.Assert(file.FileName.EndsWith(".xlsx", StringComparison.CurrentCultureIgnoreCase), "只支持导入Excel(.xlsx)文件"); var template = m_dbAdapter.Template.GetTemplate(templateGuid); file.InputStream.Seek(0, SeekOrigin.Begin); var table = ExcelUtils.ParseExcel(file.InputStream, 0, 1, 0, 9); CommUtils.Assert(table.Count >= 1, "模板任务为空"); var templateNames = table.Select(x => x[0].ToString()).ToList(); var index = templateNames.FindIndex(x => x != template.TemplateName); if (index >= 0) { CommUtils.Assert(false, "模板工作中包含[{0}]的模板名称,和当前模板[{1}]不一致", templateNames[index], template.TemplateName); } var excelIds = table.Select(x => x[1].ToString()); var distinct = excelIds.GroupBy(x => x).Where(x => x.Count() > 1).Select(x => x.Key).ToList(); CommUtils.Assert(distinct.Count() == 0, "模板任务中,模板工作ID(" + CommUtils.Join(distinct) + ")不能相同"); List <TemplateTask> templateTaskList = ParseTemplateTaskTable(table); templateTaskList.ForEach(x => x.TemplateId = template.TemplateId); TemplateTaskContainer container = new TemplateTaskContainer(); container.AddRange(templateTaskList); container = container.SortByDependency(); //id in excel, id in db var dictId = new Dictionary <int, int>(); foreach (var record in container) { var recordId = record.TemplateTaskId; record.PrevIds = record.PrevIds.ConvertAll(x => dictId[x]); var newRecord = m_dbAdapter.Task.NewTemplateTask(record); var newRecordId = newRecord.TemplateTaskId; dictId[recordId] = newRecordId; LogEditProduct(EditProductType.EditTask, null, "从Excel导入工作模板", "templateId=[" + template.TemplateId + "],新增templateTaskId=[" + newRecordId + "]"); } return ActionUtils.Success(container.Count); })); }