public class SaveValidator : AbstractValidator
{
public override void Validate(ExtendedDataEntity[] dataEntities, ValidateContext validateContext, Kingdee.BOS.Context ctx)
{
if (dataEntities == null || dataEntities.Length == 0)
{
return;
}
Dictionary<long, decimal> dictErrMaterialId = new Dictionary<long, decimal>();
//取所有物料
List<long> listMaterialId = new List<long>();
foreach (ExtendedDataEntity entityObj in dataEntities)
{
DynamicObjectCollection collection = (DynamicObjectCollection)entityObj["FEntity"];
foreach (DynamicObject rowObj in collection)
{
listMaterialId.Add((long)rowObj["FBase_Id"]);
}
}
string sql = " select a.FMATERIALID, sum(a.FBASEQTY) FQTY from T_STK_INVENTORY a where exists (select 1 from TABLE(fn_StrSplit(@FMATERIALID, ',',1)) t where t.FID = a. FMATERIALID) group by FMATERIALID ";
SqlParam param = new SqlParam("@FMATERIALID", KDDbType.udt_inttable, listMaterialId.Distinct().ToArray());
using (IDataReader dr = DBUtils.ExecuteReader(this.Context, sql, param))
{
while (dr.Read())
{
decimal qty = Convert.ToDecimal(dr["FQTY"]);
if (qty > 100)
{
dictErrMaterialId.Add(Convert.ToInt64(dr["FMATERIALID"]), qty);
}
}
}
foreach (ExtendedDataEntity entityObj in dataEntities)
{
DynamicObjectCollection collection = (DynamicObjectCollection)entityObj["FEntity"];
foreach (DynamicObject rowObj in collection)
{
if (dictErrMaterialId.ContainsKey((long)rowObj["FBase_Id"]))
{
ValidationErrorInfo errinfo = new ValidationErrorInfo("FMATERIALID", Convert.ToString(entityObj.DataEntity["Id"]),
entityObj.DataEntityIndex, Convert.ToInt32(rowObj["Id"]), "SaveValidator", "库存数量大于100", "校验失败", ErrorLevel.Error);
validateContext.AddError(entityObj, errinfo);
}
}
}
}
}
|