using Quartz;
using Entity=Sugar.Enties;
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 SendEmailToChairmanJob : IJob
    {
        NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
        /// <summary>
        /// 当有新的待审审批流到达董事长时向王帅发一个通知邮件
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task Execute(IJobExecutionContext context)
        {
            return Task.Run(() =>
            {
                try
                {
                    ////NlogHelper.quartzLogger.Info("开始");
                    //var sql = $"SELECT * FROM dbo.WorkFlowAct WHERE ToTaskName LIKE '%董事长%' AND SignDate IS NULL AND FinishDate IS NULL AND NOT EXISTS(SELECT EmailHistoryCode FROM dbo.EmailHistory WHERE MasterCode=WorkFlowAct.actcode and EmailType='WorkFlowAct')";
                    ////logger.Info($"主项计划sql:{sql}");
                    //var manager = new WorkFlowActManager();
                    //var unitManager = new UnitManager();
                    //var list = manager.Db.Ado.SqlQuery<Entity.WorkFlowAct>(sql);
                    //foreach (var item in list)
                    //{
                    //    sql = $"SELECT * FROM dbo.WorkFlowCaseProperty WHERE  WorkFlowCaseCode='{item.CaseCode}' AND (WorkFlowProcedurePropertyName='流水号' OR WorkFlowProcedurePropertyName='主题' OR   WorkFlowProcedurePropertyName='项目代码')";
                    //    var list1 = manager.Db.Ado.SqlQuery<Entity.WorkFlowCaseProperty>(sql);
                    //    string liushui = "";
                    //    string zhuti = "";
                    //    string projectCode = "";
                    //    string projectName = "";
                    //    string procedureName = "";
                    //    foreach(var item1 in list1)
                    //    {
                    //        if(item1.WorkFlowProcedurePropertyName=="流水号")
                    //        {
                    //            liushui = item1.WorkFlowProcedurePropertyValue;
                    //        }
                    //        else if(item1.WorkFlowProcedurePropertyName == "主题")
                    //        {
                    //            zhuti = item1.WorkFlowProcedurePropertyValue;
                    //        }
                    //        else if (item1.WorkFlowProcedurePropertyName == "项目代码")
                    //        {
                    //            projectCode = item1.WorkFlowProcedurePropertyValue;
                    //        }
                    //    }
                    //    sql = $"SELECT ProjectName FROM dbo.Project WHERE ProjectCode='{projectCode}'";
                    //    projectName = manager.Db.Ado.GetString(sql);
                    //    sql = $"SELECT Description FROM dbo.WorkFlowProcedure WHERE ProcedureCode='{item.ProcedureCode}'";
                    //    procedureName = manager.Db.Ado.GetString(sql);
                    //    #region 给王帅发邮件 
                    //    var username = "王帅";
                    //    var mailBox = "shuai.wang@chinahonden.com";
                        
                    //        NlogHelper.quartzLogger.Info($"给{username}发邮件开始");
                    //        EmailHelper helper = new EmailHelper();
                    //        helper.Title = $"新审批流程需要董事长审批";
                    //        helper.Cont = $"流水号为{liushui}，项目名称为{projectName},主题为{zhuti}，流程名称为{procedureName}，需要董事长审批，请查收";
                    //        helper.ReceiveEmail = mailBox;
                    //        helper.SendMail(helper);
                    //        NlogHelper.quartzLogger.Info($"给{username}发邮件结束");
                        
                    //    #endregion
                    //    #region 发邮件后做发邮件记录，防止重复发送
                    //    var emailHistory = new Entity.EmailHistory();
                    //    emailHistory.EmailHistoryCode = unitManager.GetSysCodeValue("EmailHistory");
                    //    emailHistory.EmailType = "WorkFlowAct";
                    //    emailHistory.MasterCode = item.ActCode;
                    //    emailHistory.EmailTitle = helper.Title;
                    //    emailHistory.EmailContent = helper.Cont;
                    //    emailHistory.Sender = helper.SendEMail;
                    //    emailHistory.Receiver = helper.ReceiveEmail;
                    //    emailHistory.SendDate = DateTime.Now;
                    //    manager.Db.Insertable(emailHistory).ExecuteCommand();
                    //    #endregion

                    //}

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

            });

        }
    }
}
