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

namespace PushInfo.Core.BLL
{
    public class ProjectPushBLL
    {
        private IMapProjectPushDAL mapProPushDal;

        private IPushMessageDAL pushMessageDal;

        private ISysPushDAL sysPushDal;

        private QueryProcOrSqlDAL procDsl;

        private ProjectInfoDAL proDal;

        public ProjectPushBLL()
        {
            this.mapProPushDal = DALFactory.CreateMapProjectPushDAL();
            this.pushMessageDal = DALFactory.CreatePushMessageDAL();
            this.sysPushDal = DALFactory.CreateSysPushDAL();
            this.procDsl = DALFactory.CreateQueryProcDAL();
        }

        #region 项目Id获得项目数据
        /// <summary>
        /// 项目Id获得项目数据
        /// </summary>
        /// <param name="proId">项目Id</param>
        /// <returns></returns>
        public Project_Info GetProjectById(string proId)
        {
            if (string.IsNullOrEmpty(proId))
                return new Project_Info();
            var result = proDal.GetModel(x => x.Id == proId && x.Is_Deleted == 0);
            if (result.IsSuccess())
                return result.Data;
            else
                return null;
        }
        #endregion

        #region 根据sql获得推送信息
        /// <summary>
        /// 根据存储过程获得推送信息
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="projectId"></param>
        /// <param name="type">推送类型</param>
        /// <returns></returns>
        public DataSet GetPushValueOfProc(string sql, string projectId, PushPlatform.Common.Enumerate.PushActionType type)
        {
            var result = procDsl.GetPushValue(sql, projectId, type);
            if (result.IsSuccess())
                return result.Data;
            else
                throw new Exception(result.Msg);
        }
        #endregion

        #region 项目推送数据
        public List<Map_Project_Push> GetMapProjectPush()
        {
            var result = mapProPushDal.GetList(x=>x.Is_Deleted == 0);
            if (result.IsSuccess())
                return result.Data.ToList();
            else
                throw new Exception(result.Msg);
        }
        #endregion

        #region 查询推送列表
        public List<Sys_Push> GetSysPush(string projectId)
        {
            var result = sysPushDal.GetList(x => x.Is_Deleted == 0 && x.PushSys_ProId == projectId);
            if (result.IsSuccess())
                return result.Data.ToList();
            else
                throw new Exception(result.Msg);
        }
        #endregion

        #region 查询推送列表
        /// <summary>
        /// 查询推送列表
        /// </summary>
        /// <returns></returns>
        public List<Push_Message> GetPushMessage()
        {
            var result = pushMessageDal.GetUserLastPush();
            if (result.IsSuccess())
                return result.Data.ToList();
            else
                throw new Exception(result.Msg);
        }
        #endregion

        #region 保存推送记录
        public void AddPushMeddage(Push_Message pushMessage)
        {
            var result = pushMessageDal.Add(pushMessage);
            if (!result.IsSuccess())
            {
                throw new Exception(result.Msg);
            }
        }
        #endregion
    }
}
