using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utils;

namespace PMS.NetCore.Service
{
    /// <summary>
    /// 进度报告发邮件任务
    /// </summary>
    public class SendEmailJob : IJob
    {
        NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        public Task Execute(IJobExecutionContext context)
        {
            return Task.Run(() =>
            {
                try
                {
                    ////NlogHelper.quartzLogger.Info("开始");
                    //var sql = $"select b.ProgressName,a.NodeName,a.ProgressId,a.id progressnodeid,a.PlannedStartDate from pmProgressNode a inner join pmprogress b on a.ProgressId=b.id where (b.IsMaster =1 or b.ismaster=2) and b.State=2 and a.PlannedStartDate is not null";
                    ////logger.Info($"主项计划sql:{sql}");
                    //var manager = new actionsManager();
                    //var dt = manager.Db.Ado.GetDataTable(sql);
                    //for (int i = 0; i < dt.Rows.Count; i++)
                    //{
                    //    var PlannedStartDate = Convert.ToDateTime(dt.Rows[i]["PlannedStartDate"]);
                    //    var name = dt.Rows[i]["ProgressName"].ToString() + "->" + dt.Rows[i]["NodeName"].ToString();
                    //    var progressid = Convert.ToInt32(dt.Rows[i]["progressid"]);
                    //    var progressnodeid = Convert.ToInt32(dt.Rows[i]["progressnodeid"]);
                    //    #region 给责任人发邮件
                    //    if (DateHelper.DateDiff(DateInterval.Day, DateTime.Now.AddDays(1), PlannedStartDate) == 0)  //如果计划开始时间是当前时间的前一天
                    //    {
                    //        sql = $"select u.UserName,u.MailBox from SystemUser u inner join pmProgressNodePerson p on u.UserCode=p.UserCode where p.ProgressNodeId={progressnodeid} and p.type=1";
                    //        //logger.Info($"获取责任人sql:{sql}");
                    //        var dt2 = manager.Db.Ado.GetDataTable(sql);
                    //        for (int j = 0; j < dt2.Rows.Count; j++)        //给责任人发邮件
                    //        {
                    //            var username = dt2.Rows[j]["UserName"].ToString();
                    //            var mailBox = dt2.Rows[j]["MailBox"].SConvertString();
                    //            if (mailBox.IsEmail())
                    //            {
                    //                NlogHelper.quartzLogger.Info($"给责任人{username}发邮件开始");
                    //                EmailHelper helper = new EmailHelper();
                    //                helper.Title = $"地产ERP进度计划即将开始提醒";
                    //                helper.Cont = $"此\"{name}\"于{PlannedStartDate.ToString("yyyy-MM-dd")}即将开始";
                    //                helper.ReceiveEmail = mailBox;
                    //                helper.SendMail(helper);
                    //                NlogHelper.quartzLogger.Info($"给责任人{username}发邮件结束");
                    //            }
                    //        }
                    //    }
                    //    #endregion
                    //    #region 给关注人和责任人发邮件
                    //    if (DateHelper.DateDiff(DateInterval.Day, DateTime.Now, PlannedStartDate.AddDays(1)) == 0)  //如果计划开始时间是当前时间的前一天
                    //    {
                    //        sql = $"select id from pmProgressReport where ProgressId={progressid} and ProgressNodeId={progressnodeid}";
                    //        var exist = manager.Db.Ado.GetString(sql);     //若没有任何进度报告
                    //        if (exist.IsNullOrEmpty())
                    //        {
                    //            sql = $"select u.UserName,u.MailBox from SystemUser u inner join pmProgressNodePerson p on u.UserCode=p.UserCode where p.ProgressNodeId={progressnodeid} and (p.type=9 or p.type=1)";
                    //            NlogHelper.quartzLogger.Info($"获取关注人sql:{sql}");
                    //            var dt2 = manager.Db.Ado.GetDataTable(sql);
                    //            for (int j = 0; j < dt2.Rows.Count; j++)        //给责任人发邮件
                    //            {
                    //                var username = dt2.Rows[j]["UserName"].ToString();
                    //                var mailBox = dt2.Rows[j]["MailBox"].SConvertString();
                    //                if (mailBox.IsEmail())
                    //                {
                    //                    NlogHelper.quartzLogger.Info($"给关注人和责任人{username}发邮件开始");
                    //                    EmailHelper helper = new EmailHelper();
                    //                    helper.Title = $"地产ERP进度计划填报提醒";
                    //                    helper.Cont = $"此\"{name}\"于{PlannedStartDate.ToString("yyyy-MM-dd")}已开始，未填报";
                    //                    helper.ReceiveEmail = mailBox;
                    //                    helper.SendMail(helper);
                    //                    NlogHelper.quartzLogger.Info($"给关注人和责任人{username}发邮件结束");
                    //                }
                    //            }
                    //        }

                    //    }
                    //    #endregion
                    //}

                }
                catch (Exception ero)
                {
                    NlogHelper.quartzLogger.Error(" 发邮件执行出错:" + ero, true);
                }

            });

        }
    }
}
