/// <summary> /// 获取用户所有网关下的所有的强电箱 /// </summary> /// <param name="openId">用户微信唯一Id值</param> /// <returns></returns> public JsonResult GetUserAllEle(string openId) { //使用SQl语句嵌套查询 string sql = _config.GetUserAllEle + openId + _config.PTP; /*"SELECT iot_elebox.*,MAC FROM iot_elebox LEFT JOIN iot_gateway ON iot_elebox.GateWayId =iot_gateway.ID WHERE GateWayID IN ( " + * "SELECT GateWayID FROM iot_user_gateway WHERE UserId = (" + * $"SELECT Id FROM iot_user WHERE OpenId = '{openId}'))";*/ var temp = db.Database.SqlQuery <iot_elebox>(sql).ToList(); if (temp == null) { return(Json(new { statu = StatusCode.NotFound, Message = "未找到任何设备" }, JsonRequestBehavior.AllowGet)); } List <iot_elebox> list = new List <iot_elebox>(); //将得到的数据使用result保存起来 foreach (var item in temp) { var result = new iot_elebox { ID = item.ID, GateWayId = item.GateWayId, Name = item.Name, Uid = item.Uid, Position = item.Position, CreateTime = item.CreateTime, MAC = item.MAC, EditTime = item.EditTime }; list.Add(result); } return(Json(list, JsonRequestBehavior.AllowGet)); }
public JsonResult AddEle([System.Web.Http.FromBody] iot_elebox model) { if (util.IsExist(2, model.Uid)) { return(Json(new { status = StatusCode.FAIL, meassage = "该强电箱已被添加,请勿重复添加" })); } iot_elebox ele = new iot_elebox(); var taskinfo = new { status = StatusCode.SUCCESS, message = "添加成功" }; //判断是否在空值表中存在该类型的position 如果有就将该position值赋给该模型 并且在空值表中删除该条目 int position = util.GetGateWayPositon(2, model.GateWayId); if (position > 7) { return(Json(new { status = StatusCode.FAIL, message = "强电箱负载达到最大值" })); } ele.Position = position; ele.Name = model.Name; ele.Uid = model.Uid; ele.GateWayId = model.GateWayId; ele.CreateTime = new DateTime().Date; db.iot_elebox.Add(ele); int uprows = db.SaveChanges(); if (uprows < 1) { taskinfo = new { status = StatusCode.FAIL, message = "添加失败" }; } return(Json(taskinfo, JsonRequestBehavior.DenyGet)); }