文章目录
- 1、ProductController
- 2、AdminCommonService
- 3、ProductApiService
- 4、ProductCommonService
- 5、ProductSqlService
- 1. 完整SQL分析
- 可选部分(条件筛选):
 
- 2. 涉及的表
- 3. 总结
- 4. 功能概述
 
 
查询指定管理员下所有产品所涉及的表?
SELECT * FROM `brand_admin_mapping` WHERE `admin_id`  = 75;SELECT * FROM `product` WHERE `brand_id` = 30SELECT * FROM `product` WHERE `brand_id` = 40SELECT * FROM `product_admin_mapping` WHERE `admin_id` = 75
 http://127.0.0.1:8087/product/admin/list?page=0&size=10&field=name
1、ProductController
    @GetMapping("admin/list")@ApiOperation("分页列表")public BaseResult list(PageWithSearch basePage, @ApiIgnore @SessionAttribute(Constants.ADMIN_ID) Integer adminId) {checkParam(basePage.getField(), basePage.getValue());adminId = adminCommonService.getVipIdByProduct(adminId);return BaseResult.success(productApiService.findPage(adminId, basePage));}
2、AdminCommonService
    /*** 获取商品管理人的上级vip id* 当操作者为商品管理人时获取上级的vip id* 否则返回自身*/public Integer getVipIdByProduct(Integer nowId) {return hasRole(nowId, Admin.ROLE_PRODUCT) || hasRole(nowId, Admin.ROLE_QUALIFICATION) || hasRole(nowId, Admin.ROLE_STORE) ? findCompanySuperId(nowId) : nowId;}/*** 查询公司超管id*/public Integer findCompanySuperId(Integer adminId) {return adminService.findCompanySuperId(adminId);}
3、ProductApiService
    /*** 分页列表*/public Page<ProductListDto> findPage(Integer nowId, PageWithSearch page) {Page<ProductWithShareDto> productPage = productCommonService.findPage(nowId, page);return new PageImpl<>(convertToListDto(productPage.getContent(), nowId), page.toPageable(), productPage.getTotalElements());}
4、ProductCommonService
    /*** 产品管理-产品分页列表*/public Page<ProductWithShareDto> findPage(Integer nowId, PageWithSearch basePage) {return productSqlService.findPage(nowId, basePage);}
5、ProductSqlService
    /*** 产品管理-分页列表*/public Page<ProductWithShareDto> findPage(Integer nowId, PageWithSearch basePage) {StringBuilder sql = new StringBuilder();Map<String, Object> paramMap = new HashMap<>(4);sql.append("SELECT DISTINCT ").append(SqlUtil.sqlGenerate("p", Product.class)).append(",a.send_id, a.edit_auth FROM product p ");if (!StringUtils.isEmpty(basePage.getField()) && !StringUtils.isEmpty(basePage.getValue()) && brandParamStr.contains(basePage.getField())) {sql.append("INNER JOIN brand b ON p.brand_id = b.id AND b.").append(SqlUtil.camelToUnderline(basePage.getField().replaceAll("brand", ""))).append(" LIKE :").append(basePage.getField()).append(" ");paramMap.put(basePage.getField(), "%" + basePage.getValue() + "%");}//sql.append("LEFT JOIN product_admin_mapping a ON p.id = a.product_id ");//sql.append("AND a.admin_id=").append(nowId).append(" AND a.read_auth =").append(CommonStatusEnum.NORMAL.getValue()).append(" ");//sql.append("WHERE (p.creator_id =").append(nowId).append(" OR ").append("a.id IS NOT NULL) ");sql.append("INNER JOIN product_admin_mapping a ON p.id = a.product_id AND a.admin_id=").append(nowId).append(" ");//有编辑权限 || (有查看权限 && 产品状态为显示)sql.append("WHERE (a.edit_auth =").append(CommonStatusEnum.NORMAL.getValue()).append(" OR (a.read_auth =").append(CommonStatusEnum.NORMAL.getValue()).append(" AND p.status =").append(CommonStatusEnum.NORMAL.getValue()).append(")) ");paramHandle(sql, paramMap, basePage.getField(), basePage.getValue());sql.append("ORDER BY p.ranks DESC,p.created_date DESC ");List result = executeSql(sql, paramMap, basePage.getPage(), basePage.getSize());if (result.isEmpty()) {return new PageImpl<>(Collections.emptyList(), basePage.toPageable(), 0);}return new PageImpl<>(parseToProductWithShare(result), basePage.toPageable(), countPage(nowId, basePage.getField(), basePage.getValue()));}
1. 完整SQL分析
从代码中生成的SQL大致如下:
SELECT DISTINCT p.*, a.send_id, a.edit_auth 
FROM product p 
INNER JOIN product_admin_mapping a ON p.id = a.product_id AND a.admin_id = :nowId
条件部分:
WHERE (a.edit_auth = :normalStatus OR (a.read_auth = :normalStatus AND p.status = :normalStatus))
排序部分:
ORDER BY p.ranks DESC, p.created_date DESC
可选部分(条件筛选):
当 basePage.getField() 和 basePage.getValue() 存在时:
INNER JOIN brand b ON p.brand_id = b.id AND b.<转换后的字段> LIKE :fieldValue
2. 涉及的表
- product表:产品表(- p)
- product_admin_mapping表:产品与管理员权限映射表(- a)
- brand表(条件存在时):品牌表(- b)
3. 总结
最终SQL根据逻辑最多涉及 3张表:
- product
- product_admin_mapping
- brand(可选)
4. 功能概述
- 基于管理员ID (nowId) 和搜索条件筛选产品。
- 关联管理员权限(编辑或查看权限)。
- 按照产品的排名和创建时间排序返回分页结果。
