using FangYar.Model.TRAIN; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Web; namespace HardWareInteface.TrainHandle { /// /// 战训设备接口(老版本备份) /// 2023年4月18日 swr /// public class TrainQueueTaskBak { public static FangYar.BLL.TRAIN.T_TRAININGSCORE bll_trainscore = new FangYar.BLL.TRAIN.T_TRAININGSCORE(); public static FangYar.BLL.TRAIN.T_SUBJECTGRADESTANDARD bll_standard = new FangYar.BLL.TRAIN.T_SUBJECTGRADESTANDARD(); public static FangYar.BLL.TBL.SysEmpBLL bll_emp = new FangYar.BLL.TBL.SysEmpBLL(); public static FangYar.BLL.TRAIN.T_TRAININGTASK bll_task = new FangYar.BLL.TRAIN.T_TRAININGTASK(); public static FangYar.BLL.TRAIN.T_TRAININGSCOREFLOW bll_flow = new FangYar.BLL.TRAIN.T_TRAININGSCOREFLOW(); public static ConcurrentQueue infoQueue = new ConcurrentQueue(); private static CancellationTokenSource m_DealToken = new CancellationTokenSource(); private static Task m_DealTask = Task.Factory.StartNew(DealInfoList, m_DealToken.Token); public static void infoQueueFun(infoModel data) { try { infoQueue.Enqueue(data); //添加 } catch (Exception e) { WriteLog(e.Message, data.LogPath); } WriteLog("队列长度:" + infoQueue.Count.ToString(), data.LogPath); } private static void DealInfoList() { while (!m_DealToken.IsCancellationRequested) { infoModel dataInfo = null; if (infoQueue.TryDequeue(out dataInfo)) { try { DealInfo(dataInfo); } catch (Exception ex) { WriteLog(ex.Source + ex.StackTrace + ex.Message + "DealInfoList_Error", dataInfo.LogPath); } } else { Thread.Sleep(100); } } } private static void DealInfo(infoModel infomodel) { string strLog = ""; try { strLog += "进入DealInfo" + "\r\n"; string TestName = infomodel.TestName; string RecordCount = infomodel.RecordCount; string ResultInfo = infomodel.ResultInfo; string org_id = infomodel.OrgId; #region if (Convert.ToInt32(RecordCount) > 0) { //获取成绩标准 string scoreWhere = " SUBJECTNAME = '" + TestName + "' and ORG_ID = '" + org_id + "'"; if (TestName == "3000米跑" || TestName == "蛇形跑" || TestName == "屈臂悬垂" || TestName == "5000米负重跑" || TestName == "400米救人疏散物资" || TestName == "100米负重跑" || TestName == "60米肩梯跑" || TestName == "负重上10楼" || TestName == "搬运重物折返" || TestName == "绳索攀爬" || TestName == "双杠直臂前移" || TestName == "30米×2蛇形跑") { scoreWhere += " order by cast(GENDER as SIGNED) ,cast(AGE1 as SIGNED) desc ,cast(STASYS1 as SIGNED) desc"; } else if (TestName == "屈腿仰卧起坐" || TestName == "引体向上" || TestName == "俯卧撑" || TestName == "跳绳" || TestName == "单杠卷身上" || TestName == "双杠臂屈伸") { scoreWhere += " order by cast(GENDER as SIGNED) ,cast(AGE1 as SIGNED) desc ,cast(STASYS1 as SIGNED) desc"; } DataTable dt = bll_standard.GetList(scoreWhere).Tables[0]; string[] ResultInfos = ResultInfo.Split(';'); for (int i = 0; i < ResultInfos.Length; i++) { try { if (!string.IsNullOrEmpty(ResultInfos[i])) { string[] ResultArry = ResultInfos[i].ToString().Split(','); string emp_num = ResultArry[0]; string name = ResultArry[1]; string result = ResultArry[2]; string doman = ResultArry[3]; string time = ResultArry[4]; //获取人员信息 FangYar.Model.TBL.TBL_SYS_EMP_Model model_emp = new FangYar.Model.TBL.TBL_SYS_EMP_Model(); model_emp = bll_emp.GetModelByUID(emp_num); if (model_emp == null) { strLog += "获取到人员信息为空;" + "\r\n"; } //获取任务 获取该人员的最新的一条训练任务 string taskWhere = " subjectcontent_name like '%" + TestName + "%' and org_id ='" + org_id + "' and TRAININGPERSON like '%\"ppl_Id\":\""+ emp_num + "\"%' and State='1' "; DataTable dt_task = bll_task.GetListFirst(taskWhere).Tables[0]; if (dt_task.Rows.Count == 0) { strLog += "获取到此人的训练任务信息为空:" + ResultInfos[i].ToString() + "\r\n"; ScoreFlow(org_id, TestName, RecordCount, emp_num, name, result, doman, time, "2");//将成绩流水入库 1:存在任务中 2: 不存在任务 continue; } else { strLog += "存在此人的训练任务"; ScoreFlow(org_id, TestName, RecordCount, emp_num, name, result, doman, time, "1");//将成绩流水入库 1:存在任务中 2: 不存在任务 } string SUBJECTCONTENT = dt_task.Rows[0]["SUBJECTCONTENT"].ToString(); string subjectid = ""; string subjectname = ""; JArray jarray = JsonConvert.DeserializeObject(SUBJECTCONTENT); foreach (JObject obj in jarray.Value()) { if (obj["NAME"].ToString() == TestName) { subjectid = obj["ID"].ToString(); subjectname = obj["NAME"].ToString(); break; } } strLog += "解析SUBJECTCONTENT"; string TRAINIGDATE = dt_task.Rows[0]["TRAINIGDATE"].ToString(); string TASKID = dt_task.Rows[0]["ID"].ToString(); string TASKNAME = dt_task.Rows[0]["NAME"].ToString(); string ORG_ID = dt_task.Rows[0]["ORG_ID"].ToString(); string ORG_NAME = dt_task.Rows[0]["ORG_NAME"].ToString(); //{"data":[{"ppl_Id":"18609986119","ppl_Name":"颜龙","ppl_Sex":"0","ppl_Age":"36"}]} string REPORTPERSONID = "", REPORTPERSONNAME = ""; string leaderobj = dt_task.Rows[0]["LEADTRAINING"].ToString(); JObject leaderObj = JsonConvert.DeserializeObject(leaderobj); foreach (JObject obj in leaderObj.Value("data")) { REPORTPERSONID = obj["ppl_Id"].ToString(); REPORTPERSONNAME = obj["ppl_Name"].ToString(); } strLog += "解析leaderObj"; if (model_emp != null) { FangYar.Model.TRAIN.T_TRAININGSCORE trainscore = new FangYar.Model.TRAIN.T_TRAININGSCORE(); trainscore.ID = Guid.NewGuid().ToString("N"); trainscore.USERS_UID = emp_num; trainscore.USERS_NAME = name; trainscore.SUBJECTID = subjectid; trainscore.SUBJECTNAME = subjectname; trainscore.TRAINIGDATE = TRAINIGDATE; trainscore.TASKID = TASKID; trainscore.TASKNAME = TASKNAME; trainscore.RESULT = result; if (TestName == "3000米跑" || TestName == "蛇形跑" || TestName == "屈臂悬垂" || TestName == "5000米负重跑" || TestName == "400米救人疏散物资" || TestName == "100米负重跑" || TestName == "60米肩梯跑" || TestName == "负重上10楼" || TestName == "搬运重物折返" || TestName == "绳索攀爬" || TestName == "双杠直臂前移" || TestName == "30米×2蛇形跑") { string millisecond = ""; switch (TestName) { case "3000米跑": case "屈臂悬垂": case "5000米负重跑": case "400米救人疏散物资": case "负重上10楼": case "搬运重物折返": string a = result.Split('\'')[0]; string b = result.Split('\'')[1].Split('"')[0]; millisecond = (Convert.ToInt32(a) * 60 * 1000 + Convert.ToInt32(b) * 1000).ToString(); break; case "蛇形跑": case "100米负重跑": case "60米肩梯跑": case "绳索攀爬": case "双杠直臂前移": case "30米×2蛇形跑": millisecond = (Convert.ToDouble(result) * 1000).ToString(); ; break; default: millisecond = "0"; break; } strLog += "获取成绩前"; trainscore.ACHIEVEMENT = GetScoreTime(dt, millisecond, model_emp.EMP_SEX, CalculateAgeString(Convert.ToDateTime(model_emp.BIRTHDAY), Convert.ToDateTime(time))); strLog += "成绩:" + trainscore.ACHIEVEMENT; } else if (TestName == "屈腿仰卧起坐" || TestName == "引体向上" || TestName == "俯卧撑" || TestName == "跳绳" || TestName == "单杠卷身上" || TestName == "双杠臂屈伸" || TestName == "双杠直臂前移") { strLog += "获取成绩前"; DateTime t1 = Convert.ToDateTime(model_emp.BIRTHDAY); DateTime t2 = Convert.ToDateTime(time); trainscore.ACHIEVEMENT = GetScoreFrequency(dt, result, model_emp.EMP_SEX, CalculateAgeString(Convert.ToDateTime(model_emp.BIRTHDAY), Convert.ToDateTime(time))); strLog += "成绩:" + trainscore.ACHIEVEMENT; } trainscore.REPORTPERSONID = REPORTPERSONID; trainscore.REPORTPERSONNAME = REPORTPERSONNAME; trainscore.REPORTTIME = Convert.ToDateTime(time); trainscore.ORG_ID = ORG_ID; trainscore.ORG_NAME = ORG_NAME; strLog += "入库前"; bool flag = bll_trainscore.Add(trainscore); strLog += "成绩入库操作 bll_trainscore flag :" + flag + "\r\n"; FangYar.Model.TRAIN.T_TRAININGTASK model_task = bll_task.GetModel(TASKID); model_task.STATE = "1"; bool flag_updatetask = bll_task.Update(model_task); strLog += "任务状态修改操作 flag_updatetask flag :" + flag + "\r\n"; } } } catch (Exception e) { strLog += "Exception 循环内部:" + e.Message + "\r\n"; } } } #endregion } catch (Exception e) { strLog += "Exception:" + e.Message + "\r\n"; } WriteLog(strLog, infomodel.LogPath); } //时间获取分数 private static int GetScoreTime(DataTable dt, string result, string sex, int age) { sex = sex == "男" ? "0" : "1"; int returnInt = 0; for (var y = 0; y < dt.Rows.Count; y++) { if (sex == dt.Rows[y]["GENDER"].ToString() && Convert.ToInt32(result) <= Convert.ToInt32(dt.Rows[y]["STASYS1"].ToString()) && Convert.ToInt32(age) >= Convert.ToInt32(dt.Rows[y]["AGE1"].ToString())) { returnInt = Convert.ToInt32(dt.Rows[y]["ACHIEVEMENT1"].ToString()); break; } else { continue; } } return returnInt; } //次数获取分数 private static int GetScoreFrequency(DataTable dt, string result, string sex, int age) { sex = sex == "男" ? "0" : "1"; int returnInt = 0; for (var y = 0; y < dt.Rows.Count; y++) { if (sex == dt.Rows[y]["GENDER"].ToString() && Convert.ToInt32(result) >= Convert.ToInt32(dt.Rows[y]["STASYS1"].ToString()) && Convert.ToInt32(age) >= Convert.ToInt32(dt.Rows[y]["AGE1"].ToString())) { returnInt = Convert.ToInt32(dt.Rows[y]["ACHIEVEMENT1"].ToString()); break; } else { continue; } } return returnInt; } /// /// 计算年龄字符串(周岁) /// /// 第1个日期参数 /// 第2个日期参数 private static int CalculateAgeString(DateTime p_FirstDateTime, System.DateTime p_SecondDateTime) { //判断时间段是否为正。若为负,调换两个时间点的位置。 if (System.DateTime.Compare(p_FirstDateTime, p_SecondDateTime) > 0) { System.DateTime stmpDateTime = p_FirstDateTime; p_FirstDateTime = p_SecondDateTime; p_SecondDateTime = stmpDateTime; } //定义:年、月、日 int year, month, day; //计算:天 day = p_SecondDateTime.Day - p_FirstDateTime.Day; if (day < 0) { day += System.DateTime.DaysInMonth(p_FirstDateTime.Year, p_FirstDateTime.Month); p_FirstDateTime = p_FirstDateTime.AddMonths(1); } //计算:月 month = p_SecondDateTime.Month - p_FirstDateTime.Month; if (month < 0) { month += 12; p_FirstDateTime = p_FirstDateTime.AddYears(1); } //计算:年 year = p_SecondDateTime.Year - p_FirstDateTime.Year; //返回格式化后的结果 return year; } /// /// 将成绩流水入库 /// /// /// /// /// /// /// /// /// /// private static void ScoreFlow(string org_id, string TestName, string RecordCount, string emp_num, string name, string result, string doman, string time, string uptype) { try { FangYar.Model.TRAIN.T_TRAININGSCOREFLOW model_flow = new FangYar.Model.TRAIN.T_TRAININGSCOREFLOW(); model_flow.ID = Guid.NewGuid().ToString("N"); model_flow.ORG_ID = org_id; model_flow.TASKNAME = TestName; model_flow.NUMBEROFPEOPLE = RecordCount; model_flow.EMPCODE = emp_num; model_flow.EMPNAME = name; model_flow.RESULT = result; model_flow.MANAGER = doman; model_flow.UPTIME = time; model_flow.UPTYPE = uptype; bll_flow.Add(model_flow); try { FangYar.FYMQTT.MQTT mqtt = new FangYar.FYMQTT.MQTT(); mqtt.messagePublished(System.Web.HttpUtility.UrlEncode(FangYar.Common.JsonHelper.ToJson(model_flow)), "trainingscore_" + org_id); } catch (Exception e) { } } catch (Exception e) { } } /// /// 输出日志 /// static ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim(); private static void WriteLog(string msg, string logPath) { try { LogWriteLock.EnterWriteLock(); string path = AppDomain.CurrentDomain.BaseDirectory + "log\\TrainTMQData\\" + logPath + "\\数据处理\\"; string filename = DateTime.Now.ToString("yyyyMMdd") + ".txt"; if (Directory.Exists(path) == false)//如果不存在就创建文件夹 { Directory.CreateDirectory(path); } FileInfo file = new FileInfo(path + filename); if (!file.Exists) { try { FileStream fs; fs = File.Create(path + filename); fs.Close(); } catch { } } using (FileStream fs = new FileStream(path + filename, FileMode.Append, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine("时间:" + DateTime.Now.ToString() + " 信息:" + msg); } } } catch (Exception e) { } finally { LogWriteLock.ExitWriteLock(); } } } }