
# @Extension介绍
devspore-horizon是devspore提供的对metaBO（以及对应的RO）操作前鉴权、认证，metaBO变动后消息发送、审计等功能的组件。配置horizon后，generator生成的项目的repository中相关操作上会带有@Extension注解。
```
// 普通增删改查接口
@Extension(operation = Operation.CREATE, name = "Project") 
// 用户自定义扩展接口
@Extension(operation = Operation.CUSTOM, customMethod = "methodName", name = "Project")
```
表1属性说明 
| 参数名称         | 数据类型      | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
|:---|:---|:---|
| operation    | Operation | 操作类型。此值会传递到[DataEvent](https://support.huaweicloud.com/devg-astropro/astropro_06_0016.html#astropro_06_0016__section11561346104618)对应operation字段。 - CREATE：新增操作。  - DELETE：删除操作。  - UPDATE：更新操作。  - VIEW：查询操作。  - BATCH_CREATE：批量新增。  - BATCH_UPDATE：批量更新。  - BATCH_VIEW：批量查询。  - BATCH_DELETE：批量删除。  - CUSTOM：用户自定义操作。   |
| customMethod | String    | 扩展API中定义的method对象，仅在operation为CUSTOM时有意义；此值会传递到[DataEvent](https://support.huaweicloud.com/devg-astropro/astropro_06_0016.html#astropro_06_0016__section11561346104618)对应customMethod字段。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| name         | String    | 表示本次操作的metaBO或者RO（关系类型）的名称。此值会传递到[DataEvent](https://support.huaweicloud.com/devg-astropro/astropro_06_0016.html#astropro_06_0016__section11561346104618)对应sourceName字段。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
   
 #### **操作示例**
- 新增操作：此示例表示可对名为Order的metaBO新增操作做统一的操作前认证、鉴权，操作后消息发送、缓存更新、审计等。
  ```
  /**
  * AddOrder Method
  *
  * @param order order
  * @return Order
  */
  @Extension(operation = Operation.CREATE, name = "Order")
  @Override
  public Order addOrder(Order order) {
  return orderRepository.saveSelective(order);
  }
  ```
  新增操作[插件参数DataEvent](https://support.huaweicloud.com/devg-astropro/astropro_06_0016.html#astropro_06_0016__section11561346104618)取值示例：
  表2新增操作DataEvent取值示例 
  | 参数名称         | 取值                                    |
  |:---|:---|
  | metaDocument | 服务的元数据对象，自动读取服务元数据并反序列化为metaDocument。 |
  | operation    | CREATE。                               |
  | customMethod | ""。                                   |
  | sourceName   | Order。                                |
  | argsMap      | order：创建的order对象。                     |
  | id           | order对象中Id值。                          |
  | originDo     | null。                                 |
  | targetDo     | order的克隆对象。                           |
  | methodName   | addOrder。                             |
  | className    | 该方法所在类全限定名。                           |
  | contextMap   | null。                                 |
     
  
- 批量新增操作：此示例表示可对名为Order的metaBO批量新增操作做统一的操作前认证、鉴权，操作后消息发送、缓存更新、审计等。
  ```
  /**
   * AddOrders Method
   *
   * @param orderList orderList
   * @return List<Order>
   */
  @Extension(operation = Operation.BATCH_CREATE, name = "Order")
  @Override
  public List<Order> addOrders(List<Order> orderList) {
      return orderRepository.saveSelectiveAll(orderList);    
  }
  ```
  批量新增操作[插件参数DataEvent](https://support.huaweicloud.com/devg-astropro/astropro_06_0016.html#astropro_06_0016__section11561346104618)取值示例：
  表3批量新增操作DataEvent取值示例 
  | 参数名称         | 取值                                    |
  |:---|:---|
  | metaDocument | 服务的元数据对象，自动读取服务元数据并反序列化为metaDocument。 |
  | operation    | BATCH_CREATE。                         |
  | customMethod | ""。                                   |
  | sourceName   | Order。                                |
  | argsMap      | orderList：创建的order对象集合。               |
  | id           | null。                                 |
  | originDo     | null。                                 |
  | targetDo     | null。                                 |
  | methodName   | addOrders。                            |
  | className    | 该方法所在类全限定名。                           |
  | contextMap   | null。                                 |
     
  
- 删除操作：此示例表示可对名为Order的metaBO删除操作做统一的操作前认证、鉴权，操作后消息发送、缓存更新、审计等。
  ```
  /**
   * DeleteOrderById Method
   *
   * @param orderId orderId
   * @return Integer
   */
  @Extension(operation = Operation.DELETE, name = "Order")
  @Override
  public Integer deleteOrderById(String orderId) {
      return orderRepository.deleteById(orderId);
  }
  ```
  删除操作[插件参数DataEvent](https://support.huaweicloud.com/devg-astropro/astropro_06_0016.html#astropro_06_0016__section11561346104618)取值示例：
  表4删除操作DataEvent取值示例 
  | 参数名称         | 取值                                    |
  |:---|:---|
  | metaDocument | 服务的元数据对象，自动读取服务元数据并反序列化为metaDocument。 |
  | operation    | DELETE。                               |
  | customMethod | ""。                                   |
  | sourceName   | Order。                                |
  | argsMap      | orderId：待删除的orderId。                  |
  | id           | orderId。                              |
  | originDo     | 删除前数据库中orderId对应的原始对象。                |
  | targetDo     | null。                                 |
  | methodName   | deleteOrderById。                      |
  | className    | 该方法所在类全限定名。                           |
  | contextMap   | null。                                 |
     
  
- 批量删除操作：此示例表示可对名为Order的metaBO批量删除操作做统一的操作前认证、鉴权，操作后消息发送、缓存更新、审计等。
  ```
  /**
   * DeleteOrderByIds Method
   *
   * @param orderIds orderIds
   * @return Integer
   */
  @Extension(operation = Operation.BATCH_DELETE, name = "Order")
  @Override
  public Integer deleteOrderByIds(List<String> orderIds) {
      return orderRepository.deleteAllById(orderIds);    
  }
  ```
  批量删除操作[插件参数DataEvent](https://support.huaweicloud.com/devg-astropro/astropro_06_0016.html#astropro_06_0016__section11561346104618)取值示例：
  表5批量删除操作DataEvent取值示例 
  | 参数名称         | 取值                                    |
  |:---|:---|
  | metaDocument | 服务的元数据对象，自动读取服务元数据并反序列化为metaDocument。 |
  | operation    | BATCH_DELETE。                         |
  | customMethod | ""。                                   |
  | sourceName   | Order。                                |
  | argsMap      | orderIds：待删除的orderId集合。               |
  | id           | orderId。                              |
  | originDo     | null。                                 |
  | targetDo     | null。                                 |
  | methodName   | deleteOrderByIds。                     |
  | className    | 该方法所在类全限定名。                           |
  | contextMap   | null。                                 |
     
  
- 更新操作：此示例表示可对名为Order的metaBO更新操作做统一的操作前认证、鉴权，操作后消息发送、缓存更新、审计等。
  ```
  /**
   * UpdateOrderById Method
   *
   * @param order order
   * @param orderId orderId
   * @return Order
   */
  @Extension(operation = Operation.UPDATE, name = "Order")
  @Override
  public Order updateOrderById(Order order, String orderId) {
      return orderRepository.updateById(order);
  }
  ```
  更新操作[插件参数DataEvent](https://support.huaweicloud.com/devg-astropro/astropro_06_0016.html#astropro_06_0016__section11561346104618)取值示例：
  表6更新操作DataEvent取值示例 
  | 参数名称         | 取值                                                                                                                                                                                                                         |
  |:---|:---|
  | metaDocument | 服务的元数据对象，自动读取服务元数据并反序列化为metaDocument。                                                                                                                                                                                      |
  | operation    | UPDATE。                                                                                                                                                                                                                    |
  | customMethod | ""。                                                                                                                                                                                                                        |
  | sourceName   | Order。                                                                                                                                                                                                                     |
  | argsMap      | - order：order对象。  - orderId：order对象Id。   |
  | id           | orderId。                                                                                                                                                                                                                   |
  | originDo     | 更新前数据库中orderId对应的原始对象。                                                                                                                                                                                                     |
  | targetDo     | order的克隆对象。                                                                                                                                                                                                                |
  | methodName   | updateOrderById。                                                                                                                                                                                                           |
  | className    | 该方法所在类全限定名。                                                                                                                                                                                                                |
  | contextMap   | null。                                                                                                                                                                                                                      |
     
  
- 批量更新操作：此示例表示可对名为Order的metaBO批量更新操作做统一的操作前认证、鉴权，操作后消息发送、缓存更新、审计等。
  ```
  /**
  * UpdateOrderByIds Method
  *
  * @param orderList orderList
  * @return List<Order>
  */
  @Extension(operation = Operation.BATCH_UPDATE, name = "Order")
  @Override
  public List<Order> updateOrderByIds(List<Order> orderList) {
      return orderRepository.updateByIds(orderList);    
  }
  ```
  批量更新操作[插件参数DataEvent](https://support.huaweicloud.com/devg-astropro/astropro_06_0016.html#astropro_06_0016__section11561346104618)取值示例：
  表7批量更新操作DataEvent取值示例 
  | 参数名称         | 取值                                    |
  |:---|:---|
  | metaDocument | 服务的元数据对象，自动读取服务元数据并反序列化为metaDocument。 |
  | operation    | BATCH_UPDATE。                         |
  | customMethod | ""。                                   |
  | sourceName   | Order。                                |
  | argsMap      | orderList：待更新的order对象集合。              |
  | id           | null。                                 |
  | originDo     | null。                                 |
  | targetDo     | null。                                 |
  | methodName   | updateOrderByIds。                     |
  | className    | 该方法所在类全限定名。                           |
  | contextMap   | null。                                 |
     
  
- 查询操作：此示例表示可对名为Order的metaBO查询操作做统一的操作前认证、鉴权，操作后消息发送、缓存更新、审计等。
  ```
  /**
   * GetOrderById Method
   *
   * @param orderId orderId
   * @return Order
   */
  @Extension(operation = Operation.VIEW, name = "Order")
  @Override
  public Order getOrderById(String orderId) {
      return orderRepository.findOrderById(orderId);    
  }
  ```
  查询操作[插件参数DataEvent](https://support.huaweicloud.com/devg-astropro/astropro_06_0016.html#astropro_06_0016__section11561346104618)取值示例：
  表8查询操作DataEvent取值示例 
  | 参数名称         | 取值                                    |
  |:---|:---|
  | metaDocument | 服务的元数据对象，自动读取服务元数据并反序列化为metaDocument。 |
  | operation    | VIEW。                                 |
  | customMethod | ""。                                   |
  | sourceName   | Order。                                |
  | argsMap      | orderId：order对象Id。                    |
  | id           | null。                                 |
  | originDo     | null。                                 |
  | targetDo     | null。                                 |
  | methodName   | getOrderById。                         |
  | className    | 该方法所在类全限定名。                           |
  | contextMap   | null。                                 |
     
  
- 批量查询操作：此示例表示可对名为Order的metaBO批量查询操作做统一的操作前认证、鉴权，操作后消息发送、缓存更新、审计等。
  ```
  /**
   * GetOrders Method
   *
   * @param orderQo orderQo
   * @return PageInfo<Order>
   */
  @Extension(operation = Operation.BATCH_VIEW, name = "Order")
  @Override
  public PageInfo<Order> getOrders(OrderQo orderQo) {
      return orderRepository.findOrders(orderQo);    
  }
  ```
  批量查询操作[插件参数DataEvent](https://support.huaweicloud.com/devg-astropro/astropro_06_0016.html#astropro_06_0016__section11561346104618)取值示例：
  表9批量查询操作DataEvent取值示例 
  | 参数名称         | 取值                                    |
  |:---|:---|
  | metaDocument | 服务的元数据对象，自动读取服务元数据并反序列化为metaDocument。 |
  | operation    | BATCH_VIEW。                           |
  | customMethod | ""。                                   |
  | sourceName   | Order。                                |
  | argsMap      | orderQo：orderQo对象。                    |
  | id           | null。                                 |
  | originDo     | null。                                 |
  | targetDo     | null。                                 |
  | methodName   | getOrders。                            |
  | className    | 该方法所在类全限定名。                           |
  | contextMap   | null。                                 |
     
  
- 自定义扩展API操作：此示例表示可对名为Order的metaBO中自定义扩展API"pay"操作做统一的操作前认证、鉴权，操作后消息发送、缓存更新、审计等。
  ```
  /**
   * pay
   *
   * @param payment payment
   * @param orderId orderId
   * @return OrderOrderDetailNested
   */
  @Extension(operation = Operation.CUSTOM, customMethod = "pay", name = "Order")
  @Override
  public OrderOrderDetailNested pay(Payment payment, String accountId, String orderId) {
      OrderOrderDetailNested orderOrderDetailNested = orderRepository.findOrderOrderDetailNested(accountId, orderId);
      ......
      return orderOrderDetailNested;
  }
  ```
  自定义扩展API操作[插件参数DataEvent](https://support.huaweicloud.com/devg-astropro/astropro_06_0016.html#astropro_06_0016__section11561346104618)取值示例：
  表10自定义扩展API操作DataEvent取值示例 
  | 参数名称         | 取值                                                                                                                                                                                                                                                                                                                  |
  |:---|:---|
  | metaDocument | 服务的元数据对象，自动读取服务元数据并反序列化为metaDocument。                                                                                                                                                                                                                                                                               |
  | operation    | CUSTOM。                                                                                                                                                                                                                                                                                                             |
  | customMethod | pay。                                                                                                                                                                                                                                                                                                                |
  | sourceName   | Order。                                                                                                                                                                                                                                                                                                              |
  | argsMap      | - payment：payment对象。  - accountId：accountId值。  - orderId：orderId值。   |
  | id           | null。                                                                                                                                                                                                                                                                                                               |
  | originDo     | null。                                                                                                                                                                                                                                                                                                               |
  | targetDo     | null。                                                                                                                                                                                                                                                                                                               |
  | methodName   | getOrders。                                                                                                                                                                                                                                                                                                          |
  | className    | 该方法所在类全限定名。                                                                                                                                                                                                                                                                                                         |
  | contextMap   | null。                                                                                                                                                                                                                                                                                                               |
     
  
 
