using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Dysmsapi.Model.V20170525;
using DSWeb.Common.DB;
using DSWeb.Common.Helper;
using log4net;
using MailKit.Net.Smtp;
using MimeKit;
using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
namespace DSWeb.Job.Common
{
///
/// 大简云余额变动监控提醒
///
public class AlertMinValueJob : IJob
{
private static DateTime LastCheckTime = DateTime.Now;
private static String product = "Dysmsapi";//短信API产品名称
private static String domain = "dysmsapi.aliyuncs.com";//短信API产品域名
private static String accessId = "LTAILD56g1iYWp9A";
private static String accessSecret = "YyBCXDVqj3SJF5TPR3zct02wzFlDrg";
private static String regionIdForPop = "cn-hangzhou";
private CommonDataContext commonDataContext = new CommonDataContext();
private ILog logger = LogManager.GetLogger("AlertMinValueJob");
public void Execute(IJobExecutionContext context)
{
var cfg = commonDataContext.ConfigDatas.AsNoTracking().FirstOrDefault(c => c.Module == ConfigData.ModuleSystem && c.Category == ConfigData.CateAlertMinValue);
if (cfg == null)
{
logger.Error($"平台余额不足提醒未配置");
return;
}
var jobj = JsonConvert.DeserializeAnonymousType(cfg.JsonData, new
{
AlertDelay = 0,
AlertStartHour = 0,
AlertStartMinute = 0,
AlertEndHour = 0,
AlertEndMinute = 0,
AlertMinMon = false,
AlertMinTue = false,
AlertMinWen = false,
AlertMinThu = false,
AlertMinFri = false,
AlertMinSat = false,
AlertMinSum = false
});
if (LastCheckTime.AddMinutes(jobj.AlertDelay) > DateTime.Now)
{
logger.Debug($"还未到轮训时间:{LastCheckTime},配置为{jobj.AlertDelay}分钟");
return;
}
var dt1 = DateTime.Today.AddHours(jobj.AlertStartHour).AddMinutes(jobj.AlertStartMinute);
var dt2 = DateTime.Today.AddHours(jobj.AlertEndHour).AddMinutes(jobj.AlertEndMinute);
if (DateTime.Now < dt1 || DateTime.Now > dt2)
{
logger.Debug($"不在配置的轮训时间内:{DateTime.Now},配置为{jobj.AlertStartHour}时{jobj.AlertStartMinute}分到{jobj.AlertEndHour}时{jobj.AlertEndMinute}分");
return;
}
if ((DateTime.Today.DayOfWeek == DayOfWeek.Monday && !jobj.AlertMinMon)
|| (DateTime.Today.DayOfWeek == DayOfWeek.Tuesday && !jobj.AlertMinTue)
|| (DateTime.Today.DayOfWeek == DayOfWeek.Wednesday && !jobj.AlertMinWen)
|| (DateTime.Today.DayOfWeek == DayOfWeek.Thursday && !jobj.AlertMinThu)
|| (DateTime.Today.DayOfWeek == DayOfWeek.Friday && !jobj.AlertMinFri)
|| (DateTime.Today.DayOfWeek == DayOfWeek.Saturday && !jobj.AlertMinSat)
|| (DateTime.Today.DayOfWeek == DayOfWeek.Sunday && !jobj.AlertMinSum))
{
logger.Debug($"不在配置的轮训周次内:{DateTime.Today.DayOfWeek.ToString()},配置为{jobj.AlertMinMon} {jobj.AlertMinTue} {jobj.AlertMinWen} {jobj.AlertMinThu} {jobj.AlertMinFri} {jobj.AlertMinSat} {jobj.AlertMinSum}");
return;
}
var dt = LastCheckTime.ToString("yyyy-MM-dd HH:mm:ss");
var sql = $@"
select t1.COMNAME,t1.balance_0,comp.AlertMinValue,comp.CompId,ub.EMAIL1,ub.MOBILE,comp.MinValueSMS,comp.MinValueEmail,comp.MinValueEmailAddr from
(select COMNAME,balance_0,createtime from Cust_Balance_tiglog where (balance_0-balance)<0 and createtime>'{dt}') t1
join
(select COMNAME,max(createtime) createtime from Cust_Balance_tiglog where createtime>'{dt}' group by COMNAME) t2
on t1.COMNAME=t2.COMNAME and t1.createtime=t2.createtime
join company_new comp on t1.COMNAME=comp.CompName
join user_baseinfo ub on ub.USERID=comp.AdminUser
where t1.balance_0(sql).ToList();
foreach (var item in list)
{
if (item.MinValueEmail)
{
MailHelper.SendMailService($"大简云平台余额不足提醒", $"TO:{item.COMNAME}
您当前的大简云平台余额为:{item.balance_0}元,已低于设定的提醒额度,请及时安排充值,以免影响正常使用。
————此邮件为大简云平台自动发送,请勿回复", item.MinValueEmailAddr);
}
if (item.MinValueSMS)
{
SendSMS(item.MOBILE, item.COMNAME, item.balance_0);
}
}
LastCheckTime = DateTime.Now;
}
public void SendSMS(string phoneNum, string custname, decimal balance)
{
IClientProfile profile = DefaultProfile.GetProfile(regionIdForPop, accessId, accessSecret);
DefaultProfile.AddEndpoint(regionIdForPop, regionIdForPop, product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
try
{
request.PhoneNumbers = phoneNum;
request.SignName = "大简云工作平台";
request.TemplateCode = "SMS_225986095";
request.TemplateParam = JsonConvert.SerializeObject(new
{
custname = custname,
balance = balance
});
request.OutId = "001";
logger.Debug($"平台余额不足提醒短信发送给{phoneNum},内容{request.TemplateParam}");
SendSmsResponse sendSmsResponse = acsClient.GetAcsResponse(request);
logger.Debug($"调用短信接口返回:{JsonConvert.SerializeObject(sendSmsResponse)}");
}
catch (ServerException e)
{
logger.Error(e.Message);
logger.Error(e.StackTrace);
}
catch (ClientException e)
{
logger.Error(e.Message);
logger.Error(e.StackTrace);
}
}
}
public class AlertMinValueResultData
{
public string COMNAME { get; set; }
public decimal balance_0 { get; set; }
public int AlertMinValue { get; set; }
public string CompId { get; set; }
public string EMAIL1 { get; set; }
public string MOBILE { get; set; }
public bool MinValueSMS { get; set; }
public bool MinValueEmail { get; set; }
public string MinValueEmailAddr { get; set; }
}
}