using Edu.Common.Enum.Log;
using Edu.Model.Entity.Customer;
using Edu.Model.ViewModel.Customer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Edu.Repository.Customer
{
    /// <summary>
    /// 学员日志仓储层
    /// </summary>
    public class RB_Student_LogRepository : BaseRepository<RB_Student_Log>
    {
        /// <summary>
        /// 获取学员日志分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_Log_Extend> GetStudentLogPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Student_Log_Extend query)
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendFormat(@"
SELECT A.*
FROM RB_Student_Log AS A
WHERE 1=1
");
            if (query != null)
            {
                if (query.StuId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Log_Extend.StuId), query.StuId);
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} DESC ", nameof(RB_Student_Log_Extend.LogId));
            return GetPage<RB_Student_Log_Extend>(pageIndex, pageSize, out rowsCount, builder.ToString()).ToList();
        }

        /// <summary>
        /// 新增学员日志
        /// </summary>
        /// <param name="StuId">学员编号</param>
        /// <param name="LogType">日志类型</param>
        /// <param name="LogTitle">日志标题</param>
        /// <param name="LogContent">日志内容</param>
        /// <param name="CreateBy">创建人</param>
        /// <param name="CreateType">创建人类型(1-系统用户，2-同行用户)</param>
        /// <param name="Group_Id">集团编号</param>
        /// <returns></returns>
        public bool AddStuLogRepository(int StuId, StudentLogTypeEnum LogType,string LogTitle, string LogContent, int CreateBy, int CreateType = 1, int Group_Id = 100000)
        {
            var newModel = new RB_Student_Log_Extend()
            {
                LogId = 0,
                StuId = StuId,
                LogType = LogType,
                LogTitle= LogTitle,
                LogContent = LogContent,
                CreateTime = DateTime.Now,
                CreateType = CreateType,
                CreateBy = CreateBy,
                Group_Id = Group_Id
            };
            var newId = base.Insert(newModel);
            newModel.LogId = newId;
            return newId > 0;
        }
    }
}
