Exemple #1
0
        public ActionResult Create(Device device, String attributesJson)
        {
            //此处不用能GetUser(),因为在EF中db是有状态的,并且不能修改,GetUser里的db是另一个状态的db
            //会提示“一个实体对象不能由多个 IEntityChangeTracker 实例引用。”的悲催错误
            device.UserInfo = db.UserInfoes.FirstOrDefault(u => u.Name == User.Identity.Name);
            List<Teshe.Models.Attribute> attrList = JsonConvert.DeserializeObject<List<Teshe.Models.Attribute>>(attributesJson);
            device.Attributes = attrList;
            if (ModelState.IsValid)
            {
                db.Devices.Add(device);
                db.SaveChanges();
                log.Info("用户" + User.Identity.Name + "于" + DateTime.Now.ToString() + "添加设备" + device.Name);
                return RedirectToAction("Index");
            }

            return View(device);
        }
Exemple #2
0
 public ActionResult ExportExcel(String data)
 {
     Response.ContentType = "text/plain";
     List<Device> list = JsonConvert.DeserializeObject<List<Device>>(data, dateTimeConverter);
     Device device = new Device();
     //Response.ContentType = "application/vnd.ms-excel;charset=UTF-8";
     //Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "temp.xls"));
     //Response.Clear();
     return File(device.Export(list).GetBuffer(), "application/vnd.ms-excel;charset=UTF-8", "data.xls");
 }
Exemple #3
0
        public ActionResult Edit(Device device, String attributesJson)
        {
            List<DeviceModifyRecord> recordList = new List<DeviceModifyRecord>();
            if (!String.IsNullOrEmpty(attributesJson))
            {
                List<Teshe.Models.Attribute> attrListOld = db.Attributes.Where<Teshe.Models.Attribute>(u => u.Device.Id == device.Id).ToList();
                List<Teshe.Models.Attribute> attrList = new List<Models.Attribute>();
                foreach (var i in attrListOld)
                {
                    db.Attributes.Remove(i);
                }
                if (attributesJson.Length > 5)
                {
                    attrList = new List<Models.Attribute>(JsonConvert.DeserializeObject<List<Teshe.Models.Attribute>>(attributesJson));
                }
                foreach (var i in attrList)
                {
                    db.Attributes.Add(i);
                }

                int oldc = attrListOld.Count;
                int newc = attrList.Count;
                if (oldc - newc > 0)
                {
                    int sign = 0;
                    foreach (var i in attrListOld)
                    {
                        DeviceModifyRecord record = new DeviceModifyRecord();
                        record.Device = device;
                        if (attrList.Count > sign)
                        {
                            if (i.Name != attrList[sign].Name || i.Content != attrList[sign].Content)
                            {
                                record.Content = "用户\"" + User.Identity.Name + "\"于\"" + DateTime.Now.ToString() + "\"将设备属性\"" + i.Name + "\":\"" + i.Content + "\"改为\"" + attrList[sign].Name + "\":\"" + attrList[sign].Content + "\"";
                                recordList.Add(record);
                            }
                        }
                        else
                        {
                            record.Content = "用户\"" + User.Identity.Name + "\"于\"" + DateTime.Now.ToString() + "\"删除设备属性\"" + i.Name + "\":\"" + i.Content + "\"";
                            recordList.Add(record);
                        }
                        sign++;
                    }

                }
                else
                {
                    int sign = 0;
                    foreach (var i in attrList)
                    {
                        DeviceModifyRecord record = new DeviceModifyRecord();
                        record.Device = device;
                        if (attrListOld.Count > sign)
                        {
                            if (attrListOld[sign].Name != i.Name || attrListOld[sign].Content != i.Content)
                            {
                                record.Content = "用户\"" + User.Identity.Name + "\"于\"" + DateTime.Now.ToString() + "\"将设备属性\"" + attrListOld[sign].Name + "\":\"" + attrListOld[sign].Content + "\"改为\"" + i.Name + "\":\"" + i.Content + "\"";
                                recordList.Add(record);
                            }
                        }
                        else
                        {
                            record.Content = "用户\"" + User.Identity.Name + "\"于\"" + DateTime.Now.ToString() + "\"添加设备属性\"" + i.Name + "\":\"" + i.Content + "\"";
                            recordList.Add(record);
                        }
                        sign++;

                    }
                }
                //for (int i = 0; i < Math.Abs(attrListOld.Count - attrList.Count) - 1; i++)
                //{

                //}

                device.Attributes = attrList;
            }

            //防止转换错误
            if (ModelState.IsValid)
            {

                db.Entry(device).State = EntityState.Modified;
                DbPropertyValues proOld = db.Entry(device).GetDatabaseValues();
                DbPropertyValues proNew = db.Entry(device).CurrentValues;
                foreach (var p in proOld.PropertyNames)
                {
                    var pro = device.GetType().GetProperty(p);
                    object[] attr = pro.GetCustomAttributes(typeof(System.ComponentModel.DisplayNameAttribute), false);
                    String displayName = "";
                    if (attr.Count(u => u.GetType() == typeof(System.ComponentModel.DisplayNameAttribute)) > 0)
                    {
                        displayName = ((System.ComponentModel.DisplayNameAttribute)attr[0]).DisplayName;
                    }
                    if (proOld[p] == null && proNew[p] != null)
                    {
                        DeviceModifyRecord record = new DeviceModifyRecord();
                        record.Device = device;
                        record.Content = "用户\"" + User.Identity.Name + "\"于\"" + DateTime.Now.ToString() + "\"将设备\"" + device.Name + "\"的\"" + displayName + "\"字段由\"" + proOld[p] + "\"改为\"" + proNew[p] + "\"";
                        recordList.Add(record);
                    }
                    if (p == "InputTime")
                    {
                        continue;
                    }
                    else if (proOld[p] != null && proNew[p] != null && proOld[p].ToString() != proNew[p].ToString())
                    {
                        DeviceModifyRecord record = new DeviceModifyRecord();
                        record.Device = device;
                        record.Content = "用户\"" + User.Identity.Name + "\"于\"" + DateTime.Now.ToString() + "\"将设备\"" + device.Name + "\"的\"" + displayName + "\"字段由\"" + proOld[p] + "\"改为\"" + proNew[p] + "\"";
                        recordList.Add(record);
                    }
                }
                foreach (var r in recordList)
                {
                    db.DeviceModifyRecords.Add(r);
                }
                db.SaveChanges();

                //PropertyInfo[] pro = device.GetType().GetProperties();
                //foreach (var p in pro)
                //{
                //    if (db.Entry(device).Property(p.Name).IsModified)
                //    {

                //    }
                //    else
                //    {
                //        continue;
                //    }
                //}
                log.Info("用户" + User.Identity.Name + "于" + DateTime.Now.ToString() + "修改设备" + device.Name);
                return RedirectToAction("Index");
            }
            return View(device);
        }