using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.Entity.Sys;
using QMFrameWork.Data;
using QMAPP.DAL.Sys;
using System.Data;

namespace QMAPP.BLL.Sys
{
    /// <summary>
    /// 流程设置逻辑层对象
    /// 创建者:
    /// 创建日期:
    /// </summary>   
    public class WorkFlowBLL : BaseBLL
    {
        #region 获取流程设置信息

        /// <summary>
        /// 获取流程设置信息
        /// </summary>
        /// <param name="">条件</param>
        /// <returns>信息</returns>
        public FlowSet GetWorkFlow(FlowSet model)
        {
            try
            {
                FlowSetDAL dal = new FlowSetDAL();
                //获取所有流程设置信息
                List<ZPFlow> resultList = dal.GetList();
                FlowSet flowSet = new FlowSet();


                List<ZPFlow> distinctList = FindDistinct(resultList);
                //将机构拼成字符串返回前台显示
                if (distinctList != null && distinctList.Count > 0)
                {
                    foreach (ZPFlow zpf in distinctList)
                    {
                        //查找等于当前结点的所有记录
                        List<ZPFlow> tempList =resultList.FindAll(p=>p.FlowLevel == zpf.FlowLevel);
                        string flowLevel = "";
                        int index = 0;
                        foreach (ZPFlow fl in tempList)
                        {
                            if (flowLevel.IndexOf(fl.SOrgaDesc) > 0)
                            {
                                index++;
                                continue;
                            }
                            //拼接组织字符串
                            flowLevel += fl.SOrgaDesc;
                            if (index < tempList.Count - 1)
                            {
                                flowLevel += ",";
                            }
                            index++;
                        }
                        
                        //当前为结点1
                        if (zpf.FlowLevel == 1)
                        {
                            flowSet.FlowLevel1 = flowLevel;
                        }
                        //当前为结点2
                        if (zpf.FlowLevel == 2)
                        {
                            flowSet.FlowLevel2 = flowLevel;
                        }
                        //当前为结点3
                        if (zpf.FlowLevel == 3)
                        {
                            flowSet.FlowLevel3 = flowLevel;
                        }
                    }
                }

                return flowSet;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion


        #region 查找LIST中流程结点的唯一值

        /// <summary>
        /// 查找LIST中流程结点的唯一值
        /// </summary>
        /// <param name="resultList"></param>
        /// <returns></returns>
        private List<ZPFlow> FindDistinct(List<ZPFlow> resultList)
        {
            if (resultList == null || resultList.Count <= 0)
            {
                return null;
            }
            List<ZPFlow> distinctList = new List<ZPFlow>();
            foreach (ZPFlow zpf in resultList)
            {
                //判断LIST中该 记录是否存在
                if (distinctList.Find(p => p.FlowLevel == zpf.FlowLevel) == null)
                {
                    distinctList.Add(new ZPFlow() { FlowLevel = zpf.FlowLevel });
                }
            }
            return distinctList;
        }

        #endregion


        #region 获取结点下的人员信息

        /// <summary>
        /// 获取结点下的人员信息
        /// </summary>
        /// <param name="">条件</param>
        /// <returns>信息</returns>
        public List<ZPFlow> GetUserByFlowLevel(ZPFlow zpFlow)
        {
            try
            {
                FlowSetDAL dal = new FlowSetDAL();

                return dal.GetUserByFlowLevel(zpFlow); ;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 保存流程用户

        /// <summary>
        /// 保存流程用户
        /// </summary>
        /// <returns>信息</returns>
        public int SaveSelectOrganization(string FlowLevel, string selectKey)
        {
            List<ZPFlow> insertList = new List<ZPFlow>();
            try
            {
                FlowSetDAL dal = new FlowSetDAL();
                string[] selectKeyArr = selectKey.Split(new char[] { ':' });
                foreach (string sdept in selectKeyArr)
                {
                    if (string.IsNullOrEmpty(sdept))
                    {
                        continue;
                    }
                    ZPFlow zpFlow = new ZPFlow();
                    zpFlow.PID = Guid.NewGuid().ToString();
                    zpFlow.FlowLevel = Convert.ToInt32(FlowLevel);
                    zpFlow.SDept = sdept;
                    insertList.Add(zpFlow);
                }
                return dal.SaveUser(FlowLevel,insertList); ;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        
        

    }
}