using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PushPlatform.Data;
using PushPlatform.Data.Bll;
using PushPlatform.Data.Criteria;
using PushService.Act.Model;
using PushService.Act.Model.QueryResults;

namespace PushService.Act.BLL
{
    /// <summary>
    /// 对外提供配置类
    /// </summary>
    public class PushCfgBLL
    {
        private BllSysPush bllSyspush = new BllSysPush();
        private BllMapProjectPush bllmapPropush = new BllMapProjectPush();

        #region 获取程序员配置的模板列表
        /// <summary>
        /// 
        /// </summary>
        /// <param name="likeName">模块描述模糊名</param>
        /// <returns></returns>
        public ListModel<SysPushSimple> GetSysPushSelect(string likeName)
        {
            ListModel<SysPushSimple> returnModel = new ListModel<SysPushSimple>();
            returnModel.DataList = new List<SysPushSimple>();
            CriteriaSysPush sysPushCri = new CriteriaSysPush();
            sysPushCri.EqIsDeleted = 0;
            if (!string.IsNullOrEmpty(likeName))
            {
                sysPushCri.LikeNameDes = likeName;
            }
            sysPushCri.AddOrder(CriteriaSysPush.MapCreateTime);
            var sysPushList = bllSyspush.Fetch(sysPushCri);
            foreach(var item in sysPushList)
            {
                SysPushSimple model = new SysPushSimple();
                model.Id = item.Id;
                model.Name_Des = item.Name_Des;
                returnModel.DataList.Add(model);
            }
            return returnModel;
        }
        #endregion

        #region 获取当前用户配置推送列表
        public ListModel<ProjectCfgPush> GetProCfgPushList(string projectId,int ? execStatus,string likeName,string likeBody, int? pageIndex, int? pageSize)
        {
            ListModel<ProjectCfgPush> returnModel = new ListModel<ProjectCfgPush>();
            returnModel.DataList = new List<ProjectCfgPush>();
            returnModel.Page = new PageInfo();
            CriteriaMapProjectPush criPush = new CriteriaMapProjectPush();
            criPush.EqProjectId = projectId;
            if(pageIndex!=null && pageSize!=null)
            {
                criPush.PageIndex = pageIndex.Value;
                criPush.PageSize = pageSize.Value;
            }
            criPush.EqIsDeleted = 0;
            if (execStatus != null)
            {
                criPush.EqExecStatus = execStatus;
            }
            if(!string.IsNullOrEmpty(likeName))
            {
                criPush.LikeNameDes = likeName;
            }
            if(!string.IsNullOrEmpty(likeBody))
            {
                criPush.LikePushBody = likeBody;
            }
            criPush.AddOrder(CriteriaMapProjectPush.MapCreateTime);
            var msgList = bllmapPropush.Fetch(criPush);
            string proConfigId = msgList.Count > 0 ? msgList.FirstOrDefault().PushSys_ProId : "";
            List<PushTag> pTag = new List<PushTag>();
            if (!string.IsNullOrEmpty(proConfigId))
            {
                pTag = new BllPushTag().Fetch(new CriteriaPushTag { EqPushsysProid = proConfigId, EqIsDeleted = 0, UsePage = false });
            }
            foreach (var item in msgList)
            {
                ProjectCfgPush proCfgObj = new ProjectCfgPush();
                proCfgObj.Id = item.Id;
                proCfgObj.Name_Des = item.Name_Des;
                if (!string.IsNullOrEmpty(item.Push_TagId))
                {
                    proCfgObj.Push_TagName = string.Join(",", pTag.Where(x => item.Push_TagId.IndexOf(x.Id) > -1).Select(x => x.TagName));
                }
                proCfgObj.Push_Title = item.Push_Title;
                proCfgObj.Push_Body = item.Push_Body;
                proCfgObj.Exec_Time = item.Exec_Time;
                proCfgObj.Exec_Status = item.Exec_Status;
                proCfgObj.Create_Time = item.Create_Time.Value;
                returnModel.DataList.Add(proCfgObj);
            }
            returnModel.Page.PageTotal = criPush.PageTotal;
            return returnModel;
        }
        #endregion
    }
}
