public ActionResult ImportTable(string projectGuid) { return(ActionUtils.Json(() => { var project = m_dbAdapter.Project.GetProjectByGuid(projectGuid); CheckHandleContactPermission(project); var file = Request.Files[0]; byte[] bytes = new byte[file.InputStream.Length]; file.InputStream.Read(bytes, 0, bytes.Length); file.InputStream.Seek(0, SeekOrigin.Begin); Stream newStream = new MemoryStream(bytes); var tableHeaderRowsCount = ExcelUtils.GetTableHeaderRowsCount(newStream, 0, 0, tableHeader); var table = ExcelUtils.ParseExcel(file.InputStream, 0, tableHeaderRowsCount, 0, 6); var validation = new ExcelValidation(); validation.Add(CellRange.Column(0), new CellTextValidation(1, 50)); Func <string, string> checkEDutyType = (cellText) => { try { CommUtils.ParseEnum <EDutyType>(cellText); } catch { return "无法将[" + cellText + "]解析为[职责]"; } return string.Empty; }; validation.Add(CellRange.Column(1), new CellCustomValidation(checkEDutyType, false)); validation.Add(CellRange.Column(2), new CellTextValidation(0, 30)); validation.Add(CellRange.Column(3), new CellTextValidation(0, 38)); validation.Add(CellRange.Column(3), new CellEmailValidation()); validation.Add(CellRange.Column(4), new CellTelValidation()); validation.Check(table, tableHeaderRowsCount); Func <List <object>, int, int, Contact> ParseRow = (objs, index, projectId) => { return this.ParseRow(objs, index, projectId); }; List <Contact> contacts = ExcelUtils.ParseTable <Contact>(table, project.ProjectId, ParseRow).ToList(); contacts.ForEach(x => m_dbAdapter.Contact.AddContact(x)); var logicModel = Platform.GetProject(project.ProjectGuid); contacts.ForEach(x => logicModel.Activity.Add(project.ProjectId, ActivityObjectType.Contact, x.Guid, "增加机构:" + x.OrganizationName)); return ActionUtils.Success(contacts.Count); })); }
public void TestValidation() { var table = new List <List <object> >(); table.Add(new List <object> { 7657, 1, 2, 3, "2016-09-12" }); table.Add(new List <object> { 0, 1, 2, 3, "2016-09-12" }); table.Add(new List <object> { 0, 1, 2, 3, "2016/09/12" }); table.Add(new List <object> { 0, "123.5", 2, 3, "2016-09-12" }); table.Add(new List <object> { 0, "123.5", 2, 3, "2016-09-12" }); table.Add(new List <object> { 0, "23.5", 2, 3, "2016-09-12" }); table.Add(new List <object> { 0, "23.5", 2, 3, "2016-09-12" }); var validation = new ExcelValidation(); validation.Add(CellRange.Cell(0, 0), new CellTextValidation(0, 10)); validation.Add(CellRange.Column(4), new CellTextValidation(0, 10)); validation.Add(CellRange.Column(1), new CellNumberValidation()); validation.Add(CellRange.Column(4), new CellDateValidation()); validation.Add(CellRange.Row(6), new CellNumberValidation()); var tableHeaderRowsCount = 1; validation.Check(table, tableHeaderRowsCount); }
public ActionResult ImportTable(string projectSeriesGuid) { return(ActionUtils.Json(() => { var projectSeriesLogicModel = new ProjectSeriesLogicModel(CurrentUserName, projectSeriesGuid); var project = projectSeriesLogicModel.CurrentProject.Instance; CheckPermission(PermissionObjectType.Project, project.ProjectGuid, PermissionType.Write); var file = Request.Files[0]; byte[] bytes = new byte[file.InputStream.Length]; file.InputStream.Read(bytes, 0, bytes.Length); file.InputStream.Seek(0, SeekOrigin.Begin); Stream newStream = new MemoryStream(bytes); var tableHeaderRowsCount = ExcelUtils.GetTableHeaderRowsCount(newStream, 0, 0, tableHeader); var table = ExcelUtils.ParseExcel(file.InputStream, 0, tableHeaderRowsCount, 0, 9); var validation = new ExcelValidation(); validation.Add(CellRange.Column(0), new CellTextValidation(1, 30)); validation.Add(CellRange.Column(1), new CellTextValidation(1, 500)); validation.Add(CellRange.Column(2), new CellTextValidation(1, 30)); validation.Add(CellRange.Column(3), new CellDateValidation()); validation.Add(CellRange.Column(4), new CellDateValidation(false)); //检查工作状态 Func <string, string> checkTaskStatus = (cellText) => { if (!string.IsNullOrWhiteSpace(cellText) && cellText != "-" && cellText != TranslateUtils.ToCnString(TaskStatus.Waitting) && cellText != TranslateUtils.ToCnString(TaskStatus.Overdue)) { return "导入工作的工作状态只能为空、[等待]或[逾期]"; } return string.Empty; }; validation.Add(CellRange.Column(5), new CellCustomValidation(checkTaskStatus)); //检查负责人 int projectId = project.ProjectId; var personInCharges = GetPersonInCharges(projectSeriesLogicModel, projectId); Func <List <string>, string, string> checkPersonInCharge = (personInChargeList, cellText) => { var userName = GetPersonInCharge(cellText); if (!personInChargeList.Contains(userName)) { return "负责人[" + cellText + "]不存在"; } return string.Empty; }; validation.Add(CellRange.Column(6), new CellCustomValidation(checkPersonInCharge, personInCharges)); //不检查只有taskGroup列,其他列均为空的行 var columns = new List <CellRange>() { CellRange.Column(2), CellRange.Column(3), CellRange.Column(4), CellRange.Column(5), CellRange.Column(6), CellRange.Column(7), CellRange.Column(8) }; Func <string, bool> ExceptCondition = (x) => { return x != string.Empty; }; validation.JudgeColumn = new ColumnJudge(columns, ExceptCondition); //检查开始时间是否大于截止时间 Func <object, object, string> lessThanOrEqualTo = (left, right) => { var leftText = left.ToString(); var rightText = right.ToString(); if (!string.IsNullOrWhiteSpace(leftText) && leftText != "-") { var startTime = DateTime.Parse(leftText); var endTime = DateTime.Parse(rightText); if (startTime > endTime) { return "开始时间[" + leftText + "]不能大于结束时间[" + rightText + "]"; } } return string.Empty; }; validation.CompareColumn = new ColumnComparison(CellRange.Column(3), CellRange.Column(4), lessThanOrEqualTo); validation.Check(table, tableHeaderRowsCount); //创建taskGroups List <TaskGroup> newTaskGroups = new List <TaskGroup>(); for (int iRow = 0; iRow < table.Count; iRow++) { var row = table[iRow]; var taskGroup = ParseTaskGroups(newTaskGroups, row, iRow, projectId); if (taskGroup != null) { newTaskGroups.Add(taskGroup); } } var projectLogicModel = new ProjectLogicModel(CurrentUserName, project); var permissionLogicModel = new PermissionLogicModel(projectLogicModel); var projectSeries = projectSeriesLogicModel.Instance; newTaskGroups.ForEach(x => { var taskGroup = m_dbAdapter.TaskGroup.NewTaskGroup(x.ProjectId, x.Name, x.Description); permissionLogicModel.SetPermission(projectSeries, project, taskGroup.Guid, PermissionObjectType.TaskGroup); projectLogicModel.Activity.Add(x.ProjectId, ActivityObjectType.TaskGroup, taskGroup.Guid, "新建工作组:" + taskGroup.Name); }); //创建tasks var taskGroups = m_dbAdapter.TaskGroup.GetByProjectId(projectId); var dicTaskGroups = taskGroups.ToDictionary(x => x.Name); List <Task> tasks = new List <Task>(); for (int iRow = 0; iRow < table.Count; iRow++) { var row = table[iRow].ToList(); if (row.Count > 2 && (!string.IsNullOrWhiteSpace(row[2].ToString())) && row[2].ToString() != "-") { var taskGroup = dicTaskGroups[row[0].ToString()]; tasks.Add(ParseTasks(row, iRow, taskGroup.Id, project)); } } tasks.ForEach(x => m_dbAdapter.Task.NewTask(x)); Dictionary <string, string> msgDic = new Dictionary <string, string>(); foreach (var task in tasks) { var msg = string.Format("创建task:shortCode={0};name={1};startTime={2};endTime={3};personInCharge={4};" + "taskStatus={5};taskTarget={6};taskDetail={7};projectId={8};projectName={9};taskGroupId={10}", task.ShortCode, task.Description, task.StartTime, task.EndTime, task.PersonInCharge, task.TaskStatus, task.TaskTarget, task.TaskDetail, task.ProjectId, task.ProjectName, task.TaskGroupId); msgDic[task.ShortCode] = msg; } tasks.ForEach(x => { permissionLogicModel.SetPermission(projectSeries, project, x.ShortCode, PermissionObjectType.Task); projectLogicModel.Activity.Add(projectId, ActivityObjectType.Task, x.ShortCode, "创建工作:" + x.Description); m_dbAdapter.Project.NewEditProductLog(EditProductType.CreateTask, x.ProjectId, msgDic[x.ShortCode], ""); }); var result = new { taskGroupCount = newTaskGroups.Count, taskCount = tasks.Count }; return ActionUtils.Success(result); })); }