using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dao;
using Domain;

namespace Service.Implement
{
    public class SysHotelGroupManager : GenericManagerBase<SysHotelGroup>, ISysHotelGroupManager
    {
        public ISysHotelRepository SysHotelRepository { get; set; }
        public ISysRoleRepository SysRoleRepository { get; set; }
        public ISysUserRepository SysUserRepository { get; set; }

        public override object Save(SysHotelGroup entity)
        {
            entity.Last_Modified_Time = DateTime.Now;//标识有更新
            return base.Save(entity);
        }

        public override void Update(SysHotelGroup entity)
        {
            entity.Last_Modified_Time = DateTime.Now;//标识有更新
            base.Update(entity);
        }

        public override void SaveOrUpdate(SysHotelGroup entity)
        {
            entity.Last_Modified_Time = DateTime.Now;//标识有更新
            base.SaveOrUpdate(entity);
        }

        public IList<SysHotelGroup> GetGroupList(SysHotelGroup group)
        {
            return ((ISysHotelGroupRepository)CurrentRepository).GetGroupList(group);
        }

        public override void Delete(object id)
        {
            SysHotelGroup group = CurrentRepository.Get(id);
            if (group != null)
            {
                if (SysHotelRepository.GetByGroup(group).Count() > 0)
                {
                    throw new ApplicationException("分组【" + group.Name + "】已分配有酒店，不能删除！");
                }
                if (SysRoleRepository.LoadAll().Where(r => r.SysHotelGroup == group).ToList().Count > 0)
                {
                    throw new ApplicationException("分组【" + group.Name + "】已分配有组角色，不能删除！");
                }
                if (SysUserRepository.LoadAll().Where(r => r.SysHotelGroup == group).ToList().Count > 0)
                {
                    throw new ApplicationException("分组【" + group.Name + "】已分配有组用户，不能删除！");
                }
            }
            ((ISysHotelGroupRepository)CurrentRepository).Delete(id);
        }

        public IList<object> CreateGroupTree(SysHotelGroup group)
        {
            IList<object> groupTree = new List<object>();
            groupTree.Add(new { id = group.ID, text = group.Name, iconCls = "tree-node-no-icon", children = BuildGroupTree(CurrentRepository.LoadAll().ToList(), group) });
            return groupTree;
        }

        public object CreateGroupTree2(SysHotelGroup group)
        {
            if (group == null)
            {
                return null;
            }
            var groupMenu = ((ISysHotelGroupRepository)CurrentRepository).GetGroupMenu(group.ID);
            return BuildGroupTree(groupMenu);
        }

        public IList<object> CreateGroupTreeWithHotel(SysHotelGroup group, IList<SysHotel> checkedHotels)
        {
            var currentSysHotelGroups = CurrentRepository.LoadAll().ToList();
            return BuildGroupTreeWithHotel(currentSysHotelGroups, group, checkedHotels);
        }

        /// <summary>
        /// 按层级构造分组名称
        /// 例如:
        ///		BLW-1栋-1层
        /// </summary>
        /// <param name="group"></param>
        /// <returns></returns>
        public string BuildGroupName(SysHotelGroup group)
        {
            string name = "";
            while (group != null)
            {
                if (name == "")
                {
                    name = group.Name;
                }
                else
                {
                    name = group.Name + "-" + name;
                }
                group = group.Parent;
            }
            return name;
        }

        #region Private Methods

        /// <summary>
        /// 构造楼层树，用于 easyui-tree, easyui-combotree
        /// </summary>
        /// <param name="groups"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        private IList<object> BuildGroupTree(IList<SysHotelGroup> groups, SysHotelGroup parent)
        {
            IList<object> groupTree = new List<object>();
            IList<SysHotelGroup> subGroups = groups.Where(r => r.Parent != null && r.Parent.ID == parent.ID).OrderBy(o => o.Sort).ToList();
            foreach (SysHotelGroup group in subGroups)
            {
                IList<object> children = BuildGroupTree(groups, group);
                if (children.Count == 0)
                {
                    groupTree.Add(new { id = group.ID, text = group.Name, iconCls = "tree-node-no-icon" });
                }
                else
                {
                    groupTree.Add(new { id = group.ID, text = group.Name, iconCls = "tree-node-no-icon", children = children });
                }
            }
            return groupTree;
        }

        private object BuildGroupTree(GroupMenu groupMenu)
        {
            if (groupMenu == null)
            {
                return null;
            }
            if (groupMenu.SubGroupMenus.Count <= 0)
            {
                //return new { id = groupMenu.ID, text = groupMenu.Name, iconCls = "tree-node-no-icon" };
                return new { ID = groupMenu.ID, Name = groupMenu.Name, Sort = groupMenu.Sort, iconCls = "tree-node-no-icon" };
            }
            IList<object> children = new List<object>();
            foreach (GroupMenu subGroupMenu in groupMenu.SubGroupMenus)
            {
                var subGroupTree = BuildGroupTree(subGroupMenu);
                if (subGroupTree != null)
                {
                    children.Add(subGroupTree);
                }
            }
            //return new { id = groupMenu.ID, text = groupMenu.Name, iconCls = "tree-node-no-icon", children = children };
            return new { ID = groupMenu.ID, Name = groupMenu.Name, Sort = groupMenu.Sort, children = children, iconCls = "tree-node-no-icon" };
        }

        /// <summary>
        /// 构造带房号的 easyui-tree 数据格式的分组树，房号附在其所属的结点之下
        /// 如果分支上的房号包含在 checkedHosts 则将其选中
        /// </summary>
        /// <param name="groups"></param>
        /// <param name="parent"></param>
        /// <param name="checkedHosts"></param>
        /// <returns></returns>
        private IList<object> BuildGroupTreeWithHotel(IList<SysHotelGroup> groups, SysHotelGroup parent, IList<SysHotel> checkedHotels)
        {
            IList<object> groupTree = new List<object>();
            IList<SysHotelGroup> subGroups = groups.Where(r => r.Parent == parent).OrderBy(o => o.Sort).ToList();
            foreach (SysHotelGroup group in subGroups)
            {
                IList<object> children = BuildGroupTreeWithHotel(groups, group, checkedHotels);
                if (children.Count != 0)
                {
                    groupTree.Add(new { id = group.ID, text = group.Name, iconCls = "tree-node-no-icon", children = children, attributes = new { isHotel = false } });
                }
                else
                {
                    groupTree.Add(new { id = group.ID, text = group.Name, iconCls = "tree-node-no-icon", attributes = new { isHotel = false } });
                }
            }
            var hotels = SysHotelRepository.GetByGroup(parent);
            foreach (SysHotel hotel in hotels)
            {
                if (checkedHotels != null && checkedHotels.Contains(hotel))
                {
                    groupTree.Add(new { id = hotel.ID, text = hotel.Code + "-" + hotel.Name, iconCls = "tree-node-no-icon", @checked = true, attributes = new { isHotel = true } });
                }
                else
                {
                    groupTree.Add(new { id = hotel.ID, text = hotel.Code + "-" + hotel.Name, iconCls = "tree-node-no-icon", attributes = new { isHotel = true } });
                }
            }
            return groupTree;
        }
        #endregion
    }
}
