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.

388 lines
15 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using Ys.Core.Common;
using djy.Paas.Model;
using djy.Model;
using Ys.Core.Common.Email;
namespace djy.Paas.Service
{
/// <summary>
/// 常用功能工具
/// </summary>
public class DjyTools:ServBase
{
/// <summary>
/// redis 缓存写入
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="setData"></param>
/// <param name="Key"></param>
/// <param name="TimeOut">过期秒数</param>
/// <returns></returns>
public static bool RedisSet<T>(string Key, T setData,int TimeOut=0) {
var val = "";
if (typeof(T) != typeof(string))
{
val =JsonSerializer.Serialize(setData);
}
else {
val = setData.ToString();
}
if (TimeOut > 0)
{
DbRedis.Set(Key, val, TimeOut);
}
else {
DbRedis.Set(Key, val);
}
return true;
}
/// <summary>
/// 读取redis中的对象数据 不存在返回null
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Key"></param>
/// <returns></returns>
public static T RedisGet<T>(string Key) {
var val = DbRedis.Get(Key);
if (val.IsNull())
{
return default(T);
}
else {
return YsJson.JsonToObject<T>(val);
}
}
/// <summary>
/// 获取数据字典分组列表
/// </summary>
/// <returns></returns>
public static Dictionary<string, string> GetConfigList(string GroupName = null) => new ToolsService().GetConfigList(GroupName);
/// <summary>
/// 获取配置参数
/// </summary>
/// <param name="key"></param>
/// <param name="GroupName">默认sys</param>
public static string GetConfigKey(string key, string GroupName = "sys") => new ToolsService().GetConfigKey(key,GroupName);
/// <summary>
/// 获取场站列表数据
/// </summary>
/// <returns></returns>
public static List<DjyDictDto> GetBoxStationList() {
var get = new ToolsService().GetDjyDict("boxstationlist",new ApiFromDto()).Data;
return (List<DjyDictDto>)get;
}
/// <summary>
/// 获取船务公司列表
/// </summary>
/// <returns></returns>
public static List<DjyDictDto> GetCarrierList() {
return YsJson.JsonToObject<List<DjyDictDto>>(GetConfigKey("carrierlist"));
}
/// <summary>
///
/// </summary>
/// <param name="emailArray">逗号间隔的Email地址</param>
/// <param name="Subject">Email主题</param>
/// <param name="bodyhtml">Email内容</param>
/// <param name="SendName">发件人</param>
/// <returns></returns>
public static SendResultEntity EmailSendOnLogs(string emailArray, string Subject, string bodyhtml, string SendName, string LogsMsg)
{
var email = new List<string>();
foreach (var item in emailArray.Split(','))
{
if (item.IsEmail())
email.Add(item);
}
return EmailSendOnLogs(email, Subject, bodyhtml, SendName, LogsMsg);
}
/// <summary>
/// Email发送
/// </summary>
/// <param name="email">收件人</param>
/// <param name="Subject">Email主题</param>
/// <param name="bodyhtml">Email内容</param>
/// <param name="SendName">发件人</param>
/// <param name="SendName">附件</param>
public static SendResultEntity EmailSendOnLogs(List<string> email, string Subject, string bodyhtml, string SendName, string LogsMsg, List<MailFile> FileList = null, SendServerConfigurationEntity MailConfig = null)
{
var get = new SendResultEntity();
try
{
var smlist = new List<string>();
foreach (var item in email)
{
smlist.Add(item.ToLower());
}
var mailBodyEntity = new MailBodyEntity(smlist, Subject, bodyhtml, SendName, null);
if (mailBodyEntity.SenderAddress.IsNull())
{
if (mailBodyEntity.Sender.IsNotNull())
{
mailBodyEntity.SenderAddress = mailBodyEntity.Sender;
}
else
{
mailBodyEntity.SenderAddress = sysOptionConfig.YsWebconfig.ConfigList["sys.Email_SendAccess"];
}
}
if (MailConfig == null)
{
MailConfig = new SendServerConfigurationEntity
{
SmtpHost = sysOptionConfig.YsWebconfig.ConfigList["sys.Email_SmtpHost"],
SmtpPort = int.Parse(sysOptionConfig.YsWebconfig.ConfigList["sys.Email_SmtpPort"]),
SenderAccount = sysOptionConfig.YsWebconfig.ConfigList["sys.Email_LoginName"],
SenderPassword = sysOptionConfig.YsWebconfig.ConfigList["sys.Email_Password"],
IsSsl = false
};
}
if (FileList != null)
{
mailBodyEntity.MailFiles = FileList;
}
// mailBodyEntity.Sender = MailConfig.SenderAccount;
//mailBodyEntity.SenderAddress = MailConfig.SenderAccount;
if (mailBodyEntity.Sender.IsNull())
{
mailBodyEntity.Sender = MailConfig.SenderAccount;
}
get = YsMailSendHelp.SendMail(mailBodyEntity, MailConfig);
if (LogsMsg.IsNull())
{
LogsMsg = "Email发送";
}
_LogsAdd(LogsMsg, "email", new { mailBodyEntity, MailConfig }, null, get);
}
catch (Exception ex)
{ get.ResultInformation=ex.Message;
get.ResultStatus = false;
}
return get;
}
/// <summary>
/// Email发送
/// </summary>
/// <param name="email">收件人</param>
/// <param name="Subject">Email主题</param>
/// <param name="bodyhtml">Email内容</param>
/// <param name="SendName">发件人</param>
public static SendResultEntity EmailSendOnLogs(List<string>email,string Subject,string bodyhtml,string SendName,string LogsMsg) {
var get = new SendResultEntity();
try
{
var smlist = new List<string>();
foreach (var item in email)
{
smlist.Add(item.ToLower());
}
var mailBodyEntity = new MailBodyEntity(smlist, Subject, bodyhtml, SendName, null);
if (mailBodyEntity.SenderAddress.IsNull())
{
//2022-3-7因赵工修改未完成导致提箱小票无法发送邮件暂注释掉修改后期再做处理
//if (mailBodyEntity.Sender.IsNotNull())
//{
// mailBodyEntity.SenderAddress = mailBodyEntity.Sender;
//}
//else
{
mailBodyEntity.SenderAddress = sysOptionConfig.YsWebconfig.ConfigList["sys.Email_SendAccess"];
}
}
var sendServerConfiguration = new SendServerConfigurationEntity
{
SmtpHost = sysOptionConfig.YsWebconfig.ConfigList["sys.Email_SmtpHost"],
SmtpPort = int.Parse(sysOptionConfig.YsWebconfig.ConfigList["sys.Email_SmtpPort"]),
SenderAccount = sysOptionConfig.YsWebconfig.ConfigList["sys.Email_LoginName"],
SenderPassword = sysOptionConfig.YsWebconfig.ConfigList["sys.Email_Password"],
IsSsl = false
};
get = YsMailSendHelp.SendMail(mailBodyEntity, sendServerConfiguration);
if (LogsMsg.IsNull())
{
LogsMsg = "Email发送";
}
_LogsAdd(LogsMsg, "paas_email", new { mailBodyEntity, sendServerConfiguration }, null, get);
}
catch (Exception ex)
{
get.ResultInformation = ex.Message;
get.ResultStatus = false;
}
return get;
}
/// <summary>
/// Post请求 和htmlhelp比较增加
/// </summary>
/// <param name="PostData">Json时用对象 form模式用 Dictionary<string, string> form支持 a=1&b=2字符串和对象模式 </param>
/// <param name="RequestUrl">请求地址</param>
/// <param name="postType">提交数据模式 默认json模式</param>
/// <paramref name="FileList">提交的文件数据流</paramref>
/// <param name="JsonNotWebOption"></param>
/// <param name="Token">token</param>
/// <returns></returns>
public static async Task<string> PostOnLogs<T>(T PostData, string RequestUrl,string LogMsg="", YsPsotType postType = YsPsotType.Json, List<PostFileListEntity> FileList = null, string Token = null,bool JsonNotWebOption = true)
{
if (LogMsg.IsNull()) {
LogMsg = RequestUrl;
}
var _starttime= DateTime.Now;
var gethtml =await HttpHelp.Post( PostData, RequestUrl, postType, FileList,Token ,JsonNotWebOption:JsonNotWebOption);
var _runtime = DateTime.Now - _starttime;
_LogsAdd(LogMsg, "paas_post", new {runtime=_runtime.TotalSeconds+"秒", RequestUrl, postType, Token, PostData }, null, gethtml);
return gethtml;
}
/// <summary>
/// Get 请求
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="RequestUrl"></param>
/// <param name="Getdata"></param>
/// <param name="Token"></param>
/// <returns></returns>
public static async Task<string> GetOnLogs<T>( T Getdata,string RequestUrl, string LogMsg = "", string Token = null) {
if (LogMsg.IsNull())
{
LogMsg = RequestUrl;
}
var _starttime = DateTime.Now;
var gethtml =await HttpHelp.Get(Getdata, RequestUrl, Token);
var _runtime = DateTime.Now - _starttime;
_LogsAdd(LogMsg, "paas_get", new { runtime = _runtime.TotalSeconds + "秒", RequestUrl, Getdata, Token }, null, gethtml);
return gethtml;
}
/// <summary>
/// 场站实时数据查询
/// </summary>
/// <param name="webusername">云港通用户名</param>
/// <param name="webuserpass">云港通密码</param>
/// <param name="yardid">场站代码</param>
/// <param name="mblno">提单号</param>
/// <returns></returns>
public static ReturnResult<List<StationOrderDto>> GetStationData(string webusername, string webuserpass, string yardid, string mblno) {
var rs = new ReturnResult<List<StationOrderDto>>();
try
{
var url = GetConfigKey("pyhost2") +@"/query";
var custname = "QDDJY";
var custpsw = "YGH2020";
//测试
//yardid = "JIEFENG";
//mblno = "210229097";
var postdata = new { custname, custpsw, webusername, webuserpass, yardid, mblno, isweb = 2 };
var gethtml = PostOnLogs(postdata, url,"场站时时数据请求",YsPsotType.Form).Result;
rs.MemoData = gethtml;
var json = JsonSerializer.Deserialize<JsonElement>(gethtml);
if (json.GetProperty("status").GetString() == "1")
{
var sss = json.GetProperty("message");
var getlist = json.GetProperty("message");
var datalist = new List<StationOrderDto>();
//foreach (var item in getlist)
//{
for(int i=0; i<getlist.GetArrayLength();i++)
{
var item = getlist[i];
var newdto = new StationOrderDto();
newdto.Code = item.GetProperty("CNTRNO").ToString();
newdto.BoxType = item.GetProperty("CTNALL").ToString();
newdto.BoxCount = item.GetProperty("XiangLiang").GetInt32();
newdto. GoodsCount = item.GetProperty("PKGS").GetInt32();
newdto. SealCode = item.GetProperty("SEALNO").ToString();
newdto. Weigth =item.GetProperty("KGS").GetDouble();
newdto. WeigthTare = item.GetProperty("TAREWEIGHT").GetDouble();
newdto.WeigthTotal = item.GetProperty("KGS").GetDouble() + item.GetProperty("TAREWEIGHT").GetDouble();
newdto. ShipName = item.GetProperty("VESSEL").ToString();
newdto. VoyNo = item.GetProperty("VOYNO").ToString();
newdto. CarNo = item.GetProperty("CheHao").ToString();
newdto. Pack = item.GetProperty("KINDPKGS").ToString();
newdto.Size = item.GetProperty("CBM").GetDouble();
newdto. BillBoxTime = item.GetProperty("TiXiangShiJian").ToString().ToTimestamp();
newdto. StationTime = item.GetProperty("FanChangShiJian").ToString().ToTimestamp();
newdto. HarborTime = item.GetProperty("JiGangShiJian").ToString().ToTimestamp();
newdto. ShipTime = item.GetProperty("WanChuanShiJian").ToString().ToTimestamp();
datalist.Add(newdto);
}
rs.Data = datalist;
rs.OK();
}
else {
rs.Not(json.GetProperty("message").ToString());
}
}
catch {
rs.Not("获取失败");
}
return rs;
}
}
}