您的位置:首页 > 其它

MP之插件开发-事务

2015-10-07 19:28 316 查看
MP平台在批处理数据时,一般是使用LoopDo规则,进行循环处理。

该方法的缺点是:没有事务,使得批处理在失败时,没有回滚。

开发准备--导入dll:

LY.EAP.Workflow.Plugin.dll
LY.TEC.Data.Common.dll
LY.TEC.Data.Data2.dll
LY.TEC.Excel.dll

插件代码1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using LY.TEC.Data.Data2;
using System.Data.Common;

namespace LY.CLP.MP.Plugin.DunTask
{
/// <summary>
/// 说明:催收分配管理
/// 作者:叶晓东
/// 时间:2015-10-22
/// </summary>
public class DunTaskAllot
{
/// <summary>
/// 电话催收分配
/// </summary>
/// <param name="userID">最后更新人员ID</param>
/// <param name="alloterID">任务处理人ID</param>
/// <param name="dt">数据集,包含催收任务ID,并发ID</param>
/// <returns name="dicResult">Dictionary<string,object></returns>
public Dictionary<string, object> PhoneAllot(string userID,string alloterID,DataTable dt)
{
Dictionary<string,object> dicResult = new Dictionary<string,object>();
string msg ="成功";
Gateway gateway = Gateway.Default;
DbTransaction tran = gateway.BeginTransaction();
//CustomSqlSection custom = gateway.FromCustomSql(".");
//custom.SetTransaction(tran);
try
{
foreach(DataRow dr in dt.Rows)
{
msg = UpdateAllot(gateway, tran, userID, alloterID, dr["BU_DUN_TASK_ID"].ToString(), dr["UPDATE_CONTROL_ID"].ToString());
if(msg!="成功")
{
tran.Rollback();
dicResult.Add("msg",msg);
return dicResult;
}
}
}
catch(Exception e)
{
tran.Rollback();
dicResult.Add("msg",e.ToString());
return dicResult;
}
tran.Commit();
dicResult.Add("msg", msg);
return dicResult;
}

/// <summary>
/// 修改催收任务表
/// </summary>
/// <param name="gateway">连接数据库</param>
/// <param name="tran">事务</param>
/// <param name="userID">最后更新人员ID</param>
/// <param name="alloterID">任务处理人ID</param>
/// <param name="dunID">催收任务ID</param>
/// <param name="controlID">并发ID</param>
/// <returns></returns>
public string UpdateAllot(Gateway gateway,DbTransaction tran,string userID,string alloterID,string dunID,string controlID)
{
try
{
CustomSqlSection custom = gateway.FromCustomSql(".");
string sql = string.Format(@"UPDATE T_CLP_BU_DUN_TASK SET
STATUS='0',
MODIFIER='{0}',
LAST_UPDATED_DATE=systimestamp,
BU_DUN_TASK_MAN_ID='{1}',
ASSIGN_DATE=systimestamp
WHERE
BU_DUN_TASK_ID='{2}' AND
UPDATE_CONTROL_ID='{3}'
", userID,
alloterID,
dunID,
controlID
);
custom.Sql = sql;
custom.SetTransaction(tran);
if (custom.ExecuteNonQuery() > 0)
return "成功";
else
return "失败";
}
catch (Exception e)
{
return e.ToString();
}
}
}
}


步骤:

1.连接数据库;
2.创建事务;
3.生成sql;
4.添加事务;
5.执行;
6.提交/回滚。

代码分析:

连接数据库
Gateway gateway = Gateway.Default;

default表示使用默认的连接方式,这里的默认是MP配置配置文件的连接。
单元测试用:
Gateway gateway = new Gateway(DatabaseType.Oracle, "user id=zuchekf;Data Source=KFYCXMS;Password=zuchekf;Persist Security Info=True");


初始化SQL语句
CustomSqlSection custom = gateway.FromCustomSql(".");
其中“."是一个SQL语句,由于同事们都这样使用,在这里本人跟风了一把。
其实这样使用会导致后面需要有:
custom.Sql = sql;


添加事务
custom.SetTransaction(tran);

由于UpdateAllot方法中的执行都是使用同一个事务,而且这个事务是由PhoneAllot方法传递过来的,
所以可以在PhoneAllot方法中做统一的提交和回滚。

执行SQL
custom.ExecuteNonQuery()
返回影响行数。

感谢

感谢,同事李磊的指导。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: