//
 // GET: /DataImport/BatchAddPigsties/
 public ActionResult BatchAddPigsties()
 {
     var listService = new ListService();
     ViewData["PigHouseId"] = listService.GetPigHouseList();
     ViewBag.Title = "批量添加猪栏";
     var batchAddPigstiesModel = new BatchAddPigstiesModel();
     return View(batchAddPigstiesModel.GetAllPigstiesRfid());
 }
 public ActionResult BatchAddPigsties(BatchAddPigstiesModel models)
 {
     try
     {
         var pigstyService = new PigstyService();
         foreach (AddPigstiesModel model in models.AddPigstiesModels)
         {
             var pigsty = new pigsty
                              {
                                  RFID = model.Rfid,
                                  Number = model.PigstyNumber,
                                  PigHouseId = models.PigHouseId,
                                  Capacity = model.Capacity
                              };
             pigstyService.Insert(pigsty);
         }
     }
     catch
     {
         Response.Write("<script language='JavaScript'>alert('有误!!');history.go(0);</script>");
     }
     ViewBag.Title = "成功导入";
     return View("Message");
 }
        public BatchAddPigstiesModel GetAllPigstiesRfid()
        {
            var batchAddPigstiesModel = new BatchAddPigstiesModel();
            var addPigstiesModels = new List<AddPigstiesModel>();
            batchAddPigstiesModel.AddPigstiesModels = addPigstiesModels;
            if (!Directory.Exists(@"c:\pigrecorder\pigsty"))
            {
                // 如果目录不在,则创建
                Directory.CreateDirectory(@"c:\pigrecorder\pigsty");
            }
            string[] strFiles = Directory.GetFiles(@"c:\pigrecorder\pigsty");
            foreach (string strFile in strFiles)
            {
                //读取指定文件夹下面的指定文件
                string[] lineList = File.ReadAllLines(strFile, Encoding.Default);
                //单个文件中原始的记录的2维字符串数组
                var array = new string[lineList.Length,2];
                int i = 0;
                int length = lineList.Length;
                foreach (string line in lineList)
                {
                    string[] elementList = line.Split(',');
                    for (int j = 0; j < elementList.Length; j++)
                    {
                        if (elementList[0] != "")
                            array[i, j] = elementList[j];
                    }
                    if (elementList[0] != "")
                        i++;
                    else
                        length--;
                }

                var newarray = new List<string>();
                for (i = 0; i < length; i++)
                {
                    int d = 0; //判断是否重复
                    foreach (string ind in newarray)
                    {
                        if (ind == array[i, 0])
                        {
                            d = 1;
                        }
                    }
                    if (d == 0)
                    {
                        newarray.Add(array[i, 0]);
                    }
                }
                int pigstyNumber = 0;
                for (i = 0; i < newarray.Count; i++)
                {
                    var addPigstiesModel = new AddPigstiesModel();
                    var pigstyService = new PigstyService();
                    if (pigstyService.FindByRfid(newarray[i]) == null)
                    {
                        addPigstiesModel.Rfid = newarray[i];
                        addPigstiesModel.PigstyNumber = ++pigstyNumber;
                        addPigstiesModels.Add(addPigstiesModel);
                    }
                }
            }
            return batchAddPigstiesModel;
        }