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.Linq ;
using System.Text ;
using System.IO ;
namespace JobReqWebData
{
public class JobSendMscBlDownloadRequest : IJob
{
private ILog log = LogManager . GetLogger ( typeof ( JobSendMscBlDownloadRequest ) ) ;
private const string CfgFileName = "requestjson.cfg" ;
private static string CfgFilePath = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , CfgFileName ) ;
public void Execute ( IJobExecutionContext context )
{
try
{
string connStr = context . JobDetail . JobDataMap . GetString ( "ConnectString" ) ;
string sqlQuery = context . JobDetail . JobDataMap . GetString ( "QuerySql" ) ;
string reqUrl = context . JobDetail . JobDataMap . GetString ( "ReqUrl" ) ;
string customer = context . JobDetail . JobDataMap . GetString ( "Customer" ) ;
string password = context . JobDetail . JobDataMap . GetString ( "Password" ) ;
//string webcustomer = context.JobDetail.JobDataMap.GetString("WebCustomer");
//string webpassword = context.JobDetail.JobDataMap.GetString("WebPassword");
int reqTimeout = Convert . ToInt32 ( context . JobDetail . JobDataMap . GetString ( "RequestTimeout" ) ) ;
log . Debug ( $"连接字符串:{connStr}" ) ;
log . Debug ( $"查询SQL: {sqlQuery}" ) ;
log . Debug ( $"请求URL: {reqUrl}" ) ;
log . Debug ( $"用户:{customer}" ) ;
log . Debug ( $"密码:{password}" ) ;
log . Debug ( $"请求超时:{reqTimeout}" ) ;
SqlConnection conn = new SqlConnection ( connStr ) ;
SqlDataAdapter dataAdapter = new SqlDataAdapter ( sqlQuery , conn ) ;
DataTable tQuery = new DataTable ( ) ;
dataAdapter . Fill ( tQuery ) ;
if ( tQuery . Rows . Count = = 0 )
{
log . Debug ( "没有要发送的数据!" ) ;
return ;
}
var json = DataTableToJsonHelper . ConvertToJson ( tQuery ) ;
log . Debug ( $"jsonStr: {json.ToString(Formatting.None)}" ) ;
JObject reqObj = new JObject ( ) ;
reqObj . Add ( "user_key" , new JValue ( customer ) ) ;
reqObj . Add ( "user_secret" , new JValue ( password ) ) ;
reqObj . Add ( "tasks_data" , json ) ;
log . Debug ( $"reqObj: {reqObj.ToString(Formatting.None)}" ) ;
string rtn = WebRequestHelper . DoPost ( reqUrl , reqObj . ToString ( Formatting . None ) , reqTimeout * 1000 ) ;
log . Debug ( $"数据返回:{rtn}" ) ;
var objRtn = JsonConvert . DeserializeAnonymousType ( rtn , new { status = "0" } ) ;
if ( objRtn . status = = "1" ) {
if ( tQuery . Rows . Count > 0 )
{
foreach ( DataRow row in tQuery . Rows )
{
var MBLNO = row [ "bno" ] . ToString ( ) ;
if ( ! string . IsNullOrEmpty ( MBLNO ) ) {
using ( SqlConnection dbcon = new SqlConnection ( connStr ) )
{
dbcon . Open ( ) ;
string sql = " update t_op_seae_msc set UPLOAD='1' where EXISTS (select 1 from t_op_seae where t_op_seae.主提单号='" + MBLNO + "' and t_op_seae.编号=t_op_seae_msc.编号)" ;
SqlCommand cmd = new SqlCommand ( sql , dbcon ) ;
cmd . ExecuteNonQuery ( ) ;
dbcon . Close ( ) ;
}
}
}
}
}
log . Debug ( $"爬取服务返回:(Code:{objRtn.status})" ) ;
}
catch ( Exception ex )
{
log . Error ( ex . Message ) ;
log . Error ( ex . StackTrace ) ;
}
}
}
}