/// <summary>
 ///     查看转栏信息
 /// </summary>
 /// <param name="taskNumber">任务编号</param>
 /// <returns></returns>
 public TransferPigstyInfo CheckTransferPigstyInfo(string taskNumber)
 {
     var checkTransferPigstyInfo = new TransferPigstyInfo
                                       {TransferPigstyInfoDetail = new List<TransferPigstyInfoDetail>()};
     var taskEmployee = new TaskEmployeeService();
     var taskService = new TaskService();
     var transferService = new TransferService();
     task transferPigstyTask = taskService.FindByTaskNumber(taskNumber);
     if (transferPigstyTask != null)
     {
         checkTransferPigstyInfo.TaskNumber = taskNumber;
         checkTransferPigstyInfo.EmployeeName = taskEmployee.GetEmployeeNames(transferPigstyTask.Id);
         transfer firstOrDefault = transferService.FindByTaskId(transferPigstyTask.Id).FirstOrDefault();
         if (firstOrDefault != null)
             checkTransferPigstyInfo.Meno = firstOrDefault.Memo;
         foreach (
             transfer transfer in
                 transferService.FindByTaskId(transferPigstyTask.Id).OrderByDescending(m => m.Time))
         {
             transfer lastPigsty = transferService.GetLastTransfer(transfer.PigId, transferPigstyTask.Id);
             if (lastPigsty != null && lastPigsty.pigsty.Id != transfer.pigsty.Id)
                 checkTransferPigstyInfo.TransferPigstyInfoDetail.Add(new TransferPigstyInfoDetail
                                                                          {
                                                                              SourcePigHouse =
                                                                                  lastPigsty.pigsty.pig_house.
                                                                                             pig_house_type.Name +
                                                                                  lastPigsty.pigsty.pig_house.
                                                                                             Number,
                                                                              SourcePigsty =
                                                                                  lastPigsty.pigsty.Number + "",
                                                                              Pig = transfer.pig.Number,
                                                                              TargetPigHouse =
                                                                                  transfer.pigsty.pig_house.
                                                                                           pig_house_type.Name +
                                                                                  transfer.pigsty.pig_house.
                                                                                           Number,
                                                                              TargetPigsty =
                                                                                  transfer.pigsty.Number + "",
                                                                              TransferPigstyTime = transfer.Time
                                                                          });
             else
                 checkTransferPigstyInfo.TransferPigstyInfoDetail.Add(new TransferPigstyInfoDetail
                                                                          {
                                                                              SourcePigHouse = "",
                                                                              SourcePigsty = "",
                                                                              Pig = transfer.pig.Number,
                                                                              TargetPigHouse =
                                                                                  transfer.pigsty.pig_house.
                                                                                           pig_house_type.Name +
                                                                                  transfer.pigsty.pig_house.
                                                                                           Number,
                                                                              TargetPigsty =
                                                                                  transfer.pigsty.Number + "",
                                                                              TransferPigstyTime = transfer.Time
                                                                          });
         }
     }
     return checkTransferPigstyInfo;
 }
        public ActionResult EditTransferPigstyInfo(TransferPigstyInfo updateTransferPigstyInfo)
        {
            if (updateTransferPigstyInfo != null)
            {
                if (updateTransferPigstyInfo.UpdateTransferPigstyInfo(updateTransferPigstyInfo))
                {
                    ViewBag.Title = "修改成功";
                    return View("Message");
                }
            }

            ViewBag.Title = "修改失败,请重新修改";
            return View("Message");
        }
        public ActionResult CheckTransferPigstyInfo(string taskNumber)
        {
            if (Session["CheckFeed"] == null || !(bool) Session["CheckTransferPigstyInfo"])
            {
                Response.Write("<script>history.go(0);</script>");
                return View("Error");
            }

            TransferPigstyInfo checkTransferPigstyInfo = new TransferPigstyInfo().CheckTransferPigstyInfo(taskNumber);
            if (checkTransferPigstyInfo != null)
            {
                ViewBag.Title = "查看转栏信息";
                return View(checkTransferPigstyInfo);
            }
            ViewBag.Title = "查看失败,请重新进入";
            return View("Message");
        }
        public ActionResult EditTransferPigstyInfo(string taskNumber)
        {
            if (Session["CheckFeed"] == null || !(bool) Session["EditTransferPigstyInfo"] ||
                !(bool) Session["CheckTransferPigstyInfo"])
            {
                Response.Write("<script>history.go(0);</script>");
                return View("Error");
            }

            TransferPigstyInfo checkTransferPigstyInfo = new TransferPigstyInfo().CheckTransferPigstyInfo(taskNumber);
            if (checkTransferPigstyInfo == null)
            {
                ViewBag.Title = "查看修改失败,请重新进入";
                return View("Message");
            }
            ViewData["pigsty"] = new ListService().GetFirstPigstyList();
            ViewData["pighouse"] = new ListService().GetPigHouseList();
            ViewData["pig"] = new ListService().GetFirstPigList();
            ViewBag.Title = "修改转栏信息";
            return View("NewTransferPigstyInfo", checkTransferPigstyInfo);
        }
        /// <summary>
        ///     修改转栏信息
        /// </summary>
        /// <param name="updateTransferPigstyInfo">转栏信息页面类</param>
        /// <returns></returns>
        public bool UpdateTransferPigstyInfo(TransferPigstyInfo updateTransferPigstyInfo)
        {
            var taskService = new TaskService();
            var transferService = new TransferService();
            task transferPigstyTask = taskService.FindByTaskNumber(updateTransferPigstyInfo.TaskNumber);
            int count = 0;
            if (transferPigstyTask != null)
            {
                List<transfer> transferList =
                    transferService.FindByTaskId(transferPigstyTask.Id).OrderByDescending(m => m.Time).ToList();
                for (; count < transferList.Count; count++)
                {
                    //transfer lastPigsty = transferService.GetLastTransfer(transferList[count].PigId,
                    //                                                      transferPigstyTask.Id);
                    if (count < updateTransferPigstyInfo.TransferPigstyInfoDetail.Count)
                    {
                        if (transferList[count] != null)
                        {
                            transfer lastTransfer = transferService.Find(transferList[count].Id);
                            if (updateTransferPigstyInfo.TransferPigstyInfoDetail[count].Pig == "null")
                                updateTransferPigstyInfo.TransferPigstyInfoDetail[count].Pig = lastTransfer.PigId + "";

                            if (updateTransferPigstyInfo.TransferPigstyInfoDetail[count].SourcePigsty != null)
                                lastTransfer.PigstyId =
                                    long.Parse(updateTransferPigstyInfo.TransferPigstyInfoDetail[count].SourcePigsty);
                            if (updateTransferPigstyInfo.TransferPigstyInfoDetail[count].Pig != null)
                                lastTransfer.PigId =
                                    long.Parse(updateTransferPigstyInfo.TransferPigstyInfoDetail[count].Pig);
                            transferService.Update(lastTransfer);
                        }
                        transferList[count].PigstyId =
                            long.Parse(updateTransferPigstyInfo.TransferPigstyInfoDetail[count].TargetPigsty);
                        if (updateTransferPigstyInfo.TransferPigstyInfoDetail[count].Pig != null)
                            transferList[count].PigId =
                                long.Parse(updateTransferPigstyInfo.TransferPigstyInfoDetail[count].Pig);
                        transferList[count].Memo = updateTransferPigstyInfo.Meno;
                        transferService.Update(transferList[count]);
                    }
                    else
                    {
                        transferService = new TransferService();
                        transferService.Delete(transferService.Find(transferList[count].Id));
                    }
                }
                for (; count < updateTransferPigstyInfo.TransferPigstyInfoDetail.Count; count++)
                {
                    transferService.Insert(new transfer
                                               {
                                                   TaskId = transferPigstyTask.Id,
                                                   PigstyId =
                                                       long.Parse(
                                                           updateTransferPigstyInfo.TransferPigstyInfoDetail[count].
                                                               TargetPigsty),
                                                   PigId =
                                                       long.Parse(
                                                           updateTransferPigstyInfo.TransferPigstyInfoDetail[count].Pig),
                                                   Time =
                                                       updateTransferPigstyInfo.TransferPigstyInfoDetail[count].
                                                       TransferPigstyTime,
                                                   Memo = updateTransferPigstyInfo.Meno
                                               });
                }
                return true;
            }
            return false;
        }
        /// <summary>
        ///     新建转栏信息
        /// </summary>
        /// <param name="newTransferPigstyInfo">转栏信息页面类</param>
        /// <returns>是否成功</returns>
        public bool CreateTransferPigstyInfo(TransferPigstyInfo newTransferPigstyInfo)
        {
            if (newTransferPigstyInfo == null) return false;
            var transferService = new TransferService();
            var taskStatusTypeService = new TaskStatusTypeService();
            var taskService = new TaskService();
            foreach (TransferPigstyInfoDetail transferPigstyInfoDetail in newTransferPigstyInfo.TransferPigstyInfoDetail
                )
            {
                transferService.Insert(new transfer
                                           {
                                               TaskId = long.Parse(newTransferPigstyInfo.TaskNumber),
                                               PigstyId = long.Parse(transferPigstyInfoDetail.TargetPigsty),
                                               PigId = long.Parse(transferPigstyInfoDetail.Pig),
                                               Time = transferPigstyInfoDetail.TransferPigstyTime,
                                               Memo = newTransferPigstyInfo.Meno
                                           });
            }
            task_status_type taskStatusType = taskStatusTypeService.FindByName("已完成");
            task task = taskService.Find(long.Parse(newTransferPigstyInfo.TaskNumber));
            if (taskStatusType != null && task != null)
            {
                task.TaskStatusId = taskStatusType.Id;
                taskService.Update(task);
            }

            return true;
        }
        public ActionResult NewTransferPigstyInfo()
        {
            if (Session["CheckFeed"] == null || !(bool) Session["AddTransferPigstyInfo"] ||
                !(bool) Session["CheckTransferPigstyInfo"])
            {
                Response.Write("<script>history.go(0);</script>");
                return View("Error");
            }

            var newTransferPigstyInfo = new TransferPigstyInfo();
            var taskEmployee = new TaskEmployeeService();
            IEnumerable<SelectListItem> taskNumberList = newTransferPigstyInfo.GetTaskNumberSelect(TaskTypeId);
            if (!taskNumberList.Any())
            {
                const string returnstring = "任务为空,请先添加任务";
                Response.Write("<script language='JavaScript'>alert('" + returnstring + ".');history.go(-1);</script>");
                return Content("");
            }
            SelectListItem firstOrDefault = newTransferPigstyInfo.GetTaskNumberSelect(TaskTypeId).FirstOrDefault();
            if (firstOrDefault != null)
                newTransferPigstyInfo.EmployeeName = taskEmployee.GetEmployeeNames(long.Parse(firstOrDefault.Value));
            ViewData["pigsty"] = new ListService().GetFirstPigstyList();
            ViewData["pighouse"] = new ListService().GetPigHouseList();
            ViewData["pig"] = new ListService().GetFirstPigList();
            ViewData["taskNumber"] = newTransferPigstyInfo.GetTaskNumberSelect(TaskTypeId);

            ViewBag.Title = "新建转栏信息";
            return View(newTransferPigstyInfo);
        }
        public ActionResult TransferPigstyInfo(int pageIndex = 1)
        {
            if (Session["CheckFeed"] == null || !(bool) Session["CheckTransferPigstyInfo"])
            {
                Response.Write("<script>history.go(0);</script>");
                return View("Error");
            }

            var transferPigstyInfo = new TransferPigstyInfo();

            const int pageSize = 25;

            PagedList<Info> infoPagerList;
            if (Request.Form["CheckEmployee"] != null)
            {
                // Session["TransferPigstyChoose"] = Request.Form["SearchChose"];
                Session["check"] = Request.Form["CheckEmployee"];
                Session["chooseid"] = Request.Form["SearchChose"];
                infoPagerList =
                    transferPigstyInfo.GetInfoPagedList(
                        transferPigstyInfo.GetInfoList(transferPigstyInfo.GetTransferPigstyInfoList(),
                                                       Request.Form["CheckEmployee"], Request.Form["SearchChose"]),
                        pageIndex, pageSize);
                //  Session["SearchTransferPigsty"] = Request.Form["CheckEmployee"];
            }
            else if (Session["check"] != null && Session["chooseid"] != null && Request.HttpMethod == "POST")
            {
                infoPagerList =
                    transferPigstyInfo.GetInfoPagedList(
                        transferPigstyInfo.GetInfoList(transferPigstyInfo.GetTransferPigstyInfoList(),
                                                       Session["check"] as string, Session["chooseid"] as string),
                        pageIndex, pageSize);
            }
            else
            {
                // Session["TransferPigstyChoose"] = new ListService().GetInfoSearchList().FirstOrDefault().Value;
                Session["check"] = "";
                Session["chooseid"] = "";
                infoPagerList = transferPigstyInfo.GetInfoPagedList(transferPigstyInfo.GetTransferPigstyInfoList(),
                                                                    pageIndex, pageSize);
            }
            if (Request.IsAjaxRequest())
            {
                return PartialView("UCJqInfoList", infoPagerList);
            }
            ViewData["Search"] = new ListService().GetInfoSearchList();
            return View(infoPagerList);
        }
        public ActionResult NewTransferPigstyInfo(TransferPigstyInfo newTransferPigstyInfo)
        {
            ViewBag.Title = newTransferPigstyInfo.CreateTransferPigstyInfo(newTransferPigstyInfo)
                                ? "新建转栏信息成功"
                                : "新建转栏信息失败,请重新创建";

            return View("Message");
        }