using DapperORM.Common.Models;
using PushInfo.Act.IDAL;
using PushInfo.Act.Models.QueryResults;
using PushInfo.Act.Models.Tables;
using PushInfo.Core.BLL;
using PushPlatform.Common.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PushInfo.Act.BLL
{
    public class PushConfigBLL
    {
        IMapProjectPushDAL iMPPDAL;
        ISysPushDAL spDAL;
        ISelectItemDAL siDAL;
        /// <summary>
        /// 构造函数
        /// </summary>
        public PushConfigBLL()
        {
            iMPPDAL = DALFactory.CreateMapProjectPushDAL();
            spDAL = DALFactory.CreateSysPushDAL();
            siDAL = DALFactory.CreateSelectItemDAL();
        }

        #region Sys_Push相关业务操作
        /// <summary>
        /// 获取所有Sys_Push
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Sys_Push> GetAllSysPush(string projectId)
        {
            ProjectPushBLL projectPushBll = new ProjectPushBLL();
            var pushList = projectPushBll.GetSysPush(projectId);
            List<Sys_Push> list = new List<Sys_Push>();
            pushList.ForEach(x => list.Add(new Sys_Push(x)));
            return list;
        }

        /// <summary>
        /// 根据Id获取Sys_Push
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Sys_Push GetSysPushById(string pushId)
        {
            var result = spDAL.GetSysPush(pushId);
            if (result != null)
                return result.Data;
            else
                return null;
        }

        /// <summary>
        /// 根据id获取模板
        /// </summary>
        /// <param name="pushId"></param>
        /// <returns></returns>
        public PushConfigResult GetPushTempletById(string pushId)
        {
            PushConfigResult pushConfig = new PushConfigResult();
            var result = spDAL.GetSysPush(pushId);
            if (result == null) return null;

            ObjectHelper.CopyProperties(result.Data, pushConfig);
            return pushConfig;
        }

        /// <summary>
        /// 分页获取syspush列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public PageListModel<Sys_Push> GetPushByPage(int pageIndex, string projectId)
        {
            return spDAL.GetSysPushListByPage(pageIndex, projectId);
        }

        /// <summary>
        /// 分页获取推送配置模板列表，并以通用模型返回
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public PageListModel<PushConfigResult> GetPushTempletList(int pageIndex, string projectId)
        {
            PageListModel<PushConfigResult> result = new PageListModel<PushConfigResult>();
            result.Data = new List<PushConfigResult>();

            PageListModel<Sys_Push> pageList = GetPushByPage(pageIndex, projectId);
            ObjectHelper.CopyProperties(pageList, result, new List<string> { "Data" });
            pageList.Data.ForEach(x =>
            {
                PushConfigResult pushConfig = new PushConfigResult();
                ObjectHelper.CopyProperties(x, pushConfig);
                result.Data.Add(pushConfig);
            });

            return result;
        }

        /// <summary>
        /// 保存Sys_Push
        /// </summary>
        /// <param name="pushConfig"></param>
        /// <returns></returns>
        public ResultModel SaveSysPush(Sys_Push pushConfig)
        {
            //TODO 

            pushConfig.Create_Time = DateTime.Now;
            return spDAL.SaveSysPush(pushConfig);
        }

        /// <summary>
        /// 修改Sys_Push
        /// </summary>
        /// <param name="pushConfig"></param>
        /// <returns></returns>
        public ResultModel UpdateSysPush(Sys_Push pushConfig)
        {
            //TODO 

            pushConfig.Update_Time = DateTime.Now;
            return spDAL.UpdateSysPush(pushConfig);
        }

        /// <summary>
        /// 逻辑删除Sys_Push
        /// </summary>
        /// <param name="pushId"></param>
        /// <returns></returns>
        public ResultModel DeleteSysPush(string pushId)
        {
            if (!IsPushTempletUsed(pushId))
                return new ResultModel(0, "当前模板已被使用，不能删除");
            return spDAL.DeleteSysPush(pushId);
        }
        #endregion

        #region MapProjectPush相关业务操作
        /// <summary>
        /// 根据项目获取Map_Project_Push列表
        /// </summary>
        /// <param name="projId"></param>
        /// <returns></returns>
        public IEnumerable<Map_Project_Push> GetMapProjectPushList(string projId)
        {
            var result = iMPPDAL.GetPushListByProjectId(projId);
            if (result != null)
                return result.Data;
            else
                return new List<Map_Project_Push>();
        }

        /// <summary>
        /// 根据id获取Map_Project_Push
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Map_Project_Push GetMapProjectPushById(string id)
        {
            var result = iMPPDAL.GetPushById(id);
            if (result != null)
                return result.Data;
            else
                return null;
        }

        /// <summary>
        /// 根据id获取用户的推送配置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public PushConfigResult GetConfigById(string id)
        {
            PushConfigResult pushConfig = new PushConfigResult();
            var result = iMPPDAL.GetPushById(id);
            if (result == null) return null;

            ObjectHelper.CopyProperties(result.Data, pushConfig);
            return pushConfig;
        }

        /// <summary>
        /// 分页获取Map_Project_Push
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="projId"></param>
        /// <returns></returns>
        public PageListModel<Map_Project_Push> GetMapProjectPushByPage(int pageIndex, string projId,string pushProId)
        {
            return iMPPDAL.GetPushListByPage(pageIndex, projId, pushProId);
        }

        /// <summary>
        /// 获取推送配置列表，并以通用结果返回
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="projId"></param>
        /// <returns></returns>
        public PageListModel<PushConfigResult> GetPushConfigByPage(int pageIndex, string projId, string pushProId)
        {
            PageListModel<PushConfigResult> result = new PageListModel<PushConfigResult>();
            result.Data = new List<PushConfigResult>();

            PageListModel<Map_Project_Push> pageList = GetMapProjectPushByPage(pageIndex, projId, pushProId);
            ObjectHelper.CopyProperties(pageList, result, new List<string> { "Data" });
            pageList.Data.ForEach(x =>
            {
                PushConfigResult pushConfig = new PushConfigResult();
                ObjectHelper.CopyProperties(x, pushConfig);
                result.Data.Add(pushConfig);
            });

            return result;
        }

        /// <summary>
        /// 保存Map_Project_Push
        /// </summary>
        /// <param name="mpp"></param>
        /// <returns></returns>
        public ResultModel SaveMapProjectPush(Map_Project_Push mpp)
        {
            //TODO 各种判断

            return iMPPDAL.SaveMapProjectPush(mpp);
        }

        /// <summary>
        /// 修改Map_Project_Push
        /// </summary>
        /// <param name="mpp"></param>
        /// <returns></returns>
        public ResultModel UpdateMapProjectPush(Map_Project_Push mpp)
        {
            //TODO 各种判断

            return iMPPDAL.UpdateMapProjectPush(mpp);
        }

        /// <summary>
        /// 修改Map_Project_Push
        /// </summary>
        /// <param name="mpp"></param>
        /// <returns></returns>
        public ResultModel UpdateMapProPushState(string pushCfjId, int state)
        {
            //TODO 各种判断
            Map_Project_Push mpp = new Map_Project_Push();
            mpp.Id = pushCfjId;
            mpp.Exec_Status = state;
            return iMPPDAL.UpdateMapProPushState(mpp);
        }

        /// <summary>
        /// 删除Map_Project_Push
        /// </summary>
        /// <param name="mppid"></param>
        /// <returns></returns>
        public ResultModel DeleteMapProjectPushById(string mppid)
        {
            return iMPPDAL.DeleteMapProjectPushById(mppid);
        }

        /// <summary>
        /// 判断当前模板是否在用
        /// </summary>
        /// <param name="pushId"></param>
        /// <returns></returns>
        public bool IsPushTempletUsed(string pushId)
        {
            var result = iMPPDAL.GetCountByPushId(pushId);
            return result != null && result.Result > 0;
        }
        #endregion

        /// <summary>
        /// 获取推送配置中动态添加的选项
        /// </summary>
        /// <returns></returns>
        public IEnumerable<SelectItemDetail> GetCycleDetailList()
        {
            var result = siDAL.GetCycleDetail();
            if (result != null)
                return result.Data;
            else
                return new List<SelectItemDetail>();
        }

    }
}
