需求
库表保存时,需要校验逻辑.
提交时有更深层次校验.
**状态,还有特殊校验
接口
写一个通用的校验接口,这里定义了校验时间.每个阶段校验可能需要考虑顺序,增加一个顺序字段.
public interface IOrcderCheck
{
ECheckTime CheckTime { get; }
bool Check(OrderModel orderModel, List<string> warnMsg); /// <summary>
/// 校验排序,默认制定为0即可
/// </summary>
int SortNum { get; }
}
实现类2个
public class CheckAge : IOrcderCheck
{
public ECheckTime CheckTime => ECheckTime.BeforeSign;
/// <summary>
/// 校验排序,默认制定为0即可
/// </summary>
public int SortNum { get { return ; } }
public bool Check(OrderModel orderModel, List<string> warnMsg)
{
//内部逻辑 如:年龄小于18岁,校验失败
return true;
}
}
public class CheckCaStatus : IOrcderCheck
{
/// <summary>
/// 校验排序,默认制定为0即可
/// </summary>
public int SortNum { get { return ; } }
public ECheckTime CheckTime => ECheckTime.BeforeSubmit;
DataAccess _da;
IDBContext _db;
CfcaManager _cfcaManager;
IUserInfoRepository _userInfoRepository;
public CheckCaStatus(IIndex<string, DataAccess> daArr, IIndex<string, IDBContext> dbArr,CfcaManager cfcaManager,IUserInfoRepository userInfoRepository)
{
this._da = daArr["acs"];
this._db = dbArr["acs"];
this._cfcaManager = cfcaManager;
this._userInfoRepository = userInfoRepository;
}public bool Check(OrderModel orderModel, List<string> warnMsg)
{
if (CheckDelStatus(orderModel))
{
warnMsg.Add("此申请已被删除,无法提交");
return false;
}
//判断是否电子签成功
var orderId = orderModel.OrderId;
var res= this._cfcaManager.CheckCaForSubmit(orderId, ECaCategory.Camain);
if(res.Value==)
{
return true;//电子签成功
}
if(res.WarnResult!=null)
{
warnMsg.Add(res.WarnResult.Message); //不完全电子签的,提示完成电子签
}
else
{
warnMsg.Add("请完成协议签署后再提交");
}
return warnMsg.Count == ;
} }
注册封装
扩展IServiceCollection 接口方法 AddCheckRules,注册校验类 校验入口
ValidateOrderData
传入校验实体类和校验时间
orderModel, eCheckTime
public static class OrderCheckServiceCollectionExtensions
{ public static IServiceCollection AddCheckRules(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
} services.AddSingleton<IOrcderCheck, CheckAge>();//计算年龄+贷款 > 65
services.AddSingleton<IOrcderCheck, CheckCaStatus>();//电子签名状态校验 判断删除状态 return services;
}
/// <summary>
/// 通用订单提交校验入口.
/// </summary>
/// <param name="rules"></param>
/// <param name="orderModel"></param>
/// <param name="eCheckTime"></param>
/// <returns></returns>
public static List<string> ValidateOrderData(this IEnumerable<IOrcderCheck> rules, OrderModel orderModel, ECheckTime eCheckTime)
{
List<string> warnMsg = new List<string>(); var newRules = rules.Where(r => r.CheckTime <= eCheckTime).OrderBy(o => o.SortNum);
foreach (var rule in newRules)
{ if (!rule.Check(orderModel, warnMsg))
break;
}
return warnMsg;
} }
调用
注册
services.AddCheckRules();
构造注入
IEnumerable<IOrcderCheck> _orderChecks;
public OrderCRepository(
IEnumerable<IOrcderCheck> orderChecks
)
{
this._orderChecks = orderChecks;
}
函数调用
这里直接使用函数,判断校验类错误信息.
var msg = _orderChecks.ValidateOrderData(orderModel, ECheckTime.BeforeSubmit);
if (msg.Count > )
{
return new WarnResult(string.Join('|', msg));
}