示例#1
0
        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);
            }));
        }
示例#2
0
        public ActionResult CreateTask(string projectGuid, string name, string startTime, string endTime,
                                       string prevTaskShortCodes, string taskExtensionType, string taskDetail, string taskTarget,
                                       string taskGroupGuid, string personInCharge)
        {
            return(ActionUtils.Json(() =>
            {
                CheckPermission(PermissionObjectType.TaskGroup, taskGroupGuid, PermissionType.Write);

                CommUtils.Assert(DateUtils.IsNullableDate(startTime), "开始时间必须为[YYYY-MM-DD]格式或者为空");
                ValidateUtils.Name(name, "工作名称");
                CommUtils.AssertHasContent(endTime, "截止时间不能为空");

                CommUtils.Assert(taskTarget.Length <= 100000, "工作目标不能超过100000个字符");
                CommUtils.Assert(taskDetail.Length <= 100000, "工作描述不能超过100000个字符");
                if (!string.IsNullOrWhiteSpace(personInCharge))
                {
                    CommUtils.Assert(m_dbAdapter.Authority.IsUserExist(personInCharge), "负责人[{0}]不存在", personInCharge);
                }
                var taskStartTime = DateUtils.Parse(startTime);
                if (taskStartTime != null)
                {
                    CommUtils.Assert(DateTime.Parse(endTime) >= taskStartTime,
                                     "开始时间[{0}]不能大于截止时间[{1}]", startTime, endTime);
                }

                var project = m_dbAdapter.Project.GetProjectByGuid(projectGuid);
                int?taskGroupId = null;
                if (!string.IsNullOrWhiteSpace(taskGroupGuid))
                {
                    var taskGroup = m_dbAdapter.TaskGroup.GetByGuid(taskGroupGuid);
                    CommUtils.AssertEquals(taskGroup.ProjectId, project.ProjectId, "输入工作组[TaskGroupGuid={0}]和项目[ProjectGuid={1}]不属于同一个项目", taskGroupGuid, project.ProjectGuid);
                    taskGroupId = taskGroup.Id;
                }

                int?taskExId = null;
                if (!string.IsNullOrWhiteSpace(taskExtensionType))
                {
                    var taskExType = CommUtils.ParseEnum <TaskExtensionType>(taskExtensionType);
                    var taskEx = ChineseAbs.ABSManagement.Models.TaskExtension.Create(taskExType);
                    taskEx = m_dbAdapter.Task.NewTaskExtension(taskEx);
                    taskExId = taskEx.TaskExtensionId;
                }

                var task = new Task();
                task.ProjectId = project.ProjectId;
                task.Description = name;
                task.ProjectName = project.Name;
                task.StartTime = taskStartTime;
                task.EndTime = DateTime.Parse(endTime);
                task.TaskExtensionId = taskExId;
                task.PersonInCharge = string.IsNullOrWhiteSpace(personInCharge) ? null : personInCharge;

                if (!string.IsNullOrWhiteSpace(prevTaskShortCodes))
                {
                    var shortCodes = CommUtils.Split(prevTaskShortCodes);
                    var prevTasks = m_dbAdapter.Task.GetTasks(shortCodes);
                    var prevTaskIds = prevTasks.Select(x => x.TaskId.ToString()).ToArray();
                    task.PreTaskIds = CommUtils.Join(prevTaskIds);
                }

                task.TaskDetail = taskDetail;
                task.TaskTarget = taskTarget;
                task.TaskStatus = (DateTime.Today <= task.EndTime) ? TaskStatus.Waitting : TaskStatus.Overdue;
                task.TaskGroupId = taskGroupId;
                task = m_dbAdapter.Task.NewTask(task);

                var projectLogicModel = new ProjectLogicModel(CurrentUserName, project);
                var permissionLogicModel = new PermissionLogicModel(projectLogicModel);
                var projectSeries = m_dbAdapter.ProjectSeries.GetById(project.ProjectSeriesId.Value);
                permissionLogicModel.SetPermission(projectSeries, project, task.ShortCode, PermissionObjectType.Task);

                projectLogicModel.Activity.Add(project.ProjectId, ActivityObjectType.Task, task.ShortCode, "创建工作:" + task.Description);

                var msg = string.Format("创建task:shortCode={0};name={1};startTime={2};endTime={3};taskExtensionType={4};"
                                        + "taskDetail={5};taskTarget={6};prevTaskShortCodes={7};projectGuid={8};taskGroupGuid={9}",
                                        task.ShortCode, name, startTime, endTime, taskExtensionType, taskDetail, taskTarget, prevTaskShortCodes, projectGuid, taskGroupGuid);
                LogEditProduct(EditProductType.CreateTask, task.ProjectId, msg, "");

                return ActionUtils.Success(1);
            }));
        }