using DSWeb.Common.DB;
using DSWeb.Common.DbContexts;
using DSWeb.Common.DbContexts.Model;
using DSWeb.DLL.SendEdiToYard;
using log4net;
using Newtonsoft.Json.Linq;
using Quartz;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using JsonConvert = Newtonsoft.Json.JsonConvert;
namespace DSWeb.Job.SendEdiToYard
{
///
/// 读取主单状态
///
public class ReadMainStatusJob : IJob
{
private ILog logger = LogManager.GetLogger("SendEdiToYardJob");
public static List WORKINGMBLNOList=new List();
public void Execute(IJobExecutionContext context)
{
//logger.Debug("运行测试");
////业务逻辑
//if (DateTime.Now > Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 06:00:00") && DateTime.Now < Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:00:00"))
//{
// logger.Debug("调取回执");
// UpdSendState();
//}
if (WORKINGMBLNOList == null) {
WORKINGMBLNOList = new List();
}
ReadDelayZhuDan();
}
public static void ReadDelayZhuDan() {
//首先查找延迟读取表 WLXX_MBLNO_DELAY 的所有 读取时间晚于当前时间的记录
EdiDataContext edc = new EdiDataContext();
//var delaylist = edc.WLXX_MBLNO_DELAY.Where(x => x.READTIME <= DateTime.Now).ToList();
var readhead = WLXX.获取待读主单(edc);
try
{
//如果在处理中 跳过
if (WORKINGMBLNOList.Exists(x => x == readhead.MBLNO))
{
EdiDataContext.SaveLog("舱单直发", "处理中跳过", readhead.MBLNO + ";" + readhead.READTIME, "");
//WLXX.删除待读主单(readhead.MFNO);
return;
}
else
{
//EdiDataContext.SaveLog("舱单直发", "处理中_增加", readhead.MBLNO + ";" + readhead.READTIME, "");
WORKINGMBLNOList.Add(readhead.MBLNO);
}
var _r = new Result_md();
//如果主单信息已存在 直接将分单设为可发送
if (DoSend.主单信息已存在(readhead))
{
if (!DoSend.存在可发分单(readhead))
{
//无待发分单
_r = WLXX.ReadWLXX(readhead.MBLNO, readhead.MFNO, "临时读取");
if (_r.Success == true)
{
WLXX.删除待读主单(readhead.MFNO);
}
else
{
DelayRead(readhead, edc);
}
}
else
{
if (!DoSend.分单已直发(readhead))
{
DelayRead_Success(readhead, edc, 3);
DoSend.待发分单列表设为可发送(readhead);
}
else
{
WLXX.删除待读主单(readhead.MFNO);
}
}
}
else
{
//20210310 改为如果有待发分单的话 调用读回执加主单信息的接口;如无待发分单 仍然调用读取回执接口
if (DoSend.待发分单存在(readhead))
{
//有待发分单 则需要读取主单信息
_r = WLXX.Read_StatusZDXX(readhead.MBLNO, readhead.USERID, readhead.MFNO);
if (_r.Success == true)
{
//目前逻辑是 如果分单发送成功 待读主单待发分单都会结束
DelayRead_Success(readhead, edc);
DoSend.待发分单列表设为可发送(readhead);
}
else
{
DelayRead(readhead, edc);
}
}
else
{
//无待发分单
_r = WLXX.ReadWLXX(readhead.MBLNO, readhead.MFNO, "临时读取");
if (_r.Success == true)
{
WLXX.删除待读主单(readhead.MFNO);
}
else
{
DelayRead(readhead, edc);
}
}
}
}
catch (Exception e) {
var msg = e.Message;
if (e.InnerException != null && e.Message.IndexOf("inner exception") > 0) {
msg = e.InnerException.Message;
}
EdiDataContext.SaveLog("舱单直发", "主进程错误", "【"+readhead.MBLNO + ";" + readhead.READTIME+"】"+ msg, "");
}
//EdiDataContext.SaveLog("舱单直发", "处理中_去除", readhead.MBLNO + ";" + readhead.READTIME, "");
WORKINGMBLNOList.Remove(readhead.MBLNO);
}
public static void DelayRead(WLXX_MBLNO_DELAY_md readhead,EdiDataContext edc, int delaytime=3)
{
readhead.DelayNextTime(edc, delaytime);
if (readhead.isOutoftime())
{
OutofTime(readhead);
}
}
public static void DelayRead_Success(WLXX_MBLNO_DELAY_md readhead, EdiDataContext edc, int delaytime = 3)
{
//readhead.DelayNextTime_Success(edc, delaytime);
readhead.DelayNextTime(edc, delaytime);
}
private static void OutofTime(WLXX_MBLNO_DELAY_md headdata) {
//DoSend.直发主单设为发送异常(headdata, "未能获得舱单回执");
WLXX.删除待读主单(headdata.MFNO);
DoSend.待发分单删除(headdata.MFNO);
DoSend.待发分单设为超时(headdata.MFNO);
}
}
///
/// 直发分单
///
public class SendFendanJob : IJob
{
private ILog logger = LogManager.GetLogger("SendFendanJob");
public static List WORKINGMBLNOList = new List();
public void Execute(IJobExecutionContext context)
{
if (WORKINGMBLNOList == null)
{
WORKINGMBLNOList = new List();
}
SendFendan();
}
public static void SendFendan()
{
EdiDataContext edc = new EdiDataContext();
var fendanlist = edc.OP_SEAE_EDI_FENDANSEND.Where(x => x.CANSEND==true ).ToList();
if (fendanlist != null && fendanlist.Count > 0)
{
var fendanhead = fendanlist[0];
//如果在处理中 跳过
if (WORKINGMBLNOList.Exists(x => x == fendanhead.MBLNO))
{
EdiDataContext.SaveLog("舱单直发", "处理中跳过", "待发分单"+fendanhead.MBLNO);
return;
}
else
{
EdiDataContext.SaveLog("舱单直发", "处理中增加", "待发分单" + fendanhead.MBLNO);
WORKINGMBLNOList.Add(fendanhead.MBLNO);
}
//20210827 改为开始读取时 先将cansend置为0
//如果后期需要的话在后期置为1
DoSend.待发分单删除(fendanhead.BSNO);
//20210824 定时任务发送待发分单时 只发送“发送中”状态的分单
//防止出现【发送时选择的主分单同时选择,但不是全部分单,然后延迟发送分单时,将之前发送时未选择的分单也发出】的情况。
var billList = edc.Edis.Where(x=>x.BSNO== fendanhead.BSNO && (x.EDISTATUS=="发送中"|| x.EDISTATUS == "发送超时" || x.EDISTATUS == "发送失败")).ToList();
if (billList.Count == 0)
{
DoSend.待发分单删除(fendanhead.BSNO);
WLXX.删除待读主单(fendanhead.BSNO);
return;
}
//20210602 增加判断 如果读回 则比对
//1【读回的主单信息中的件数 重量删除标志】
var 本地分单与读回主单一致 = CheckData.ZDXXChanged(fendanhead.BSNO);
if (!本地分单与读回主单一致.Success)
{
ChangeInfo.SetEdiStatus(billList, "新增", fendanhead.SHOWNAME, fendanhead.DCARRIER, EdiSendStatus.Fail, 本地分单与读回主单一致.Message);
WLXX.删除待读主单(fendanhead.BSNO);
DoSend.待发分单删除(fendanhead.BSNO);
}
//var mfnolist = billList.Select(s => s.MFNO).ToList();
var beizhu = "";
var fendanFeeList = ChangeInfo.MakeChangeList(edc, fendanhead.SHOWNAME, fendanhead.COMPANYNAME, fendanhead.COMPANYNAME, billList, fendanhead.USERID, 0,ref beizhu, true);
var 价格表内的type = ChangeInfo.getCustFeeType(ChangeInfo.getSendType("新增"), true);
try
{
try
{
fendanhead.FILENAME = DoSend.MakeFenDanFile_WithFileName(billList, 9, fendanhead.SENDCODE, fendanhead.FILENAME, fendanhead.USERID);
}
catch (Exception e) {
EdiDataContext.SaveLog("舱单直发", "分单文件错误", e.Message+";;"+ fendanhead.SENDCODE+";;"+ fendanhead.FILENAME, fendanhead.USERID);
ChangeInfo.SetEdiStatus(billList, "新增", fendanhead.SHOWNAME, fendanhead.DCARRIER, EdiSendStatus.Unusual, "自动发送的分单文件生成错误,可能分单信息发生变动,请尝试手工重新发送。");
DoSend.待发分单删除(fendanhead.BSNO);
WLXX.删除待读主单(fendanhead.BSNO);
}
DoSend.待发分单删除(fendanhead.BSNO);
var _dosend = new DoSend();
var _r = _dosend.SendFendanDirect(billList, fendanhead.DCARRIER, fendanhead.FILENAME, fendanhead.USERID, fendanhead.SHOWNAME, "新增");
if (_r.Success)
{
WLXX.删除待读主单(fendanhead.BSNO);
var pricehead = edc.Cust_Price.FirstOrDefault(x => x.COMNAME == fendanhead.COMPANYNAME && x.SENDTYPE == 价格表内的type);
//var balance = edc.Cust_Balance.FirstOrDefault(x => x.COMNAME == fendanhead.COMPANYNAME);
var contentStr = _r.Data.ToString();
foreach (var item in billList)
{
//var hblnoSuccess =DoSend.getSuccessStr(item.HBLNO);
//if (contentStr.Contains(hblnoSuccess))
//{
var CustFee = fendanFeeList.First(x => x.HBLNO == item.HBLNO);
ChangeInfo.扣费(edc, CustFee, ref pricehead, fendanhead.COMPANYNAME);
//}
}
edc.SaveChanges();
}
else
{
if (_r.Message.Contains("提交接口错误"))
{
/*[COAU7230499780]{"stats": "0", "message": "提交接口错误: HTTPConnectionPool(host='119.29.114.124', port=50549): Max retries exceeded with rl: http://56.qdcdc.com/cnm/Index.aspx (Cased by ProxyError('Cannot connect to proxy.', RemoteDisconnected('Remote end closed connection withot response',)))"} */
DoSend.待发分单重置(fendanhead.BSNO);
}
else
{
WLXX.删除待读主单(fendanhead.BSNO);
}
}
EdiDataContext.SaveLog("舱单直发", "处理中移除1", "待发分单" + fendanhead.MBLNO);
WORKINGMBLNOList.Remove(fendanhead.MBLNO);
}
catch (Exception e) {
ChangeInfo.SetEdiStatus(billList, "新增", fendanhead.SHOWNAME, fendanhead.DCARRIER, EdiSendStatus.Unusual,e.Message);
DoSend.待发分单删除(fendanhead.BSNO);
EdiDataContext.SaveLog("舱单直发", "处理中移除2", "待发分单" + fendanhead.MBLNO);
WORKINGMBLNOList.Remove(fendanhead.MBLNO);
}
}
}
}
public class JobHelper
{
//public static string DoPost(string url, string json, int timeout = 10000)
//{
// string responseString = "";//post返回的结果
// ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
// ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
// HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
// req.Method = "POST";
// req.Timeout = timeout;
// req.Headers.Add("Accept-Encoding", "gzip, deflate");
// if (!string.IsNullOrWhiteSpace(json))
// {
// byte[] postBytes = Encoding.UTF8.GetBytes(json);
// req.ContentType = "application/json; charset=utf-8";
// req.ContentLength = Encoding.UTF8.GetByteCount(json);
// Stream stream = req.GetRequestStream();
// stream.Write(postBytes, 0, postBytes.Length);
// stream.Close();
// }
// else
// {
// req.ContentLength = 0;
// }
// var response = req.GetResponse();
// responseString = GetResponseBody((HttpWebResponse)response);
// return responseString;
//}
}
}