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.

205 lines
10 KiB
C#

using DSWeb.Common.DB;
using DSWeb.Common.Helper;
using DSWeb.Common.Model;
using log4net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Quartz;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DSWeb.Job.Common
{
public class TaskXiahuozhiJob : IJob
{
private CommonDataContext commonData = new CommonDataContext();
private CompanyDataContext companyData = new CompanyDataContext();
private BookingDB bookingData = new BookingDB();
private OpTaskDataContext taskDataContext = new OpTaskDataContext();
private ILog logger = LogManager.GetLogger("TaskXiahuozhiJob");
public void Execute(IJobExecutionContext context)
{
var todoList = commonData.BackgroundTaskCommon
.Where(t => t.Status == BackgroundTaskCommon.StatusCreate && t.Type == BackgroundTaskCommon.TypeBookingXiahuozhi)
.OrderBy(t => t.CreateTime)
.Take(10)
.ToList();
if (todoList.Count > 0)
{
logger.Debug($"发现{todoList.Count}条待自动下货纸的任务");
todoList.ForEach(t =>
{
t.Status = BackgroundTaskCommon.StatusDoing;
t.ExecuteCount++;
t.ExecuteTime = DateTime.Now;
});
commonData.SaveChanges();
foreach (var item in todoList)
{
var ordno = item.ParamData.ToString();
var order = bookingData.Orders.FirstOrDefault(o => o.ORDNO == ordno);
if (order == null)
{
var msg = $"订舱信息未找到:{ordno}";
logger.Error(msg);
item.Status = BackgroundTaskCommon.StatusFail;
item.ResultData = msg;
commonData.SaveChanges();
continue;
}
var paraXHZ = commonData.ParamValues.AsNoTracking().FirstOrDefault(x => x.CompId == order.CORPID && x.ParaCode == "AUTO_XIAHUOZHI");
if (paraXHZ == null || paraXHZ.ItemCode != "YES")
{
var msg = $"所属公司配置为非自动下货纸,不执行自动下货纸:{order.MBLNO}";
logger.Error(msg);
item.Status = BackgroundTaskCommon.StatusFail;
item.ResultData = msg;
commonData.SaveChanges();
continue;
}
//费用自结的不自动下货纸
if (order.FEE_SELF)
{
item.Status = BackgroundTaskCommon.StatusFail;
item.ResultData = $"费用自结,不处理自动下货纸:{order.MBLNO}";
}
else
{
if (order.StaXiaHuoZhi == Constants.STA_XIAHUOZHI_SENT)
{
item.Status = BackgroundTaskCommon.StatusFail;
item.ResultData = $"下货纸处理失败:该票数据已发送过下货纸";
logger.Error($"自动放舱处理下货纸失败:{order.MBLNO},错误信息:该票数据已发送过下货纸");
}
else
{
//下货纸
var dirPath = ConfigurationManager.AppSettings["MyshippingTaskFilePath"];
var filePath = Path.Combine(dirPath, "FtpFiles");
if (XiahuozhiHelpler.Send(commonData, bookingData, order.ORDNO, "9", filePath, out string mess))
{
item.Status = BackgroundTaskCommon.StatusSuccess;
item.ResultData = $"下货纸处理成功";
logger.Debug($"下货纸处理成功:{order.MBLNO}");
#region 订单货运状态
var staLog = new OP_STATUS_LOG();
staLog.GID = Guid.NewGuid().ToString();
staLog.RELATIVE_ID = order.ORDNO;
staLog.STATUS = $"自动发送下货纸-{order.YARD}";
staLog.OP_TIME = DateTime.Now;
staLog.OP_USER = "系统";
staLog.CATEGORY = OP_STATUS_LOG.CateShip;
bookingData.StatusLogs.Add(staLog);
#endregion
//反馈下货纸状态
if (!HttpFeedbackHelper.FeedbackXiaHuoZhi(commonData, order.CORPID, order.BSNO, true, out string rtnMsg))
{
logger.Error($"反馈下货纸状态出错:{rtnMsg},提单号:{order.MBLNO}");
}
order.StaXiaHuoZhi = Constants.STA_XIAHUOZHI_SENT;
#region 记录动作
var mqSendObj = new DjyActionLog()
{
ModuleName = EActionModule.Booking.ToString(),
ActionType = EActionType.XiaHuoZhiSend.ToString(),
ActionCate = "单票",
SourceOp = "发送下货纸",
ActionTime = DateTime.Now,
Carrier = order.CARRIERID,
IsAuto = true,
UserId = order.USERID,
UserName = order.INPUTBY,
CompId = order.CORPID,
CompName = order.CompName,
MBLNO = order.MBLNO,
RelativeId = order.ORDNO,
CtrnNO = string.Empty,
Remark = string.Empty,
ExtData = string.Empty
};
ActionLogHelper.SendActionLog(mqSendObj);
#endregion
//2022年7月22日下货纸完成后所有任务都自动完成
var staCreate = OpTaskStatus.Create.ToString();
taskDataContext.OpTasks
.Where(x => x.ComId == order.CORPID && x.MBLNO == order.MBLNO && x.TASKSTATUS == staCreate)
.ToList()
.ForEach(x =>
{
x.TASKSTATUS = OpTaskStatus.Complete.ToString();
x.COMPLETETYPE = "自动";
x.COMPLETETIME = DateTime.Now;
});
taskDataContext.SaveChanges();
#region 2022年7月29日下货纸扣费紧急添加8月1日使用
var bstype = 19;
var sendtype = 1001;
var cusPri = companyData.Cust_Price.AsNoTracking().FirstOrDefault(x => x.BSTYPE == bstype && x.SENDTYPE == sendtype && x.COMNAME == order.CompName);
var cusBal = companyData.Cust_Balance.FirstOrDefault(x => x.COMNAME == order.CompName);
if (cusPri != null && cusBal != null) //设置了单价才扣费
{
var custFee = companyData.Cust_Fee.AsNoTracking().FirstOrDefault(x => x.BSTYPE == bstype && x.SENDTYPE == sendtype && x.BSNO == order.ORDNO);
if (custFee == null)
{
//扣费
cusBal.Balance -= cusPri.PRICE;
CustFee addFee = new CustFee();
addFee.GID = Guid.NewGuid().ToString().Replace("-", "");
addFee.MBLNO = order.MBLNO;
addFee.PRICE = cusPri.PRICE;
addFee.CREATETIME = DateTime.Now;
addFee.SENDTIME = DateTime.Now;
addFee.BSTYPE = bstype;
addFee.BEIZHU = $"下货纸";
addFee.SENDTYPE = sendtype;
addFee.SENDUSER = order.INPUTBY;
addFee.SENDCOM = order.CompName;
addFee.COMNAME = order.CompName;
addFee.SENDUSERID = order.USERID;
addFee.LURURENID = order.USERID;
companyData.Cust_Fee.Add(addFee);
companyData.SaveChanges();
}
}
#endregion
}
else
{
item.Status = BackgroundTaskCommon.StatusFail;
item.ResultData = $"下货纸处理失败:{mess}";
logger.Error($"自动放舱处理下货纸失败:{order.MBLNO},错误信息:{mess}");
DingTalkMessageHelper.SendGroupMessage("下货纸失败通知", $"提单号为{order.MBLNO}的下货纸未能成功发送,异常信息:{mess}", "异常通知", ConfigurationManager.AppSettings["DingTalkCmaExceptNotify"]);
}
}
}
commonData.SaveChanges();
bookingData.SaveChanges();
}
}
}
}
}