前段时间阅读Edison Zhou写的文章:https://www.cnblogs.com/edisonchou/p/7260758.html;
于是想到自己的工作中LogicHandler中的OrderLogicHandler逻辑处理类中代码太长,长达1000行,不方便阅读;主要是因为针对增员减员订单分开处理,单处理的流程都是一样的,只是各自的内容不一样,所以为了阅读代码便利,进行代码重整,采用模板方法模式对此进行了整理。
处理流程是:验证申报的人员资料----》得到符合要求的订单----》生成订单----》保存。
/// <summary>
/// 商保申报减员
/// </summary>
/// <param name="orderDto"></param>
/// <param name="successList"></param>
/// <param name="failList"></param>
/// <returns></returns>
public object SaveCutInsurance(OrderCutApiDto orderDto, out List<string> successList, out Dictionary<string, string> failList)
{
successList = new List<string>();
failList = new Dictionary<string, string>();
OrderApiHelper<OrderCutApiDto> orderApiHelper = new OrderApiCutHelper(_orderDetailRep, _policyRep, _publicLogic, _mapper, _packageRep, _v_CustomerInfoRep);//模板类具体使用
orderApiHelper.ValidApply(orderDto, out successList, out failList);
if (successList.Count() > 0)
{
OrderEntity order = orderApiHelper.GetOrder(orderDto, successList);
_orderRep.Save(order);
}
return new
{
Success = successList,
Fail = failList
};
}
/// <summary>
/// 商保申报新增
/// </summary>
/// <param name="orderDto"></param>
/// <param name="successList"></param>
/// <param name="failList"></param>
/// <param name="msg">返回的验证信息</param>
/// <returns></returns>
public object SaveAddInsurance(OrderAddApiDto orderDto, out List<string> successList, out Dictionary<string, string> failList)
{
successList = new List<string>();
failList = new Dictionary<string, string>();
OrderApiHelper<OrderAddApiDto> orderApiHelper = new OrderApiAddHelper(_orderDetailRep,_policyRep,_publicLogic,_mapper,_packageRep,_v_CustomerInfoRep);//模板类具体使用
orderApiHelper.ValidApply(orderDto,out successList,out failList);
if (successList.Count() > 0)
{
OrderEntity order = orderApiHelper.GetOrder(orderDto, successList);
_orderRep.Save(order);
}
return new
{
Success = successList,
Fail = failList
};
}
模板类:OrderApiHelper,继承的子类中都会用到的共同方法可以写在父类中,且要声明类型为protected,这样子类OrderApiCutHelper中可以在重写的方法中直接调用。如HasPackage(),IsIninsurance()等方法
1 public abstract class OrderApiHelper<T>
2 {
3 protected IQueryable<V_CustomerInfo> customerList;
4 protected IPackageRepository _packageRep;
5 protected IV_CustomerInfoRepository _v_CustomerInfoRep;
6 protected IOrderDetailRepository _orderDetailRep;
7 protected IPublicLogicHandler _publicLogic;
8 protected IPolicyRepository _policyRep;
9 protected IMapper _mapper;
10 public OrderApiHelper(IOrderDetailRepository orderDetailRep, IPolicyRepository policyRep, IPublicLogicHandler publicLogic, IMapper mapper, IPackageRepository packageRep, IV_CustomerInfoRepository v_CustomerInfoRep)
11 {
12 _v_CustomerInfoRep = v_CustomerInfoRep;
13 _packageRep = packageRep;
14 _orderDetailRep = orderDetailRep;
15 _mapper = mapper;
16 _publicLogic = publicLogic;
17 _policyRep = policyRep;
18 customerList = _v_CustomerInfoRep.Entities;
19 }
20
21 /// <summary>
22 /// 商保申报验证
23 /// </summary>
24 /// <param name="orderdetail"></param>
25 /// <param name="customerId"></param>
26 /// <param name="success"></param>
27 /// <param name="fail"></param>
28 /// <returns></returns>
29 public abstract void ValidApply(T orderDto,out List<string> successList,out Dictionary<string, string> failList);
30
31 public abstract OrderEntity GetOrder(T orderDto, List<string> successList);
32
33 /// <summary>
34 /// 验证套餐是否存在或上架
35 /// </summary>
36 /// <param name="packageNumber"></param>
37 /// <returns></returns>
38 protected bool HasPackage(string packageNumber)
39 {
40 return _packageRep.Entities.Any(p => p.SerialNumber == packageNumber && p.State == true && p.PackageRefEntities.Count > 0);
41 }
42
43 /// <summary>
44 /// 判断此人是否在保
45 /// </summary>
46 /// <param name="orderdetail"></param>
47 /// <returns></returns>
48 protected bool IsIninsurance(string idCard, string packageNumber)
49 {
50
51 var queryadd = _orderDetailRep.Entities.Where(o => o.IdCard == idCard
52 && o.PackageNumber == packageNumber
53 && o.OperationType == OperationTypeEnum.增员.ToString()
54 && o.Status == OrderDetailStatusEnum.反馈成功.ToString()
55 );
56
57 var querycut = _orderDetailRep.Entities.Where(o => o.IdCard == idCard
58 && o.PackageNumber == packageNumber
59 && o.OperationType == OperationTypeEnum.减员.ToString()
60 && o.Status == OrderDetailStatusEnum.反馈成功.ToString()
61 );
62
63 if (queryadd.Count() > querycut.Count())
64 return true;
65 else
66 return false;
67 }
68
69 /// <summary>
70 /// 判断生效时间内存不存在保单
71 /// </summary>
72 /// <param name="orderdetail"></param>
73 /// <returns></returns>
74 protected bool HasPolicy(string packageNumber, DateTime effectiveTime)
75 {
76 var query = _packageRep.Get(p => p.SerialNumber == packageNumber).PackageRefEntities?
77 .Where(r =>
78 r.ProductEntity.PolicyRefEntities.Count() > 0
79 && r.ProductEntity.PolicyRefEntities?.Where(p =>
80 p.PolicyEntity.StartTime <= effectiveTime
81 && p.PolicyEntity.EndTime > effectiveTime).Count() > 0);
82 if (query.Count() > 0)
83 return true;
84 else
85 return false;
86 }
87 }
具体子类:OrderApiCutHelper,OrderApiAddHelper继承OrderApiHelper重写各自的验证和生成订单方法。
1 public class OrderApiCutHelper : OrderApiHelper<OrderCutApiDto>
2 {
3
4 public OrderApiCutHelper(IOrderDetailRepository orderDetailRep, IPolicyRepository policyRep, IPublicLogicHandler publicLogic, IMapper mapper, IPackageRepository packageRep, IV_CustomerInfoRepository v_CustomerInfoRep) : base(orderDetailRep, policyRep, publicLogic, mapper, packageRep, v_CustomerInfoRep)
5 {
6 }
7
8 public override OrderEntity GetOrder(OrderCutApiDto orderDto, List<string> successList)
9 {
10 }
11
12 /// <summary>
13 /// 商保申报减员
14 /// </summary>
15 /// <param name="orderDto"></param>
16 /// <param name="successList"></param>
17 /// <param name="failList"></param>
18 /// <returns></returns>
19 public override void ValidApply(OrderCutApiDto orderDto, out List<string> successList, out Dictionary<string, string> failList)
20 {
21 }
22 }
23
24 //OrderApiAddHelper类同上一样