public static async Task LogRequest(string content, string method = null, string ip = null, short businessType = 0) { try{ var dbtext = string.Empty; var dbmethod = string.Empty; var dbip = string.Empty; var contentlenth = 4150; var shortlength = 44; if (!string.IsNullOrEmpty(content)) { var lenth = content.Length; dbtext = lenth > contentlenth?content.Substring(0, contentlenth) : content; } if (!string.IsNullOrEmpty(method)) { dbmethod = method.Length > shortlength?method.Substring(0, shortlength) : method; } if (!string.IsNullOrEmpty(ip)) { dbip = ip.Length > shortlength?ip.Substring(0, shortlength) : ip; } await Task.Run(() => { using (var logdb = new carsContext()) { logdb.Carslog.Add(new Carslog { Content = dbtext, Businesstype = businessType, Ip = dbip, Method = dbmethod, Time = DateTime.Now }); logdb.SaveChanges(); } }); } catch (Exception ex) { Console.WriteLine("db log error :{0}", ex.Message); } }
static void clean(carsContext db, string identity, businessType bbtype, DateTime dsubmit) { var busi = db.Carbusiness.FirstOrDefault(b => b.Identity == identity && b.Businesstype == (short)bbtype && b.Integrated == true && b.Exporttime == dsubmit); if (busi != null) { db.Carbusinesshis.Add(new Carbusinesshis { Identity = busi.Identity, Businesstype = busi.Businesstype, Time = DateTime.Now, }); db.Carbusiness.Remove(busi); var pics = db.Carbusinesspic.Where(a => a.Identity == identity && a.Businesstype == (int)bbtype); foreach (var pic in pics) { db.Carbusinesspic.Remove(pic); } } db.SaveChanges(); }
static void DbToFileForExtranetToIntranet() { var date = DateTime.Now; var dir = string.Format("{0}-{1}-{2}-{3}-{4}-{5}", date.Year, date.Month.ToString("D2"), date.Day.ToString("D2"), date.Hour.ToString("D2"), date.Minute.ToString("D2"), date.Second.ToString("D2")); var dbtofilefname = "cars.txt"; var home = "/home/carbusiness";// Environment.GetEnvironmentVariable("HOME"); var dbtofp = Path.Combine(home, dbtofilePath); if (!Directory.Exists(dbtofp)) { Directory.CreateDirectory(dbtofp); } var fname = Path.Combine(dbtofp, dbtofilefname); using (var db = new carsContext()) { //冗余传输,防止边界平台not delivery var rebusi = db.Carbusiness.Where(async => async.Integrated == true && async.Exporttime.CompareTo(date.AddDays(-1)) >= 0 ); Console.WriteLine("redundant is {1}, {2} records need to be archived", ",", date, rebusi.Count()); foreach (var rere in rebusi) { var picsr = db.Carbusinesspic.Where(c => c.Businesstype == rere.Businesstype && c.Identity == rere.Identity && c.Uploaded == true); if (picsr.Count() >= global.businesscount[(businessType)rere.Businesstype]) { var aouser = db.Caruser.FirstOrDefault(aa => aa.Identity == rere.Identity); if (aouser == null || string.IsNullOrEmpty(aouser.Name)) { continue; } var bt = (businessType)rere.Businesstype; if (bt != businessType.scrap) { NewMethod(aouser.Photofile, bt.ToString(), home); } var addr = string.Empty; if (!string.IsNullOrEmpty(rere.Province)) { addr += rere.Province; } if (!string.IsNullOrEmpty(rere.City)) { addr += rere.City; } if (!string.IsNullOrEmpty(rere.County)) { addr += rere.County; } if (!string.IsNullOrEmpty(rere.Postaddr)) { addr += rere.Postaddr; } var line = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}", rere.Identity, ((businessType)rere.Businesstype).ToString(), addr, rere.Acceptingplace, rere.Cartype, aouser.Phone, aouser.Name, rere.Platetype, rere.Platenumber1 + rere.Platenumber2, rere.Exporttime.ToString("yyyy/MM/dd HH:mm:ss"), aouser.Photofile); File.AppendAllText(fname, line + "\r\n"); } } var theuser = db.Carbusiness.Where(async => async.Integrated == false); Console.WriteLine("today is {1}, {2} records need to be archived", ",", date, theuser.Count()); foreach (var re in theuser) { var pics = db.Carbusinesspic.Where(c => c.Businesstype == re.Businesstype && c.Identity == re.Identity && c.Uploaded == true); if (pics.Count() >= global.businesscount[(businessType)re.Businesstype]) { var aouser = db.Caruser.FirstOrDefault(aa => aa.Identity == re.Identity); if (aouser == null || string.IsNullOrEmpty(aouser.Name)) { continue; } var bt = (businessType)re.Businesstype; if (bt != businessType.scrap) { NewMethod(aouser.Photofile, bt.ToString(), home); } var addr = string.Empty; if (!string.IsNullOrEmpty(re.Province)) { addr += re.Province; } if (!string.IsNullOrEmpty(re.City)) { addr += re.City; } if (!string.IsNullOrEmpty(re.County)) { addr += re.County; } if (!string.IsNullOrEmpty(re.Postaddr)) { addr += re.Postaddr; } var line = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}", re.Identity, ((businessType)re.Businesstype).ToString(), addr, re.Acceptingplace, re.Cartype, aouser.Phone, aouser.Name, re.Platetype , re.Platenumber1 + re.Platenumber2, date.ToString("yyyy/MM/dd HH:mm:ss"), aouser.Photofile); File.AppendAllText(fname, line + "\r\n"); re.Integrated = true; re.Status = 1; re.Exporttime = date; re.Waittime = date; } } db.SaveChanges(); } var exp = Path.Combine(home, exportPath); if (!Directory.Exists(exp)) { Directory.CreateDirectory(exp); } var zipfname = Path.Combine(exp, dir + "-cars"); var a = new System.Diagnostics.Process(); a.StartInfo.UseShellExecute = true; a.StartInfo.Arguments = string.Format(" -9 {0} {1}/* -r", zipfname, dbtofp); a.StartInfo.FileName = "zip"; a.Start(); a.WaitForExit(); }
public commonresponse again() { var accinfo = highlevel.GetInfoByToken(Request.Headers); if (accinfo.status != responseStatus.ok) { return(accinfo); } try { var theuser = _db.Carbusiness.FirstOrDefault(i => i.Identity == accinfo.Identity && i.Businesstype == (short)accinfo.businessType); if (theuser == null) { return(highlevel.commonreturn(responseStatus.iderror)); } var reason = string.Empty; if (!string.IsNullOrEmpty(theuser.Reason)) { reason = theuser.Reason; } _db.Carbusinesshis.Add(new Carbusinesshis { Identity = theuser.Identity, Businesstype = theuser.Businesstype, Completed = true, Time = theuser.Finishtime, Status = theuser.Status, Waittime = theuser.Waittime, Processtime = theuser.Processtime, Finishtime = theuser.Finishtime, Exporttime = theuser.Exporttime, Cartype = theuser.Cartype, Platetype = theuser.Platetype, Scrapplace = theuser.Scrapplace, Reason = reason }); theuser.Integrated = false; theuser.Status = (short)businessstatus.unknown; theuser.Exporttime = new DateTime(2000, 1, 1); var pics = _db.Carbusinesspic.Where(i => i.Identity == accinfo.Identity && i.Businesstype == (short)accinfo.businessType); foreach (var p in pics) { p.Uploaded = false; } _db.SaveChanges(); } catch (Exception ex) { _log.LogError("db error:{0}", ex.Message); return(highlevel.commonreturn(responseStatus.dberror)); } try { var he = Request.Host.ToString(); foreach (var a in Request.Headers) { he += "--" + a.Key + "=" + a.Value; } Task.Run(() => highlevel.LogRequest(he + accinfo.Identity, "again", Request.HttpContext.Connection.RemoteIpAddress.ToString(), (short)accinfo.businessType)); } catch (Exception ex) { _log.LogError("dblog error:", ex); } return(highlevel.commonreturn(responseStatus.ok)); }
public commonresponse uploadpic([FromBody] uploadpicrequest input) { // highlevel.LogRequest("uploadpic", "uploadpic", Request.HttpContext.Connection.RemoteIpAddress.ToString()); if (input == null) { return(highlevel.commonreturn(responseStatus.requesterror)); } var accinfo = highlevel.GetInfoByToken(Request.Headers); if (accinfo.status != responseStatus.ok) { return(accinfo); } _log.LogInformation("uploadpic-{3}: id={0},bt={1},pictype={2}", accinfo.Identity, accinfo.businessType, input.picType, DateTime.Now); if (!savePic(input.picture, input.picType, accinfo.photofile, accinfo.businessType)) { return(highlevel.commonreturn(responseStatus.fileprocesserror)); } if (input.picType == picType.unknown) { return(highlevel.commonreturn(responseStatus.pictypeerror)); } if (accinfo.businessType == businessType.unknown) { return(highlevel.commonreturn(responseStatus.businesstypeerror)); } try { using (var ddbb = new carsContext()) { var already = ddbb.Carbusinesspic.FirstOrDefault(i => i.Businesstype == (int)accinfo.businessType && i.Identity == accinfo.Identity && i.Pictype == (short)input.picType); if (already == null) { var newpic = new Carbusinesspic { Identity = accinfo.Identity, Businesstype = (short)accinfo.businessType, Pictype = (short)input.picType, Uploaded = true, Time = DateTime.Now }; // highlevel.infolog(_log, "uploadpic", JsonConvert.SerializeObject(newpic)); var ret = ddbb.Carbusinesspic.Add(newpic); // highlevel.infolog(_log, "uploadpic88", JsonConvert.SerializeObject(ret.Entity)); } else { already.Uploaded = true; already.Time = DateTime.Now; } ddbb.SaveChanges(); } } catch (Exception ex) { highlevel.errorlog(_log, "uploadpic", ex); } try { var he = Request.Host.ToString(); foreach (var a in Request.Headers) { he += "--" + a.Key + "=" + a.Value; } Task.Run(() => highlevel.LogRequest(he + input.picType + accinfo.Identity, "uploadpic", Request.HttpContext.Connection.RemoteIpAddress.ToString(), (short)accinfo.businessType)); } catch (Exception ex) { _log.LogError("dblog error:", ex); } _log.LogInformation("uploadpic-{3}: id={0},bt={1},pictype={2}---over", accinfo.Identity, accinfo.businessType, input.picType, DateTime.Now); return(new commonresponse { status = responseStatus.ok }); }
static void FileToDb() { using (var db = new carsContext()) { var filebase = "result.txt"; var home = "/home/carbusiness";// Environment.GetEnvironmentVariable("HOME"); var fname = Path.Combine(home, importPath, filebase); if (!File.Exists(fname)) { Console.WriteLine("file {0} does not exist, exit.{1}", fname, DateTime.Now); return; } var content = File.ReadAllLines(fname); Console.WriteLine("{0} lines need to import", content.Length); foreach (var line in content) { var fields = line.Split(','); if (fields.Length < 6) { Console.WriteLine(" invalid data line {0},{1}", fields.Length, line); continue; } var identity = fields[0]; var btype = fields[1]; var bbtype = businessType.unknown; if (!Enum.TryParse(btype, out bbtype)) { Console.WriteLine(" businessType error line {0},{1}", fields.Length, line); continue; } var success = fields[2]; var desc = fields[3]; var timed = fields[4]; var timedd = DateTime.Now; if (!DateTime.TryParse(timed, out timedd)) { Console.WriteLine(" process time error line {0},{1}", timed, line); // Console.WriteLine(" businessType ={0},identity={1},success={2},desc={3},timedd={4},bbtype={5}", // btype, identity, success, desc, timedd, (int)bbtype); continue; } var submittime = fields[5]; var dsubmit = DateTime.Now; if (!DateTime.TryParse(submittime, out dsubmit)) { Console.WriteLine(" submittime error line {0},{1}", submittime, line); continue; } switch (success) { case "1": clean(db, identity, bbtype, dsubmit); break; case "2": clean(db, identity, bbtype, dsubmit); var tuser = db.Caruser.FirstOrDefault(a => a.Identity == identity); if (tuser != null) { tuser.Blacklist = true; db.SaveChanges(); } break; case "3": var busi3 = db.Carbusiness.FirstOrDefault(b => b.Identity == identity && b.Businesstype == (short)bbtype && b.Integrated == true && b.Exporttime == dsubmit); if (busi3 != null) { busi3.Status = (short)businessstatus.process; // busi3.Reason=desc; busi3.Processtime = timedd; db.SaveChanges(); } break; case "4": var busi4 = db.Carbusiness.FirstOrDefault(b => b.Identity == identity && b.Businesstype == (short)bbtype && b.Integrated == true && b.Exporttime == dsubmit); if (busi4 != null) { busi4.Status = (short)businessstatus.failure; busi4.Reason = desc; busi4.Finishtime = timedd; db.SaveChanges(); } break; case "7": clean(db, identity, bbtype, dsubmit); break; case "8": var tuser8 = db.Caruser.FirstOrDefault(a => a.Identity == identity); if (tuser8 != null) { tuser8.Blacklist = false; db.SaveChanges(); } break; default: Console.WriteLine(" invalid data line {0},{1}", fields.Length, line); break; } } } }
public loginresponse login(string name, string identify, string phone, businessType businessType) { if (string.IsNullOrEmpty(identify) || identify == "undefined") { return(new loginresponse { status = responseStatus.iderror }); } if (string.IsNullOrEmpty(name) || name == "undefined") { return(new loginresponse { status = responseStatus.nameerror }); } if (string.IsNullOrEmpty(phone) || phone == "undefined") { return(new loginresponse { status = responseStatus.phoneerror }); } if (businessType == businessType.unknown) { return(new loginresponse { status = responseStatus.businesstypeerror }); } var btype = (short)businessType; // var picsl = new List<short>(); var token = GetToken(); var photofile = string.Empty; var response = new loginresponse { status = responseStatus.ok, blacklist = false, businessstatus = businessstatus.unknown, submitted = false, content = "unknown", token = token, // okpic = picsl.ToArray() }; var encryptedIdentity = CryptographyHelpers.StudyEncrypt(identify); try { _log.LogWarning("businessType={0},Scrapplace={1}", businessType, 111); var theuser = _db1.Caruser.FirstOrDefault(i => i.Identity == encryptedIdentity); if (theuser == null) { photofile = token; _db1.Caruser.Add(new Caruser { Identity = encryptedIdentity, Blacklist = false, Photofile = token, Phone = phone, Name = name }); _db1.SaveChanges(); } else { photofile = theuser.Photofile; } _log.LogWarning("businessType={0},Scrapplace={1}", businessType, 222); var business = _db1.Carbusiness.FirstOrDefault(i => i.Identity == encryptedIdentity && i.Businesstype == (short)businessType); if (business == null) { _db1.Carbusiness.Add(new Carbusiness { Identity = encryptedIdentity, Businesstype = (short)businessType, Completed = false, Time = DateTime.Now, }); _db1.SaveChanges(); } else { _log.LogWarning("businessType={0},Scrapplace={1}", businessType, business.Scrapplace); var pics = _db1.Carbusinesspic.Where(c => c.Businesstype == btype && c.Identity == encryptedIdentity && c.Uploaded == true); response.businessstatus = (businessstatus)business.Status; response.finish_time = business.Finishtime; response.wait_time = business.Waittime; response.process_time = business.Processtime; if (!string.IsNullOrEmpty(business.Reason)) { response.content = business.Reason; } if (response.businessstatus == businessstatus.failure) { response.submitted = true; } else { if (businessType != businessType.scrap && pics.Count() >= global.businesscount[businessType]) { response.submitted = true; } if (businessType == businessType.scrap && business.Scrapplace != 0) { response.submitted = true; } } } if (theuser != null && theuser.Blacklist == true) { response.blacklist = true; } } catch (Exception ex) { _log.LogError("login -- process error:{0}", ex.Message); } try { var redisdb = highlevel.redis.GetDatabase(); redisdb.StringSet(token, JsonConvert.SerializeObject(new idinfo { Identity = encryptedIdentity, photofile = photofile, businessType = businessType })); redisdb.KeyExpire(token, TimeSpan.FromDays(30)); } catch (Exception ex) { _log.LogError("redis key process error:{0}", ex.Message); } var found = false; foreach (var a in global.tokens) { if (a.idinfo.Identity == encryptedIdentity && a.idinfo.businessType == businessType) { a.Token = token; found = true; break; } } if (!found) { global.tokens.Add(new Ptoken { idinfo = new idinfo { Identity = encryptedIdentity, photofile = photofile, businessType = businessType }, Token = token }); } try { var he = Request.Host.ToString(); foreach (var a in Request.Headers) { he += "--" + a.Key + "=" + a.Value; } Task.Run(() => highlevel.LogRequest(he + name + phone + encryptedIdentity + JsonConvert.SerializeObject(response), "login", Request.HttpContext.Connection.RemoteIpAddress.ToString(), (short)businessType)); } catch (Exception ex) { _log.LogError("dblog error:", ex); } return(response); }
public commonresponse scrapuserupdate([FromBody] surequest inputRequest) { try { if (inputRequest == null) { _log.LogInformation("login,{0}", responseStatus.requesterror); return(new ScrapLoginRes { status = responseStatus.requesterror }); } var token = Request.Headers["Token"].First(); var found = false; var scrapplace = ScrapPlace.unknown; var user = string.Empty; foreach (var a in tokens) { if (a.Token == token) { scrapplace = a.scrapPlace; user = a.Identity; found = true; break; } } if (!found) { return(new commonresponse { status = responseStatus.tokenerror }); } var theuser = _db1.Operator.FirstOrDefault(async => async.Id == user);//|| async.Identity == cryptographicid); if (theuser == null) { _log.LogInformation("login,{0}", responseStatus.nouser); return(new ScrapLoginRes { status = responseStatus.nouser }); } if (!string.IsNullOrEmpty(inputRequest.name)) { theuser.Name = inputRequest.name; } if (!string.IsNullOrEmpty(inputRequest.password)) { theuser.Password = inputRequest.password; } if (!string.IsNullOrEmpty(inputRequest.phone)) { theuser.Phone = inputRequest.phone; } _db1.SaveChanges(); return(new ScrapLoginRes { status = 0, }); } catch (Exception ex) { _log.LogError("login,{0}", ex); return(new ScrapLoginRes { status = responseStatus.processerror }); } }
public commonresponse CompleteScrap(string id) { try { var token = Request.Headers["Token"].First(); _log.LogInformation("pushmessage2,token is {0},{1},id", token, id); var found = false; var scrapplace = ScrapPlace.unknown; foreach (var a in tokens) { if (a.Token == token) { scrapplace = a.scrapPlace; found = true; break; } } _log.LogInformation("DeleteScrap,{0},,", 111); if (!found) { _log.LogInformation("DeleteScrap,{0}", responseStatus.tokenerror); return(new commonresponse { status = responseStatus.tokenerror }); } var enid = CryptographyHelpers.StudyEncrypt(id); using (var db = new carsContext()) { var ss = db.Carbusiness.FirstOrDefault(a => a.Businesstype == (short)businessType.scrap && a.Scrapplace == (short)scrapplace && a.Identity == enid); if (ss != null) { db.Carbusinesshis.Add(new Carbusinesshis { Identity = ss.Identity, Businesstype = ss.Businesstype, Completed = true, Time = ss.Waittime, Status = ss.Status, Postaddr = ss.Postaddr, Waittime = ss.Waittime, Processtime = ss.Processtime, Finishtime = ss.Finishtime, Exporttime = ss.Exporttime, Cartype = ss.Cartype, Platetype = ss.Platetype, Scrapplace = ss.Scrapplace, Reason = ss.Reason }); db.Carbusiness.Remove(ss); db.SaveChanges(); } } Task.Run(() => highlevel.LogRequest("CompleteScrap", "CompleteScrap", Request.HttpContext.Connection.RemoteIpAddress.ToString(), 0)); return(new commonresponse { status = responseStatus.ok }); } catch (Exception ex) { _log.LogError("{0}-{1}-{2}", DateTime.Now, "CompleteScrap", ex.Message); return(new commonresponse { status = responseStatus.processerror, content = ex.Message }); } }