using System;
using System.IO;
using System.Text;
using System.Data;
using System.Collections.Generic;

using BrnShop.Core;

namespace BrnShop.Services
{
    /// <summary>
    /// 后台分类操作管理类
    /// </summary>
    public class AdminCategories : Categories
    {
        //分类选择列表缓存文件
        private const string _admincategoryselectlistcachefile = "/Administration/Cache/Category/SelectList.txt";

        /// <summary>
        /// 后台获得分类列表
        /// </summary>
        /// <returns></returns>
        public static List<CategoryInfo> AdminGetCategoryList()
        {
            return BrnShop.Data.Categories.AdminGetCategoryList();
        }

        /// <summary>
        /// 后台获得分类列表
        /// </summary>
        /// <returns></returns>
        public static List<CategoryInfo> AdminGetCategoryTree()
        {
            List<CategoryInfo> categoryList = new List<CategoryInfo>();
            List<CategoryInfo> sourceCategoryList = AdminCategories.AdminGetCategoryList();
            AdminCategories.CreateCategoryTree(sourceCategoryList, categoryList, 0);
            return categoryList;
        }

        /// <summary>
        /// 后台通过分类名称获得分类id
        /// </summary>
        /// <param name="categoryName">分类名称</param>
        /// <returns></returns>
        public static int AdminGetCateIdByName(string categoryName)
        {
            foreach (CategoryInfo categoryInfo in AdminGetCategoryList())
            {
                if (categoryInfo.Name == categoryName)
                    return categoryInfo.CateId;
            }
            return 0;
        }

        /// <summary>
        /// 后台获得分类
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <returns></returns>
        public static CategoryInfo AdminGetCategoryById(int cateId)
        {
            foreach (CategoryInfo categoryInfo in AdminGetCategoryList())
            {
                if (categoryInfo.CateId == cateId)
                    return categoryInfo;
            }

            return null;
        }

        /// <summary>
        /// 后台判断是否有子分类
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <returns></returns>
        public static bool AdminIsHasChildCategoryByCateId(int cateId)
        {
            foreach (CategoryInfo categoryInfo in AdminGetCategoryList())
            {
                if (categoryInfo.ParentId == cateId)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// 更新分类
        /// </summary>
        public static void UpdateCategory(CategoryInfo categoryInfo, int oldParentId)
        {
            //父分类改变时
            if (oldParentId != categoryInfo.ParentId)
            {
                //新的父分类
                CategoryInfo newParentCategoryInfo = null;
                if (categoryInfo.ParentId > 0)
                    newParentCategoryInfo = AdminGetCategoryById(categoryInfo.ParentId);

                if (categoryInfo.ParentId > 0)
                {
                    categoryInfo.Layer = newParentCategoryInfo.Layer + 1;
                    categoryInfo.Path = newParentCategoryInfo.Path + "," + categoryInfo.CateId;
                }
                else
                {
                    categoryInfo.Layer = 1;
                    categoryInfo.Path = categoryInfo.CateId.ToString();
                }

            }

            BrnShop.Data.Categories.UpdateCategory(categoryInfo);

            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_LIST);

            WriteAdminCategorySelectListCache(GetCategoryList());
        }

        /// <summary>
        /// 创建分类
        /// </summary>
        public static void CreateCategory(CategoryInfo categoryInfo)
        {
            //父分类
            CategoryInfo parentCategoryInfo = null;
            if (categoryInfo.ParentId > 0)
                parentCategoryInfo = AdminGetCategoryById(categoryInfo.ParentId);

            //设置分类层级数
            if (categoryInfo.ParentId > 0)
                categoryInfo.Layer = parentCategoryInfo.Layer + 1;
            else
                categoryInfo.Layer = 1;

            categoryInfo.Path = "";
            int categoryId = BrnShop.Data.Categories.CreateCategory(categoryInfo);

            categoryInfo.CateId = categoryId;

            //设置分类path
            if (categoryInfo.ParentId > 0)
                categoryInfo.Path = parentCategoryInfo.Path + "," + categoryId;
            else
                categoryInfo.Path = categoryId.ToString();

            BrnShop.Data.Categories.UpdateCategory(categoryInfo);

            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_LIST);

            WriteAdminCategorySelectListCache(GetCategoryList());
        }

        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <returns>0代表此分类下还有分类未删除,1代表删除成功</returns>
        public static int DeleteCategoryById(int cateId)
        {
            if (AdminIsHasChildCategoryByCateId(cateId))
            {
                return 0;
            }

            BrnShop.Data.Categories.DeleteCategoryById(cateId);

            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_LIST);
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_BRANDLIST + cateId);
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_FILTERAANDVLIST + cateId);
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_AANDVLISTJSONCACHE + cateId);

            WriteAdminCategorySelectListCache(GetCategoryList());
            return 1;
        }

        /// <summary>
        /// 隐藏分类
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <returns>0代表此分类下还有分类,1代表隐藏成功</returns>
        public static int HideCategoryById(int cateId)
        {
            if (AdminIsHasChildCategoryByCateId(cateId))
            {
                return 0;
            }
            BrnShop.Data.Categories.HideCategoryById(cateId);
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_LIST);
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_BRANDLIST + cateId);
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_FILTERAANDVLIST + cateId);
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_AANDVLISTJSONCACHE + cateId);

            WriteAdminCategorySelectListCache(GetCategoryList());
            return 1;
        }

        /// <summary>
        /// 展示分类
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <returns>0代表此分类下还有分类,1代表显示成功</returns>
        public static int ShowCategoryById(int cateId)
        {
            if (AdminIsHasChildCategoryByCateId(cateId))
            {
                return 0;
            }
            BrnShop.Data.Categories.ShowCategoryById(cateId);
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_LIST);

            WriteAdminCategorySelectListCache(GetCategoryList());
            return 1;
        }

        /// <summary>
        /// 将分类选择列表写入缓存文件
        /// </summary>
        private static void WriteAdminCategorySelectListCache(List<CategoryInfo> categoryList)
        {
            StringBuilder strBui = new StringBuilder("<div id='categoryTree'><table width='100%'><thead><tr><th align='left'>分类名称</th><th width='100' align='left'>管理操作</th></tr></thead><tbody>");

            foreach (CategoryInfo info in categoryList)
            {
                bool isHasChild = categoryList.Find(x => x.ParentId == info.CateId) != null;
                strBui.AppendFormat("<tr class='tree{0}' tree_pid='{1}' tree_id='{2}'><th>{3}<span {4} onclick='CategoryTree(this)'></span>{5}</th><td><a href='javascript:;' onclick='SetSelectedCategory(this)' class='editOperate'>[选择]</a></td></tr>", info.Layer, info.ParentId, info.CateId, CommonHelper.GetHtmlSpan(info.Layer - 1), isHasChild ? "class='close'" : "", info.Name);
            }

            strBui.AppendFormat("</tbody></table></div>");

            try
            {
                string filePath = IOHelper.GetMapPath(_admincategoryselectlistcachefile);
                using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    Byte[] info = System.Text.Encoding.UTF8.GetBytes(strBui.ToString());
                    fs.Write(info, 0, info.Length);
                    fs.Flush();
                    fs.Close();
                }
            }
            catch
            { }
        }





        /// <summary>
        /// 更新属性分组
        /// </summary>
        /// <param name="newAttributeGroupInfo">新属性分组</param>
        /// <param name="oldAttributeGroupInfo">原属性分组</param>
        public static void UpdateAttributeGroup(AttributeGroupInfo attributeGroupInfo)
        {
            BrnShop.Data.Categories.UpdateAttributeGroup(attributeGroupInfo);
        }

        /// <summary>
        /// 创建属性分组
        /// </summary>
        public static void CreateAttributeGroup(AttributeGroupInfo attributeGroupInfo)
        {
            BrnShop.Data.Categories.CreateAttributeGroup(attributeGroupInfo);
        }

        /// <summary>
        /// 删除属性分组
        /// </summary>
        /// <param name="attrGroupId">属性分组id</param>
        /// <returns>0代表此分类下还有展示属性未删除,1代表删除成功</returns>
        public static void DeleteAttributeGroupById(int attrGroupId)
        {
            BrnShop.Data.Categories.DeleteAttributeGroupById(attrGroupId);
        }






        /// <summary>
        /// 更新属性
        /// </summary>
        /// <param name="newAttributeInfo">新属性</param>
        /// <param name="oldAttributeInfo">原属性</param>
        public static void UpdateAttribute(AttributeInfo attributeInfo)
        {
            BrnShop.Data.Categories.UpdateAttribute(attributeInfo);
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_FILTERAANDVLIST + attributeInfo.CateId);
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_FILTERAANDVLIST + attributeInfo.CateId);
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_AANDVLISTJSONCACHE + attributeInfo.CateId);
        }

        /// <summary>
        /// 创建属性
        /// </summary>
        /// <param name="attributeInfo">属性信息</param>
        /// <param name="attributeGroupInfo">属性组信息</param>
        public static void CreateAttribute(AttributeInfo attributeInfo, AttributeGroupInfo attributeGroupInfo)
        {
            BrnShop.Data.Categories.CreateAttribute(attributeInfo, attributeGroupInfo.AttrGroupId, attributeGroupInfo.Name, attributeGroupInfo.DisplayOrder);
            //BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_FILTERAANDVLIST + attributeInfo.CateId);
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_AANDVLISTJSONCACHE + attributeInfo.CateId);
        }

        /// <summary>
        /// 删除属性
        /// </summary>
        /// <param name="attrId">属性id</param>
        public static void DeleteAttributeById(int attrId)
        {
            AttributeInfo attributeInfo = GetAttributeById(attrId);
            BrnShop.Data.Categories.DeleteAttributeById(attrId);
            if (attributeInfo.IsFilter == 1)
                BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_FILTERAANDVLIST + attributeInfo.CateId);
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_AANDVLISTJSONCACHE + attributeInfo.CateId);
        }

        /// <summary>
        /// 后台获得属性列表
        /// </summary>
        /// <param name="cateId">分类id</param>
        /// <param name="sort">排序</param>
        /// <returns></returns>
        public static DataTable AdminGetAttributeList(int cateId, string sort)
        {
            return BrnShop.Data.Categories.AdminGetAttributeList(cateId, sort);
        }

        /// <summary>
        /// 后台获得属性列表排序
        /// </summary>
        /// <param name="sortColumn">排序字段</param>
        /// <param name="sortDirection">排序方向</param>
        /// <returns></returns>
        public static string AdminGetAttributeListSort(string sortColumn, string sortDirection)
        {
            return BrnShop.Data.Categories.AdminGetAttributeListSort(sortColumn, sortDirection);
        }







        /// <summary>
        /// 创建属性值
        /// </summary>
        public static void CreateAttributeValue(AttributeValueInfo attributeValueInfo)
        {
            BrnShop.Data.Categories.CreateAttributeValue(attributeValueInfo);
            AttributeInfo attributeInfo = GetAttributeById(attributeValueInfo.AttrId);
            if (attributeInfo.IsFilter == 1)
            {
                BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_FILTERAANDVLIST + attributeInfo.CateId);
            }
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_AANDVLISTJSONCACHE + attributeInfo.CateId);
        }

        /// <summary>
        /// 删除属性值
        /// </summary>
        /// <param name="attrValueId">属性值id</param>
        public static void DeleteAttributeValueById(int attrValueId)
        {
            if (attrValueId < 1) return;

            AttributeValueInfo attributeValueInfo = GetAttributeValueById(attrValueId);
            AttributeInfo attributeInfo = GetAttributeById(attributeValueInfo.AttrId);
            BrnShop.Data.Categories.DeleteAttributeValueById(attrValueId);
            if (attributeInfo.IsFilter == 1)
            {
                BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_FILTERAANDVLIST + attributeInfo.CateId);
            }
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_ATTRIBUTEVALUE_INFO + attrValueId);
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_AANDVLISTJSONCACHE + attributeInfo.CateId);
        }

        /// <summary>
        /// 更新属性值
        /// </summary>
        public static void UpdateAttributeValue(AttributeValueInfo attributeValueInfo)
        {
            BrnShop.Data.Categories.UpdateAttributeValue(attributeValueInfo);
            AttributeInfo attributeInfo = GetAttributeById(attributeValueInfo.AttrId);
            if (attributeInfo.IsFilter == 1)
            {
                BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_FILTERAANDVLIST + attributeInfo.CateId);
            }
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_ATTRIBUTEVALUE_INFO + attributeValueInfo.AttrValueId);
            BrnShop.Core.BSPCache.Remove(CacheKeys.SHOP_CATEGORY_AANDVLISTJSONCACHE + attributeInfo.CateId);
        }
    }
}