using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PushPlatform.Data.Bll;
using PushPlatform.Data;
using PushPlatform.Data.Criteria;
using System.Web.Mvc;
using PushPlatform.Common.Enumerate;
using System.Data;
using PushPlatform.Data.Blls;

namespace PushPlatform.Background.DropDown
{
    public class DropDownManager
    {
        static BllSelectItemDetail bllselItem = new BllSelectItemDetail();
        #region 数据库连接下拉
        public static List<SelectListItem> DbConnectDDL()
        {
            List<SelectListItem> ddl = new List<SelectListItem>();

            CriteriaSelectItemDetail criteria = new CriteriaSelectItemDetail {EqIsDeleted = 0, UsePage = false };
            criteria.InSelectType.Add((int)SelectType.Mysql连接);
            criteria.InSelectType.Add((int)SelectType.Sql连接);
            criteria.AddOrder(CriteriaSelectItemDetail.MapCreateTime, true);
            var list = bllselItem.Fetch(criteria).ToList();

            if (list != null && list.Count > 0)
            {
                list.ForEach(x =>
                {
                    ddl.Add(new SelectListItem
                    {
                        Text = x.Description,
                        Value = x.Id
                    });
                });
            }

            return ddl;
        }
        #endregion

        #region 子项目里面的角色下拉
        public static List<SelectListItem> DbRoleDDL(string roleSql,string projectId,string dbConnectId)
        {
            List<SelectListItem> ddl = new List<SelectListItem>();
            roleSql = roleSql.Replace("@projectId", "'" + projectId + "'");
            DataTable list = DbHelpBll.FetchSql(roleSql, dbConnectId);
            if (list != null && list.Rows.Count > 0)
            {
                foreach(DataRow row in list.Rows)
                {
                    ddl.Add(new SelectListItem
                    {
                        Value = row["Id"].ToString(),
                        Text = row["Role_Name"].ToString()
                    });
                }
            }
            return ddl;
        }
        #endregion

        #region 项目微信模板的下拉
        public static List<SelectListItem> WechatTemListDDL(string projectId)
        {
            List<SelectListItem> ddl = new List<SelectListItem>();

            CriteriaWechatMsgmodel criteria = new CriteriaWechatMsgmodel { EqProjectid = projectId, EqIsDeleted = 0, UsePage = false };
            criteria.AddOrder(CriteriaSelectItemDetail.MapCreateTime, true);
            var list = new BllWechatMsgmodel().Fetch(criteria).ToList();

            if (list != null && list.Count > 0)
            {
                list.ForEach(x =>
                {
                    ddl.Add(new SelectListItem
                    {
                        Text = x.Title,
                        Value = x.Id
                    });
                });
            }
            return ddl;
        }
        #endregion

        #region 根据枚举类型返回下拉
        /// <summary>
        /// 枚举类型
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static List<SelectListItem> EnumDDL(Type enumType)
        {
            var list = Enum.GetValues(enumType);
            List<SelectListItem> ddl = new List<SelectListItem>();
            if (list != null && list.Length > 0)
            {
                foreach (var item in list)
                {
                    ddl.Add(new SelectListItem
                    {
                        Text = item.ToString(),
                        Value = Convert.ToInt32(item).ToString()
                    });
                }
            }
            return ddl;
        }
        #endregion
    }
}