using Mall.Model.Entity.MarketingCenter;
using Mall.Model.Extend.MarketingCenter;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace Mall.Repository.MarketingCenter
{
    /// <summary>
    /// 分享详情实体仓储层
    /// </summary>
    public class RB_Share_FriendRepository : BaseRepository<RB_Share_Friend>
    {
        /// <summary>
        /// 获取分享详情分页列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="rowsCount">总条数</param>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Share_Friend_Extend> GetShareFriendPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Share_Friend_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT * 
FROM rb_share_friend
WHERE 1=1 AND Status=0
");
            if (query != null)
            {
                if (query.TenantId > 0)
                {
                    builder.Append($" AND {nameof(RB_Share_Friend_Extend.TenantId)}={query.TenantId}");
                }
                if (query.TenantId > 0)
                {
                    builder.Append($" AND {nameof(RB_Share_Friend_Extend.MallBaseId)}={query.MallBaseId}");
                }
                if (query.CateId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Share_Friend_Extend.CateId),query.CateId);
                }
                if (!string.IsNullOrWhiteSpace(query.CateIds))
                {
                    builder.AppendFormat(" AND {0} IN({1}) ", nameof(RB_Share_Friend_Extend.CateId), query.CateIds);
                }
                if (!string.IsNullOrWhiteSpace(query.KeyWords))
                {
                    builder.AppendFormat(" AND {0} LIKE '%{1}%' ", nameof(RB_Share_Friend_Extend.MaterialInfo), query.KeyWords);
                }
                if (!string.IsNullOrWhiteSpace(query.QStartDate))
                {
                    builder.AppendFormat(" AND {0}>='{1}' ", nameof(RB_Share_Friend_Extend.CreateDate), Convert.ToDateTime(query.QStartDate).ToString("yyyy-MM-dd"));
                }
                if (!string.IsNullOrWhiteSpace(query.QEndDate))
                {
                    builder.AppendFormat(" AND {0}<='{1} 23:59:59' ", nameof(RB_Share_Friend_Extend.CreateDate), Convert.ToDateTime(query.QEndDate).ToString("yyyy-MM-dd"));
                }
            }
            return GetPage<RB_Share_Friend_Extend>(pageIndex, pageSize, out rowsCount, builder.ToString()).ToList();
        }


        /// <summary>
        /// 获取分享详情列表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Share_Friend_Extend> GetShareFriendListRepository(RB_Share_Friend_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT * 
FROM rb_share_friend
WHERE 1=1 AND Status=0
");
            if (query != null)
            {
                if (query.TenantId > 0)
                {
                    builder.Append($" AND {nameof(RB_Share_Friend_Extend.TenantId)}={query.TenantId}");
                }
                if (query.TenantId > 0)
                {
                    builder.Append($" AND {nameof(RB_Share_Friend_Extend.MallBaseId)}={query.MallBaseId}");
                }
                if (query.CateId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Share_Friend_Extend.CateId), query.CateId);
                }
                if (!string.IsNullOrWhiteSpace(query.CateIds))
                {
                    builder.AppendFormat(" AND {0} IN({1}) ", nameof(RB_Share_Friend_Extend.CateId), query.CateIds);
                }
                if (!string.IsNullOrWhiteSpace(query.KeyWords))
                {
                    builder.AppendFormat(" AND {0} LIKE '%{1}%' ", nameof(RB_Share_Friend_Extend.MaterialInfo), query.KeyWords);
                }
                if (!string.IsNullOrWhiteSpace(query.QStartDate))
                {
                    builder.AppendFormat(" AND {0}>='{1}' ", nameof(RB_Share_Friend_Extend.CreateDate), Convert.ToDateTime(query.QStartDate).ToString("yyyy-MM-dd"));
                }
                if (!string.IsNullOrWhiteSpace(query.QEndDate))
                {
                    builder.AppendFormat(" AND {0}<='{1} 23:59:59' ", nameof(RB_Share_Friend_Extend.CreateDate), Convert.ToDateTime(query.QEndDate).ToString("yyyy-MM-dd"));
                }
            }
            return Get<RB_Share_Friend_Extend>(builder.ToString()).ToList();
        }
    }
}