using PushInfo.Core.IDAL;
using PushInfo.Core.Models.Tables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DapperORM.Common.Models;
using PushInfo.Core.Models.QueryResults;
using PushPlatform.Common.Enumerate;
using PushInfo.Core.DAL.MySql;

namespace PushInfo.Core.BLL
{
    public class MapProjectPushBLL
    {
        /// <summary>
        /// 项目任务配置表数据访问接口
        /// </summary>
        private IMapProjectPushDAL mapProPushDal;
        /// <summary>
        /// 微信推送模板数据访问接口
        /// </summary>
        private IWechatMsgmodelDAL wechatMsgDal;

        private SysSendCfgDAL sendCfgDal;
        /// <summary>
        /// 构造函数
        /// </summary>
        public MapProjectPushBLL()
        {
            this.mapProPushDal = DALFactory.CreateMapProjectPushDAL();
            this.wechatMsgDal = DALFactory.CreateWechatMsgmodelDAL();
            this.sendCfgDal = DALFactory.CreateSysSendCfgDAL();
        }

        #region 获取未安排触发器的任务
        /// <summary>
        /// 获取未安排触发器的任务（包括新建的和修改的）
        /// </summary>
        /// <returns></returns>
        internal List<Map_Project_Push> GetStandByTask()
        {
            int taskStatus_Create_Wait = Convert.ToInt32(ExecuteStatus.新建未安排触发器);
            int taskStatus_Modify_Wait = Convert.ToInt32(ExecuteStatus.修改后未安排触发器);
            int taskStatus_Remove_Wait = Convert.ToInt32(ExecuteStatus.等待从触发器中移除);
            var result = mapProPushDal.GetList(x =>x.Push_Type != 0 && x.Is_Deleted == 0 && (x.Exec_Status == taskStatus_Create_Wait || x.Exec_Status == taskStatus_Modify_Wait || x.Exec_Status == taskStatus_Remove_Wait));
            if (result.IsSuccess())
                return result.Data.ToList();
            else
                return new List<Map_Project_Push>();
        }
        #endregion

        #region 修改任务状态
        /// <summary>
        /// 修改任务状态
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="excuteStatus">枚举 ExecStatus</param>
        /// <returns></returns>
        public ResultHelper<string> Modify_Task_Excute(string id, ExecuteStatus excuteStatus,string triggerKey = "",DateTime? execTime = null)
        {
            ResultModel result = null;
            if (string.IsNullOrEmpty(id))
                return new ResultHelper<string> { result = 0, msg = "修改数据不存在" };
            Map_Project_Push updateTask = new Map_Project_Push();
            updateTask.Exec_Status = Convert.ToInt32(excuteStatus);
            updateTask.Update_Time = DateTime.Now;
            updateTask.Trigger_Key = triggerKey;
            if(execTime != null)
            {
                updateTask.Exec_Time = execTime.GetValueOrDefault();
                result = mapProPushDal.Update(updateTask, x => new { x.Exec_Status, x.Update_Time, x.Trigger_Key ,x.Exec_Time}, x => x.Id == id);
            }
            else
            {
                result = mapProPushDal.Update(updateTask, x => new { x.Exec_Status, x.Update_Time, x.Trigger_Key }, x => x.Id == id);
            }
            if (result.IsSuccess())
                return new ResultHelper<string> { result = 1, msg = "修改成功",data = triggerKey };
            else
                return new ResultHelper<string> { result = 0, msg = "修改失败", data = triggerKey };
        }
        #endregion

        #region 根据主键查询推送配置
        /// <summary>
        /// 根据主键查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Map_Project_Push Query_Sys_Task_By_Id(string id)
        {
            //if (string.IsNullOrEmpty(id.ToString()))
            //    return new Map_Project_Push();
            //var result = mapProPushDal.GetModel(x => x.Id == id);
            //if (result.IsSuccess())
            //    return result.Data;
            //else
            //    return null;
            return mapProPushDal.GetModel(id);
        }
        #endregion

        #region 根据通用配置Id，还有项目Id查询推送配置
        /// <summary>
        /// 根据主键查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Map_Project_Push Query_SysTask_ByProId(string sysPushId, string projectId)
        {
            //if (string.IsNullOrEmpty(id.ToString()))
            //    return new Map_Project_Push();
            //var result = mapProPushDal.GetModel(x => x.Id == id);
            //if (result.IsSuccess())
            //    return result.Data;
            //else
            //    return null;
            return mapProPushDal.GetModel(sysPushId, projectId);
        }
        #endregion

        #region 根据主键查询微信模板信息
        /// <summary>
        /// 根据主键查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Wechat_Msgmodel Query_WxModel_By_Id(string id)
        {
            if (string.IsNullOrEmpty(id))
                return new Wechat_Msgmodel();
            var result = wechatMsgDal.GetModel(x => x.Id == id);
            if (result.IsSuccess())
                return result.Data;
            else
                return null;
        }
        #endregion

        #region 根据项目Id查询推送第三方平台配置
        /// <summary>
        /// 根据项目Id查询推送第三方平台配置
        /// </summary>
        /// <param name="projectId">项目Id</param>
        /// <param name="sendCfg">配置类型</param>
        /// <returns></returns>
        public Sys_Send_Cfg GetSendCfgByProId(string projectId, SendCfgType sendCfg)
        {
            if (string.IsNullOrEmpty(projectId))
                return new Sys_Send_Cfg();
            var result = sendCfgDal.GetModel(x => x.ProjectId == projectId && x.Type == (int)sendCfg && x.Is_Deleted == 0);
            if (result.IsSuccess())
                return result.Data;
            else
                return null;
        }
        #endregion

        #region 根据项目Id查询推送第三方平台配置
        /// <summary>
        /// 根据项目Id查询推送第三方平台配置
        /// </summary>
        /// <param name="projectId">项目Id</param>
        /// <param name="sendCfg">配置类型</param>
        /// <returns></returns>
        public List<Sys_Send_Cfg> GetSendCfgByProId(string projectId)
        {
            if (string.IsNullOrEmpty(projectId))
                return new List<Sys_Send_Cfg>();
            var result = sendCfgDal.GetList(x => x.ProjectId == projectId && x.Is_Deleted == 0);
            if (result.IsSuccess())
                return result.Data.ToList();
            else
                return null;
        }
        #endregion

    }
}
