根据日期等生成流水码
/// <summary>
/// 设置生成记录数,在指定表结构下记录当天生成记录总数
/// 相应表结构应包含打印日期,打印记录数两个字段
/// 需要批处理
/// </summary>
/// <param name="args">
/// string : 表名
/// int : 新增记录数
/// </param>
/// <returns>
/// rtn>=0时设置成功
/// output:int表示最新记录总数
/// </returns>
private static SqlProcessRes SetRcdDateCountEx(SqlCommand sqlCmd, object args)
{
//@1044
string tableName = (string)(args as object[])[0];
int qty = (int)(args as object[])[1];
Hashtable htLimitCond = (Hashtable)(args as object[])[2];
int? qtyFirst = null;
if ((args as object[]).Length > 3
&& (args as object[])[3] != null
)
{
qtyFirst = (int)(args as object[])[3];
}
//@1099
bool isReturnRcdID = false;
if ((args as object[]).Length > 4)
{
isReturnRcdID = (bool)(args as object[])[4];
}
List<string> listCountLimit = null;
if ((args as object[]).Length > 5)
{
listCountLimit = (List<string>)(args as object[])[5];
}
if (listCountLimit == null)//若为null,默认为全部限制条件
{
listCountLimit = new List<string>();
foreach (string key in htLimitCond.Keys)
{
listCountLimit.Add(key);
}
}
string limitCmd = "";
foreach (string key in htLimitCond.Keys)
{
limitCmd += " and " + key + "=@" + key;
}
//更新计数器
string strCmd = "update @tablename set"
+ " count=count + @qty"
+ " where 1=1"
+ limitCmd;
strCmd = strCmd.Replace("@tablename", tableName);
SetCmdTxt(sqlCmd, strCmd);
int qtyIncrement = qty;
sqlCmd.Parameters.AddWithValue("@qty", qtyIncrement);
foreach (string key in htLimitCond.Keys)
{
sqlCmd.Parameters.AddWithValue("@" + key, htLimitCond[key]);
}
int rtn = sqlCmd.ExecuteNonQuery();
if (rtn > 1)
return new SqlProcessRes(-1, null, "存在重复流水");
if (rtn == 0)
{
string cmdLimitPara = "";
foreach (string key in htLimitCond.Keys)
{
cmdLimitPara += ", " + key;
}
//生成计数器
strCmd = "insert into @tablename"
+ " (count" + cmdLimitPara + ")"
+ " values(@count" + cmdLimitPara.Replace(", ", ",@") + ")";
strCmd = strCmd.Replace("@tablename", tableName);
SetCmdTxt(sqlCmd, strCmd);
qtyIncrement = qtyFirst ?? qty;
sqlCmd.Parameters.AddWithValue("count", qtyIncrement);
if (htLimitCond != null)
{
foreach (string key in htLimitCond.Keys)
{
sqlCmd.Parameters.AddWithValue("@" + key, htLimitCond[key]);
}
}
sqlCmd.ExecuteNonQuery();
}
SqlDataReader dr;
int idRcd = 0;
if (isReturnRcdID)
{
strCmd = "select id, count from @tablename where 1=1" + limitCmd;
strCmd = strCmd.Replace("@tablename", tableName);
SetCmdTxt(sqlCmd, strCmd);
//sqlCmd.Parameters.AddWithValue("@printDate", printdate);
foreach (string key in htLimitCond.Keys)
{
sqlCmd.Parameters.AddWithValue("@" + key, htLimitCond[key]);
}
dr = sqlCmd.ExecuteReader();
if (dr.Read())
{
//count = ConvertToInt(dr["count"]);
idRcd = ConvertToInt(dr["id"]);
}
dr.Close();
}
int count = -1;
strCmd = "select sum(count) as count from @tablename where 1=1";
strCmd = strCmd.Replace("@tablename", tableName);
foreach (string key in listCountLimit)
{
strCmd += string.Format(" and {0}=@{0}", key);
}
sqlCmd.ResetCmdTxt(strCmd);
foreach (string key in listCountLimit)
{
sqlCmd.Parameters.AddWithValue("@" + key, htLimitCond[key]);
}
dr = sqlCmd.ExecuteReader();
if (dr.Read())
{
count = ConvertToInt(dr["count"]);
}
dr.Close();
return new SqlProcessRes(qtyIncrement, new object[] { count, idRcd });
}