/// <summary>
 /// 更新实体
 /// </summary>
 /// <param name="BaseWorkFlowStepEntity">实体</param>
 public int UpdateEntity(BaseWorkFlowStepEntity BaseWorkFlowStepEntity)
 {
     SQLBuilder sqlBuilder = new SQLBuilder(DbHelper);
     sqlBuilder.BeginUpdate(this.CurrentTableName);
     this.SetEntity(sqlBuilder, BaseWorkFlowStepEntity);
     if (UserInfo != null)
     {
         sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldModifiedUserId, UserInfo.Id);
         sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldModifiedBy, UserInfo.RealName);
     }
     sqlBuilder.SetDBNow(BaseWorkFlowStepEntity.FieldModifiedOn);
     sqlBuilder.SetWhere(BaseWorkFlowStepEntity.FieldId, BaseWorkFlowStepEntity.Id);
     return sqlBuilder.EndUpdate();
 }
 /// <summary>
 /// 设置实体
 /// </summary>
 /// <param name="BaseWorkFlowStepEntity">实体</param>
 private void SetEntity(SQLBuilder sqlBuilder, BaseWorkFlowStepEntity BaseWorkFlowStepEntity)
 {
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldCategoryCode, BaseWorkFlowStepEntity.CategoryCode);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldObjectId, BaseWorkFlowStepEntity.ObjectId);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldWorkFlowId, BaseWorkFlowStepEntity.WorkFlowId);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldActivityId, BaseWorkFlowStepEntity.ActivityId);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldCode, BaseWorkFlowStepEntity.Code);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldFullName, BaseWorkFlowStepEntity.FullName);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldAuditDepartmentId, BaseWorkFlowStepEntity.AuditDepartmentId);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldAuditDepartmentName, BaseWorkFlowStepEntity.AuditDepartmentName);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldAuditUserId, BaseWorkFlowStepEntity.AuditUserId);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldAuditUserCode, BaseWorkFlowStepEntity.AuditUserCode);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldAuditUserRealName, BaseWorkFlowStepEntity.AuditUserRealName);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldAuditRoleId, BaseWorkFlowStepEntity.AuditRoleId);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldAuditRoleRealName, BaseWorkFlowStepEntity.AuditRoleRealName);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldActivityType, BaseWorkFlowStepEntity.ActivityType);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldAllowPrint, BaseWorkFlowStepEntity.AllowPrint);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldAllowEditDocuments, BaseWorkFlowStepEntity.AllowEditDocuments);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldSortCode, BaseWorkFlowStepEntity.SortCode);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldEnabled, BaseWorkFlowStepEntity.Enabled);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldDeletionStateCode, BaseWorkFlowStepEntity.DeletionStateCode);
     sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldDescription, BaseWorkFlowStepEntity.Description);
 }
 /// <summary>
 /// 获取实体
 /// </summary>
 /// <param name="id">主键</param>
 public BaseWorkFlowStepEntity GetEntity(string id)
 {
     BaseWorkFlowStepEntity BaseWorkFlowStepEntity = new BaseWorkFlowStepEntity(this.GetDataTable(new KeyValuePair<string, object>(BaseWorkFlowStepEntity.FieldId, id)));
     return BaseWorkFlowStepEntity;
 }
 /// <summary>
 /// 更新
 /// </summary>
 /// <param name="BaseWorkFlowStepEntity">实体</param>
 public int Update(BaseWorkFlowStepEntity BaseWorkFlowStepEntity)
 {
     return this.UpdateEntity(BaseWorkFlowStepEntity);
 }
 /// <summary>
 /// 添加
 /// </summary>
 /// <param name="BaseWorkFlowStepEntity">实体</param>
 /// <param name="identity">自增量方式</param>
 /// <param name="returnId">返回主鍵</param>
 /// <returns>主键</returns>
 public string Add(BaseWorkFlowStepEntity BaseWorkFlowStepEntity, bool identity, bool returnId)
 {
     this.Identity = identity;
     this.ReturnId = returnId;
     return this.AddEntity(BaseWorkFlowStepEntity);
 }
 /// <summary>
 /// 添加实体
 /// </summary>
 /// <param name="workFlowActivityEntity">实体</param>
 public string AddEntity(BaseWorkFlowStepEntity workFlowActivityEntity)
 {
     string sequence = string.Empty;
     if (workFlowActivityEntity.SortCode == null || workFlowActivityEntity.SortCode == 0)
     {
         BaseSequenceManager sequenceManager = new BaseSequenceManager(DbHelper, this.Identity);
         sequence = sequenceManager.GetSequence(this.CurrentTableName);
         workFlowActivityEntity.SortCode = int.Parse(sequence);
     }
     SQLBuilder sqlBuilder = new SQLBuilder(DbHelper, this.Identity, this.ReturnId);
     sqlBuilder.BeginInsert(this.CurrentTableName, BaseWorkFlowStepEntity.FieldId);
     if (!this.Identity)
     {
         sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldId, workFlowActivityEntity.Id);
     }
     else
     {
         if (!this.ReturnId && (DbHelper.CurrentDbType == DbTypes.Oracle || DbHelper.CurrentDbType == DbTypes.DB2))
         {
             if (DbHelper.CurrentDbType == DbTypes.Oracle)
             {
                 sqlBuilder.SetFormula(BaseWorkFlowStepEntity.FieldId, "SEQ_" + this.CurrentTableName.ToUpper() + ".NEXTVAL ");
             }
             if (DbHelper.CurrentDbType == DbTypes.DB2)
             {
                 sqlBuilder.SetFormula(BaseWorkFlowStepEntity.FieldId, "NEXT VALUE FOR SEQ_" + this.CurrentTableName.ToUpper());
             }
         }
         else
         {
             if (this.Identity && (DbHelper.CurrentDbType == DbTypes.Oracle || DbHelper.CurrentDbType == DbTypes.DB2))
             {
                 if (workFlowActivityEntity.Id == null)
                 {
                     if (string.IsNullOrEmpty(sequence))
                     {
                         BaseSequenceManager sequenceManager = new BaseSequenceManager(DbHelper, this.Identity);
                         sequence = sequenceManager.GetSequence(this.CurrentTableName);
                     }
                     workFlowActivityEntity.Id = int.Parse(sequence);
                 }
                 sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldId, workFlowActivityEntity.Id);
             }
         }
     }
     this.SetEntity(sqlBuilder, workFlowActivityEntity);
     if (UserInfo != null)
     {
         sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldCreateUserId, UserInfo.Id);
         sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldCreateBy, UserInfo.RealName);
     }
     sqlBuilder.SetDBNow(BaseWorkFlowStepEntity.FieldCreateOn);
     if (UserInfo != null)
     {
         sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldModifiedUserId, UserInfo.Id);
         sqlBuilder.SetValue(BaseWorkFlowStepEntity.FieldModifiedBy, UserInfo.RealName);
     }
     sqlBuilder.SetDBNow(BaseWorkFlowStepEntity.FieldModifiedOn);
     if (DbHelper.CurrentDbType == DbTypes.SqlServer && this.Identity)
     {
         sequence = sqlBuilder.EndInsert().ToString();
     }
     else
     {
         sqlBuilder.EndInsert();
     }
     return sequence;
 }
 /// <summary>
 /// 添加
 /// </summary>
 /// <param name="BaseWorkFlowStepEntity">实体</param>
 /// <returns>主键</returns>
 public string Add(BaseWorkFlowStepEntity BaseWorkFlowStepEntity)
 {
     return this.AddEntity(BaseWorkFlowStepEntity);
 }
 //-----------------------------------------------------
 //                  启动工作流 自由流
 //-----------------------------------------------------
 /// <summary>
 /// 启动工作流(自由流转)
 /// </summary>
 /// <param name="workFlowManager"></param>
 /// <param name="objectId"></param>
 /// <param name="objectFullName"></param>
 /// <param name="categoryCode"></param>
 /// <param name="categoryFullName"></param>
 /// <param name="workFlowCode"></param>
 /// <param name="toUserId"></param>
 /// <param name="toDepartmentId"></param>
 /// <param name="toRoleId"></param>
 /// <param name="auditIdea"></param>
 /// <returns></returns>
 public string FreeStart(IWorkFlowManager workFlowManager, string objectId, string objectFullName, string categoryCode, string categoryFullName, string workFlowCode, string toUserId, string toDepartmentId = null, string toRoleId = null, string auditIdea = null)
 {
     // 获取当前工作流步骤
     string currentId = this.GetCurrentId(categoryCode, objectId);
     // 定义步骤实体
     BaseWorkFlowStepEntity workFlowStepEntity = new BaseWorkFlowStepEntity();
     // FreeWorkFlow 自由工作流
     workFlowStepEntity.WorkFlowId = 0;
     // 获取排序码
     workFlowStepEntity.SortCode = int.Parse(new BaseSequenceManager().GetSequence(workFlowCode, 10000000));
     // 是否提交给用户审批
     if (!string.IsNullOrEmpty(toUserId))
     {
         workFlowStepEntity.AuditUserId = toUserId;
         workFlowStepEntity.AuditUserRealName = new BaseUserManager().GetEntity(toUserId).RealName;
     }
     // 是否提交给部门审批
     if (!string.IsNullOrEmpty(toDepartmentId))
     {
         workFlowStepEntity.AuditDepartmentId = toDepartmentId;
         workFlowStepEntity.AuditDepartmentName = new BaseOrganizeManager().GetEntity(toDepartmentId).FullName;
     }
     // 是否提交给角色审批
     if (!string.IsNullOrEmpty(toRoleId))
     {
         workFlowStepEntity.AuditRoleId = toRoleId;
         workFlowStepEntity.AuditRoleRealName = new BaseRoleManager().GetEntity(toRoleId).RealName;
     }
     if (workFlowManager != null)
     {
         workFlowManager.BeforeAutoStatr(objectId);
     }
     // 如果已经存在
     if (currentId.Length > 0)
     {
         // 获取当前处于什么状态
         string auditstatus = this.GetProperty(currentId, BaseWorkFlowCurrentEntity.FieldAuditStatus);
         // 如果还是开始审批状态的话,允许他再次提交把原来的覆盖掉
         if (auditstatus == AuditStatus.StartAudit.ToString()
             || auditstatus == AuditStatus.AuditReject.ToString())
         {
             // 更新
             this.UpdataAuditStatr(currentId, categoryCode, categoryFullName, objectId, objectFullName, auditIdea, workFlowStepEntity);
         }
         // 不是的话则返回
         else
         {
             // 该单据已进入审核状态不能在次提交
             this.ReturnStatusCode = StatusCode.ErrorChanged.ToString();
             // 返回为空可以判断
             currentId = null;
             return currentId;
         }
     }
     else
     {
         // 新增
         currentId = this.StepAuditStatr(categoryCode, categoryFullName, objectId, objectFullName, auditIdea, workFlowStepEntity);
     }
     BaseWorkFlowCurrentEntity workFlowCurrentEntity = this.GetEntity(currentId);
     // 发送提醒信息,若发给指定的某个人了,就不发给部门的提示信息了
     if (workFlowManager != null)
     {
         if (!string.IsNullOrEmpty(workFlowStepEntity.AuditUserId))
         {
             workFlowStepEntity.AuditDepartmentId = null;
             workFlowStepEntity.AuditRoleId = null;
         }
         workFlowManager.SendRemindMessage(workFlowCurrentEntity, AuditStatus.StartAudit, new string[] { workFlowCurrentEntity.CreateUserId, workFlowStepEntity.AuditUserId }, workFlowStepEntity.AuditDepartmentId, workFlowStepEntity.AuditRoleId);
     }
     // 成功工作流后的处理
     if (workFlowManager != null && !string.IsNullOrEmpty(objectId))
     {
         workFlowManager.AfterAutoStatr(objectId);
     }
     // 运行成功
     this.ReturnStatusCode = StatusCode.OK.ToString();
     this.ReturnStatusMessage = this.GetStateMessage(this.ReturnStatusCode);
     return currentId;
 }
        //-----------------------------------------------------
        //                  启动工作流 步骤流
        //-----------------------------------------------------
        /// <summary>
        /// 启动工作流(步骤流转)
        /// </summary>
        /// <param name="workFlowManager">审批流程管理器</param>
        /// <param name="objectId">单据主键</param>
        /// <param name="objectFullName">单据名称</param>
        /// <param name="categoryCode">单据分类</param>
        /// <param name="categoryFullName">单据分类名称</param>
        /// <param name="workFlowCode">工作流程</param>
        /// <param name="auditIdea">审批意见</param>
        /// <param name="dtWorkFlowActivity">需要走的流程</param>
        /// <returns>主键</returns>
        public string AutoStatr(IWorkFlowManager workFlowManager, string objectId, string objectFullName, string categoryCode, string categoryFullName = null, string workFlowCode = null, string auditIdea = null, DataTable dtWorkFlowActivity = null)
        {
            string currentId = string.Empty;
            if (dtWorkFlowActivity == null || dtWorkFlowActivity.Rows.Count == 0)
            {
                return currentId;
            }
            lock (WorkFlowCurrentLock)
            {
                BaseWorkFlowStepEntity workFlowStepEntity = null;
                // 这里需要读取一下
                if (dtWorkFlowActivity == null)
                {

                }
                workFlowStepEntity = new BaseWorkFlowStepEntity(dtWorkFlowActivity.Rows[0]);
                if (!string.IsNullOrEmpty(workFlowStepEntity.AuditUserId))
                {
                    // 若是任意人可以审核的,需要进行一次人工选任的工作
                    if (workFlowStepEntity.AuditUserId.Equals("Anyone"))
                    {
                        return null;
                    }
                }
                // 1. 先把已有的流程设置功能都删除掉
                BaseWorkFlowStepManager workFlowStepManager = new BaseWorkFlowStepManager(this.DbHelper, this.UserInfo);
                workFlowStepManager.Delete(
                    new KeyValuePair<string, object>(BaseWorkFlowStepEntity.FieldCategoryCode, categoryCode)
                    , new KeyValuePair<string, object>(BaseWorkFlowStepEntity.FieldObjectId, objectId));
                // 2. 把当前的流程设置保存好
                foreach (DataRow dataRow in dtWorkFlowActivity.Rows)
                {
                    workFlowStepEntity = new BaseWorkFlowStepEntity(dataRow);
                    workFlowStepEntity.ActivityId = workFlowStepEntity.Id;
                    workFlowStepEntity.CategoryCode = categoryCode;
                    workFlowStepEntity.ObjectId = objectId;
                    workFlowStepEntity.Id = null;
                    workFlowStepManager.Add(workFlowStepEntity);
                }
                workFlowStepEntity = new BaseWorkFlowStepEntity(dtWorkFlowActivity.Rows[0]);

                // 3. 启动审核流程
                currentId = this.GetCurrentId(categoryCode, objectId);
                BaseWorkFlowCurrentEntity workFlowCurrentEntity = null;
                if (currentId.Length > 0)
                {
                    // 获取当前处于什么状态
                    string auditstatus = this.GetProperty(currentId, BaseWorkFlowCurrentEntity.FieldAuditStatus);
                    // 如果还是开始审批状态的话,允许他再次提交把原来的覆盖掉
                    if (auditstatus == AuditStatus.StartAudit.ToString())
                    {
                        this.UpdataAuditStatr(currentId, categoryCode, categoryFullName, objectId, objectFullName, auditIdea, workFlowStepEntity);
                    }
                    // 不是的话则返回
                    else
                    {
                        // 该单据已进入审核状态不能在次提交
                        this.ReturnStatusCode = StatusCode.ErrorChanged.ToString();
                        // 返回为空可以判断
                        currentId = null;
                    }
                }
                else
                {
                    workFlowManager.BeforeAutoStatr(objectId);
                    currentId = this.StepAuditStatr(categoryCode, categoryFullName, objectId, objectFullName, auditIdea, workFlowStepEntity);
                    workFlowCurrentEntity = this.GetEntity(currentId);
                    // 发送提醒信息,若发给指定的某个人了,就不发给部门的提示信息了
                    if (workFlowManager != null)
                    {
                        if (!string.IsNullOrEmpty(workFlowStepEntity.AuditUserId))
                        {
                            workFlowStepEntity.AuditDepartmentId = null;
                            workFlowStepEntity.AuditRoleId = null;
                        }
                        workFlowManager.SendRemindMessage(workFlowCurrentEntity, AuditStatus.StartAudit, new string[] { workFlowCurrentEntity.CreateUserId, workFlowStepEntity.AuditUserId }, workFlowStepEntity.AuditDepartmentId, workFlowStepEntity.AuditRoleId);
                    }
                    // 成功工作流后的处理
                    if (!string.IsNullOrEmpty(objectId))
                    {
                        workFlowManager.AfterAutoStatr(objectId);
                    }
                    // 运行成功
                    this.ReturnStatusCode = StatusCode.OK.ToString();
                    this.ReturnStatusMessage = this.GetStateMessage(this.ReturnStatusCode);
                }
            }
            return currentId;
        }
        private int UpdataAuditStatr(string id, string categoryCode, string categoryFullName, string objectId, string objectFullName, string auditIdea, BaseWorkFlowStepEntity workFlowStepEntity)
        {
            BaseWorkFlowCurrentEntity workFlowCurrentEntity = this.GetEntity(id);
            workFlowCurrentEntity.CategoryCode = categoryCode;
            workFlowCurrentEntity.CategoryFullName = categoryFullName;
            workFlowCurrentEntity.ObjectId = objectId;
            workFlowCurrentEntity.ObjectFullName = objectFullName;
            workFlowCurrentEntity.WorkFlowId = workFlowStepEntity.WorkFlowId;
            workFlowCurrentEntity.ActivityId = workFlowStepEntity.Id;
            workFlowCurrentEntity.SendDate = DateTime.Now;
            workFlowCurrentEntity.AuditDate = DateTime.Now;
            workFlowCurrentEntity.AuditStatus = AuditStatus.StartAudit.ToString();
            workFlowCurrentEntity.AuditStatusName = AuditStatus.StartAudit.ToDescription();

            // 是否提交给组织机构审批
            if (!string.IsNullOrEmpty(workFlowStepEntity.AuditDepartmentId))
            {
                workFlowCurrentEntity.ToDepartmentId = workFlowStepEntity.AuditDepartmentId;
                workFlowCurrentEntity.ToDepartmentName = workFlowStepEntity.AuditDepartmentName;
            }
            // 是否提交给角色审批
            if (!string.IsNullOrEmpty(workFlowStepEntity.AuditRoleId))
            {
                workFlowCurrentEntity.ToDepartmentId = workFlowStepEntity.AuditRoleId;
                workFlowCurrentEntity.ToDepartmentName = workFlowStepEntity.AuditRoleRealName;
            }
            // 是否提交给用户审批
            if (!string.IsNullOrEmpty(workFlowStepEntity.AuditUserId))
            {
                BaseUserManager userManager = new BaseUserManager(UserInfo);
                BaseUserEntity userEntity = userManager.GetEntity(workFlowStepEntity.AuditUserId);
                workFlowCurrentEntity.ToUserId = workFlowStepEntity.AuditUserId;
                workFlowCurrentEntity.ToUserRealName = userEntity.RealName;
                // TODO 用户的部门信息需要处理
                if (!string.IsNullOrEmpty(userEntity.DepartmentId))
                {
                    BaseOrganizeManager organizeManager = new BaseOrganizeManager(UserInfo);
                    BaseOrganizeEntity organizeEntity = organizeManager.GetEntity(userEntity.DepartmentId);
                    workFlowCurrentEntity.ToDepartmentId = userEntity.DepartmentId;
                    workFlowCurrentEntity.ToDepartmentName = organizeEntity.FullName;
                }
            }
            // 当前审核人的信息写入当前工作流
            // workFlowCurrentEntity.AuditUserId = this.UserInfo.Id;
            // workFlowCurrentEntity.AuditUserCode = this.UserInfo.Code;
            // workFlowCurrentEntity.AuditUserRealName = this.UserInfo.RealName;
            workFlowCurrentEntity.AuditIdea = auditIdea;
            workFlowCurrentEntity.AuditDate = DateTime.Now;
            return this.UpdateEntity(workFlowCurrentEntity);
        }
 private string StepAuditStatr(string categoryCode, string categoryFullName, string objectId, string objectFullName, string auditIdea, BaseWorkFlowStepEntity workFlowStepEntity)
 {
     BaseWorkFlowCurrentEntity workFlowCurrentEntity = new BaseWorkFlowCurrentEntity();
     // 1.这个是工作流的第一个发出日期,需要全局保留,其实也是创建日期了,但是有重新审核的这个说法,若有2次重新审核的事项,的确需要保留这个字段
     // workFlowCurrentEntity.CallBack = workFlowManager.GetType().ToString();
     workFlowCurrentEntity.WorkFlowId = workFlowStepEntity.WorkFlowId;
     workFlowCurrentEntity.ActivityId = workFlowStepEntity.Id;
     // 这里是为了优化越级审核用的,按排序嘛进行先后顺序排序
     BaseWorkFlowActivityManager workFlowActivityManager = new BaseWorkFlowActivityManager(this.DbHelper, this.UserInfo);
     workFlowCurrentEntity.CategoryCode = categoryCode;
     workFlowCurrentEntity.CategoryFullName = categoryFullName;
     workFlowCurrentEntity.ObjectId = objectId;
     workFlowCurrentEntity.ObjectFullName = objectFullName;
     // 2.当前审核人的信息写入当前工作流
     // workFlowCurrentEntity.AuditUserId = this.UserInfo.Id;
     // workFlowCurrentEntity.AuditUserCode = this.UserInfo.Code;
     // workFlowCurrentEntity.AuditUserRealName = this.UserInfo.RealName;
     workFlowCurrentEntity.AuditIdea = auditIdea;
     workFlowCurrentEntity.AuditStatus = AuditStatus.StartAudit.ToString();
     workFlowCurrentEntity.AuditStatusName = AuditStatus.StartAudit.ToDescription();
     workFlowCurrentEntity.SendDate = DateTime.Now;
     // 3.接下来需要待审核的对象的信息
     workFlowCurrentEntity.ToUserId = workFlowStepEntity.AuditUserId;
     workFlowCurrentEntity.ToUserRealName = workFlowStepEntity.AuditUserRealName;
     workFlowCurrentEntity.ToDepartmentId = workFlowStepEntity.AuditDepartmentId;
     workFlowCurrentEntity.ToDepartmentName = workFlowStepEntity.AuditDepartmentName;
     workFlowCurrentEntity.ToRoleId = workFlowStepEntity.AuditRoleId;
     workFlowCurrentEntity.ToRoleRealName = workFlowStepEntity.AuditRoleRealName;
     // 4.这些标志信息需要处理好,这里表示工作流还没完成生效,还在审批中的意思。
     workFlowCurrentEntity.SortCode = workFlowStepEntity.SortCode;
     workFlowCurrentEntity.Enabled = 0;
     workFlowCurrentEntity.DeletionStateCode = 0;
     return this.AddEntity(workFlowCurrentEntity);
 }