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 JobDjyD6Xf : IJob
{
private ILog log = LogManager . GetLogger ( typeof ( JobDjyD6Xf ) ) ;
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 FeeName = context . JobDetail . JobDataMap . GetString ( "FeeName" ) ;
string CustName = context . JobDetail . JobDataMap . GetString ( "CustName" ) ;
string companySecret = context . JobDetail . JobDataMap . GetString ( "DjyCompanySecret" ) ;
string bsType = context . JobDetail . JobDataMap . GetString ( "DjybsType" ) ;
string COSCOAUTOCREATEFEE = context . JobDetail . JobDataMap . GetString ( "COSCOAUTOCREATEFEE" ) ;
string CoscoCustName = context . JobDetail . JobDataMap . GetString ( "COSCOCustName" ) ;
string ENTEROPERATOR = context . JobDetail . JobDataMap . GetString ( "ENTEROPERATOR" ) ;
int reqTimeout = Convert . ToInt32 ( context . JobDetail . JobDataMap . GetString ( "RequestTimeout" ) ) ;
long ts = DataTableToJsonHelper . ConvertDateTimeToInt ( DateTime . Now ) ;
JsonD6XFSend reqObj = new JsonD6XFSend ( ) ;
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();
JsonD6XFData jobjTemp = new JsonD6XFData ( ) ;
jobjTemp . page = 1 ;
jobjTemp . limit = 10000 ;
jobjTemp . BsType = Convert . ToInt16 ( bsType ) ;
jobjTemp . Name = companyName ;
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 )
{
var BS = GETBSNO ( returndata . mblno , dbcon ) ;
if ( returndata . price = = "" | | returndata . price = = null ) returndata . price = "0" ;
if ( returndata . ctnrCount = = "" | | returndata . ctnrCount = = null ) returndata . ctnrCount = "0" ;
var feecustname = CustName ;
if ( COSCOAUTOCREATEFEE = = "1" & & returndata . forwarder = = "中远海运" ) {
if ( Convert . ToDecimal ( returndata . price ) = = 0 ) returndata . price = "30" ;
feecustname = CoscoCustName ;
}
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 ('" + BS . 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 ( ) ;
if ( ! string . IsNullOrEmpty ( BS . BSNO ) & & BS . FEESTATUS ! = "提交财务" & & BS . FEESTATUS ! = "费用封帐" & & BS . BSSTATUS ! = "应付完毕" )
{
if ( Convert . ToDecimal ( returndata . price ) ! = 0 & & ! string . IsNullOrEmpty ( feecustname ) & & ! string . IsNullOrEmpty ( FeeName ) )
{
sql = "INSERT INTO [t_ch_fee] ([类型],[费用状态],[编号],[费用名称],[结算单位],[客户名称],[标准]" ;
sql = sql + ",[单价],[数量],[金额],[币别],[汇率],[备注],[结算金额],[开票金额]" ;
sql = sql + ",[录入人],[录入日期],[是否对帐],[顺序],[冲抵金额]" ;
sql = sql + ",[机密],[是否垫付],[申请金额],[不开发票],[费用英文名称]" ;
sql = sql + ",[申请开票],[提交],[不含税价],[税率],[税额],PROJECTFEEID) VALUES " ;
sql = sql + "('付','审核通过','" + BS . BSNO + "','" + FeeName + "','','" + feecustname + "','票'" ;
sql = sql + "," + returndata . price + ",1," + returndata . price + ",'RMB',1,'',0,0" ;
sql = sql + ",'" + ENTEROPERATOR + "',GETDATE(),0,0,0" ;
sql = sql + ",0,0,0,0,'" + FeeName + "'" ;
sql = sql + ",0,0," + returndata . price + ",0,0,'" + returndata . gid + "')" ;
log . Debug ( $"{returndata.mblno},-费用语句-{sql}" ) ;
//dbcon.Open();
SqlCommand cmd2 = new SqlCommand ( sql , dbcon ) ;
cmd2 . ExecuteNonQuery ( ) ;
//dbcon.Close();
}
}
}
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 OPSEAE GETBSNO ( string MBLNO , SqlConnection dbcon )
{
var OPSEAE = new OPSEAE ( ) ;
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT 编号,费用状态,业务状态 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 )
{
OPSEAE . BSNO = row [ "编号" ] . ToString ( ) ;
OPSEAE . FEESTATUS = row [ "费用状态" ] . ToString ( ) ;
OPSEAE . BSSTATUS = row [ "业务状态" ] . ToString ( ) ;
}
}
return OPSEAE ;
}
}
public class JsonD6XFSend
{
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 JsonD6XFData data { get ; set ; }
}
public class JsonD6XFData
{
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 COMNAME { get; set; }
public string Name { get ; set ; }
}
public class JsonD6XFResponse
{
public bool status { get ; set ; }
public string message { get ; set ; }
public string code { get ; set ; }
public string count { get ; set ; }
public List < D6XFData > data { get ; set ; }
}
public class D6XFData
{
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 ; }
}
}