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.

164 lines
7.0 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using DSRecCallback.Models;
using DSWeb.Common.DB;
using log4net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
namespace DSRecCallback.Controllers
{
public class VGMController : Controller
{
private static Queue<ReqVgmRecModel> _queue = new Queue<ReqVgmRecModel>();
private static bool isRunning = false;
private static object _lock = new object();
private VgmDataContext vgmData = new VgmDataContext();
private ILog log = LogManager.GetLogger("VGMController");
[HttpPost]
public ActionResult Rec(string msg)
{
RespCommon resp = new RespCommon();
log.Debug($"收到vgm回执{msg}");
ReqVgmRecModel[] items = null;
try
{
items = JsonConvert.DeserializeObject<ReqVgmRecModel[]>(msg);
}
catch
{
resp.Code = RespCommon.RespCodeInvalidParam;
resp.Msg = "参数有误";
return Json(resp);
}
if (items == null || items.Length == 0)
{
resp.Code = RespCommon.RespCodeInvalidParam;
resp.Msg = "参数有误";
return Json(resp);
}
lock (_lock)
{
//放入队列单线程处理
foreach (var item in items)
{
_queue.Enqueue(item);
}
if (!isRunning)
{
log.Debug($"启动处理task");
isRunning = true;
Task.Run(() =>
{
while (_queue.Count > 0)
{
try
{
var item = _queue.Dequeue(); //取出一条进行处理
if (string.IsNullOrEmpty(item.businessId)
|| string.IsNullOrEmpty(item.id)
|| (item.resultCode != 1 && item.resultCode != 2 && item.resultCode != 3)
|| item.resultCreateTime == DateTime.MinValue)
{
log.Error($"json格式有误或数据不全{JsonConvert.SerializeObject(item)}");
continue;
}
var ctn = vgmData.Ctns.AsNoTracking().FirstOrDefault(c => c.ctn_id == item.businessId);
if (ctn == null)
{
log.Error($"未找到VGM箱信息{item.businessId}");
continue;
}
var vgm = vgmData.Vgms.FirstOrDefault(v => v.MFNO == ctn.MFNO);
if (vgm == null)
{
log.Error($"未找到VGM主单信息{ctn.MFNO}");
continue;
}
//根据箱子,先插入“暂无回执”状态的记录,后面再更新
var ctnList = vgmData.Ctns.AsNoTracking().Where(c => c.MFNO == vgm.MFNO).Select(c => c.CNTRNO).ToList(); //箱信息中的所有箱号
var staList = vgmData.Status.AsNoTracking().Where(s => s.mfno == vgm.MFNO).Select(c => c.cntrno).ToList(); //状态信息中的所有箱号
var notExist = ctnList.Where(x => !staList.Contains(x)).ToList(); //箱信息中有、状态信息中没有的箱号
var needRemove = staList.Where(x => !ctnList.Contains(x)).ToList(); //状态信息中有、箱信息中没有的箱号
//根据箱信息中的箱号,插入状态
if (notExist.Count > 0)
{
foreach (var cntrno in notExist)
{
var staIns = new OP_SEAE_VGM_STATUS();
staIns.gid = Guid.NewGuid().ToString();
staIns.mfno = vgm.MFNO;
staIns.cntrno = cntrno;
staIns.mblno = vgm.MBLNO;
staIns.createtime = DateTime.Now;
staIns.statustext = "暂无回执";
vgmData.Status.Add(staIns);
log.Debug($"箱状态信息不存在,插入:{cntrno} {vgm.MBLNO}");
}
}
//根据箱信息中的箱号,删除状态信息
if (needRemove.Count > 0)
{
foreach (var cntrno in needRemove)
{
vgmData.Status.RemoveRange(vgmData.Status.Where(x => x.mfno == vgm.MFNO && x.cntrno == cntrno));
log.Debug($"箱状态信息无效,删除:{cntrno} {vgm.MBLNO}");
}
}
vgmData.SaveChanges(); //先存库
//找到箱状态信息,并更新
var staObj = vgmData.Status.First(s => s.cntrno == ctn.CNTRNO && s.mfno == vgm.MFNO);
staObj.statustext = item.resultCode == 1 ? "船公司已接收" : "船公司拒绝";
staObj.statustime = item.resultCreateTime;
staObj.TIPS = item.resultCode != 1 ? item.rejectCause : string.Empty;
vgmData.SaveChanges();
log.Debug($"成功处理vgm回执ctnid:{item.id}cntno:{ctn.CNTRNO}mblno:{vgm.MBLNO}");
}
catch (Exception ex)
{
var exp = ex;
while (exp != null)
{
log.Error(exp.Message);
log.Error(exp.StackTrace);
exp = exp.InnerException;
}
}
}
isRunning = false;
log.Debug($"处理task完成");
});
}
}
return Json(resp);
}
}
}