using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Edu.AOP.CustomerAttribute;
using Edu.Model.Entity.App;
using Edu.Repository.App;
using Edu.Repository.System;

namespace Edu.Module.System
{
    /// <summary>
    /// App首页处理类
    /// </summary>
    public class AppHomePageModule
    {
        /// <summary>
        /// 首页banner仓储层对象
        /// </summary>
        private readonly RB_HomePage_BannerRepository homePageBannerRepository = new RB_HomePage_BannerRepository();

        /// <summary>
        /// 首页标签仓储层
        /// </summary>
        private readonly RB_HomePage_LableRepository homeLableRepository = new RB_HomePage_LableRepository();

        private readonly RB_StageRepository stageRepository= new RB_StageRepository();

        #region 首页banner图片列表
        /// <summary>
        /// 获取学生app首页banner列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_HomePage_Banner> GetHomePageBannerList(RB_HomePage_Banner query)
        {
            return homePageBannerRepository.GetHomePageBannerList(query);
        }

        /// <summary>
        /// 添加修改轮播图信息
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool SetHomePage(List<RB_HomePage_Banner> list)
        {
            bool flag = false;
            var oldList = homePageBannerRepository.GetHomePageBannerList(new RB_HomePage_Banner { Group_Id = list.FirstOrDefault()?.Group_Id ?? 0 });
            oldList.ForEach(x => x.Status = Common.Enum.DateStateEnum.Delete);
            homePageBannerRepository.UpdateBatch(oldList);
            foreach (var item in list)
            {
                if (item.BannerId == 0)
                {
                    flag = homePageBannerRepository.Insert(item) > 0;
                }
                else
                {
                    flag = homePageBannerRepository.Update(item);
                }
            }
            return flag;
        }
        #endregion

        #region 首页banner标签列表
        /// <summary>
        /// 获取学生app首页bannerLabel列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_HomePage_Lable> GetHomePageLableList(RB_HomePage_Lable query)
        {
            return homeLableRepository.GetHomePageLableList(query);
        }

        /// <summary>
        /// 添加修改首页标签信息
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool SetHomePageLable(List<RB_HomePage_Lable> list)
        {
            bool flag = false;
            var oldList = homeLableRepository.GetHomePageLableList(new RB_HomePage_Lable { Group_Id = list.FirstOrDefault()?.Group_Id ?? 0 });
            oldList.ForEach(x => x.Status = Common.Enum.DateStateEnum.Delete);
            homeLableRepository.UpdateBatch(oldList);
            foreach (var item in list)
            {
                if (item.LableId == 0)
                {
                    flag = homeLableRepository.Insert(item) > 0;
                }
                else
                {
                    flag = homeLableRepository.Update(item);
                }
            }
            return flag;
        }
        #endregion

        #region 客户阶段

        /// <summary>
        /// 获取客户阶段以及数量
        /// </summary>
        /// <param name="gid"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public Object GetPeersCustomerStageCount(int gid,int id)
        {
            var list = stageRepository.GetPeersCustomerStageCount(gid,id);

            if (list != null && list.Count > 0)
            {
                var result = list.Select(x => new
                {
                    x.Id,
                    x.StageName,
                    x.ChildCount
                });
                return result;
            }
            return list;
        }

        #endregion
    }
}
