龙空技术网

NET+EF+MVC如何将List列表数据存入Redis中

娱乐一分哥 367

前言:

目前各位老铁们对“mvc接收list”大体比较注重,咱们都想要剖析一些“mvc接收list”的相关文章。那么小编同时在网上搜集了一些对于“mvc接收list””的相关内容,希望大家能喜欢,朋友们一起来了解一下吧!

在开发机器人接口的时候,机器开机后会读取一些设备或学生老师一些基础数据,我们可以将这些多条信息加入到list列表中,然后存入redis中,每次只要从redis中取值就可以了(如果没有就写入Redis),这样增加了机器的运行速度,对优化机器人接口程序非常重要(接收实体嵌套实体和列表写法,然后输出json,这块以后单独写一篇文章介绍),下边笔者给了一个方法,以供参考。

一、获取学生考勤时间设置列表将其存入redis中

#region 获取学生考勤时间设置列表

public List<Api_attendancetime> GetbabyattendancetimeModel(decimal KindergartenID, decimal TrialID)

{

List<Api_attendancetime> attendancelist = new List<Api_attendancetime>();

if (TrialID > 0 && KindergartenID > 0)

{

HashOperator operators1 = new HashOperator();

string key = Utils.GetDescription(KeyEnum.LoginUserCache.HXbabyattendancetime) + KindergartenID;

var ser = new ObjectSerializer();

ef.Configuration.ProxyCreationEnabled = false;

bool IsExist = operators1.Exist<string>(key, key);

//不存在

if (IsExist != true)

{

List<gpt_baby_attendancetime> list = ef.gpt_baby_attendancetime.Where(a => a.TrialID == TrialID && a.KindergartenID == KindergartenID).AsNoTracking().ToList();

if (list != null && list.Count > 0)

{

foreach (var item in list)

{

Api_attendancetime info = new Api_attendancetime();

if (item.testingtype == 0)

{

info.testingtype = 0;

info.message = "晨检完成";

info.StartDate = item.starttime + ":00";

info.EndDate = item.endtime + ":00";

attendancelist.Add(info);

}

else if (item.testingtype == 1)

{

info.testingtype = 1;

info.message = "午检完成";

info.StartDate = item.starttime + ":00";

info.EndDate = item.endtime + ":00";

attendancelist.Add(info);

}

else if (item.testingtype == 2)

{

info.testingtype = 2;

info.message = "晚检完成";

info.StartDate = item.starttime + ":00";

info.EndDate = item.endtime + ":00";

attendancelist.Add(info);

}

}

}

else

{

Api_attendancetime info = new Api_attendancetime();

info.testingtype = 0;

info.message = "晨检完成";

info.StartDate = "00:00:00";

info.EndDate = "10:59:00";

attendancelist.Add(info);

Api_attendancetime info2 = new Api_attendancetime();

info2.testingtype = 1;

info2.message = "午检完成";

info2.StartDate = "11:00:00";

info2.EndDate = "14:59:00";

attendancelist.Add(info2);

Api_attendancetime info3 = new Api_attendancetime();

info3.testingtype = 2;

info3.message = "晚检完成";

info3.StartDate = "15:00:00";

info3.EndDate = "23:59:00";

attendancelist.Add(info3);

}

operators1.Set<byte[]>(key, key, ser.Serialize(attendancelist));

operators1.SetExpire(key, DateTime.Now.AddMinutes(30));

}

else

{

attendancelist = ser.Deserialize(operators1.Get<byte[]>(key, key)) as List<Api_attendancetime>;

}

}

return attendancelist;

}

#endregion 获取学生考勤时间设置列表

二、根据主机ID获取机器人信息

#region 根据主机ID获取机器人信息

[HttpGet]

public HttpResponseMessage GetKindergartenInfoByHostID(long _timestamp = 0, String appKey = null, String _sign = null, decimal host_id = 0)

{

HttpResponseMessage result = null;

var ReturnStr = "{\"status\":\"N\",\"msg\":\"参数错误\",\"data\":{}}";

if (host_id > 0)

{

try

{

gpt_bbw_host model = ef.gpt_bbw_host.Where(a => a.host_id == host_id).AsNoTracking().FirstOrDefault();

if (model != null)

{

//yefei 2022-08-04

gpt_teacher_attendancetime baby_m = ef.gpt_teacher_attendancetime.Where(a => a.TrialID == model.TrialID && a.KindergartenID == model.KindergartenID).AsNoTracking().FirstOrDefault();

if (model.bindingstate == 1)

{

M_bbw_host info = new M_bbw_host();

info.subsidiary_id = model.subsidiary_id; //子公司主键id

info.TrialID = model.TrialID;

info.KindergartenID = model.KindergartenID;

info.kindergartenName = model.kindergarten;

M_teacher_worktime m_Teacher_Worktime = new M_teacher_worktime();

if (baby_m != null)

{

if (!string.IsNullOrEmpty(baby_m.workstarttime) && !string.IsNullOrEmpty(baby_m.workendtime) && !string.IsNullOrEmpty(baby_m.startminrange) && !string.IsNullOrEmpty(baby_m.startmaxrange) && !string.IsNullOrEmpty(baby_m.endminrange) && !string.IsNullOrEmpty(baby_m.endmaxrange))

{

m_Teacher_Worktime.workstarttime = baby_m.workstarttime.Trim() + ":00";

m_Teacher_Worktime.workendtime = baby_m.workendtime.Trim() + ":00";

m_Teacher_Worktime.startminrange = baby_m.startminrange.Trim() + ":00";

m_Teacher_Worktime.startmaxrange = baby_m.startmaxrange.Trim() + ":00";

m_Teacher_Worktime.endminrange = baby_m.endminrange.Trim() + ":00";

m_Teacher_Worktime.endmaxrange = baby_m.endmaxrange.Trim() + ":00";

info.m_teacher_worktime = m_Teacher_Worktime;

}

else

{

m_Teacher_Worktime.workstarttime = "06:00:00";

m_Teacher_Worktime.workendtime = "18:00:00";

m_Teacher_Worktime.startminrange = "05:00:00";

m_Teacher_Worktime.startmaxrange = "11:59:00";

m_Teacher_Worktime.endminrange = "13:00:00";

m_Teacher_Worktime.endmaxrange = "23:59:00";

info.m_teacher_worktime = m_Teacher_Worktime;

}

}

else

{

m_Teacher_Worktime.workstarttime = "06:00:00";

m_Teacher_Worktime.workendtime = "18:00:00";

m_Teacher_Worktime.startminrange = "05:00:00";

m_Teacher_Worktime.startmaxrange = "11:59:00";

m_Teacher_Worktime.endminrange = "13:00:00";

m_Teacher_Worktime.endmaxrange = "23:59:00";

info.m_teacher_worktime = m_Teacher_Worktime;

}

if (!string.IsNullOrEmpty(model.up_time))

{

info.up_time = model.up_time;

}

else

{

info.up_time = "06:00";

}

if (!string.IsNullOrEmpty(model.close_time))

{

info.close_time = model.close_time;

}

else

{

info.close_time = "18:00";

}

//1;网络优先 2:离线识别(默认)

if (model.Recognition == null)

{

info.Recognition = 2;

}

else

{

info.Recognition = model.Recognition;

}

if (model.len != null)

{

info.len = (double)model.len;

}

else

{

info.len = 0;

}

//是否开启状态栏

if (model.isOpenStatusBar == 0)

{

info.isOpenStatusBar = 0;

}

else

{

info.isOpenStatusBar = 1;

}

info.isDebug = model.isDebug == 1 ? 1 : 0;

//获取晨检午检晚检时间

List<Api_attendancetime> attendancetimelist = new WisdomStars.Bll.EF.SelectListByEF().GetbabyattendancetimeModel(model.KindergartenID, model.TrialID);

info.attendancetimelist = attendancetimelist;

#region 身高体重基础信息

List<M_basic_param> constitutionlist = new List<M_basic_param>();

List<gpt_basic_param> basiclist = ef.gpt_basic_param.AsNoTracking().ToList();

foreach(var item2 in basiclist)

{

M_basic_param info2 = new M_basic_param();

info2.type = item2.param_type;

info2.title = item2.basic_param_name;

//-1:未知 0:女 1:男

if (item2.sex == 1)

{

info2.sex = 1;

}

else if (item2.sex == 2)

{

info2.sex = 0;

}

else

{

info2.sex = -1;

}

info2.minAge = (int)item2.agestart;

info2.maxAge = (int)item2.ageend;

info2.lower_limit = item2.lower_limit;

info2.upper_limit = item2.upper_limit;

constitutionlist.Add(info2);

}

info.constitutionlist = constitutionlist;

#endregion

ReturnStr = "{\"status\":\"Y\",\"msg\":\"获取成功\",\"data\":" + info.ToEnJsonString() + "}";

}

else

{

ReturnStr = "{\"status\":\"N\",\"msg\":\"请绑定设备\",\"data\":{}}";

}

}

else

{

ReturnStr = "{\"status\":\"N\",\"msg\":\"主机不存在\",\"data\":{}}";

}

}

catch (Exception ex)

{

WisdomStarts.Common.Log.NetLog.WriteTextLog("GetKindergartenInfoByHostID错误", ex.ToString());

}

}

result = new HttpResponseMessage { Content = new StringContent(ReturnStr, Encoding.GetEncoding("UTF-8"), "application/json") };

return result;

}

#endregion 根据主机ID获取机器人信息

标签: #mvc接收list