040模块记录(数据)的权限设计(5)聚合操作
前面几节设计的都是把权限如何加在基准模块之上的,现在讨论一下聚合字段的权限。某个基准模块需要附加子模块的聚合字段也是比较常见的需求,比如“订单”模块中没有总金额,需要把“订单明细”的总金额汇总起来作为一个字段来显示。比如说要查看各个省份的订单个数等需求。
还是使用上一节中加入的二个权限:第一个是加在“订单”模块的父模块“省份”之上的,其路径为:客户单位--市--省,第二是加在“客户”的“等级“模块之上,其路径为tf_Rate,这二个同件同时满足。
select ( select count(*) from _Attachment where tf_moduleId = '6040' and tf_moduleIdValue = _this.tf_ordersId ) as `tf_attachmentCount`,getAttachmentNames( '6040',_this.tf_ordersId ) as `tf_attachmentTooltip`,_this.tf_ordersId as `tf_ordersId`,_this.tf_ordersNumber as `tf_ordersNumber`,_this.tf_date as `tf_date`,........ 生成的子模块的聚合查询 (select COUNT( _thisAggregate.tf_ordersDetailId) from `OrdersDetail` as _thisAggregate where _thisAggregate.tf_ordersId = _this.tf_ordersId ) as `OrdersDetail.tf_Orders.tf_ordersDetailId.count` from `Orders` as _this left outer join `City` `_this_14` on `_this_14`.tf_cityId = `_this`.tf_fromCityId left outer join `Province` `_this_14_21` on `_this_14_21`.tf_provinceId = `_this_14`.tf_provinceId left outer join `Salesman` `_this_12` on `_this_12`.tf_salesmanId = `_this`.tf_salesmanId left outer join `Storage` `_this_13` on `_this_13`.tf_storageId = `_this`.tf_storageId left outer join `City` `_this_13_21` on `_this_13_21`.tf_cityId = `_this_13`.tf_cityId left outer join `Province` `_this_13_21_31` on `_this_13_21_31`.tf_provinceId = `_this_13_21`.tf_provinceId left outer join `Customer` `_this_11` on `_this_11`.tf_customerId = `_this`.tf_customerId left outer join `City` `_this_11_21` on `_this_11_21`.tf_cityId = `_this_11`.tf_cityId left outer join `Province` `_this_11_21_31` on `_this_11_21_31`.tf_provinceId = `_this_11_21`.tf_provinceId left outer join `Rate` `_this_11_23` on `_this_11_23`.tf_rateId = `_this_11`.tf_rateId left outer join `City` `_this_15` on `_this_15`.tf_cityId = `_this`.tf_toCityId where ( _this_11_21_31.tf_provinceId in ( '06','07','08' ) ) and ( _this_11_23.tf_rateId in ( '10' ) )
在上面的sql语句中,加入了一个生成每条订单的“订单明细个数”的字段,在生成订单明细的这条聚合查询的时候,只是加入了一个限定条件,就是订单明细的的订单id等于基准模块订单的id,而全局的限定全限省份和等级并没有加入进去。这是因为这二个条件已经加在订单上了,所以不用再加到子查询里面了。
select _this.tf_rateId as `tf_rateId`,_this.tf_name as `tf_name`,(select COUNT( _thisAggregate.tf_ordersDetailId) from `OrdersDetail` as _thisAggregate left outer join `Orders` `_thisAggregate_11` on `_thisAggregate_11`.tf_ordersId = `_thisAggregate`.tf_ordersId left outer join `Customer` `_thisAggregate_11_21` on `_thisAggregate_11_21`.tf_customerId = `_thisAggregate_11`.tf_customerId left outer join `City` `_thisAggregate_11_21_31` on `_thisAggregate_11_21_31`.tf_cityId = `_thisAggregate_11_21`.tf_cityId left outer join `Province` `_thisAggregate_11_21_31_41` on `_thisAggregate_11_21_31_41`.tf_provinceId = `_thisAggregate_11_21_31`.tf_provinceId left outer join `Rate` `_thisAggregate_11_21_33` on `_thisAggregate_11_21_33`.tf_rateId = `_thisAggregate_11_21`.tf_rateId where ( _thisAggregate_11_21_31_41.tf_provinceId in ( '06','08' ) ) and _thisAggregate_11_21.tf_rateId = _this.tf_rateId and ( _thisAggregate_11_21_33.tf_rateId in ( '10' ) ) ) as `OrdersDetail.tf_Orders.tf_Customer.tf_Rate.tf_ordersDetailId.count` from `Rate` as _this上面的sql语句中可以看到,在生成的 “订单明细”的子查询中,加入了二个我们限定的全局条件,因为这二个条件都是加在订单明细的父模块上,并且都不是加在基准模块的父模块上,因此需要加在子查询中。在上面的查询中,这个人能看到的子查询的统计出来的数据,就是那三个限定条件省的订单的数据。
一句话来概括:基准模块或其父模块上的权限,不用加在子查询中。第一个查询基准模块是“订单”,二个全局限定条件是“订单”和“客户单位”,所以子模询中不用加入这二个条件了。第二个查询基准模块是“等级”,“订单”和“客户单位”都是他的子模块,因此在生成子查询的时候要把条件加进去。