You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.5 KiB
C#

using ocean;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
namespace demo.DbModule
{
public class Mes
{
Thread tRun;
private static Mes _instance;
private bool quit = false;
public static Mes Instance
{
get {
if (_instance == null)
_instance = new Mes();
return _instance;
}
}
private Mes()
{
tRun = new Thread(Run);
}
public void Quit()
{
quit = true;
tRun.Join();
}
private void Run()
{
DateTime prevReqTime = DateTime.Now;
try
{
string msgurl = System.Configuration.ConfigurationManager.AppSettings["MesUrl"];
while (!quit)
{
if ((DateTime.Now - prevReqTime).TotalSeconds >= 10)
{
prevReqTime = DateTime.Now;
//查询未上传的记录
string querySql = "select * from products where upstatus=0 and trynum<10";
DataTable dt = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, querySql);
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
try
{
if (quit)
break;
string updateSql = "update products set upstatus={0},trynum=trynum+1,result=CONCAT(coalesce(result,''),',{1}') where id={2}";
int upstatus = 0;
List<RequestData> reqList = new List<RequestData>();
RequestData rd = new RequestData();
rd.cBarCode = row["sn"] != null ? row["sn"].ToString() : "";
rd.cFileName = row["filename"].ToString();
rd.cMode = row["mode"] != null ? row["mode"].ToString() : "";
rd.cStatus = row["status"].ToString();
reqList.Add(rd);
string resultjson = LFData.Post(msgurl, reqList.ToArray());
if (resultjson.ToUpper().IndexOf("SUCCESS") > 0)
{
upstatus = 1;
}
updateSql = string.Format(updateSql, upstatus, resultjson, row["id"]);
MySQLHelper.ExecuteNonQuery(MySQLHelper.DBConnectionString, updateSql);
}
catch (Exception)
{
//log
}
}
}
}
else
{
Thread.Sleep(100);
}
}
}
catch (Exception)
{
//log
}
}
public void Start()
{
tRun.Start();
}
}
}