using DSWeb.Areas.Dispatch.DB;
using DSWeb.Areas.Dispatch.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
namespace DSWeb.Areas.Dispatch.Helper
{
public static class DispatchHelper
{
private static string appid = ConfigurationManager.AppSettings["BeitongDispatchAppId"];
private static string dispatchServerUrl = ConfigurationManager.AppSettings["BeitongDispatchServerUrl"];
///
/// 抢单
///
///
///
public static bool SendDispacthMessage(string leid, out string message)
{
DataContext dataContext = new DataContext();
var query = from i in dataContext.InfoClients
join d in dataContext.Drivers on i.SHORTNAME equals d.InfoClient
where i.ISTRUCK
select new
{
d.OpenId,
d.InfoClient
};
var infoList = query.ToList();
var truckInfo = dataContext.OpLetterTrucks.FirstOrDefault(t => t.LE_ID == leid);
//如果重复发电,删除先前的
dataContext.Grabs.RemoveRange(dataContext.Grabs.Where(g => g.LE_ID == leid));
//插入发单信息
foreach (var item in infoList)
{
GrabInfo grab = new GrabInfo();
grab.GID = Guid.NewGuid().ToString();
grab.InfoClient = item.InfoClient;
grab.LE_ID = truckInfo.LE_ID;
grab.SendTime = DateTime.Now;
grab.IsGrab = false;
dataContext.Grabs.Add(grab);
}
dataContext.SaveChanges();
StringBuilder sbErr = new StringBuilder();
bool allSuccess = true;
#region 微信禁止板消息群发,暂注释
//保存完数据后,再发送消息
//foreach (var item in infoList)
//{
// List> values = new List>();
// values.Add(new Tuple("TITLE", "您有一条新的抢单通知", "#FF0000"));
// values.Add(new Tuple("XX", truckInfo.CTNS, ""));
// values.Add(new Tuple("DW", truckInfo.KGS, ""));
// values.Add(new Tuple("ZHSJ", truckInfo.FACTRYTIME, ""));
// values.Add(new Tuple("JGSJ", truckInfo.CLOSINGDATE.HasValue ? truckInfo.CLOSINGDATE.Value.ToString("yyyy-MM-dd") : "", ""));
// values.Add(new Tuple("GCDZ", truckInfo.FACTRYADDR, ""));
// values.Add(new Tuple("FOOTTER", "点击查看详情", "#0000FF"));
// string mess = string.Empty;
// bool success = TemplateMessageHelper.SendTemplateMessage(appid, "5zWCiYhS44naUs1dolJuMtAhzqvrfpdHUK6JQJdtkLw", values, item.OpenId, $"{dispatchServerUrl}/Dispatch/Dispatch/GrabDetail?leid={truckInfo.LE_ID}&openid={item.OpenId}", out mess);
// if (!success)
// {
// sbErr.AppendLine(mess);
// allSuccess = false;
// }
//}
#endregion
message = sbErr.ToString();
return allSuccess;
}
///
/// 派车
///
///
///
public static bool DispacthMessage(string leid, out string message)
{
DataContext dataContext = new DataContext();
var truckInfo = dataContext.OpLetterTrucks.FirstOrDefault(t => t.LE_ID == leid);
var driver = dataContext.Drivers.FirstOrDefault(d => d.InfoClient == truckInfo.TRUCK);
var opseae = dataContext.OpSeaes.FirstOrDefault(o => o.BSNO == truckInfo.BSNO);
if (driver == null)
{
message = "所选车队没有已注册的司机";
return false;
}
//todo:发送消息
List> values = new List>();
values.Add(new Tuple("first", "您有一条新的派车通知\r\n", ""));
values.Add(new Tuple("keyword1", opseae.MBLNO, ""));
values.Add(new Tuple("keyword2", truckInfo.FACTRYADDR, ""));
values.Add(new Tuple("keyword3", truckInfo.FACTRYTIME, ""));
values.Add(new Tuple("keyword4", $"{truckInfo.CTNS} {truckInfo.KGS}", ""));
values.Add(new Tuple("keyword5", DateTime.Today.ToString("yyyy-MM-dd"), ""));
if (truckInfo.CLOSINGDATE.HasValue)
{
values.Add(new Tuple("remark", $"截港时间:{truckInfo.CLOSINGDATE.Value.ToString("yyyy-MM-dd")}\r\n\r\n请按时到场站拉货", ""));
}
else
{
values.Add(new Tuple("remark", $"请按时到场站拉货", ""));
}
string mess = string.Empty;
bool success = TemplateMessageHelper.SendTemplateMessage(appid, "hHh9N9xY1sVK6d8wxS-ObEV-0gWsPh_iCR13K447VXU", values, driver.OpenId, $"{dispatchServerUrl}/Dispatch/Dispatch/DispatchDetail?leid={truckInfo.LE_ID}", out mess);
if (!success)
{
message = mess;
return false;
}
else
{
message = "";
return true;
}
}
}
}