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 JobGetMscBlDownloadRequest : IJob
{
private ILog log = LogManager . GetLogger ( typeof ( JobGetMscBlDownloadRequest ) ) ;
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" , message = "" } ) ;
if ( objRtn . status = = "1" ) {
var JsonMscSwBObj = JsonConvert . DeserializeObject < JsonMscBlResponse > ( rtn ) ;
foreach ( var item in JsonMscSwBObj . data )
{
if ( ! string . IsNullOrEmpty ( item . path ) ) {
using ( SqlConnection dbcon = new SqlConnection ( connStr ) )
{
dbcon . Open ( ) ;
string sql = " delete from t_op_seae_msc_file where 主提单号='" + item . bno + "' insert into t_op_seae_msc_file(编号,主提单号,路径) select top 1 编号,主提单号,'" + item . path + "' from t_op_seae where 主提单号='" + item . bno + "'" ;
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 ) ;
}
}
}
public class JsonMscBlResponse
{
public List < JsonMscBlDataResponse > data { get ; set ; }
public string status { get ; set ; }
public string message { get ; set ; }
}
public class JsonMscBlDataResponse
{
public string bno { get ; set ; }
public string message { get ; set ; }
public string path { get ; set ; }
public string status { get ; set ; }
}
}