You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/JobReqWebData/JobGetData.cs

350 lines
14 KiB
C#

3 years ago
using log4net;
using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace JobReqWebData
{
public class JobGetData : IJob
{
private ILog log = LogManager.GetLogger(typeof(JobGetData));
public void Execute(IJobExecutionContext context)
{
try
{
string connStr = context.JobDetail.JobDataMap.GetString("ConnectString");
string reqUrl = context.JobDetail.JobDataMap.GetString("ReqUrl");
string customer = context.JobDetail.JobDataMap.GetString("Customer");
string password = context.JobDetail.JobDataMap.GetString("Password");
int reqTimeout = Convert.ToInt32(context.JobDetail.JobDataMap.GetString("RequestTimeout"));
log.Debug($"连接字符串:{connStr}");
log.Debug($"请求URL{reqUrl}");
log.Debug($"用户:{customer}");
log.Debug($"密码:{password}");
log.Debug($"请求超时:{reqTimeout}");
//访问url获取爬取的数据
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("custname", customer);
dic.Add("psw", password);
log.Debug($"准备请求数据:{dic.Count}");
string rtn = WebRequestHelper.DoPost(reqUrl, dic, reqTimeout*1000);
log.Debug($"数据返回:{rtn}");
//测试
//string rtn = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "response.json"));
#region 模板
//var jsonTemplate = new
//{
// TD = new dynamic[]
// {
// new
// {
// GID = "",
// CZNAME = "",
// YARDNAME = "",
// ChuanMing_En = "",
// ChuanMing_Cn = "",
// HangCi = "",
// HaiGuanHangCi = "",
// HangXian = "",
// MBLNO = "",
// XiangXing = "",
// XiangLiang = "",
// MuDiGang = "",
// ZhongZhuanGang = "",
// JiGangKaiShiShiJian = "",
// JiGangJieShuShiJian = "",
// ChuanGongSi = "",
// TongGuanFangShi = "",
// CreateTime = DateTime.Now,
// XieHuoGang = "",
// KaoGangMaTou = "",
// ZhuangChuanMaTou = "",
// SongHuoDiZhi = "",
// LiHuoSongHuoChangQu = "",
// JieGangShiJian = ""
// }
// },
// XD = new dynamic[]
// {
// new
// {
// GID = "",
// MBLNO = "",
// YARDNAME = "",
// XiangHao = "",
// XiangXing = "",
// FengHao = "",
// NeiDianWaiDian = "",
// XiangPiZhong = "",
// HuoZhong = "",
// ZongZhong = "",
// VGM = "",
// CheHao = "",
// TiXiangShiJian = "",
// FanChangShiJian = "",
// JiGangShiJian = "",
// HuaBo = "",
// ZhiDingXiangHaoTiXiangChangQu = "",
// ShangJianXiangHao = "",
// CreateTime = DateTime.Now
// }
// },
// FD = new dynamic[]
// {
// new
// {
// GID = "",
// MBLNO = "",
// YARDNAME = "",
// FDH = "",
// XiangHao = "",
// XiangXing = "",
// QianFeng = "",
// JianShu = "",
// BaoZhuang = "",
// ZhongLiang = "",
// ChiMa = "",
// CreateTime = DateTime.Now
// }
// },
// WL = new dynamic[]
// {
// new
// {
// GID = "",
// MBLNO = "",
// CZNAME = "",
// HuoWuZhuangTai = "",
// ShenBaoXiangShu = "",
// JianShu = "",
// ZhongLiang = "",
// YuPeiShiJian = "",
// YuPeiJieGuo = "",
// YunDiShiJian = "",
// YunDiJieGuo = "",
// FangXingShiJian = "",
// FangXingZhuangTai = "",
// ZhuangZaiShiJian = "",
// ZhuangZaiJieGuo = "",
// CreateTime = DateTime.Now
// }
// }
//};
#endregion
var parseJsonObj = JsonConvert.DeserializeObject<JsonResponse>(rtn);
//入库
using (SqlConnection dbcon = new SqlConnection(connStr))
{
dbcon.Open();
using (SqlTransaction trans = dbcon.BeginTransaction())
{
#region 提单
//foreach (var item in parseJsonObj.TD)
//{
// string sql = ExecuteCommand(item.GetType(), item, item.MBLNO);
// SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
// cmd.ExecuteNonQuery();
//}
#endregion
#region 箱号
foreach (var item in parseJsonObj.XD)
{
string sql = ExecuteCommand(item.GetType(), item, item.XiangHao,"XiangHao");
SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
cmd.ExecuteNonQuery();
}
#endregion
#region 分单
//foreach (var item in parseJsonObj.FD)
//{
// string sql = ExecuteCommand(item.GetType(), item, item.FDH, "FDH");
// SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
// cmd.ExecuteNonQuery();
//}
#endregion
#region 物流
foreach (var item in parseJsonObj.WL)
{
string sql = ExecuteCommand(item.GetType(), item, item.MBLNO);
SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
cmd.ExecuteNonQuery();
}
#endregion
#region 云港通
foreach (var item in parseJsonObj.YGT.QdPortChuanBoJiHua)
{
string sql = ExecuteCommand(item.GetType(), item, item.TDH, "TDH");
SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
cmd.ExecuteNonQuery();
}
//foreach (var item in parseJsonObj.YGT.QdPortDanChuanChuKouKLBJH)
//{
// string sql = ExecuteCommand(item.GetType(), item, item.ZTDH, "ZTDH");
// SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
// cmd.ExecuteNonQuery();
//}
//foreach (var item in parseJsonObj.YGT.QdPortDanChuanChuKouMTCQ)
//{
// string sql = ExecuteCommand(item.GetType(), item, item.ZTDH, "ZTDH");
// SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
// cmd.ExecuteNonQuery();
//}
//foreach (var item in parseJsonObj.YGT.QdPortDanChuanChuKouZYJH)
//{
// string sql = ExecuteCommand(item.GetType(), item, item.ZTDH, "ZTDH");
// SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
// cmd.ExecuteNonQuery();
//}
//foreach (var item in parseJsonObj.YGT.QdPortDanChuanChuKouZYQK)
//{
// string sql = ExecuteCommand(item.GetType(), item, item.ZTDH, "ZTDH");
// SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
// cmd.ExecuteNonQuery();
//}
foreach (var item in parseJsonObj.YGT.QdPortDanPiaoChuKouBGDFX)
{
string sql = ExecuteCommand(item.GetType(), item, item.TDH, "TDH");
SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
cmd.ExecuteNonQuery();
}
foreach (var item in parseJsonObj.YGT.QdPortDanPiaoChuKouMTXX)
{
string sql = ExecuteCommand(item.GetType(), item, item.XH, "XH");
SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
cmd.ExecuteNonQuery();
}
//foreach (var item in parseJsonObj.YGT.QdPortDanPiaoChuKouWLLH)
//{
// string sql = ExecuteCommand(item.GetType(), item, item.TDH, "TDH");
// SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
// cmd.ExecuteNonQuery();
//}
//foreach (var item in parseJsonObj.YGT.QdPortDanPiaoChuKouWLSH)
//{
// string sql = ExecuteCommand(item.GetType(), item, item.XH, "XH");
// SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
// cmd.ExecuteNonQuery();
//}
foreach (var item in parseJsonObj.YGT.QdPortDanPiaoChuKouYDBG)
{
string sql = ExecuteCommand(item.GetType(), item, item.XH, "XH");
SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
cmd.ExecuteNonQuery();
}
//foreach (var item in parseJsonObj.YGT.QdPortDanPiaoJinKouHGBGDFX)
//{
// string sql = ExecuteCommand(item.GetType(), item, item.XH, "XH");
// SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
// cmd.ExecuteNonQuery();
//}
//foreach (var item in parseJsonObj.YGT.QdPortDanPiaoChuKouZXD)
//{
// string sql = ExecuteCommand(item.GetType(), item, item.TDH, "TDH");
// SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
// cmd.ExecuteNonQuery();
//}
foreach (var item in parseJsonObj.YGT.QdPortDanPiaoChuKouZZFX)
{
string sql = ExecuteCommand(item.GetType(), item, item.XH, "XH");
SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
cmd.ExecuteNonQuery();
}
#endregion
#region 单一窗口
foreach (var item in parseJsonObj.SDITDS)
{
string sql = ExecuteCommand(item.GetType(), item, item.MBLNO);
SqlCommand cmd = new SqlCommand(sql, dbcon, trans);
cmd.ExecuteNonQuery();
}
#endregion
trans.Commit();
}
dbcon.Close();
}
}
catch (Exception ex)
{
log.Error(ex.Message);
log.Error(ex.StackTrace);
}
}
private string ExecuteCommand(Type itemType, object item, string mblno, string mblnoColName = "MBLNO")
{
string sqlDel = $"delete {itemType.Name} where {mblnoColName}='{mblno}';";
string colStr = string.Empty;
string valStr = string.Empty;
PropertyInfo[] propertys = itemType.GetProperties();
foreach (var prop in propertys)
{
if (colStr.Length > 0)
{
colStr += ",";
valStr += ",";
}
colStr += prop.Name;
valStr += $"'{prop.GetValue(item, null)}'";
}
string sqlIns = $"insert into {item.GetType().Name}({colStr}) values({valStr});";
return sqlDel + sqlIns;
}
}
}