﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OneDream.Models;
using OneDream.Enum;

namespace OneDream.DAL
{
    public class ProductDAL : BaseDAL
    {
        #region banner

        public IList<OD_Banner> GetBannerList()
        {
            var list = entityInstance.OD_Banner.Where(m => !m.DelFlag).OrderBy(m => m.Sort).ToList();
            return list;
        }

        #endregion

        /// <summary>
        /// 获取商品信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ProductModel GetProductInfoByID(int id)
        {
            ProductModel model = new ProductModel();
            model.Info = entityInstance.OD_Product.FirstOrDefault(m => !m.DelFlag && m.ID == id);
            model.Info = model.Info == null ? new OD_Product() : model.Info;
            model.ProductGallery = entityInstance.OD_ProductGallery.Where(m => m.ProductID == id).OrderBy(m => m.Sort).ToList();
            return model;
        }

        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <param name="sc"></param>
        /// <param name="se"></param>
        /// <param name="oe"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public IList<od_v_term> GetProductListByCondition(ProductSearchConditionModel sc, SortEnum se, OrderByEnum oe, int pageIndex, int pageSize, out int total)
        {
            var list = entityInstance.od_v_term.Where(m => m.Status == (int)sc.Status);
            if (sc.CategoryID > 0)
            {
                list = list.Where(m => m.CategoryID == sc.CategoryID);
            }
            if (!string.IsNullOrEmpty(sc.Keyword))
            {
                var contactProductIdList = entityInstance.od_v_product_dictionary.Where(m => m.Type == (int)DictionaryTypeEnum.Tag && m.Value.Contains(sc.Keyword)).Select(m => m.ProductID).ToList();
                list = list.Where(m => m.ProductName.Contains(sc.Keyword) || contactProductIdList.Contains(m.ProductID));
            }
            if (sc.R1)
                list = list.Where(m => m.R1);
            if (sc.R2)
                list = list.Where(m => m.R2);
            if (sc.Hot)
                list = list.Where(m => m.Hot);
            total = list.Count();
            switch (se)
            {
                case SortEnum.Popular:
                    list = list.OrderByDescending(m => m.TotalSell);
                    break;
                case SortEnum.Rest:
                    list = list.OrderBy(m => m.Rest);
                    break;
                case SortEnum.Latest:
                    list = list.OrderByDescending(m => m.CreateTime);
                    break;
                case SortEnum.Total:
                    if (oe == OrderByEnum.ASC)
                        list = list.OrderBy(m => m.Total);
                    else
                        list = list.OrderByDescending(m => m.Total);
                    break;
                default:
                    list = list.OrderByDescending(m => m.TotalSell);
                    break;
            }

            var rst = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return rst;
        }

        public IList<ProductModel> GetProductListByCondition(ProductSearchConditionModel sc, int pageIndex, int pageSize, out int total)
        {
            var list = (from p in entityInstance.OD_Product
                        join c in entityInstance.OD_Category on p.CategoryID equals c.ID
                        where !p.DelFlag && !c.DelFlag
                        select new ProductModel()
                        {
                            Info = p,
                            CategoryName = c.Name
                        });
            if (sc.CategoryID > 0)
            {
                list = list.Where(m => m.Info.CategoryID == sc.CategoryID);
            }
            if (!string.IsNullOrEmpty(sc.Keyword))
            {
                list = list.Where(m => m.Info.Name.Contains(sc.Keyword));
            }
            total = list.Count();
            var rst = list.OrderByDescending(m => m.Info.ID).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return rst;
        }

        public IList<od_v_term> GetProductListOfEveryCategory(int cnt)
        {
            //string sql = "select ID, TermNo, ProductID, DictionaryID, [Current], Rest, Total, DefaultValue, AutoStart, CreateTime, ProductName, Thumbnail, CategoryID, TotalSell, Type, [Key], Value, Status from(select ID, TermNo, ProductID, DictionaryID, [Current], Rest, Total, DefaultValue, AutoStart, CreateTime, ProductName, Thumbnail, CategoryID, TotalSell, Type, [Key], Value, Status,row_number() over(partition by categoryid order by newid()) as rowindex from od_v_term where Status = " + (int)TermStatusEnum.Ongoing + ") a where rowindex <= {0}";
            string sql = "select * from(select *,row_number() over(partition by categoryid order by newid()) as rowindex from od_v_term where Status = " + (int)TermStatusEnum.Ongoing + ") a where rowindex <= {0}";
            var rst = entityInstance.od_v_term.SqlQuery(sql, new object[] { cnt }).ToList();
            return rst;
        }

    }
}
