Пример #1
0
        public ActionResult UpdateTask(TaskParamDto model)
        {
            if (model.TaskStatusId == TaskStatusTypes.ToDo && !string.IsNullOrEmpty(model.ResultContent))
            {
                return(AjaxMessage("Uyarı", "Sonuç girilen bir işin <b>İş Durumu</b> Alanı Yapılacak olarak bırakılamaz.", MessageTypes.warning));
            }
            //if ((model.TaskStatusId == TaskStatusTypes.Removed || model.TaskStatusId == TaskStatusTypes.Done) && string.IsNullOrEmpty(model.ResultContent))
            //    return AjaxMessage("Uyarı", "Kaldırılan veya Tamamlanan bir işin <b>Sonuç</b> Alanı zorunludur.", MessageTypes.warning);
            if ((model.TaskStatusId == TaskStatusTypes.Removed || model.TaskStatusId == TaskStatusTypes.Done) && model.DueDate == null)
            {
                return(AjaxMessage("Uyarı", "Kaldırılan veya Tamamlanan bir işin <b>Kapanma Tarihi</b> Alanı zorunludur.", MessageTypes.warning));
            }
            var entity             = _workService.FindTask(model.Id);
            var entityTaskStatusId = entity.TaskStatusId;
            var entityAssigned     = entity.Assigned;

            try
            {
                var personId = Accesses.PersonId;
                _uow.BeginTransaction();

                if (entity.TaskStatusId != model.TaskStatusId | entity.Assigned != model.Assigned)
                {
                    var entityTaskHistory = new TaskHistory
                    {
                        CreatedBy    = personId,
                        CreatedDate  = DateTime.Now,
                        Assigned     = model.Assigned,
                        TaskId       = entity.Id,
                        TaskStatusId = model.TaskStatusId,
                    };
                    _workService.InsertTaskHistory(entityTaskHistory);
                    _uow.SaveChanges();
                }

                entity.Assigned      = model.Assigned;
                entity.Description   = model.Description;
                entity.DueDate       = model.DueDate;
                entity.ProjectId     = model.ProjectId;
                entity.RequestedBy   = model.RequestedBy;
                entity.RequestedDate = model.RequestedDate;
                entity.ResultContent = model.ResultContent;
                entity.TaskStatusId  = model.TaskStatusId;
                entity.Title         = model.Title;
                entity.DueDate       = (model.TaskStatusId == TaskStatusTypes.Removed || model.TaskStatusId == TaskStatusTypes.Done) ? model.DueDate : null;
                entity.UnitId        = model.UnitId;
                entity.Queue         = model.Queue;
                entity.Followers     = model.Followers;

                _workService.UpdateTask(entity);
                _uow.SaveChanges();

                var entityMedia = (Media)Session["Media" + 1];
                if (entityMedia != null | Session["Clear"] == null)
                {
                    var resultList = _mediaService.GetTaskMediaById(entity.Id);

                    for (int i = 0; i < resultList.Count; i++)
                    {
                        _mediaService.DeleteTaskMedia(resultList[i]);
                        _uow.SaveChanges();

                        var entityM = _mediaService.FindMedia(resultList[i].MediaId);
                        _mediaService.DeleteMedia(entityM);
                        _uow.SaveChanges();
                    }

                    if (Session["counter"] != null)
                    {
                        var counter = int.Parse(Session["counter"].ToString());

                        for (int i = 1; i <= counter; i++)
                        {
                            var entityM = (Media)Session["Media" + i];
                            entityM.CreatedDate = DateTime.Now;
                            entityM.MediaTypeId = MediaTypes.IsYonetimi;
                            _mediaService.InsertMedia(entityM);
                            _uow.SaveChanges();

                            _mediaService.InsertTaskMedia(new TaskMedia
                            {
                                CreatedBy   = Accesses.PersonId,
                                CreatedDate = DateTime.Now,
                                MediaId     = entityM.Id,
                                TaskId      = entity.Id
                            });
                            _uow.SaveChanges();
                        }
                    }
                }

                _uow.Commit();


                if (bool.Parse(Resources.Parameter.IsSendMailForWork))
                {
                    try
                    {
                        if (model.TaskStatusId == TaskStatusTypes.Done && model.TaskStatusId != entityTaskStatusId)
                        {
                            var requestedBy = _personService.FindPerson(entity.RequestedBy);
                            var assigned    = _personService.FindPerson(entity.Assigned);
                            var ccList      = model.Followers != null?model.Followers.Split(',').ToList().Select(p => _personService.FindPerson(int.Parse(p)).Email).Where(p => string.IsNullOrEmpty(p) == false).ToList() : new List <string>();

                            if (assigned.Email != null)
                            {
                                ccList.Add(assigned.Email);
                            }

                            if (requestedBy.Email != null)
                            {
                                var    sr   = new StreamReader(Server.MapPath(@"/Views/Shared/EmailTemplate/") + "TaskDoneEmail.html");
                                string body = sr.ReadToEnd();

                                var result       = _workService.GetTaskByTaskId(entity.Id);
                                var folowerNames = _workService.GetTaskFolowerNames(entity.Id);

                                string messageBody = body.Replace("##Assigned", result[0].Assigned).Replace("##RequestedBy", result[0].RequestedBy)
                                                     .Replace("##taskId", entity.Id.ToString()).Replace("##RequestedDate", result[0].RequestedDate).Replace("##ProjectName", result[0].ProjectName)
                                                     .Replace("##Title", result[0].Title).Replace("##Description", result[0].Description != null ? result[0].Description.Replace("\n", "<br/>") : "").Replace("##CreatedDate", result[0].CreatedDate)
                                                     .Replace("##DueDate", result[0].DueDate).Replace("##ResultContent", result[0].ResultContent != null ? result[0].ResultContent.Replace("\n", "<br/>") : "")
                                                     .Replace("##Followers", folowerNames);

                                var attachementList = new List <Attachment>();
                                if (Session["counter"] != null)
                                {
                                    var counter = int.Parse(Session["counter"].ToString());
                                    for (int i = 1; i <= counter; i++)
                                    {
                                        var entityM = (Media)Session["Media" + i];
                                        attachementList.Add(new Attachment(new MemoryStream(entityM.Value), entityM.FileName, entityM.ContentType));
                                    }
                                }
                                MailHelper.SendMail("Kapanan İş - " + result[0].Title, messageBody, requestedBy.Email, ccList.Distinct().ToList(), attachementList);
                            }
                        }
                        else
                        if (entityAssigned != model.Assigned)
                        {
                            var requestedBy = _personService.FindPerson(entity.RequestedBy);
                            var assigned    = _personService.FindPerson(entity.Assigned);
                            var ccList      = model.Followers != null?model.Followers.Split(',').ToList().Select(p => _personService.FindPerson(int.Parse(p)).Email).Where(p => string.IsNullOrEmpty(p) == false).ToList() : new List <string>();

                            if (requestedBy.Email != null)
                            {
                                ccList.Add(requestedBy.Email);
                            }

                            if (assigned.Email != null)
                            {
                                var    sr   = new StreamReader(Server.MapPath(@"/Views/Shared/EmailTemplate/") + "TaskDetailEmail.html");
                                string body = sr.ReadToEnd();

                                var result       = _workService.GetTaskByTaskId(entity.Id);
                                var folowerNames = _workService.GetTaskFolowerNames(entity.Id);

                                string messageBody = body.Replace("##Assigned", result[0].Assigned).Replace("##RequestedBy", result[0].RequestedBy)
                                                     .Replace("##taskId", entity.Id.ToString()).Replace("##RequestedDate", result[0].RequestedDate).Replace("##ProjectName", result[0].ProjectName)
                                                     .Replace("##Title", result[0].Title).Replace("##Description", result[0].Description != null ? result[0].Description.Replace("\n", "<br/>") : "")
                                                     .Replace("##CreatedBy", result[0].CreatedBy).Replace("##CreatedDate", result[0].CreatedDate).Replace("##Followers", folowerNames);

                                var attachementList = new List <Attachment>();
                                if (Session["counter"] != null)
                                {
                                    var counter = int.Parse(Session["counter"].ToString());
                                    for (int i = 1; i <= counter; i++)
                                    {
                                        var entityM = (Media)Session["Media" + i];
                                        attachementList.Add(new Attachment(new MemoryStream(entityM.Value), entityM.FileName, entityM.ContentType));
                                    }
                                }
                                MailHelper.SendMail("Yeni İş - " + result[0].Title, messageBody, assigned.Email, ccList.Distinct().ToList(), attachementList);
                            }
                        }
                    }
                    catch (Exception ex) { }
                }
                return(AjaxMessage("", "İşlem başarıyla gerçekleşti.$$window.location='/Work/NewOrEdit?Id=" + entity.Id + "'", MessageTypes.funcAndMessage));
            }
            catch (Exception ex)
            {
                _uow.Rollback();
                throw ex;
            }
        }