using DapperORM.Common.Models;
using PushInfo.Core.BLL;
using PushInfo.Core.Models.QueryResults;
using PushInfo.Core.Models.Tables;
using PushPlatform.Common.Enumerate;
using PushPlatform.Common.Util;
using PushService.Act.Model.Request;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PushService.Act.BLL
{
    public class OutProcessTaskBLL
    {
        private static MapProjectPushBLL mProPushBll = new MapProjectPushBLL();
        private static ProcessTaskBLL proTaskBll = new ProcessTaskBLL();

        #region 系统主动调用发送任务,数据来源配置表查询
        /// <summary>
        /// 系统主动调用发送任务,数据来源配置表查询
        /// </summary>
        /// <param name="proCfgId">配置表的Id</param>
        /// <param name="userId">用户Id</param>
        /// <returns>执行结果1-成功，0-失败</returns>
        public static ResultModel SendPushOfProCfg(string proCfgId, string userId, Dictionary<string, string> valMap = null)
        {
            ResultModel result = new ResultModel();
            try
            {
                Map_Project_Push task = mProPushBll.Query_Sys_Task_By_Id(proCfgId);
                if (task == null)
                {
                    LogHelper.Info("执行任务时查询结果为NULL，不正常-" + task.Name_Des);
                    result.Result = 0;
                    result.Msg = "传输的配置表的Id有误，推送失败。";
                    return result;
                }
                if (task.Exec_Status == (int)ExecuteStatus.已经停用)
                {
                    result.Result = 0;
                    result.Msg = "推送任务已停用，推送失败，请查询配置信息。";
                    return result;
                }
                task.valMap = valMap;
                mProPushBll.Modify_Task_Excute(proCfgId, ExecuteStatus.已安排触发器, task.Trigger_Key, null);
                task.User_Id = userId;
                if(proTaskBll.StartTask(task))
                {
                    result.Result = 1;
                    result.Msg = "发送成功";
                }
                else
                {
                    result.Result = 0;
                    result.Msg = "发送失败";
                }
            }
            catch (Exception ex)
            {
                result.Result = 0;
                result.Msg = ex.Message;
                LogHelper.Error(ex);
            }
            return result;
        }
        #endregion

        #region 系统主动调用发送任务,数据来源配置表查询
        /// <summary>
        /// 系统主动调用发送任务,数据来源配置表查询
        /// </summary>
        /// <param name="proCfgId">配置表的Id</param>
        /// <param name="userId">用户Id</param>
        /// <returns>执行结果1-成功，0-失败</returns>
        public static ResultModel SendPushOfProSyspush(string sysPushId, string projectId, Dictionary<string, string> valMap = null)
        {
            ResultModel result = new ResultModel();
            try
            {
                Map_Project_Push task = mProPushBll.Query_SysTask_ByProId(sysPushId, projectId);
                if (task == null)
                {
                    LogHelper.Info("执行任务时查询结果为NULL，不正常-" + task.Name_Des);
                    result.Result = 0;
                    result.Msg = "传输的配置表的Id有误，推送失败。";
                    return result;
                }
                if (task.Exec_Status == (int)ExecuteStatus.已经停用)
                {
                    result.Result = 0;
                    result.Msg = "推送任务已停用，推送失败，请查询配置信息。";
                    return result;
                }
                task.valMap = valMap;
                mProPushBll.Modify_Task_Excute(task.Id, ExecuteStatus.已安排触发器, task.Trigger_Key, null);

                if (proTaskBll.StartTask(task))
                {
                    result.Result = 1;
                    result.Msg = "发送成功";
                }
                else
                {
                    result.Result = 0;
                    result.Msg = "发送失败";
                }
            }
            catch (Exception ex)
            {
                result.Result = 0;
                result.Msg = ex.Message;
                LogHelper.Error(ex);
            }
            return result;
        }
        #endregion

        #region 
        public static ResultModel SendPushOfExternal(PushValueModel valModel)
        {
            ProcessTaskBLL taskBll = new ProcessTaskBLL();
            ResultModel result = new ResultModel();
            try
            {
                Map_Project_Push task = mProPushBll.Query_Sys_Task_By_Id(valModel.ProCfg_Id);
                if (task == null)
                {
                    result.Result = 0;
                    result.Msg = "传输的配置表的Id有误，推送失败。";
                    return result;
                }
                if(task.Exec_Status == (int)ExecuteStatus.已经停用)
                {
                    result.Result = 0;
                    result.Msg = "推送任务已停用，推送失败，请查询配置信息。";
                    return result;
                }
                mProPushBll.Modify_Task_Excute(valModel.ProCfg_Id, ExecuteStatus.已安排触发器, task.Trigger_Key, null);
                task.Push_Style = valModel.Push_Style?? task.Push_Style;
                task.Push_TagId = valModel.Push_TagId?? task.Push_TagId;
                task.Push_Title = valModel.Push_Title ?? task.Push_Title;
                task.Push_Body = valModel.Push_Body ?? task.Push_Body;
                task.Wx_MsmTemp = valModel.Wx_MsmTemp ?? task.Wx_MsmTemp;
                task.Push_IosLink = valModel.Push_IosLink ?? task.Push_IosLink;
                task.Push_AndLink = valModel.Push_AndLink ?? task.Push_AndLink;
                task.Push_WebLink = valModel.Push_WebLink ?? task.Push_WebLink;
                task.Push_WechatLink = valModel.Push_WechatLink ?? task.Push_WechatLink;
                DataMessage data = new DataMessage();
                data.userId = valModel.User_Id;
                data.clientType = valModel.Client_Type;
                data.clientID = valModel.Client_ID;
                data.realName = valModel.Real_Name;
                data.mobile = valModel.Mobile;
                data.openId = valModel.Open_Id;
                if (taskBll.ConfirmSend(task, data))
                {
                    result.Result = 1;
                    result.Msg = "发送成功";
                }
                else
                {
                    result.Result = 0;
                    result.Msg = "发送失败";
                }
                
            }
            catch (Exception ex)
            {
                result.Result = 0;
                result.Msg = ex.Message;
                LogHelper.Error(ex);
            }
            return result;
        }
        #endregion


    }
}
