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

namespace Edu.Repository.Log
{
    /// <summary>
    /// 流程修改日志仓储层
    /// </summary>
    public class RB_Flow_LogRepository : BaseRepository<RB_Flow_Log>
    {
        /// <summary>
        /// 获取流程修改日志列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Flow_Log_ViewModel> GetFlowLogListRepository(RB_Flow_Log_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT *
FROM RB_Flow_Log
WHERE 1=1 
");
            builder.AppendFormat(" AND {0}=0 ", nameof(RB_Flow_Log_ViewModel.Status));
            if (query != null)
            {
                if (query.FlowId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Flow_Log_ViewModel.FlowId),query.FlowId);
                }
                if (!string.IsNullOrEmpty(query.QFlowIds))
                {
                    builder.AppendFormat(" AND {0} IN({1}) ", nameof(RB_Flow_Log_ViewModel.FlowId), query.QFlowIds);
                }
            }
            return Get<RB_Flow_Log_ViewModel>(builder.ToString()).ToList();
        }

        /// <summary>
        /// 获取流程修改日志分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Flow_Log_ViewModel> GetFlowLogPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Flow_Log_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT *
FROM RB_Flow_Log
WHERE 1=1 
");
            builder.AppendFormat(" AND {0}=0 ", nameof(RB_Flow_Log_ViewModel.Status));
            if (query != null)
            {
                if (query.FlowId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Flow_Log_ViewModel.FlowId), query.FlowId);
                }
                if (!string.IsNullOrEmpty(query.QFlowIds))
                {
                    builder.AppendFormat(" AND {0} IN({1}) ", nameof(RB_Flow_Log_ViewModel.FlowId), query.QFlowIds);
                }
            }
            return GetPage<RB_Flow_Log_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString()).ToList();
        }

        /// <summary>
        /// 细呢子流程日志
        /// </summary>
        /// <param name="oldModel"></param>
        /// <returns></returns>
        public bool AddFlowLogRepository(RB_Flow_Log oldModel)
        {
            var model = new RB_Flow_Log()
            {
                CreateBy = oldModel.CreateBy,
                FlowId = oldModel.FlowId,
                Group_Id = oldModel.Group_Id,
                LogContent = oldModel.LogContent.TrimStart(',').TrimEnd(','),
                CreateTime = DateTime.Now,
                Status = 0,
            };
            return base.Insert(model) > 0;
        }
    }
}
