using log4net ;
using Newtonsoft.Json ;
using Newtonsoft.Json.Linq ;
using Quartz ;
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Data.SqlClient ;
using System.IO ;
using System.Linq ;
using System.Text ;
namespace JobCreateFee
{
public class JobDjyXf : IJob
{
private ILog log = LogManager . GetLogger ( typeof ( JobDjyXf ) ) ;
private const string CfgFileName = "starttime.cfg" ;
private static string CfgFilePath = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , CfgFileName ) ;
public void Execute ( IJobExecutionContext context )
{
log . Debug ( $"Execute开始" ) ;
try
{
string connStr = context . JobDetail . JobDataMap . GetString ( "ConnectString" ) ;
string reqUrl = context . JobDetail . JobDataMap . GetString ( "DjyReqUrl" ) ;
string companyCode = context . JobDetail . JobDataMap . GetString ( "DjyCompanyCode" ) ;
string companyName = context . JobDetail . JobDataMap . GetString ( "DjyCompanyName" ) ;
string companySecret = context . JobDetail . JobDataMap . GetString ( "DjyCompanySecret" ) ;
string bsType = context . JobDetail . JobDataMap . GetString ( "DjybsType" ) ;
string carrier = context . JobDetail . JobDataMap . GetString ( "DjyCarrier" ) ;
int reqTimeout = Convert . ToInt32 ( context . JobDetail . JobDataMap . GetString ( "RequestTimeout" ) ) ;
long ts = DataTableToJsonHelper . ConvertDateTimeToInt ( DateTime . Now ) ;
JsonXFSend reqObj = new JsonXFSend ( ) ;
var runid = Guid . NewGuid ( ) . ToString ( ) ;
reqObj . runid = runid ;
reqObj . userId = companyCode ;
reqObj . module = "expendlist" ;
reqObj . bsType = bsType ;
reqObj . sendType = "0" ;
reqObj . timestamp = ts ;
var md5str = DataTableToJsonHelper . MD5Encrypt32 ( runid + companyCode + "expendlist" + bsType + "0" + ts . ToString ( ) + companySecret ) ;
reqObj . md5 = md5str ;
//JArray jarrTemps = new JArray();
JsonXFData jobjTemp = new JsonXFData ( ) ;
jobjTemp . page = 1 ;
jobjTemp . limit = 10000 ;
jobjTemp . BsType = 5 ;
var starttimestr = File . ReadAllText ( CfgFilePath ) ;
if ( ! string . IsNullOrEmpty ( starttimestr ) ) {
long startts = DataTableToJsonHelper . ConvertDateTimeToInt ( Convert . ToDateTime ( starttimestr ) ) ;
jobjTemp . starttime = startts ;
}
var endtime = DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss" ) ;
long endts = DataTableToJsonHelper . ConvertDateTimeToInt ( Convert . ToDateTime ( endtime ) ) ;
jobjTemp . endtime = endts ;
reqObj . data = jobjTemp ;
var sendstr = JsonConvert . SerializeObject ( reqObj ) ;
log . Debug ( $"发送包:{sendstr}" ) ;
//发送请求数据
string rtn = WebRequestHelper . DoPost ( reqUrl , sendstr , reqTimeout * 1000 ) ;
//var objRtn = JsonConvert.DeserializeAnonymousType(rtn, new { status = "", message = "" });
log . Debug ( $"数据返回:{rtn}" ) ;
var parseJsonObj = JsonConvert . DeserializeObject < JsonXFResponse > ( rtn ) ;
if ( parseJsonObj . status )
{
var returnlist = parseJsonObj . data ;
try
{
using ( SqlConnection dbcon = new SqlConnection ( connStr ) )
{
dbcon . Open ( ) ;
foreach ( var returndata in returnlist )
{
if ( returndata . sendcom . IndexOf ( "捷丰" ) > 0 )
{
var bsno = GETD6BSNO ( returndata . mblno , dbcon ) ;
if ( returndata . price = = "" | | returndata . price = = null ) returndata . price = "0" ;
if ( returndata . ctnrCount = = "" | | returndata . ctnrCount = = null ) returndata . ctnrCount = "0" ;
string sql = " if (select GID from t_op_txxp_log where GID='" + returndata . gid + "') is null INSERT INTO t_op_txxp_log([编号],[GID],[beizhu],[mblno],[bsstatus],[bstype],[createtime]" ;
sql = sql + ",[price],[sendtype],[ctnrCount],[ctnrInfo]) values ('" + bsno + "','" + returndata . gid + "'" ;
sql = sql + ",'" + returndata . beizhu + "','" + returndata . mblno + "','" + returndata . bsstatus + "','" + returndata . bstype + "','" + returndata . createtime + "'" ;
sql = sql + "," + returndata . price + ",'" + returndata . sendtype + "'," + returndata . ctnrCount + ",'" + returndata . ctnrInfo + "')" ;
SqlCommand cmd = new SqlCommand ( sql , dbcon ) ;
cmd . ExecuteNonQuery ( ) ;
var headstr = "" ;
if ( returndata . mblno . Length > 3 )
{
headstr = returndata . mblno . Substring ( 0 , 3 ) ;
}
if ( string . IsNullOrEmpty ( bsno ) & & ( headstr = = "QGD" | | headstr = = "AMQ" | | headstr = = "AMG" | | headstr = = "AYQ" ) )
{
SaveMail ( dbcon , "CMABKG@SDSMARTLOGISTICS.COM" , returndata . mblno + "此单号已退仓,但是系统检测到打印过小票" + returndata . ctnrCount + "次,请知悉,谢谢" , returndata . mblno + "此单号已退仓,但是系统检测到打印过小票" + returndata . ctnrCount + "次,请知悉,谢谢" , "1" ) ;
}
}
}
dbcon . Close ( ) ;
}
}
catch ( Exception e ) {
log . Debug ( $"记录插入错误:{e.Message}" ) ;
}
File . WriteAllText ( CfgFilePath , endtime ) ;
}
else {
log . Debug ( $"数据返回错误:{parseJsonObj.message}" ) ;
}
}
catch ( Exception ex )
{
log . Error ( ex . Message ) ;
log . Error ( ex . StackTrace ) ;
}
}
public static int SaveMail ( SqlConnection dbcon , string sendto , string title , string body , string smtpid )
{
var sqlstr = $" INSERT INTO [Mail_Send] ([GID],[SendTo],[Title],[Body],[SendStatus],[TryCount],[CreateTime],[SmtpConfig],[Sender],[ShowName]) select newid(),'{sendto}','{title}','{body.Replace(" ' ", " ' ' ")}','Create',0,GETDATE(),'{smtpid}','','' " ;
SqlCommand cmd = new SqlCommand ( sqlstr , dbcon ) ;
return cmd . ExecuteNonQuery ( ) ;
}
static public string GETD6BSNO ( string MBLNO , SqlConnection dbcon )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT TOP 1 编号 BSNO from t_op_seae where 主提单号='" + MBLNO + "'" ) ;
SqlDataAdapter adapter = new SqlDataAdapter ( strSql . ToString ( ) , dbcon ) ;
DataTable table = new DataTable ( ) ;
adapter . Fill ( table ) ;
var BSNO = "" ;
if ( table . Rows . Count > 0 )
{
foreach ( DataRow row in table . Rows )
{
BSNO = row [ "BSNO" ] . ToString ( ) ;
}
}
return BSNO ;
}
}
public class JsonXFSend
{
public string runid { get ; set ; }
public string userId { get ; set ; }
public string module { get ; set ; }
public string bsType { get ; set ; }
public string sendType { get ; set ; }
public long timestamp { get ; set ; }
public string md5 { get ; set ; }
public JsonXFData data { get ; set ; }
}
public class JsonXFData
{
public int page { get ; set ; }
public int limit { get ; set ; }
public int BsType { get ; set ; }
public long starttime { get ; set ; }
public long endtime { get ; set ; }
public string Name { get ; set ; }
}
public class JsonXFResponse
{
public bool status { get ; set ; }
public string message { get ; set ; }
public string code { get ; set ; }
public string count { get ; set ; }
public List < XFData > data { get ; set ; }
}
public class XFData
{
public string gid { get ; set ; }
public string beizhu { get ; set ; }
public string bsno { get ; set ; }
public string mblno { get ; set ; }
public string bsstatus { get ; set ; }
public string bstype { get ; set ; }
public string createtime { get ; set ; }
public string price { get ; set ; }
public string sendtype { get ; set ; }
public string sendcom { get ; set ; }
public string ctnrCount { get ; set ; }
public string ctnrInfo { get ; set ; }
}
}