using System;
using System.Collections.Generic;
using HcUtility.Comm;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Data.Common;
namespace HcUtility.Core
{
///
/// 功能: 单据保存的类
/// 创建: 吴伟 2009-05-08
///
///
public class ModelObjectDBBill : ModelObjectDB
{
protected override DBResult BeforeSaveHandler(List dataList, Database db,
IDbConnection idbConn, IDbTransaction idbTran, ref List listCmd)
{
DBResult result = new DBResult();
result.Success = true;
result.Message = "";
try
{
foreach (ModelObjectBase modeldata in dataList)
{
ModelObjectBill mobBill = (ModelObjectBill)modeldata;
if (mobBill.DbOperationType == DbOperationType.DbotIns)
{
string headBillno = mobBill.GetBillNoValue().ToString();
if (headBillno == String.Empty || headBillno == "*")
{
headBillno = mobBill.GetNewBillNo(db);
mobBill.SetPropertyValue(mobBill.GetBillNoFieldName(),headBillno);
if (headBillno == "" || headBillno == "*")
throw new ModelDbException("取得单据号出现错误!");
}
}
foreach (List moblist in modeldata.BodyList)
{
result = SaveBodyListBillNo(moblist, mobBill.GetBillNoValue().ToString());
if (!result.Success)
{
throw new ModelDbException(result.Message);
}
}
//当为修改时判断时间戳
if (mobBill.DbOperationType == DbOperationType.DbotUpd)
{
result = mobBill.CheckTimeMark(db,idbConn,idbTran,ref listCmd);
if (!result.Success)
break;
}
}
}
catch(Exception e)
{
result.Success = false;
if (!(e is ModelDbException))
result.Message = e.ToString();
}
return result;
}
protected override DBResult AfterSaveHandler(List dataList, Database db,
IDbConnection idbConn, IDbTransaction idbTran, ref List listCmd)
{
DBResult result = new DBResult();
result.Success = true;
List listBillNo = new List();
try
{
foreach (ModelObjectBase mob in dataList)
{
ModelObjectBill mobBill = (ModelObjectBill)mob;
string billno = mobBill.GetBillNoValue().ToString();
if (listBillNo.Contains(billno))
continue;
else
listBillNo.Add(billno);
result = mobBill.AfterSaveHandler(db,idbConn,idbTran,ref listCmd);
if(!result.Success)
throw new ModelDbException(result.Message);
}
}
catch(Exception e)
{
result.Success = false;
if (!(e is ModelDbException))
result.Message = e.ToString();
}
return result;
}
private DBResult SaveBodyListBillNo(List moblist, string billNo) {
DBResult result = new DBResult();
result.Success = true;
foreach (ModelObjectBase mod in moblist) {
result = SaveBodyBillNo(mod, billNo);
if (!result.Success)
break;
}
return result;
}
private DBResult SaveBodyBillNo(ModelObjectBase modeldata, string billNo) {
DBResult result = new DBResult();
result.Success = true;
ModelObjectBill modelBill = (ModelObjectBill)modeldata;
if (modelBill.DbOperationType == DbOperationType.DbotIns)
modelBill.SetPropertyValue(modelBill.GetBillNoFieldName(), billNo);
foreach (List moblist in modeldata.BodyList) {
result = SaveBodyListBillNo(moblist, billNo);
if (!result.Success)
break;
}
return result;
}
private string GetNewBillNo(Database db, string ywType)
{
string billNo = String.Empty;
using (DbCommand cmd = db.GetStoredProcCommand("sSysGetBillNo"))
{
db.AddInParameter(cmd, "ps_BillType", DbType.String, ywType);
db.AddOutParameter(cmd, "ps_BillNo", DbType.String, 20);
db.ExecuteNonQuery(cmd);
billNo = Convert.ToString(db.GetParameterValue(cmd, "ps_BillNo"));
}
return billNo;
}
}
}