using Edu.AOP.CustomerAttribute;
using Edu.Common.API;
using Edu.Common.Enum.User;
using Edu.Common.Plugin;
using Edu.Model.CacheModel;
using Edu.Model.Entity.User;
using Edu.Model.Public;
using Edu.Model.ViewModel.User;
using Edu.Repository.User;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using VT.FW.DB;

namespace Edu.Module.User
{
    /// <summary>
    /// 流程管理处理类
    /// </summary>
    public class WorkFlowManagementModule
    {
        private readonly Rb_Workflow_TemplateRepository respository = new Rb_Workflow_TemplateRepository();
        private readonly Rb_Workflow_AuditRepository respositoryAudit = new Rb_Workflow_AuditRepository();
        private readonly Rb_Workflow_AuditUserRepository respositoryUser = new Rb_Workflow_AuditUserRepository();
        private readonly Rb_Workflow_ConditionRepository respositoryCondition = new Rb_Workflow_ConditionRepository();
        private readonly Rb_Workflow_AskforleaveRepository respositoryAskforleave = new Rb_Workflow_AskforleaveRepository();
        private readonly Rb_Workflow_EvectionRepository respositoryEvection = new Rb_Workflow_EvectionRepository();
        private readonly Rb_Workflow_GoOutRepository respositoryGoout = new Rb_Workflow_GoOutRepository();
        private readonly Rb_Workflow_ReissuecardRepository respositoryReissuecar = new Rb_Workflow_ReissuecardRepository();
        private readonly Rb_Workflow_VisualrangeRepository visualrangeRepository = new Rb_Workflow_VisualrangeRepository();
        private readonly RB_AccountRepository accountRepository = new RB_AccountRepository();


        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public List<Rb_Workflow_Template> GetTemplateList(UserInfo userInfo)
        {
            return respository.GetTempateList(userInfo.Group_Id, userInfo.School_Id);
        }

        /// <summary>
        /// 根据id获取数据
        /// </summary>
        /// <param name="tid"></param>
        /// <returns></returns>
        public Rb_Workflow_Template GetTemplate(int tid)
        {
            var model = respository.GetEntity(tid);
            return model;
        }

        /// <summary>
        /// 保存修改模版
        /// </summary>
        /// <param name="demodel"></param>
        /// <param name="GroupId"></param>
        /// <param name="BranchId"></param>
        /// <param name="eid"></param>
        /// <returns></returns>
        public bool SetTemplate(Rb_Workflow_Template demodel, int GroupId, int BranchId, int eid)
        {
            bool flag = false;
            if (demodel.Id > 0)//修改
            {
                IDictionary<string, object> fileds = new Dictionary<string, object>
                {
                    { nameof(RB_Department.Status), demodel.Status },
                    { nameof(RB_Department.UpdateBy), eid },
                    { nameof(RB_Department.UpdateTime), DateTime.Now }
                };
                IList<WhereHelper> whereHelpers = new List<WhereHelper>
                {
                    new WhereHelper()
                    {
                        FiledName = nameof(Rb_Workflow_Template.Id),
                        FiledValue = demodel.Id,
                        OperatorEnum = OperatorEnum.Equal
                    }
                };
                flag = respository.Update(fileds, whereHelpers);
            }
            else
            {//新增
                demodel.AuditDistinct = 1;
                demodel.CreateBy = eid;
                demodel.CreateTime = DateTime.Now;
                string[] Ttype = Common.Plugin.EnumHelper.ToName(demodel.TemplateType).Split('|');
                demodel.Icon = Ttype[1];
                demodel.Name = Ttype[0];
                demodel.GroupType = (WFTGroupTypeEnum)Convert.ToInt32(Ttype[2]);
                demodel.NotificationCP = 1;
                demodel.RB_BranchId = BranchId;
                demodel.RB_GroupId = GroupId;
                demodel.Status = 1;
                demodel.UpdateBy = eid;
                demodel.UpdateTime = DateTime.Now;
                demodel.Type = (int)WFTempTypeEnum.OA;
                int tid = respository.Insert(demodel);
                if (tid > 0) flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 获取默认审核人
        /// </summary>
        /// <param name="TemplateId"></param>
        /// <returns></returns>
        public Rb_Workflow_Condition_Extend GetDefaultCondition(int TemplateId)
        {
            var list = respositoryCondition.GetDefaultCondition(TemplateId);
            var tmodel = respository.GetEntity(TemplateId);
            int TemplateType = 1;
            string TemplateTypeName = "";
            if (tmodel != null)
            {
                TemplateType = (int)tmodel.TemplateType;
                TemplateTypeName = tmodel.Name;
            }
            if (list.Count > 0)
            {
                var model = list[0];
                model.Description = "";
                List<Rb_Workflow_Audit_Extend> alist = respositoryAudit.GetDefaultAudit(model.Id);
                foreach (var item in alist)
                {
                    if (item.AuditType == 2)
                        item.AuditUserList = respositoryUser.GetConditionAuditUser(item.Id);
                }
                model.AuditList = alist;
                List<Rb_Workflow_Audituser_Extend> ulist = respositoryUser.GetDefaultAuditUser(model.Id);
                model.AuditUserList = ulist;
                model.TemplateType = TemplateType;
                model.TemplateTypeName = TemplateTypeName;
                return model;
            }
            else
            {
                return new Rb_Workflow_Condition_Extend() { TemplateId = TemplateId, Sort = 0, Id = 0, TemplateType = TemplateType, TemplateTypeName = TemplateTypeName };
            }
        }

        /// <summary>
        /// 获取分条建审核
        /// </summary>
        /// <param name="TemplateId"></param>
        /// <returns></returns>
        public List<Rb_Workflow_Condition_Extend> GetOtherConditionList(int TemplateId)
        {
            var list = respositoryCondition.GetOtherConditionList(TemplateId);
            List<Rb_Workflow_Audit_Extend> alist = respositoryAudit.GetAllAudit(TemplateId);
            List<Rb_Workflow_Audituser_Extend> ulist = respositoryUser.GetAllAuditUser(TemplateId);
            var tmodel = respository.GetEntity(TemplateId);
            foreach (var model in list)
            {
                string description = "如果";
                if (tmodel.TemplateType == WFTTemplateTypeEnum.AskForLeave)
                {
                    description = "请假类型属于：";
                    List<int> aflStr = JsonConvert.DeserializeObject<List<int>>(model.AskforleaveType);
                    string askType = "";
                    foreach (int afl in aflStr)
                    {
                        askType += Common.Plugin.EnumHelper.ToName((AskforleaveTypeEnum)afl) + "、";
                    }
                    if (askType != "")
                    {
                        description += askType[0..^1];
                    }
                    description += " 并且 ";
                }
                //string dayOrHours = model.DayHour == 1 ? "时长(天数)" : "时长(小时)";
                string dayOrHours = "时长(小时)";
                if (tmodel != null && tmodel.TemplateType == WFTTemplateTypeEnum.Evection)
                {
                    dayOrHours = "时长(天数)";
                }
                switch (model.ConditionType)
                {
                    case (int)EquationTypeEnum.LessThan:
                        description += dayOrHours + "<" + model.StartValue;
                        break;
                    case (int)EquationTypeEnum.GreaterThan: description += dayOrHours + ">" + model.StartValue; break;
                    case (int)EquationTypeEnum.EqualTo: description += dayOrHours + "=" + model.StartValue; break;
                    case (int)EquationTypeEnum.LessThanOrEqualTo: description += dayOrHours + "≤" + model.StartValue; break;
                    case (int)EquationTypeEnum.GreaterThanOrEqualTo: description += dayOrHours + "≥" + model.StartValue; break;
                    case (int)EquationTypeEnum.InBetween:
                        if (model.StartType == 1)
                        {
                            description += model.StartValue + "<" + dayOrHours;
                        }
                        else
                        {
                            description += model.StartValue + "≤" + dayOrHours;
                        }
                        if (model.EndType == 1)
                        {
                            description += "<" + model.EndValue;
                        }
                        else
                        {
                            description += "≤" + model.EndValue;
                        }
                        break;
                }

                model.Description = description;
                model.AuditList = alist.Where(x => x.ConditionId == model.Id).ToList();
                model.AuditUserList = ulist.Where(x => x.AuditId == model.Id).ToList();
            }
            return list;
        }

        /// <summary>
        /// 获取条件详情
        /// </summary>
        /// <param name="ConditionId"></param>
        /// <returns></returns>
        public Rb_Workflow_Condition_Extend GetConditionAndAuditUser(int ConditionId)
        {
            var model = respositoryCondition.GetEntity(ConditionId).RefMapperTo<Rb_Workflow_Condition_Extend>();
            List<Rb_Workflow_Audit_Extend> alist = respositoryAudit.GetDefaultAudit(model.Id);
            foreach (var item in alist)
            {
                if (item.AuditType == 2)
                    item.AuditUserList = respositoryUser.GetConditionAuditUser(item.Id);
            }
            model.AuditList = alist;
            var tmodel = respository.GetEntity(model.TemplateId);
            if (tmodel != null)
            {
                model.TemplateTypeName = tmodel.Name;
                model.TemplateType = (int)tmodel.TemplateType;
            }
            if (tmodel != null && tmodel.TemplateType == WFTTemplateTypeEnum.AskForLeave)
            {
                var AuditType = EnumHelper.EnumToList(typeof(AskforleaveTypeEnum));
                List<AskforleaveTypeList> asklist = new List<AskforleaveTypeList>();
                List<int> askStr = JsonConvert.DeserializeObject<List<int>>(model.AskforleaveType);
                foreach (var item in AuditType)
                {
                    AskforleaveTypeList askmodel = new AskforleaveTypeList
                    {
                        Id = item.Id,
                        Name = item.Name
                    };
                    if (askStr.Contains(askmodel.Id))
                    {
                        askmodel.Is_show = 1;
                    }
                    else { askmodel.Is_show = 0; }
                    asklist.Add(askmodel);
                }
                model.aftList = asklist.OrderBy(x => x.Id).ToList();
            }
            List<Rb_Workflow_Audituser_Extend> ulist = respositoryUser.GetDefaultAuditUser(model.Id);
            model.AuditUserList = ulist;
            return model;
        }

        /// <summary>
        /// 保存分条件
        /// </summary>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public bool SetOtherCondition(Rb_Workflow_Condition_Extend dmodel)
        {
            var trans = respository.DbTransaction;
            try
            {

                if (dmodel.Id > 0)
                {
                    Rb_Workflow_Condition cmodel = respositoryCondition.GetEntity(dmodel.Id);
                    cmodel.AskforleaveType = dmodel.AskforleaveType;
                    cmodel.ConditionType = dmodel.ConditionType;
                    cmodel.DayHour = dmodel.DayHour;
                    cmodel.EndType = dmodel.EndType;
                    cmodel.EndValue = dmodel.EndValue;
                    cmodel.Sort = dmodel.Sort;
                    cmodel.StartType = dmodel.StartType;
                    cmodel.StartValue = dmodel.StartValue;
                    bool Isok = respositoryCondition.Update(cmodel, trans);
                    if (Isok)
                    {
                        //删除所有的审核人  指定成员  抄送人  再重新添加
                        respositoryCondition.DelConditionAuditAndUser(dmodel.Id, trans);
                        //新增审核人 和  指定成员
                        int Num = 0;
                        if (dmodel.AuditList != null)
                        {
                            foreach (var item in dmodel.AuditList)
                            {
                                Num++;
                                Rb_Workflow_Audit auditmodel = new Rb_Workflow_Audit
                                {
                                    AuditDescription = item.AuditDescription,
                                    AuditType = item.AuditType,
                                    AuditWay = item.AuditWay,
                                    RoleOrInitiator = item.RoleOrInitiator,
                                    Sort = Num,
                                    ConditionId = dmodel.Id
                                };
                                int auditId = respositoryAudit.Insert(auditmodel, trans);
                                if (item.AuditType == 2)
                                {
                                    foreach (var d in item.AuditUserList)
                                    {
                                        Rb_Workflow_Audituser aumodel = new Rb_Workflow_Audituser
                                        {
                                            AuditId = auditId,
                                            EmployeeId = d.EmployeeId,
                                            EmType = 1,
                                            Sort = d.Sort
                                        };
                                        respositoryUser.Insert(aumodel, trans);
                                    }
                                }
                            }
                        }
                        if (dmodel.AuditUserList != null)
                        {
                            foreach (var item in dmodel.AuditUserList)
                            {
                                Rb_Workflow_Audituser aumodel = new Rb_Workflow_Audituser
                                {
                                    AuditId = dmodel.Id,
                                    EmployeeId = item.EmployeeId,
                                    EmType = 2,
                                    Sort = item.Sort
                                };
                                respositoryUser.Insert(aumodel, trans);
                            }
                        }
                    }
                }
                else
                {
                    Rb_Workflow_Condition cmodel = new Rb_Workflow_Condition
                    {
                        AskforleaveType = dmodel.AskforleaveType,
                        ConditionType = dmodel.ConditionType,
                        DayHour = dmodel.DayHour,
                        EndType = dmodel.EndType,
                        EndValue = dmodel.EndValue
                    };
                    int sort = respositoryCondition.GetConditionSortMax(dmodel.TemplateId.Value);
                    cmodel.Sort = sort + 1;
                    cmodel.StartType = dmodel.StartType;
                    cmodel.StartValue = dmodel.StartValue;
                    cmodel.TemplateId = dmodel.TemplateId;
                    int cid = respositoryCondition.Insert(cmodel, trans);
                    if (cid > 0)
                    {//新增审核人 和  指定成员
                        int Num = 0;
                        if (dmodel.AuditList != null)
                        {
                            foreach (var item in dmodel.AuditList)
                            {
                                Num++;
                                Rb_Workflow_Audit auditmodel = new Rb_Workflow_Audit
                                {
                                    AuditDescription = item.AuditDescription,
                                    AuditType = item.AuditType,
                                    AuditWay = item.AuditWay,
                                    RoleOrInitiator = item.RoleOrInitiator,
                                    Sort = Num,
                                    ConditionId = cid
                                };
                                int auditId = respositoryAudit.Insert(auditmodel, trans);
                                if (item.AuditType == 2)
                                {
                                    foreach (var d in item.AuditUserList)
                                    {
                                        Rb_Workflow_Audituser aumodel = new Rb_Workflow_Audituser
                                        {
                                            AuditId = auditId,
                                            EmployeeId = d.EmployeeId,
                                            EmType = 1,
                                            Sort = d.Sort
                                        };
                                        respositoryUser.Insert(aumodel, trans);
                                    }
                                }
                            }
                        }
                        if (dmodel.AuditUserList != null)
                        {
                            foreach (var item in dmodel.AuditUserList)
                            {
                                Rb_Workflow_Audituser aumodel = new Rb_Workflow_Audituser
                                {
                                    AuditId = cid,
                                    EmployeeId = item.EmployeeId,
                                    EmType = 2,
                                    Sort = item.Sort
                                };
                                respositoryUser.Insert(aumodel, trans);
                            }
                        }
                    }
                }
                respository.DBSession.Commit();
            }
            catch (Exception ex)
            {
                LogHelper.Write(ex, "SetOtherCondition");
                respository.DBSession.Rollback("SetOtherCondition");
                return false;
            }
            return true;

        }

        /// <summary>
        /// 删除分条件
        /// </summary>
        /// <param name="ConditionId"></param>
        /// <returns></returns>
        public bool DelOtherCondition(int ConditionId)
        {
            return respositoryCondition.DelOtherCondition(ConditionId) > 0;
        }

        /// <summary>
        /// 保存默认条件
        /// </summary>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public bool SetDefaultCondition(Rb_Workflow_Condition_Extend dmodel)
        {
            var trans = respository.DbTransaction;
            try
            {
                if (dmodel.Id > 0)
                {
                    Rb_Workflow_Condition cmodel = respositoryCondition.GetEntity(dmodel.Id);
                    //删除所有的审核人  指定成员  抄送人  再重新添加
                    respositoryCondition.DelConditionAuditAndUser(dmodel.Id, trans);
                    //新增审核人 和  指定成员
                    int Num = 0;
                    if (dmodel.AuditList != null)
                        foreach (var item in dmodel.AuditList)
                        {
                            Num++;
                            Rb_Workflow_Audit auditmodel = new Rb_Workflow_Audit
                            {
                                AuditDescription = item.AuditDescription,
                                AuditType = item.AuditType,
                                AuditWay = item.AuditWay,
                                RoleOrInitiator = item.RoleOrInitiator,
                                Sort = Num,
                                ConditionId = dmodel.Id
                            };
                            int auditId = respositoryAudit.Insert(auditmodel, trans);
                            if (item.AuditType == 2)
                            {
                                foreach (var d in item.AuditUserList)
                                {
                                    Rb_Workflow_Audituser aumodel = new Rb_Workflow_Audituser
                                    {
                                        AuditId = auditId,
                                        EmployeeId = d.EmployeeId,
                                        EmType = 1,
                                        Sort = d.Sort
                                    };
                                    respositoryUser.Insert(aumodel, trans);
                                }
                            }
                        }
                    if (dmodel.AuditUserList != null)
                        foreach (var item in dmodel.AuditUserList)
                        {
                            Rb_Workflow_Audituser aumodel = new Rb_Workflow_Audituser
                            {
                                AuditId = dmodel.Id,
                                EmployeeId = item.EmployeeId,
                                EmType = 2,
                                Sort = item.Sort
                            };
                            respositoryUser.Insert(aumodel, trans);
                        }
                    //同时修改分条件排序
                    if (dmodel.ConditionSortList != null)
                        foreach (var item in dmodel.ConditionSortList)
                        {
                            IDictionary<string, object> fileds = new Dictionary<string, object>
                        {
                            { nameof(Rb_Workflow_Condition.Sort), item.Sort },
                        };
                            IList<WhereHelper> whereHelpers = new List<WhereHelper>
                        {
                            new WhereHelper()
                            {
                                FiledName = nameof(Rb_Workflow_Condition.Id),
                                FiledValue = item.ConditionId,
                                OperatorEnum = OperatorEnum.Equal
                            }
                        };
                            respositoryCondition.Update(fileds, whereHelpers, trans);
                        }
                }
                else
                {
                    Rb_Workflow_Condition cmodel = new Rb_Workflow_Condition
                    {
                        Sort = 0,
                        TemplateId = dmodel.TemplateId
                    };
                    int cid = respositoryCondition.Insert(cmodel, trans);
                    if (cid > 0)
                    {//新增审核人 和  指定成员
                        int Num = 0;
                        if (dmodel.AuditList != null)
                            foreach (var item in dmodel.AuditList)
                            {
                                Num++;
                                Rb_Workflow_Audit auditmodel = new Rb_Workflow_Audit
                                {
                                    AuditDescription = item.AuditDescription,
                                    AuditType = item.AuditType,
                                    AuditWay = item.AuditWay,
                                    RoleOrInitiator = item.RoleOrInitiator,
                                    Sort = Num,
                                    ConditionId = cid
                                };
                                int auditId = respositoryAudit.Insert(auditmodel, trans);
                                if (item.AuditType == 2)
                                {
                                    foreach (var d in item.AuditUserList)
                                    {
                                        Rb_Workflow_Audituser aumodel = new Rb_Workflow_Audituser
                                        {
                                            AuditId = auditId,
                                            EmployeeId = d.EmployeeId,
                                            EmType = 1,
                                            Sort = d.Sort
                                        };
                                        respositoryUser.Insert(aumodel, trans);
                                    }
                                }
                            }
                        if (dmodel.AuditUserList != null)
                            foreach (var item in dmodel.AuditUserList)
                            {
                                Rb_Workflow_Audituser aumodel = new Rb_Workflow_Audituser
                                {
                                    AuditId = cid,
                                    EmployeeId = item.EmployeeId,
                                    EmType = 2,
                                    Sort = item.Sort
                                };
                                respositoryUser.Insert(aumodel, trans);
                            }

                        //同时修改分条件排序
                        if (dmodel.ConditionSortList != null)
                            foreach (var item in dmodel.ConditionSortList)
                            {
                                IDictionary<string, object> fileds = new Dictionary<string, object>
                        {
                            { nameof(Rb_Workflow_Condition.Sort), item.Sort },
                        };
                                IList<WhereHelper> whereHelpers = new List<WhereHelper>
                            {
                                new WhereHelper()
                                {
                                    FiledName = nameof(Rb_Workflow_Condition.Id),
                                    FiledValue = item.ConditionId,
                                    OperatorEnum = OperatorEnum.Equal
                                }
                            };
                                respositoryCondition.Update(fileds, whereHelpers, trans);
                            }
                    }
                }
                respository.DBSession.Commit();
            }
            catch (Exception ex)
            {
                LogHelper.Write(ex, "SetDefaultCondition");
                respository.DBSession.Rollback("SetDefaultCondition");
                return false;
            }

            return true;
        }

        /// <summary>
        /// 判断默认条件重复
        /// </summary>
        /// <param name="TemplateId"></param>
        /// <param name="ConditionId"></param>
        /// <returns></returns>
        public int GetDefaultConditionDistinct(int TemplateId, int ConditionId)
        {
            return respositoryCondition.GetDefaultConditionDistinct(TemplateId, ConditionId);
        }

        /// <summary>
        /// 获取请假分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="dmodel"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<Rb_Workflow_Askforleave_Extend> GetAskforleavePageList(int pageIndex, int pageSize, Rb_Workflow_Askforleave_Extend dmodel, out long count)
        {
            List<Rb_Workflow_Askforleave_Extend> pageList = respositoryAskforleave.GetAskforleavePageList(pageIndex, pageSize, dmodel, out count);
            return pageList;
        }
        /// <summary>
        /// 获取出差分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="dmodel"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<Rb_Workflow_Evection_Extend> GetEvectionPageList(int pageIndex, int pageSize, Rb_Workflow_Evection_Extend dmodel, out long count)
        {
            List<Rb_Workflow_Evection_Extend> pageList = respositoryEvection.GetEvectionPageList(pageIndex, pageSize, dmodel, out count);
            return pageList;
        }
        /// <summary>
        /// 获取外出分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="dmodel"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<Rb_Workflow_GoOut_Extend> GetGoOutPageList(int pageIndex, int pageSize, Rb_Workflow_GoOut_Extend dmodel, out long count)
        {
            List<Rb_Workflow_GoOut_Extend> pageList = respositoryGoout.GetGoOutPageList(pageIndex, pageSize, dmodel, out count);
            return pageList;
        }
        /// <summary>
        /// 获取补卡分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="dmodel"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<Rb_Workflow_Reissuecard_Extend> GetReissuecardPageList(int pageIndex, int pageSize, Rb_Workflow_Reissuecard_Extend dmodel, out long count)
        {
            List<Rb_Workflow_Reissuecard_Extend> pageList = respositoryReissuecar.GetReissuecardPageList(pageIndex, pageSize, dmodel, out count);
            return pageList;
        }
        /// <summary>
        /// 获取OA考勤分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="dmodel"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<Rb_Workflow_Askforleave_Extend> GetOAPageList(int pageIndex, int pageSize, Rb_Workflow_Askforleave_Extend dmodel, out long count)
        {
            List<Rb_Workflow_Askforleave_Extend> pageList = respositoryAskforleave.GetOAPageList(pageIndex, pageSize, dmodel, out count);
            return pageList;
        }
        /// <summary>
        /// 设置审核可见范围
        /// </summary>
        /// <param name="tid"></param>
        /// <param name="List"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool AddTempleteEmployee(int tid, List<Rb_Workflow_Visualrange> List)
        {
            //删除原有
            try
            {
                visualrangeRepository.DeleteVisualrange(tid);
                foreach (var item in List)
                {
                    item.TemplateId = tid;
                }
                visualrangeRepository.InsertBatch(List);
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Write(ex, "AddTempleteEmployee");
                return false;
            }
        }
        /// <summary>
        /// 根据模版id 获取可见范围的人
        /// </summary>
        /// <param name="tid"></param>
        /// <returns></returns>
        public List<Rb_Workflow_Visualrange_Extend> GetTempleteEmployee(int tid)
        {
            return visualrangeRepository.GetList(tid);
        }

        /// <summary>
        /// 获取员工列表
        /// </summary>
        /// <param name="empIds"></param>
        /// <returns></returns>
        public List<Employee_ViewModel> GetEmpList(string empIds)
        {
            return accountRepository.GetEmployeeListRepository(new Employee_ViewModel() { QIds = empIds });
        }
    }
}
