/// <summary>
 ///     返回同一异常的一类生猪猪栏
 /// </summary>
 /// <param name="inspection"></param>
 /// <returns></returns>
 public Dictionary<long, string> GetPigstyInfoNums(inspection inspection)
 {
     long exceptionTypeId = 0;
     var pigstyStr = new Dictionary<long, string>();
     string str = "first";
     int length;
     foreach (
         inspection_exception inspectionItem in inspection.inspection_exception.OrderBy(m => m.ExceptionTypeId))
     {
         if (exceptionTypeId != inspectionItem.ExceptionTypeId)
         {
             length = str.Length;
             pigstyStr.Add(exceptionTypeId, str.Substring(0, length - 1));
             str = "";
             str += inspectionItem.pigsty.Number + ",";
             exceptionTypeId = inspectionItem.ExceptionTypeId;
         }
         else
         {
             str += inspectionItem.pigsty.Number + ",";
         }
     }
     length = str.Length;
     pigstyStr.Add(exceptionTypeId, str.Substring(0, length - 1));
     pigstyStr.Remove(0);
     return pigstyStr;
 }
 /// <summary>
 ///     清空相应的巡查猪舍的异常
 /// </summary>
 /// <param name="inspection"></param>
 /// <returns></returns>
 public bool ClearInspectionException(inspection inspection)
 {
     List<inspection_exception> inspectionExceptionList = inspection.inspection_exception.ToList();
     foreach (inspection_exception exception in inspectionExceptionList)
     {
         var inspectionExceptionService = new InspectionExceptionService();
         inspectionExceptionService.Delete(inspectionExceptionService.Find(exception.Id));
     }
     return true;
 }
        /// <summary>
        ///     新建巡查信息
        /// </summary>
        /// <param name="newInfo">巡查页面类</param>
        /// <returns></returns>
        public bool CreateInspectInfo(InspectInfo newInfo)
        {
            if (newInfo == null) return false;
            var inspectionService = new InspectionService();
            var taskStatusTypeService = new TaskStatusTypeService();
            var taskService = new TaskService();
            Dictionary<long, Dictionary<long, long>> pigstyAndexception = SortPigstyAndexception(newInfo);
            foreach (var inspectDetail in pigstyAndexception)
            {
                InspectDetail newInfoDetail =
                    newInfo.InspectDetail.FirstOrDefault(m => m.PigHouse.Equals(inspectDetail.Key + ""));
                if (newInfoDetail == null) continue;
                var inspection = new inspection
                                     {
                                         TaskId = long.Parse(newInfo.TaskNumber),
                                         EmployeeId = long.Parse(newInfo.EmployeeName),
                                         PigHouseId = inspectDetail.Key,
                                         Time = newInfoDetail.DetailTime,
                                         Memo = newInfo.Meno
                                     };

                foreach (var pigsty in inspectDetail.Value)
                {
                    inspection.inspection_exception.Add(new inspection_exception
                                                            {
                                                                PigstyId = pigsty.Key,
                                                                ExceptionTypeId = pigsty.Value
                                                            });
                }

                inspectionService.Insert(inspection);
            }
            task_status_type taskStatusType = taskStatusTypeService.FindByName("正在进行");
            task task = taskService.Find(long.Parse(newInfo.TaskNumber));
            if (taskStatusType != null && task != null)
            {
                task.TaskStatusId = taskStatusType.Id;
                taskService.Update(task);
            }
            return true;
        }
        public string InspectDataImport(TaskImportModel taskImportModel, string memo)
        {
            int taskflag = 0; //标志,判断当天是否有巡查任务
            long employeeId = _employeeService.FindByNumber(taskImportModel.EmployeeNumber).Id;
            List<task_employee> taskEmployee = _taskEmployeeService.FindByEmployeeId(employeeId);
            if (taskEmployee != null)
            {
                long taskId = JudgeHaveTaskCirculate(taskEmployee, taskImportModel.TaskType, taskImportModel);
                if (taskId != 0)
                {
                    taskImportModel.TaskNumber = _taskService.Find(taskId).TaskNumber;
                    taskflag = 1;
                }
            }
            //当天有巡查任务
            if (taskflag == 1)
            {
                foreach (RawDataDetail detail in taskImportModel.RawDataDetails)
                {
                    //当猪舍不为空,而其他都为空的时候,导入数据库
                    if (detail.PigHouseId != 0 && detail.PigstyId == 0 && detail.PigId == 0)
                    {
                        var inspection = new inspection
                                             {
                                                 EmployeeId = employeeId,
                                                 PigHouseId = detail.PigHouseId,
                                                 Memo = memo,
                                                 Time = detail.Time,
                                                 TaskId = _taskService.FindByTaskNumber(taskImportModel.TaskNumber).Id
                                             };
                        _inspectionService.Insert(inspection);
                    }
                }
                //修改任务状态
                task task = _taskService.FindByTaskNumber(taskImportModel.TaskNumber);
                if (task.EndTime != null)
                {
                    if (task.EndTime <= taskImportModel.EndTime)
                    {
                        task.TaskStatusId = 3;
                    }
                    else
                    {
                        task.TaskStatusId = 2;
                    }
                }
                else
                {
                    task.TaskStatusId = 2;
                }
                _taskService.Update(task);
            }
            else
            {
                return "请确保" + taskImportModel.EmployeeName + "的任务是否选择正确,或者该员工没有" + taskImportModel.TaskType + "的任务";
            }

            return "";
        }