public ActionResult EditPreventionDiseaseTask(string taskNumber)
        {
            if (Session["CheckFeed"] == null || !(bool)Session["EditPreventionDisease"] ||
                !(bool)Session["CheckPreventionDisease"])
            {
                Response.Write("<script>history.go(0);</script>");
                return View("Error");
            }

            PreventionDiseaseTask updateTask = new PreventionDiseaseTask().GetPreventionDiseaseTask(taskNumber);

            if (updateTask == null)
            {
                return View("Error");
            }

            var listService = new ListService();

            ViewData["PreventionType"] = listService.GetPreventionTypeList();
            ViewData["pighouse"] = listService.GetPigHouseList();
            ViewData["taskStatus"] = listService.GetTaskStatusList();
            ViewData["PigStyPigType"] = listService.GetPigStyPigTypeList();
            ViewBag.Title = "修改防疫任务";
            return View("NewPreventionDiseaseTask", updateTask);
        }
 public ActionResult EditPreventionDiseaseTask(PreventionDiseaseTask updatetask)
 {
     if (!ModelState.IsValid || !updatetask.UpdatePreventionDiseaseTask(updatetask))
     {
         return View("Error");
     }
     ViewBag.Title = "修改成功";
     return View("Message");
 }
        public ActionResult CheckPreventionDiseaseTask(string taskNumber)
        {
            if (Session["CheckFeed"] == null || !(bool)Session["CheckPreventionDisease"])
            {
                Response.Write("<script>history.go(0);</script>");
                return View("Error");
            }

            PreventionDiseaseTask preventionDiseaseTask =
                new PreventionDiseaseTask().GetPreventionDiseaseTask(taskNumber);
            return preventionDiseaseTask != null ? View(preventionDiseaseTask) : View("Error");
        }
        public ActionResult NewPreventionDiseaseTask(PreventionDiseaseTask newtask)
        {
            if (!ModelState.IsValid ||
                !newtask.NewPreventionDiseaseTask(newtask, TaskTypeId, User.Identity.Name.Split('|')[0]))
            {
                return View("Error");
            }

            ViewBag.Title = "新建任务成功";
            return View("Message");
        }
        public PreventionDiseaseTask GetPreventionDiseaseTask(string taskNumber)
        {
            var taskService = new TaskService();
            task task = taskService.FindByTaskNumber(taskNumber);

            if (task == null)
            {
                return null;
            }

            var taskEmployeeService = new TaskEmployeeService();
            List<task_pigsty> taskPigsties = task.task_pigsty.ToList();
            var checkTask = new PreventionDiseaseTask
                                {
                                    TaskNumber = task.TaskNumber,
                                    StartTime = task.StartTime,
                                    EndTime = task.EndTime,
                                    EmployeeName = taskEmployeeService.GetEmployeeNames(task.Id),
                                    EmployeeNumber = taskEmployeeService.GetEmployeeNumbers(task.Id),
                                    Status = task.task_status_type.Name,
                                    Memo = task.Memo
                                };

            task_prevention taskPrevention = task.task_prevention.FirstOrDefault();

            if (taskPrevention != null)
            {
                checkTask.DiseaseName = taskPrevention.prevention_type.Name;
                checkTask.Medicine.MedicineName =
                    new MedicineService().Find(taskPrevention.prevention_type.MedicineId).Name;
                checkTask.Medicine.Quality = taskPrevention.prevention_type.Quality;
                checkTask.Medicine.Unit = new UnitService().Find(taskPrevention.prevention_type.UnitId).Name;
            }

            var pigHouseService = new PigHouseService();
            var taskPigstyService = new TaskPigstyService();
            var taskPigService = new TaskPigService();
            List<TaskDetail> details =
                taskPigstyService.GetPigHouseIds(taskPigsties).Select(pigHouseid => new TaskDetail
                                                                                        {
                                                                                            PigHouse =
                                                                                                pigHouseService
                                                                                                    .
                                                                                                    Find
                                                                                                    (pigHouseid)
                                                                                                    .
                                                                                                    pig_house_type
                                                                                                    .
                                                                                                    Name +
                                                                                                pigHouseService
                                                                                                    .
                                                                                                    Find
                                                                                                    (pigHouseid)
                                                                                                    .
                                                                                                    Number,
                                                                                            Pigsty =
                                                                                                taskPigstyService
                                                                                                .
                                                                                                GetPigstyNums
                                                                                                (task
                                                                                                     .Id,
                                                                                                 pigHouseid),
                                                                                            Pig = taskPigService.GetMumPigBypigStys(
                                                                                            taskPigstyService.GetPigstyNums(task.Id, pigHouseid)
                                                                                            , pigHouseid)
                                                                                        }).ToList();

            foreach (var munPigHouseId in taskPigService.GetPigHouseByMum(task.Id))
            {

                details.Add(new TaskDetail()
                {
                    PigHouse = pigHouseService.Find(munPigHouseId).pig_house_type.Name+pigHouseService.Find(munPigHouseId).Number,
                    Pigsty = taskPigService.GetPigstyNumsByMum(task.Id, munPigHouseId),
                    Pig = "母猪"
                });
            }

            if (details.Count != 0)
            {
                checkTask.Taskdetails = details;
            }
            return checkTask;
        }
        public bool UpdatePreventionDiseaseTask(PreventionDiseaseTask updateTask)
        {
            var taskService = new TaskService();

            task task = taskService.FindByTaskNumber(updateTask.TaskNumber);

            if (task == null)
            {
                return false;
            }

            task.StartTime = updateTask.StartTime;
            task.EndTime = updateTask.EndTime;
            task.Memo = updateTask.Memo;

            if (task.task_prevention.FirstOrDefault() == null)
            {
                task.task_prevention.Add(new task_prevention
                                             {
                                                 PreventionTypeId = long.Parse(updateTask.DiseaseName)
                                             });
            }
            else
            {
                task.task_prevention.First().PreventionTypeId = long.Parse(updateTask.DiseaseName);
            }

            new TaskEmployeeService().UpdateByNumberStr(task, updateTask.EmployeeNumber);

            var pigstyService = new PigstyService();

            List<long> pigstyList = (from taskDetail in updateTask.Taskdetails
                                     from pigsty in taskDetail.Pigsty.Split(',')
                                     select
                                         pigstyService.FindByNumber(int.Parse(pigsty)).First(
                                             m => m.PigHouseId == long.Parse(taskDetail.PigHouse)).Id).ToList();

            int oldLen = task.task_pigsty.Count;
            int newLen = pigstyList.Count;
            int leastLength = oldLen < newLen ? oldLen : newLen;

            for (int i = 0; i < leastLength; i++)
            {
                task.task_pigsty.ElementAt(i).PigstyId = pigstyList[i];
            }

            var taskPigstyService = new TaskPigstyService();

            for (int i = leastLength; i < oldLen; i++)
            {
                taskPigstyService.Delete(taskPigstyService.Find(task.task_pigsty.ElementAt(i).Id));
            }

            for (int i = leastLength; i < newLen; i++)
            {
                task.task_pigsty.Add(new task_pigsty
                                         {
                                             PigstyId = pigstyList[i]
                                         });
            }

            taskService.Update(task);

            return true;
        }
        public bool NewPreventionDiseaseTask(PreventionDiseaseTask newTask, long taskTypeId, string createEmployee)
        {
            var employeeService = new EmployeeService();

            var task = new task
                           {
                               TaskNumber = newTask.TaskNumber,
                               StartTime = newTask.StartTime,
                               EndTime = newTask.EndTime,
                               CreateTime = DateTime.Now,
                               CreateEmployeeId = employeeService.FindByNumber(createEmployee).Id,
                               TaskStatusId = 1,
                               TaskTypeId = taskTypeId,
                               Memo = newTask.Memo,
                           };

            foreach (string employeeNumber in newTask.EmployeeNumber.Split(','))
            {
                task.task_employee.Add(new task_employee {EmployeeId = employeeService.FindByNumber(employeeNumber).Id});
            }

            var pigstyService = new PigstyService();
            var transferService = new TransferService();
            foreach (TaskDetail detail in newTask.Taskdetails)
            {
                string[] pigstyNum = detail.Pigsty.Split(new[] {','});
                long pighouseid = long.Parse(detail.PigHouse);
                foreach (string pigstydetail in pigstyNum)
                {
                    var taskPigsty = new task_pigsty();
                    int number = int.Parse(pigstydetail);
                    if (detail.Pig == "2") {
                        taskPigsty.PigstyId = pigstyService.GetPigsty(number, pighouseid).Id;
                       var  munPigList=transferService.FindByMunPig(taskPigsty.PigstyId);

                       foreach (var munPig in munPigList)
                           task.task_pig.Add(new task_pig() {
                               PigId = munPig.PigId
                             });
                    }
                    else
                    {
                        taskPigsty.PigstyId = pigstyService.GetPigsty(number, pighouseid).Id;
                        task.task_pigsty.Add(taskPigsty);
                    }
                }
            }

            task.task_prevention.Add(new task_prevention
                                         {
                                             PreventionTypeId = long.Parse(newTask.DiseaseName)
                                         });

            var taskService = new TaskService();
            taskService.Insert(task);

            return true;
        }