Beispiel #1
0
        //将生猪的信息导入到数据库中
        public void ImportNewPig(BatchAddPigsModel model)
        {
            foreach (PigsModel t in model.Pigs)
            {
                //父代不为空
                string[] maleparent = null;
                if (t.MaleParentNumber != null)
                {
                    maleparent = t.MaleParentNumber.Split(new[] {','});
                }
                var pig = new pig
                              {
                                  Number = t.PigNumber,
                                  RFID = t.Rfid,
                                  Birthday = t.Birthday,
                                  PigTypeId = t.PigTypeId
                              };
                if (model.FemaleParentId != 0)
                {
                    pig.FemaleParentId = model.FemaleParentId;
                }

                //耳号不为空
                if (t.EarNumber != null)
                {
                    pig.EarNumber = Convert.ToInt64(t.EarNumber);
                }
                //父代若不为空再插入数据库
                if (maleparent != null)
                {
                    if (maleparent.Length == 1)
                    {
                        pig.MostLikelyMaleParentId = PigService.FindByNumber(maleparent[0]).Id;
                    }
                    if (maleparent.Length == 2)
                    {
                        pig.MostLikelyMaleParentId = PigService.FindByNumber(maleparent[0]).Id;
                        pig.SecondLikelyMaleParentId = PigService.FindByNumber(maleparent[1]).Id;
                    }
                    if (maleparent.Length == 3)
                    {
                        pig.MostLikelyMaleParentId = PigService.FindByNumber(maleparent[0]).Id;
                        pig.SecondLikelyMaleParentId = PigService.FindByNumber(maleparent[1]).Id;
                        pig.ThirdLikelyMaleParentId = PigService.FindByNumber(maleparent[2]).Id;
                    }
                }
                PigService.Insert(pig);
                string rfid = t.Rfid;
                //插入每头猪的转栏信息
                var transfer = new transfer
                                   {
                                       TaskId = model.TaskId,
                                       PigstyId = model.PigstyId,
                                       Time = t.Birthday,
                                       PigId = PigService.FindByRfid(rfid).Id
                                   };
                TransferService.Insert(transfer);
            }
        }
        public List<BatchAddPigsModel> BatchAddPigs()
        {
            _batchAddPigsModels = new List<BatchAddPigsModel>();
            _allTaskImportModel = new AllTaskImportModel();
            TaskImportModel dtaskImportModel = null;
            //若存在c:\pigrecorderd这个目录
            if (Directory.Exists(@"c:\pigrecorder"))
            {
                _taskImportModels = new List<TaskImportModel>();
                string[] strFiles = Directory.GetFiles(@"c:\pigrecorder");
                foreach (
                    TaskImportModel rTaskImportModel in
                        strFiles.Select(strFile => _allTaskImportModel.GetFilearray(strFile))
                                .SelectMany(rTaskImportModels => rTaskImportModels))
                {
                    _taskImportModels.Add(rTaskImportModel);
                }
                _allTaskImportModel.TaskImportModels = _taskImportModels;
                //首先先从文件中找到有添加生猪任务的人员,然后在根据他用pda所采集的信息中是否有母猪来判断这个任务是不是添加生猪任务的信息
                foreach (TaskImportModel taskImportModel in _taskImportModels)
                {
                    int taskflag = 0; //标志,判断当天是否有添加生猪的任务
                    BatchAddPigsModel batchAddPigsModel = null;
                    List<PigsModel> pigs = null;
                    long employeeId = _employeeService.FindByNumber(taskImportModel.EmployeeNumber).Id;
                    List<task_employee> taskEmployee = _taskEmployeeService.FindByEmployeeId(employeeId);
                    if (taskEmployee != null)
                    {
                        //在所有的任务里判断在这一天是否有添加生猪任务
                        TaskImportModel model = taskImportModel;
                        foreach (task_employee taskemployee in from taskemployee in taskEmployee
                                                               let taskTypeId =
                                                                   _taskService.Find(taskemployee.TaskId).TaskTypeId
                                                               where _taskTypeService.Find(taskTypeId).Name == "添加生猪"
                                                               let startTime =
                                                                   _taskService.Find(taskemployee.TaskId).StartTime
                                                               where
                                                                   startTime.ToShortDateString() ==
                                                                   model.StartTime.ToShortDateString()
                                                               select taskemployee)
                        {
                            taskflag = 1;
                            taskImportModel.TaskNumber = _taskService.Find(taskemployee.TaskId).TaskNumber;
                        }
                    }
                    //这个工作人员今天有添加生猪的任务,还要对其所采集的数据判断,这份数据是否属于添加生猪的数据
                    if (taskflag != 1) continue;
                    int addPigFlag = 1; //判断是否是添加生猪的任务
                    foreach (RawDataDetail detail in
                            taskImportModel.RawDataDetails.Where(
                                detail => _pigService.FindByRfid(detail.Rfid) == null &&
                                          _pigstyService.FindByRfid(detail.Rfid) == null &&
                                          _pigHouseService.FindByRfid(detail.Rfid) == null &&
                                          _employeeService.FindByRfid(detail.Rfid) == null))
                    {
                        addPigFlag = 0;
                    }
                    //采集数据中有母猪的RFID而且有未识别的RFID,说明该任务是添加生猪的任务
                    if (addPigFlag != 0) continue;
                    dtaskImportModel = taskImportModel;
                    foreach (RawDataDetail detail in taskImportModel.RawDataDetails)
                    {
                        //找到猪栏的rfid
                        if (detail.PigstyId != 0 && detail.PigId == 0)
                        {
                            batchAddPigsModel = new BatchAddPigsModel
                                                    {
                                                        EmployeeName = taskImportModel.EmployeeName,
                                                        TaskId =
                                                            _taskService.FindByTaskNumber(
                                                                taskImportModel.TaskNumber).Id,
                                                        PigstyId = detail.PigstyId,
                                                        PigstyNumber = detail.PigstyNumber,
                                                        PigHouseId = detail.PigHouseId,
                                                        PigHouseName = detail.PigHouseName,
                                                        PigHouseNumber = detail.PigHouseNumber
                                                    };

                            _batchAddPigsModels.Add(batchAddPigsModel);

                            pigs = new List<PigsModel>();
                            batchAddPigsModel.Pigs = pigs;
                        }
                        else if (detail.PigId != 0)
                        {
                            //找到母猪的rfid
                            if (_pigService.Find(detail.PigId).pig_type.Name == "母猪")
                            {
                                if (batchAddPigsModel != null)
                                {
                                    batchAddPigsModel.FemaleParentId = detail.PigId;
                                    batchAddPigsModel.FemaleParentNumber = detail.PigNumber;
                                }
                            }
                        }
                        //找到生猪的rfid
                        if (detail.PigId != 0 || detail.PigHouseId != 0 || detail.PigstyId != 0 || detail.Rfid == null)
                            continue;
                        var addPig = new PigsModel {Birthday = detail.Time.AddDays(-20)};
                        if (batchAddPigsModel != null)
                        {
                            addPig.PigNumber = detail.Rfid;
                            addPig.FemaleParentId = batchAddPigsModel.FemaleParentId;
                            addPig.PigTypeId = _pigTypeService.FindByName("仔猪").Id;
                            addPig.FemaleParentNumber = batchAddPigsModel.FemaleParentNumber;
                        }
                        addPig.Rfid = detail.Rfid;
                        if (pigs == null) continue;
                        pigs.Add(addPig);
                        batchAddPigsModel.Pigs = pigs;
                    }
                }
            }
            else
            {
                Directory.CreateDirectory(@"c:\pigrecorder");
            }

            //删除猪栏下的猪都导入的BatchAddPigsModel
            List<BatchAddPigsModel> rBatchAddPigsModels =
                _batchAddPigsModels.Where(t => t != null && t.Pigs.Count != 0).ToList();
            //如果这个任务下没有生猪可以导入,则将该任务从原始数据剪切到import目录下
            if (rBatchAddPigsModels.Count == 0 && dtaskImportModel != null)
            {
                _allTaskImportModel.CutFileAndSaveData(dtaskImportModel);
            }
            return rBatchAddPigsModels;
        }
        public ActionResult ImportBatchAddPigs(BatchAddPigsModel model)
        {
            if (Session["CheckFeed"] == null || !(bool) Session["AddAddNewPig"] || !(bool) Session["CheckAddNewPig"])
            {
                Response.Write("<script>history.go(0);</script>");
                return View("Error");
            }

            try
            {
                //将生猪导入数据库
                _pigsModel.ImportNewPig(model);
            }
            catch (Exception)
            {
                Response.Write("<script language='JavaScript'>alert('导入过程出现未知错误.');history.go(-1);</script>");
            }
            return View();
        }