using Edu.Model.ViewModel.Flow;
using Edu.Repository.Flow;
using Edu.Repository.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VT.FW.DB;

namespace Edu.Module.System
{
    /// <summary>
    /// 基础相关流程设置
    /// </summary>
    public class BasicFlowModule
    {
        /// <summary>
        /// 流程仓储层对象
        /// </summary>
        private readonly RB_FlowRepository flowRepository = new RB_FlowRepository();

        /// <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>
        private readonly RB_Flow_LogRepository flow_LogRepository = new RB_Flow_LogRepository();

        /// <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> GetFlowPageListModule(int pageIndex, int pageSize, out long rowsCount, RB_Flow_ViewModel query)
        {
            var list = flowRepository.GetFlowPageListRepository(pageIndex, pageSize, out rowsCount, query);
            return list;
        }

        /// <summary>
        /// 获取流程信息
        /// </summary>
        /// <param name="FlowId"></param>
        /// <returns></returns>
        public RB_Flow_ViewModel GetFlowModule(object FlowId)
        {
            return flowRepository.GetFlowRepository(FlowId);
        }


        /// <summary>
        /// 新增修改流程
        /// </summary>
        /// <param name="extModel"></param>
        /// <returns></returns>
        public virtual bool SetFlowModule(RB_Flow_ViewModel extModel)
        {
            bool flag = false;
            string logContent = "";
            RB_Flow_ViewModel oldModel = null;
            oldModel = GetFlowModule(extModel.FlowId);
            extModel.FlowId = oldModel?.FlowId ?? 0;
            if (extModel.FlowId > 0)
            {
               
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Flow_ViewModel.FlowName),extModel.FlowName },
                    {nameof(RB_Flow_ViewModel.UpdateBy),extModel.UpdateBy },
                    {nameof(RB_Flow_ViewModel.UpdateTime),extModel.UpdateTime },
                };
                if ((oldModel?.FlowName??"") != extModel.FlowName)
                {
                    logContent += string.Format(",流程名称由【{0}】修改为【{1}】", (oldModel?.FlowName ?? ""), extModel.FlowName);
                }
                flag = flowRepository.Update(fileds, new WhereHelper(nameof(RB_Flow_ViewModel.FlowId), extModel.FlowId));
            }
            else
            {
                logContent = string.Format("新增【{0}】流程", extModel.FlowName);
                var newId = flowRepository.Insert(extModel);
                extModel.FlowId = newId;
                flag = newId > 0;
            }
            var oldauditList = flow_AduitInfoRepository.GetFlowAduitInfoListRepository(new RB_Flow_AduitInfo_ViewModel() { FlowId = extModel.FlowId });
            var oldflowNodeList = flow_NodeRepository.GetFlowNodeListRepository(new RB_Flow_Node_ViewModel() { FlowId = extModel.FlowId });
            //以前没有流程节点(直接新增)
            if (oldflowNodeList == null)
            {
                int SortNum = 1;
                foreach (var item in extModel.FlowNodeList)
                {
                    item.FlowId = extModel.FlowId;
                    item.SortNum = SortNum;
                    if (item.FlowAduitList != null )
                    {
                        if (item.FlowAduitList.Count == 1)
                        {
                            item.NodeName = item.FlowAduitList[0].AduitName;
                        }
                        else
                        {
                            item.NodeName = string.Join(",", item.FlowAduitList.Select(qitem => qitem.AduitName));
                        }
                    }
                    var newNodeId = flow_NodeRepository.Insert(item);
                    item.NodeId = newNodeId;
                    flag = newNodeId > 0;
                    if (item.FlowAduitList != null && item.FlowAduitList.Count > 0)
                    {
                        foreach (var subItem in item.FlowAduitList)
                        {
                            subItem.NodeId = item.NodeId;
                            subItem.FlowId = extModel.FlowId;
                            flag= flow_AduitInfoRepository.Insert(subItem)>0;
                        }
                    }
                    SortNum++;
                }
            }
            else
            {
                //现在没有节点信息[删除以前的节点和审核人]
                if (extModel.FlowNodeList == null || (extModel.FlowNodeList != null && extModel.FlowNodeList.Count == 0))
                {
                    flow_NodeRepository.DeleteOne(new WhereHelper(nameof(RB_Flow_Node_ViewModel.FlowId), extModel.FlowId));
                    flow_AduitInfoRepository.DeleteOne(new WhereHelper(nameof(RB_Flow_AduitInfo_ViewModel.FlowId), extModel.FlowId));
                }
                else
                {
                    var deleteList = oldflowNodeList.Where(qitem => !extModel.FlowNodeList.Any(oldItem => qitem.NodeId == oldItem.NodeId)).ToList();
                    foreach (var dItem in deleteList)
                    {
                        if (dItem.NodeId > 0)
                        {
                            flow_NodeRepository.Delete(dItem.NodeId);
                            flow_AduitInfoRepository.DeleteOne(new WhereHelper(nameof(RB_Flow_AduitInfo_ViewModel.NodeId), dItem.NodeId));
                            logContent += string.Format(",删除【{0}】节点", dItem.NodeName);
                        }
                    }
                    int SortNum = 1;
                    foreach (var nItem in extModel.FlowNodeList)
                    {
                        nItem.FlowId = extModel.FlowId;
                        nItem.SortNum = SortNum;
                        if (nItem.FlowAduitList != null)
                        {
                            if (nItem.FlowAduitList.Count == 1)
                            {
                                nItem.NodeName = nItem.FlowAduitList[0].AduitName;
                            }
                            else
                            {
                                nItem.NodeName = string.Join(",", nItem.FlowAduitList.Select(qitem => qitem.AduitName));
                            }
                        }
                        if (nItem.NodeId == 0)
                        {
                            var newNodeId = flow_NodeRepository.Insert(nItem);
                            nItem.NodeId = newNodeId;
                            flag = newNodeId > 0;
                        }
                        else
                        {
                            flag= flow_NodeRepository.Update(nItem);
                        }
                        var auditDeleteList = oldauditList.Where(qitem => qitem.NodeId == nItem.NodeId);
                        if (nItem.FlowAduitList != null && nItem.FlowAduitList.Count > 0)
                        {
                            flow_AduitInfoRepository.DeleteOne(new WhereHelper(nameof(RB_Flow_AduitInfo_ViewModel.NodeId), nItem.NodeId));
                            foreach (var subItem in nItem.FlowAduitList)
                            {
                                subItem.NodeId = nItem.NodeId;
                                subItem.FlowId = extModel.FlowId;
                                flag= flow_AduitInfoRepository.Insert(subItem)>0;
                            }
                        }
                        SortNum++;
                    }
                }
            }

            //添加日志
            if (!string.IsNullOrEmpty(logContent))
            {
                flow_LogRepository.AddFlowLogRepository(new Model.Entity.Log.RB_Flow_Log()
                {
                    CreateBy = extModel.CreateBy,
                    Group_Id = extModel.Group_Id,
                    FlowId = extModel.FlowId,
                    LogContent = logContent
                });
            }
            return flag;
        }
    }
}