using FangYar.Model.TRAIN; using System; using System.Collections.Concurrent; using System.Data; using System.IO; using System.Threading; using System.Threading.Tasks; namespace HardWareInteface.TrainHandle { /// /// 拉萨的战训设备接口 /// public class TrainQueueTaskLs { public const string subjectOrg = "D8DC637B8B984848A63F82A018AFAEB0"; 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 = matchingSubject(infomodel.TestName); string RecordCount = infomodel.RecordCount; string ResultInfo = infomodel.ResultInfo; string org_id = infomodel.OrgId; int upTrue = 0,upFalse = 0; string taskArr = ""; #region if (Convert.ToInt32(RecordCount) > 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]; string updateScoreSql = "update train_score set result = '" + result + "'" + " ,reportperson_name = '" + doman + "'" + " ,report_time = '" + time + "'" + " ,source_type = 3" + " where extend1 = '" + emp_num + "'"; int a = FangYar.Common.MySqlHelper.Execute(updateScoreSql); ////获取训练成绩 获取该人员的最新的一条训练成绩 //string scoreSql = "SELECT ts.id,ts.task_id FROM train_score ts left join train_task tt on ts.task_id = tt.id" + // " WHERE ts.org_id = '" + org_id + "'" + // " and ts.emp_id = '" + emp_num + "' and ts.subject_name = '" + TestName + "' " + // " and (tt.state = 0 or tt.state = 1)" + // " and (ts.result is null or result = '') and (ts.score is null or ts.score = '')" + // " and (ts.remark is null or remark = '') ORDER BY ts.training_date DESC LIMIT 0,1"; //DataTable dt_task = FangYar.Common.MySqlHelper.Query(scoreSql).Tables[0]; //if (dt_task.Rows.Count == 0) //{ // strLog += "获取到此人(" + name + ")的训练成绩信息为空:" + ResultInfos[i].ToString() + "\r\n"; // ScoreFlow(org_id, TestName, RecordCount, emp_num, name, result, doman, time, "2");//将成绩流水入库 1:存在任务中 2: 不存在任务 // continue; //} //else //{ // strLog += "存在此人(" + name + ")的训练成绩\r\n"; // ScoreFlow(org_id, TestName, RecordCount, emp_num, name, result, doman, time, "1");//将成绩流水入库 1:存在任务中 2: 不存在任务 //} //string scoreId = dt_task.Rows[0]["ID"].ToString(); //string taskId = dt_task.Rows[0]["TASK_ID"].ToString(); //if (!taskArr.Contains(taskId)) { taskArr += taskId + ","; } //string updateScoreSql = "update train_score set result = '"+ result + "'" + // " ,reportperson_name = '" + doman + "'" + // " ,report_time = '" + time + "'" + // " ,source_type = 3" + // " where id = '" + scoreId + "'"; //int a = FangYar.Common.MySqlHelper.Execute(updateScoreSql); var flag = true; if (a >= 1) { upTrue += a; } else { upFalse += a; flag = false; } strLog += "成绩表 extend1编号:" + emp_num + ",操作:" + flag + "\r\n"; /** * 修改训练任务状态 * 屏蔽原因:只上报了结果未上报得分,还需要在平台进行编辑,所以不能改任务状态。 */ //string updateTaskSql = "update train_task set state = 2" + // " where id = '" + taskId + "'"; //int b = FangYar.Common.MySqlHelper.Execute(updateScoreSql); //upScoreNum += b; //flag = b >= 1 ? true : false; //strLog += "任务ID:" + scoreId + ",操作:" + flag + "\r\n"; } } catch (Exception e) { strLog += "Exception 循环内部:" + e.Message + "\r\n"; } } } #endregion strLog += "成功:" + upTrue + "(条),失败:" + upFalse + "(条)\r\n"; strLog += "任务ID集合:" + taskArr + "\r\n"; } catch (Exception e) { strLog += "Exception:" + e.Message + "\r\n"; } WriteLog(strLog, infomodel.LogPath); } //获取匹配的系统训练科目 private static string matchingSubject(string subjectName) { string _subjectName = subjectName; switch (subjectName) { case "俯卧撑": _subjectName = "男子俯卧撑"; break; case "屈腿仰卧起坐": _subjectName = "男子屈腿仰卧起坐"; break; case "100米负重跑": _subjectName = "男子100米负重跑"; break; case "单杠卷身上": _subjectName = "男子单杠卷身上"; break; case "双杠臂屈伸": _subjectName = "男子双杠臂屈伸"; break; case "绳索攀爬": _subjectName = "男子4楼攀爬绳索"; break; case "30米×2蛇形跑": _subjectName = "男子30×2蛇形跑"; break; } return _subjectName; } /// /// 将成绩流水入库 /// /// /// /// /// /// /// /// /// /// 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 { string scoreFlowId = Guid.NewGuid().ToString("N"); FangYar.Model.TRAIN.T_TRAININGSCOREFLOW model_flow = new FangYar.Model.TRAIN.T_TRAININGSCOREFLOW(); model_flow.ID = scoreFlowId; 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; string addScoreFlowSql = "insert into train_score_flow(id,org_id,task_name,person_num,emp_code,emp_name,result,manager,up_time,up_type,extend1) values('" + scoreFlowId + "','" + org_id + "','" + TestName + "','" + RecordCount + "','" + emp_num + "','" + name + "','" + result + "','" + doman + "','" + time + "','" + uptype + "','" + DateTime.Now.ToString() + "')"; FangYar.Common.MySqlHelper.Execute(addScoreFlowSql); //string addScoreFlowSql2 = "insert into t_trainingscoreflow(id,org_id,taskname,NUMBEROFPEOPLE,empcode,empname,result,manager,uptime,uptype) values('" + scoreFlowId + "','" + org_id + "','" + TestName + "','" + RecordCount + "','" + emp_num + "','" + name + "','" + result + "','" + doman + "','" + time + "','" + uptype + "')"; //FangYar.Common.MySqlHelper.Execute(addScoreFlowSql2); 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) { FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo() { message = msg, msgType = FangYar.Common.EnumLogMsgTypeEnum.Error, path = "TrainTMQData\\" + 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(); //} } } }