using Edu.Model.Entity.DataStatistics;
using Edu.Model.ViewModel.DataStatistics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Edu.Repository.DataStatistics
{
    /// <summary>
    /// 市场部数据统计仓储层
    /// </summary>
    public class RB_Market_DataRepository : BaseRepository<RB_Market_Data>
    {
        /// <summary>
        /// 获取市场部数据统计列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Market_Data_Extend> GetMarketListRepository(RB_Market_Data_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Market_Data AS A
WHERE 1=1
");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Market_Data_Extend.Group_Id), query.Group_Id);
                }
                if (query.CreateBy > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Market_Data_Extend.CreateBy), query.CreateBy);
                }
                if (!string.IsNullOrEmpty(query.QCreateByIds))
                {
                    builder.AppendFormat(@" AND A.{0} IN({1}) ", nameof(RB_Market_Data_Extend.CreateBy), query.QCreateByIds);
                }
                if (query.DeptId > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Market_Data_Extend.DeptId), query.DeptId);
                }
                if (!string.IsNullOrEmpty(query.StartTime))
                {
                    builder.AppendFormat(" AND A.{0}>='{1}' ", nameof(RB_Market_Data_Extend.Date), query.StartTime);
                }
                if (!string.IsNullOrEmpty(query.EndTime))
                {
                    builder.AppendFormat(" AND A.{0}<='{1} 23:59:59' ", nameof(RB_Market_Data_Extend.Date), query.EndTime);
                }
            }
            return Get<RB_Market_Data_Extend>(builder.ToString()).ToList();
        }

        /// <summary>
        /// 新增市场部数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool InsertMarketRepository(RB_Market_Data_Extend model)
        {
            var newModel = new RB_Market_Data_Extend()
            {
                Date = model.Date,

                ClueCount = model.ClueCount,
                YestodayClue = model.YestodayClue,
                CurWeekClue=model.CurWeekClue,
                LastWeekClue = model.LastWeekClue,
                CurMonthClue=model.CurMonthClue,
                LastMonthClue = model.LastMonthClue,

                VisitCount = model.VisitCount,
                YestodayVisit = model.YestodayVisit,
                CurWeekVisit=model.CurWeekVisit,
                LastWeekVisit = model.LastWeekVisit,
                CurMonthVisit=model.CurMonthVisit,
                LastMonthVisit = model.LastMonthVisit,

                TrialLessonCount = model.TrialLessonCount,
                YestodayTrialLesson = model.YestodayTrialLesson,
                CurWeekTrialLesson=model.CurWeekTrialLesson,
                LastWeekTrialLesson = model.LastWeekTrialLesson,
                CurMonthTrialLesson=model.CurMonthTrialLesson,
                LastMonthTrialLesson = model.LastMonthTrialLesson,

                FollowCount = model.FollowCount,
                YestodayFollow = model.YestodayFollow,
                CurWeekFollow=model.CurWeekFollow,
                LastWeekFollow = model.LastWeekFollow,
                CurMonthFollow=model.CurMonthFollow,
                LastMonthFollow = model.LastMonthFollow,

                SaleMoney = model.SaleMoney,
                YestodaySale = model.YestodaySale,
                CurWeekSale=model.CurWeekSale,
                LastWeekSale = model.LastWeekSale,
                CurMonrhSale=model.CurMonrhSale,
                LastMonthSale = model.LastMonthSale,

                CreateBy = model.CreateBy,
                CreateTime = DateTime.Now,
                DeptId = model.DeptId,
                Group_Id = model.Group_Id
            };
            var newId = base.Insert(newModel);
            newModel.Id = newId;
            return newId > 0;
        }

        /// <summary>
        /// 清楚历史数据
        /// </summary>
        /// <returns></returns>
        public void ClearMarketRepository()
        {
            string sql = "TRUNCATE TABLE RB_Market_Data ";
            base.Execute(sql);
        }
    }
}
