一、创建或更新包含子表的表单
二、流程事件变化后执行某些逻辑
三、发送消息提醒

一、创建或更新包含子表的表单
1、首先介绍关于在后端创建包含子表的表单

//根据ID查询当前表单数据
string sql1 = "select Warehouse1 as Warehouse1,MyProject as MyProject,Class1 as Class1,People as People,Dept as Dept," +
    "Principal as Principal from I_D000399SgProductionAcquisition where Objectid = '" + ObjectId + "'";
System.Data.DataTable table1 = this.Engine.Query.QueryTable(sql1, null);
int rowCount1 = table1.Rows.Count;

//根据ID查询当前表单子表数据
string sql2 = "select ControlCatalogue as ControlCatalogue,No as No,MaterialName as MaterialName,Type as Type,Unit1 as Unit1 " +
    "from I_D000399SgBugApplyDetails where parentobjectid = '" + ObjectId + "'";
System.Data.DataTable table2 = this.Engine.Query.QueryTable(sql2, null);
int rowCount2 = table2.Rows.Count;

List < H3.DataModel.BizObject > _list = new List<H3.DataModel.BizObject>();
for(int i = 0;i < rowCount2; i++) {
    //创建子表对象
    H3.DataModel.BizObject childBo = new H3.DataModel.BizObject(this.Request.Engine, this.Request.Engine.BizObjectManager.GetPublishedSchema("D000399SqOutbound01"), this.Request.UserContext.UserId);
    childBo["ControlCatalogue"] = table2.Rows[i]["ControlCatalogue"].ToString();
    childBo["No"] = table2.Rows[i]["No"].ToString();
    childBo["MaterialName"] = table2.Rows[i]["MaterialName"].ToString();
    childBo["Type"] = table2.Rows[i]["Type"].ToString();
    childBo["Unit1"] = table2.Rows[i]["Unit1"].ToString();
    childBo.Create();
    _list.Add(childBo);
}

//创建主表对象
H3.DataModel.BizObject bo = new H3.DataModel.BizObject(this.Engine, this.Engine.BizObjectManager.GetPublishedSchema("D000399SqOutbound"), this.Request.UserContext.UserId);
bo["WDate"] = DateTime.Today.ToString("yyyy-MM-dd");
bo["YearMonth"] = DateTime.Today.ToString("yyyyMM");
bo["Title"] = "审核批量,生产领用出库";
bo["Acquisition"] = ObjectId;
bo["No"] = No;
bo["Money"] = AllMoney;
for(int i = 0;i < rowCount1; i++) {
    bo["Warehouse1"] = table1.Rows[i]["Warehouse1"].ToString();
    bo["MyProject"] = table1.Rows[i]["MyProject"].ToString();
    bo["Class"] = table1.Rows[i]["Class1"].ToString();
    bo["People"] = table1.Rows[i]["People"].ToString();
    bo["Dept"] = table1.Rows[i]["Dept"].ToString();
    bo["Principal"] = table1.Rows[i]["Principal"].ToString();
}
bo["D000399SqOutbound01"] = _list.ToArray();
bo.Status = H3.DataModel.BizObjectStatus.Effective;
bo.Create();

上述代码是根据ID,查询某表单以及其子表数据,再将数据对应赋值给创建的表单以及创建表单的子表,这里表单或者其子表的ID都是系统自己随机生成,同时可以设置创建表单的状态,需要注意的是这里创建后的表单继续会触发它对应的业务规则。

2、再介绍关于在后端更新包含子表的表单:

//查询当前表单子表中需要冲减的数量
string sql1 = "select ControlCatalogue as ControlCatalogue,Num3 as Num3 from I_D000399SgBuyDetails" +
    " where parentobjectId = '" + BuyApply + "' and Num3 != 0";
System.Data.DataTable table1 = this.Engine.Query.QueryTable(sql2, null);
int rowCount1 = table1.Rows.Count;

H3.Data.Filter.Filter filter = new H3.Data.Filter.Filter();
H3.Data.Filter.And andMatcher = new H3.Data.Filter.And();

andMatcher.Add(new H3.Data.Filter.ItemMatcher("ObjectId", H3.Data.ComparisonOperatorType.Equal, UseAllPlan));
filter.Matcher = andMatcher;
H3.DataModel.BizObjectSchema accountSchema = this.Request.Engine.BizObjectManager.GetPublishedSchema("D000399SgUseAllPlan");
H3.DataModel.BizObject[] customers = H3.DataModel.BizObject.GetList(this.Request.Engine, this.Request.UserContext.UserId,
    accountSchema, H3.DataModel.GetListScopeType.GlobalAll, filter);
if(customers != null && customers.Length > 0)
{
    H3.DataModel.BizObject[]  details = (H3.DataModel.BizObject[]) customers[0]["D000399SgUseAllDetails"];
    List < H3.DataModel.BizObject > boList = new List<H3.DataModel.BizObject>();
    //保存原子表记录,先获取子表内容,循环将其原有数据添加到boList
    if(details != null)
    {
        foreach(H3.DataModel.BizObject dt in details)
        {
            for(int i = 0;i < rowCount1; i++) {
                string ControlCatalogue = table1.Rows[i]["ControlCatalogue"].ToString();
                string Num3 = table1.Rows[i]["Num3"].ToString();

                if(string.Equals(dt["ControlCatalogue"], ControlCatalogue))
                {
                    int temp1 = Convert.ToInt32(dt["Num4"]) - Convert.ToInt32(Num3);
                    dt["Num4"] = Convert.ToString(temp1);
                    int temp2 = Convert.ToInt32(dt["Num5"]) + Convert.ToInt32(Num3);
                    dt["Num5"] = Convert.ToString(temp2);
                }
            }
            boList.Add(dt);
        }
    }
    H3.DataModel.BizObjectSchema schema = this.Request.Engine.BizObjectManager.GetPublishedSchema("D000399SgUseAllPlan");
    H3.DataModel.BizObject schemaObject = new H3.DataModel.BizObject(this.Request.Engine, schema, this.Request.UserContext.UserId);
    schemaObject.ObjectId = UseAllPlan;  //更新的表单ID为UseAllPlan
    schemaObject.Load();
    schemaObject["D000399SgUseAllDetails"] = boList.ToArray();
    schemaObject.Update();
}

上述代码是根据ID,更新目标表单的子表数据,需要注意的是这里更新后的表单同样会触发它对应的业务规则,如果不想触发业务规则,也可采用sql语句更新的方式,如下所示。

string UpdateSql = "update I_D150876F9b6ce658d6854729aa83d8ad3afcff86 set F0000048='" + controlValue + "' where F0000027='" + objId + "'";
this.Engine.Query.QueryTable(UpdateSql, null);

二、流程事件变化后执行某些逻辑

当一个表单生效或作废后,若有业务规则首先会执行业务规则,通常有更新或新增等操作。然后再执行流程变化后事件。前提是当前表单已经开启了表单流程。代码如下:

protected override void OnWorkflowInstanceStateChanged(H3.Workflow.Instance.WorkflowInstanceState oldState, H3.Workflow.Instance.WorkflowInstanceState newState)
    {
        //流程审批结束,业务规则执行。还有个审批流程结束(finished状态下直接删除),就直接删除也会触发业务规则执行。在submit里面处理
        if(oldState == H3.Workflow.Instance.WorkflowInstanceState.Running && newState == H3.Workflow.Instance.WorkflowInstanceState.Finished)
        {
            DoAfterPass1(this.Request.BizObject.ObjectId);
        }
        //流程审批结束后,重新激活,业务规则会执行。还有个审批流程结束(finished状态下直接删除),就直接删除也会触发业务规则执行。在submit里面处理
        if(oldState == H3.Workflow.Instance.WorkflowInstanceState.Finished && newState == H3.Workflow.Instance.WorkflowInstanceState.Running)
        {
            DoAfterPass2(this.Request.BizObject.ObjectId);
        }
        base.OnWorkflowInstanceStateChanged(oldState, newState);
    }

this.Request.BizObject.ObjectId; //当前表单的ID

三、发送消息提醒

H3.Task.UserTask task = new H3.Task.UserTask();
task.ObjectId = Guid.NewGuid().ToString();
task.Sender = this.Request.UserContext.UserId; //任务的发送人ID
task.UserId = this.Request.UserContext.UserId; //任务的接收人ID
task.AppCode = ""; //应用的Code
task.TaskType = H3.Task.TaskType.Reminder; //任务类型为提醒
task.SchemaCode = ""; //表单的Code
task.TargetType = LinkTargetType.BizObject; //链接类型
task.TargetId = ""; //链接表单对象的ID
task.TargetName = ""; //链接表单对象的名称
task.Name = "这是测试提醒标题!"; //提醒标题
task.Summary = "这是测试提醒内容!"; //提醒的内容
task.AlertTime = DateTime.Now;  // 提醒时间
task.ReminderType = H3.Task.ReminderType.Once; //提醒的类型
task.StartTime = DateTime.Now;    //开始时间
task.TaskState = H3.Task.TaskState.Unfinished;  //任务的状态
task.AlertState = H3.Task.AlertState.Waiting;  //提醒的状态
this.Request.Engine.TaskManager.AddTask(task);//添加任务