public void Save(DeviceDataDefineModel entity)
 {
     using (var db = new HXContext())
     {
         db.Entry <DeviceDataDefineModel>(entity).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
     }
 }
 public void Add(DeviceDataDefineModel entity)
 {
     using (var db = new HXContext())
     {
         db.DeviceDataDefine.Add(entity);
         db.SaveChanges();
     }
 }
 public void Remove(DeviceDataDefineModel entity)
 {
     using (var db = new HXContext())
     {
         var d = db.DeviceControlData.Where(a => a.DataDefineId == entity.Id).ToList();
         db.DeviceControlData.RemoveRange(d);
         db.Entry <DeviceDataDefineModel>(entity).State = System.Data.Entity.EntityState.Deleted;
         db.SaveChanges();
     }
 }
Пример #4
0
        public DeviceDataDefineViewModel DataDefineAdd(DeviceDataDefineViewModel dvm)
        {
            //验证用户是否有编辑的权限,验证该条数据是否已经被添加过,添加数据
            DeviceModel dm = new DeviceRepository().FindDeviceAndDataDefine(dvm.DeviceSn, dvm.Token);

            if (dm == null)
            {
                dvm.Success = false;
                dvm.Message = "不存在关联的设备";
                return(dvm);
            }
            #region 验证用户权限
            int  projectId = dm.ProjectId.Value;
            bool bRet      = new UserService().IsAuthProject(dvm.Account, dvm.Token, projectId, 1);
            if (!bRet)
            {
                dvm.Success = false;
                dvm.Message = "该用户无添加设备的权限";
                return(dvm);
            }
            #endregion

            DeviceDataDefineModel ddm = dm.DeviceDataDefine.Where(a => a.DataKey == dvm.DataKey).FirstOrDefault();
            if (ddm != null)
            {
                dvm.Success = false;
                dvm.Message = "已存在此数据键";
                return(dvm);
            }
            try
            {
                ddm              = new DeviceDataDefineModel();
                ddm.DataKey      = dvm.DataKey;
                ddm.DataName     = dvm.DataName;
                ddm.DataType     = dvm.DataType;
                ddm.DefaultValue = dvm.DefaultValue;
                ddm.Unit         = dvm.Unit;
                ddm.PanelId      = dvm.PanelId;
                ddm.DeviceSn     = dm.DeviceSn;
                _ddr.Add(ddm);
                dvm.Id      = ddm.Id;
                dvm.Success = true;
                dvm.Message = "添加数据栏位成功";
            }
            catch (Exception ex)
            {
                dvm.Success = false;
                dvm.Message = "添加数据栏位失败" + ex.Message;
            }
            return(dvm);
        }