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

namespace Edu.Repository.Flow
{
    /// <summary>
    /// 流程设置仓储层【退课相关】
    /// </summary>
    public class RB_FlowRepository : BaseRepository<RB_Flow>
    {
        /// <summary>
        /// 流程节点仓储层对象
        /// </summary>
        private readonly RB_Flow_NodeRepository flow_NodeRepository = new RB_Flow_NodeRepository();

        /// <summary>
        /// 流程节点审核人
        /// </summary>
        private readonly RB_Flow_AduitInfoRepository flow_AduitInfoRepository = new RB_Flow_AduitInfoRepository();

        /// <summary>
        /// 获取流程设置分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Flow_ViewModel> GetFlowPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Flow_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT * 
FROM RB_Flow
WHERE 1=1
");
            builder.AppendFormat(" AND {0}={1} ", nameof(RB_Flow_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Flow_ViewModel.Group_Id), query.Group_Id);
                }
            }
            return GetPage<RB_Flow_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString()).ToList();
        }

        /// <summary>
        /// 获取流程信息
        /// </summary>
        /// <param name="FlowId"></param>
        /// <returns></returns>
        public RB_Flow_ViewModel GetFlowRepository(object FlowId)
        {
            var extModel = base.GetEntity<RB_Flow_ViewModel>(FlowId);
            if (extModel != null)
            {
                if (extModel.FlowId > 0)
                {
                    var auditList = flow_AduitInfoRepository.GetFlowAduitInfoListRepository(new RB_Flow_AduitInfo_ViewModel() { FlowId = extModel.FlowId });
                    extModel.FlowNodeList = flow_NodeRepository.GetFlowNodeListRepository(new RB_Flow_Node_ViewModel() { FlowId = extModel.FlowId });
                    if (extModel.FlowNodeList != null && extModel.FlowNodeList.Count > 0)
                    {
                        foreach (var item in extModel.FlowNodeList)
                        {
                            item.FlowAduitList = auditList?.Where(qitem => qitem.NodeId == item.NodeId)?.ToList();
                        }
                    }
                }
            }
            return extModel;
        }
    }
}
