feat(brandgating): 品牌Gating库后端模块并入管理后台
直连主库既有 biz_brand_gating 表(原独立小网页后台 8090 下线的前置),
六端点挂 /api/admin/brand-gating,权限键 admin_brand_gating,走 admin
第一道门前缀守卫无需额外登记。列表/统计/新增/删除/启停用对齐桌面版
(停用记 disabled_at、恒刷 updated_at);批量导入 { text } 按桌面版
parse_batch 逐条同款解析,去重靠唯一键+批内四键预检。
SQL 账本 115→124、select 账本 78→86,8 条 select 点入 NOT_MIGRATED。
This commit is contained in:
+118
@@ -0,0 +1,118 @@
|
||||
package com.nanri.aiimage.modules.brandgating.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingAddRequest;
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingImportRequest;
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingStatusRequest;
|
||||
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingAddResultVo;
|
||||
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingDeleteResultVo;
|
||||
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingImportResultVo;
|
||||
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingPageVo;
|
||||
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingStatsVo;
|
||||
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingStatusResultVo;
|
||||
import com.nanri.aiimage.modules.brandgating.service.BrandGatingService;
|
||||
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 品牌Gating库(管理后台):独立小网页后台(gating_web_admin.py)并入数富管理后台,
|
||||
* 共用主库同一张 biz_brand_gating 表。
|
||||
*
|
||||
* <p>页面权限键 {@code admin_brand_gating}(后台菜单 column_key,经
|
||||
* {@link PermissionMenuService#requireConsolePageAccess} 把门);鉴权第一道门由
|
||||
* AdminApiGuardFilter 的 /api/admin 前缀兜底(见 ApiGuardCoverageAuditor 口径)。</p>
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/admin/brand-gating")
|
||||
@Tag(name = "品牌Gating库(后台)", description = "品牌Gating库的列表/筛选/统计、单条新增、启停、删除与批量导入。")
|
||||
public class BrandGatingController {
|
||||
|
||||
/** 后台菜单权限键(columns.column_key,与 V164 菜单行同键)。 */
|
||||
private static final String PERMISSION_KEY = "admin_brand_gating";
|
||||
private static final String DENIAL_MESSAGE = "无权访问品牌Gating库";
|
||||
|
||||
private final BrandGatingService brandGatingService;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
private final PermissionMenuService permissionMenuService;
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "分页查询品牌Gating库", description = "keyword 模糊匹配品牌/ASIN,country/gatingType/status/source 精确筛选,ORDER BY id DESC。")
|
||||
public ApiResponse<BrandGatingPageVo> page(
|
||||
HttpServletRequest request,
|
||||
@Parameter(description = "品牌/ASIN 模糊搜索") @RequestParam(name = "keyword", required = false) String keyword,
|
||||
@Parameter(description = "国家(「全部」= 不限)") @RequestParam(name = "country", required = false) String country,
|
||||
@Parameter(description = "Gating 类型(「全部」= 不限)") @RequestParam(name = "gatingType", required = false) String gatingType,
|
||||
@Parameter(description = "状态:1=生效 0=停用,不传=全部") @RequestParam(name = "status", required = false) String status,
|
||||
@Parameter(description = "来源(「全部」= 不限)") @RequestParam(name = "source", required = false) String source,
|
||||
@Parameter(description = "页码(1 起)") @RequestParam(name = "page", required = false) Long page,
|
||||
@Parameter(description = "每页数量(默认 10,上限 200)") @RequestParam(name = "size", required = false) Long size) {
|
||||
requireBrandGatingAccess(request);
|
||||
return ApiResponse.success(brandGatingService.page(page, size, keyword, country, gatingType, status, source));
|
||||
}
|
||||
|
||||
@GetMapping("/stats")
|
||||
@Operation(summary = "品牌Gating库统计", description = "total/active/disabled/brands 与按类型/国家/来源的分布(分组只数生效行,国家按 count DESC)。")
|
||||
public ApiResponse<BrandGatingStatsVo> stats(HttpServletRequest request) {
|
||||
requireBrandGatingAccess(request);
|
||||
return ApiResponse.success(brandGatingService.stats());
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@Operation(summary = "单条新增", description = "brand 必填;缺省字段落桌面版同款默认(country=全部、gatingType=品牌gating、source=MANUAL);撞唯一键回 added=0。")
|
||||
public ApiResponse<BrandGatingAddResultVo> add(
|
||||
HttpServletRequest request,
|
||||
@Valid @RequestBody BrandGatingAddRequest body) {
|
||||
requireBrandGatingAccess(request);
|
||||
return ApiResponse.success(brandGatingService.add(body));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@Operation(summary = "删除单条", description = "物理删除该行,返回实际删除数。")
|
||||
public ApiResponse<BrandGatingDeleteResultVo> delete(
|
||||
HttpServletRequest request,
|
||||
@PathVariable("id") Long id) {
|
||||
requireBrandGatingAccess(request);
|
||||
return ApiResponse.success(brandGatingService.delete(id));
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/status")
|
||||
@Operation(summary = "启用/停用", description = "status=1 恢复生效并清 disabled_at;status=0 停用并记 disabled_at=NOW();都刷 updated_at。")
|
||||
public ApiResponse<BrandGatingStatusResultVo> updateStatus(
|
||||
HttpServletRequest request,
|
||||
@PathVariable("id") Long id,
|
||||
@Valid @RequestBody BrandGatingStatusRequest body) {
|
||||
requireBrandGatingAccess(request);
|
||||
return ApiResponse.success(brandGatingService.updateStatus(id, body.getStatus()));
|
||||
}
|
||||
|
||||
@PostMapping("/import")
|
||||
@Operation(summary = "批量导入", description = "body 优先 { text }:按桌面版批量导入行格式解析(逗号/分号/制表符切列,# 注释行跳过);也兼容 { items } 直传。重复行跳过,返回 { added, skipped }。")
|
||||
public ApiResponse<BrandGatingImportResultVo> importRows(
|
||||
HttpServletRequest request,
|
||||
@RequestBody BrandGatingImportRequest body) {
|
||||
requireBrandGatingAccess(request);
|
||||
return ApiResponse.success(brandGatingService.importRows(body));
|
||||
}
|
||||
|
||||
/** 逐端点把门:登录态 + 后台菜单权限(admin_brand_gating)。 */
|
||||
private void requireBrandGatingAccess(HttpServletRequest request) {
|
||||
AdminUserEntity operator = adminAuthSupport.requireUser(request);
|
||||
permissionMenuService.requireConsolePageAccess(operator, DENIAL_MESSAGE, PERMISSION_KEY);
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.nanri.aiimage.modules.brandgating.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingFilterQuery;
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingNameCountRow;
|
||||
import com.nanri.aiimage.modules.brandgating.model.entity.BrandGatingEntity;
|
||||
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingRowVo;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 品牌Gating库(biz_brand_gating)数据访问。
|
||||
*
|
||||
* <p>列表筛选/统计分组/启停这类动态 SQL 全部收口在本 Mapper 的 XML 里
|
||||
* ({@code mapper/brandgating/BrandGatingMapper.xml}),筛选值一律 {@code #{}} 占位符;
|
||||
* 单条新增/删除走 MyBatis-Plus BaseMapper 既有能力。</p>
|
||||
*/
|
||||
@Mapper
|
||||
public interface BrandGatingMapper extends BaseMapper<BrandGatingEntity> {
|
||||
|
||||
/** 筛选命中总数。 */
|
||||
long countByFilter(@Param("f") BrandGatingFilterQuery filter);
|
||||
|
||||
/** 当前页行:ORDER BY id DESC(对齐桌面版 list_gating)。 */
|
||||
List<BrandGatingRowVo> selectRows(@Param("f") BrandGatingFilterQuery filter);
|
||||
|
||||
/** 全表行数。 */
|
||||
long countAll();
|
||||
|
||||
/** 按状态计数:1=生效、0=停用。 */
|
||||
long countByStatus(@Param("status") int status);
|
||||
|
||||
/** 生效行里的去重品牌数。 */
|
||||
long countDistinctBrands();
|
||||
|
||||
/** 统计:按 Gating 类型分布(仅生效行)。 */
|
||||
List<BrandGatingNameCountRow> countGroupByType();
|
||||
|
||||
/** 统计:按国家分布(仅生效行,count DESC,对齐桌面版 by_country)。 */
|
||||
List<BrandGatingNameCountRow> countGroupByCountry();
|
||||
|
||||
/** 统计:按来源分布(仅生效行)。 */
|
||||
List<BrandGatingNameCountRow> countGroupBySource();
|
||||
|
||||
/**
|
||||
* 启停(对齐桌面版 set_status_gating):status=1 清 disabled_at、=0 记 disabled_at=NOW(),
|
||||
* 两种都刷 updated_at=NOW()。
|
||||
*/
|
||||
int updateStatus(@Param("id") Long id, @Param("status") int status);
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.nanri.aiimage.modules.brandgating.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 品牌Gating库单条新增请求(POST /api/admin/brand-gating)。
|
||||
*
|
||||
* <p>brand 必填(服务端校验,对齐桌面版 add_gating 的「品牌不能为空」);
|
||||
* 其余字段可缺省,由服务端落桌面版同款默认:country→「全部」、gatingType→「品牌gating」、
|
||||
* asin/shopName→NULL、remark→空串、source→「MANUAL」。</p>
|
||||
*/
|
||||
@Data
|
||||
public class BrandGatingAddRequest {
|
||||
|
||||
private String brand;
|
||||
private String asin;
|
||||
private String country;
|
||||
private String gatingType;
|
||||
private String source;
|
||||
private String shopName;
|
||||
private String remark;
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.nanri.aiimage.modules.brandgating.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 品牌Gating库列表筛选载体(Mapper XML 的 #{f.xxx} 取值源)。
|
||||
*
|
||||
* <p>筛选口径对齐桌面版 gating_web_admin.py list_gating(L138-156):
|
||||
* keyword 模糊匹配 brand/asin,country/gatingType/source 精确等值(「全部」= 不限),
|
||||
* status 精确等值(null = 不限)。分页偏移与容量由 Service 归一化后带入。</p>
|
||||
*/
|
||||
@Data
|
||||
public class BrandGatingFilterQuery {
|
||||
|
||||
/** 品牌/ASIN 模糊搜索词(已 trim;空串 = 不限)。 */
|
||||
private String keyword;
|
||||
/** 国家精确等值(空串或「全部」= 不限)。 */
|
||||
private String country;
|
||||
/** Gating 类型精确等值(空串或「全部」= 不限)。 */
|
||||
private String gatingType;
|
||||
/** 状态精确等值:1=生效 0=停用(null = 不限)。 */
|
||||
private Integer status;
|
||||
/** 来源精确等值(空串或「全部」= 不限)。 */
|
||||
private String source;
|
||||
/** 分页偏移((page-1)*size,已归一化)。 */
|
||||
private Long offset;
|
||||
/** 每页条数(已归一化)。 */
|
||||
private Long limit;
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.nanri.aiimage.modules.brandgating.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 品牌Gating库批量导入请求(POST /api/admin/brand-gating/import)。
|
||||
*
|
||||
* <p>优先用 {@link #text}:原始粘贴文本整段交后端,按桌面版 parse_batch 的行格式解析
|
||||
* (中英文逗号/分号/制表符切列,字段序 品牌国家/类型/ASIN/备注,空行与 # 注释行跳过)。
|
||||
* 也兼容 {@link #items} 直传(text 为空时生效),每条按与单条新增同语义落库。</p>
|
||||
*/
|
||||
@Data
|
||||
public class BrandGatingImportRequest {
|
||||
|
||||
/** 原始粘贴文本(桌面版批量导入文本框的整段内容)。 */
|
||||
private String text;
|
||||
|
||||
/** 直传行(与 text 二选一,text 优先)。 */
|
||||
private List<Item> items;
|
||||
|
||||
@Data
|
||||
public static class Item {
|
||||
private String brand;
|
||||
private String country;
|
||||
private String gatingType;
|
||||
private String asin;
|
||||
private String remark;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.nanri.aiimage.modules.brandgating.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 品牌Gating库统计分组单行(stats 的 byType/byCountry/bySource 共用)。
|
||||
* SQL 侧 {@code SELECT xxx AS name, COUNT(*) AS count} 直接映射。
|
||||
*/
|
||||
@Data
|
||||
public class BrandGatingNameCountRow {
|
||||
|
||||
private String name;
|
||||
private long count;
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.nanri.aiimage.modules.brandgating.model.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 品牌Gating库启停请求体(POST /api/admin/brand-gating/{id}/status)。
|
||||
* status:1=生效(清 disabled_at)、0=停用(记 disabled_at=NOW())。
|
||||
*/
|
||||
@Data
|
||||
public class BrandGatingStatusRequest {
|
||||
|
||||
@NotNull(message = "status 不能为空")
|
||||
private Integer status;
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.nanri.aiimage.modules.brandgating.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 品牌Gating库(biz_brand_gating)。
|
||||
*
|
||||
* <p>表在主库(aiimage)早已存在——原先是独立小网页后台(gating_web_admin.py,直连同一库)在维护,
|
||||
* 本次把它并入数富管理后台,桌面/网页与后台共用同一份数据。<b>不建表、不走 Flyway</b>:
|
||||
* 本模块只做实体/Mapper 映射既有表。status:1=生效、0=停用(停用记 disabled_at,恢复清空)。</p>
|
||||
*/
|
||||
@Data
|
||||
@TableName("biz_brand_gating")
|
||||
public class BrandGatingEntity {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
private String brand;
|
||||
private String asin;
|
||||
private String country;
|
||||
private String gatingType;
|
||||
/** 1=生效 0=停用。 */
|
||||
private Integer status;
|
||||
private String source;
|
||||
private String shopName;
|
||||
private String remark;
|
||||
private LocalDateTime createdAt;
|
||||
private LocalDateTime updatedAt;
|
||||
private LocalDateTime disabledAt;
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.nanri.aiimage.modules.brandgating.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/** 单条新增结果:1=写入成功,0=命中唯一键重复被跳过(桌面版 add_gating 返回 False 的同语义)。 */
|
||||
@Data
|
||||
public class BrandGatingAddResultVo {
|
||||
|
||||
private int added;
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.nanri.aiimage.modules.brandgating.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/** 删除结果:实际删除的行数(0=该 id 不存在)。 */
|
||||
@Data
|
||||
public class BrandGatingDeleteResultVo {
|
||||
|
||||
private int deleted;
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.nanri.aiimage.modules.brandgating.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/** 批量导入结果:added=实际写入行数,skipped=跳过行数(批内重复/命中唯一键重复/无效行)。 */
|
||||
@Data
|
||||
public class BrandGatingImportResultVo {
|
||||
|
||||
private int added;
|
||||
private int skipped;
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.nanri.aiimage.modules.brandgating.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** 品牌Gating库分页负载(GET /api/admin/brand-gating → { total, rows })。 */
|
||||
@Data
|
||||
public class BrandGatingPageVo {
|
||||
|
||||
private long total;
|
||||
private List<BrandGatingRowVo> rows;
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.nanri.aiimage.modules.brandgating.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 品牌Gating库列表单行(GET /api/admin/brand-gating 的 rows 元素)。
|
||||
* 字段与契约一一对应;时间字段原样透传(桌面版列表同列展示)。
|
||||
*/
|
||||
@Data
|
||||
public class BrandGatingRowVo {
|
||||
|
||||
private Long id;
|
||||
private String brand;
|
||||
private String asin;
|
||||
private String country;
|
||||
private String gatingType;
|
||||
/** 1=生效 0=停用。 */
|
||||
private Integer status;
|
||||
private String source;
|
||||
private String shopName;
|
||||
private String remark;
|
||||
private LocalDateTime createdAt;
|
||||
private LocalDateTime updatedAt;
|
||||
private LocalDateTime disabledAt;
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.nanri.aiimage.modules.brandgating.model.vo;
|
||||
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingNameCountRow;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 品牌Gating库统计负载(GET /api/admin/brand-gating/stats)。
|
||||
* 口径对齐桌面版 stats()(L165-178):active/disabled 按全表数,byType/byCountry/bySource
|
||||
* 只数 status=1 的行,byCountry 按 count DESC。
|
||||
*/
|
||||
@Data
|
||||
public class BrandGatingStatsVo {
|
||||
|
||||
/** 全表行数(含停用)。 */
|
||||
private long total;
|
||||
/** 生效行数(status=1)。 */
|
||||
private long active;
|
||||
/** 停用行数(status=0)。 */
|
||||
private long disabled;
|
||||
/** 生效行里的去重品牌数。 */
|
||||
private long brands;
|
||||
/** 按 Gating 类型分布(仅生效行)。 */
|
||||
private List<BrandGatingNameCountRow> byType;
|
||||
/** 按国家分布(仅生效行,count DESC)。 */
|
||||
private List<BrandGatingNameCountRow> byCountry;
|
||||
/** 按来源分布(仅生效行)。 */
|
||||
private List<BrandGatingNameCountRow> bySource;
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.nanri.aiimage.modules.brandgating.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/** 启停结果:实际更新的行数(0=该 id 不存在)。 */
|
||||
@Data
|
||||
public class BrandGatingStatusResultVo {
|
||||
|
||||
private int updated;
|
||||
}
|
||||
+276
@@ -0,0 +1,276 @@
|
||||
package com.nanri.aiimage.modules.brandgating.service;
|
||||
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.brandgating.mapper.BrandGatingMapper;
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingAddRequest;
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingFilterQuery;
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingImportRequest;
|
||||
import com.nanri.aiimage.modules.brandgating.model.entity.BrandGatingEntity;
|
||||
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingAddResultVo;
|
||||
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingDeleteResultVo;
|
||||
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingImportResultVo;
|
||||
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingPageVo;
|
||||
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingRowVo;
|
||||
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingStatsVo;
|
||||
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingStatusResultVo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 品牌Gating库(biz_brand_gating)业务:独立小网页后台(gating_web_admin.py)并入数富管理后台。
|
||||
*
|
||||
* <p>行为口径逐条对齐桌面版(直连同一 aiimage 库,两端共用同一份数据):
|
||||
* 列表筛选/统计见 {@code list_gating}/`stats`(L138-178),新增默认值见 {@code add_gating}
|
||||
* (L181-193:asin/shop_name 空→NULL、country 空→「全部」、gating_type 空→「品牌gating」、
|
||||
* source 固定「MANUAL」、remark 空→空串,created_at/updated_at 由库默认维护、INSERT 不显式写),
|
||||
* 启停见 {@code set_status_gating}(L196-201),批量导入见 {@code parse_batch}/{@code batch_add}
|
||||
* (L208-237)。重复判定沿用桌面版的唯一键兜底:INSERT 撞唯一键即跳过。</p>
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class BrandGatingService {
|
||||
|
||||
/** 手动新增行的固定来源(桌面版 add_gating/batch_add 同口径)。 */
|
||||
public static final String SOURCE_MANUAL = "MANUAL";
|
||||
/** 缺省国家(桌面版默认值,也是表内真实取值)。 */
|
||||
public static final String DEFAULT_COUNTRY = "全部";
|
||||
/** 缺省 Gating 类型。 */
|
||||
public static final String DEFAULT_GATING_TYPE = "品牌gating";
|
||||
|
||||
/** 列表默认页容量(对齐桌面版 page_size=10)。 */
|
||||
public static final long DEFAULT_PAGE_SIZE = 10;
|
||||
/** 列表页容量上限(防御性截断,前端档位 10/20/50)。 */
|
||||
public static final long MAX_PAGE_SIZE = 200;
|
||||
|
||||
/** 批量导入的去重维度:同 品牌+国家+类型+ASIN 的行只写第一条(批内重复跳过)。 */
|
||||
private static final String IMPORT_DEDUP_KEY_FORMAT = "%s\n%s\n%s\n%s";
|
||||
|
||||
private final BrandGatingMapper brandGatingMapper;
|
||||
|
||||
// ---------------------------------------------------------------- 列表与统计
|
||||
|
||||
public BrandGatingPageVo page(Long page, Long size, String keyword, String country,
|
||||
String gatingType, String status, String source) {
|
||||
BrandGatingFilterQuery filter = new BrandGatingFilterQuery();
|
||||
filter.setKeyword(trimToEmpty(keyword));
|
||||
filter.setCountry(trimToEmpty(country));
|
||||
filter.setGatingType(trimToEmpty(gatingType));
|
||||
filter.setStatus(parseStatusFilter(status));
|
||||
filter.setSource(trimToEmpty(source));
|
||||
long pageSize = normalizePageSize(size);
|
||||
filter.setLimit(pageSize);
|
||||
filter.setOffset((normalizePage(page) - 1) * pageSize);
|
||||
|
||||
BrandGatingPageVo vo = new BrandGatingPageVo();
|
||||
vo.setTotal(brandGatingMapper.countByFilter(filter));
|
||||
List<BrandGatingRowVo> rows = brandGatingMapper.selectRows(filter);
|
||||
vo.setRows(rows);
|
||||
return vo;
|
||||
}
|
||||
|
||||
public BrandGatingStatsVo stats() {
|
||||
BrandGatingStatsVo vo = new BrandGatingStatsVo();
|
||||
vo.setTotal(brandGatingMapper.countAll());
|
||||
vo.setActive(brandGatingMapper.countByStatus(1));
|
||||
vo.setDisabled(brandGatingMapper.countByStatus(0));
|
||||
vo.setBrands(brandGatingMapper.countDistinctBrands());
|
||||
vo.setByType(brandGatingMapper.countGroupByType());
|
||||
vo.setByCountry(brandGatingMapper.countGroupByCountry());
|
||||
vo.setBySource(brandGatingMapper.countGroupBySource());
|
||||
return vo;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 单条操作
|
||||
|
||||
public BrandGatingAddResultVo add(BrandGatingAddRequest request) {
|
||||
BrandGatingAddResultVo vo = new BrandGatingAddResultVo();
|
||||
String brand = trimToEmpty(request.getBrand());
|
||||
if (brand.isEmpty()) {
|
||||
throw new BusinessException(400, "品牌不能为空");
|
||||
}
|
||||
BrandGatingEntity entity = new BrandGatingEntity();
|
||||
entity.setBrand(brand);
|
||||
applyOptionalDefaults(entity, request.getAsin(), request.getCountry(),
|
||||
request.getGatingType(), request.getRemark());
|
||||
entity.setSource(firstNonBlank(request.getSource(), SOURCE_MANUAL));
|
||||
entity.setShopName(trimToNull(request.getShopName()));
|
||||
// 撞唯一键 = 已存在同口径行:桌面版 add_gating 捕 IntegrityError 返回 False,这里同语义回 0
|
||||
try {
|
||||
brandGatingMapper.insert(entity);
|
||||
vo.setAdded(1);
|
||||
} catch (DuplicateKeyException ex) {
|
||||
vo.setAdded(0);
|
||||
}
|
||||
return vo;
|
||||
}
|
||||
|
||||
public BrandGatingDeleteResultVo delete(Long id) {
|
||||
BrandGatingDeleteResultVo vo = new BrandGatingDeleteResultVo();
|
||||
vo.setDeleted(brandGatingMapper.deleteById(id));
|
||||
return vo;
|
||||
}
|
||||
|
||||
public BrandGatingStatusResultVo updateStatus(Long id, Integer status) {
|
||||
if (status == null || (status != 0 && status != 1)) {
|
||||
throw new BusinessException(400, "status 只能为 0(停用)或 1(生效)");
|
||||
}
|
||||
BrandGatingStatusResultVo vo = new BrandGatingStatusResultVo();
|
||||
vo.setUpdated(brandGatingMapper.updateStatus(id, status));
|
||||
return vo;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 批量导入
|
||||
|
||||
public BrandGatingImportResultVo importRows(BrandGatingImportRequest request) {
|
||||
List<BrandGatingImportRequest.Item> items = resolveImportItems(request);
|
||||
BrandGatingImportResultVo vo = new BrandGatingImportResultVo();
|
||||
Set<String> seen = new HashSet<>();
|
||||
for (BrandGatingImportRequest.Item item : items) {
|
||||
String brand = trimToEmpty(item.getBrand());
|
||||
String country = firstNonBlank(item.getCountry(), DEFAULT_COUNTRY);
|
||||
String gatingType = firstNonBlank(item.getGatingType(), DEFAULT_GATING_TYPE);
|
||||
String asin = trimToEmpty(item.getAsin());
|
||||
if (brand.isEmpty()) {
|
||||
// 文本解析路径不会产出空品牌(空段被丢弃),items 直传路径在这里拦下
|
||||
vo.setSkipped(vo.getSkipped() + 1);
|
||||
continue;
|
||||
}
|
||||
String dedupKey = String.format(IMPORT_DEDUP_KEY_FORMAT, brand, country, gatingType, asin);
|
||||
if (!seen.add(dedupKey)) {
|
||||
vo.setSkipped(vo.getSkipped() + 1);
|
||||
continue;
|
||||
}
|
||||
BrandGatingEntity entity = new BrandGatingEntity();
|
||||
entity.setBrand(brand);
|
||||
entity.setAsin(asin.isEmpty() ? null : asin);
|
||||
entity.setCountry(country);
|
||||
entity.setGatingType(gatingType);
|
||||
entity.setSource(SOURCE_MANUAL);
|
||||
entity.setShopName(null);
|
||||
entity.setRemark(item.getRemark() == null ? "" : item.getRemark().strip());
|
||||
// 撞唯一键 = 库里已有同口径行(桌面版 batch_add 捕 IntegrityError 计 skipped 的同语义)
|
||||
try {
|
||||
brandGatingMapper.insert(entity);
|
||||
vo.setAdded(vo.getAdded() + 1);
|
||||
} catch (DuplicateKeyException ex) {
|
||||
vo.setSkipped(vo.getSkipped() + 1);
|
||||
}
|
||||
}
|
||||
return vo;
|
||||
}
|
||||
|
||||
/** text 优先(照桌面版行格式解析),text 为空回退 items 直传;两者都缺 → 400。 */
|
||||
private List<BrandGatingImportRequest.Item> resolveImportItems(BrandGatingImportRequest request) {
|
||||
if (request == null) {
|
||||
throw new BusinessException(400, "导入内容为空");
|
||||
}
|
||||
if (request.getText() != null && !request.getText().isBlank()) {
|
||||
return parseBatchText(request.getText());
|
||||
}
|
||||
if (request.getItems() != null && !request.getItems().isEmpty()) {
|
||||
return request.getItems();
|
||||
}
|
||||
throw new BusinessException(400, "导入内容为空");
|
||||
}
|
||||
|
||||
/**
|
||||
* 桌面版 parse_batch(gating_web_admin.py L208-223)的逐行移植:
|
||||
* <ul>
|
||||
* <li>按行拆分;每行去首尾空白与 BOM(\uFEFF);</li>
|
||||
* <li>空行与 # 开头的注释行跳过;</li>
|
||||
* <li>行内按中英文逗号/分号/制表符([,\t;,;])切分,各段去空白后丢掉空段;</li>
|
||||
* <li>字段序:品牌、国家(缺省「全部」)、Gating类型(缺省「品牌gating」)、ASIN(缺省空)、
|
||||
* 备注(缺省空);第 5 列之后的段照桌面版丢弃。</li>
|
||||
* </ul>
|
||||
*/
|
||||
public static List<BrandGatingImportRequest.Item> parseBatchText(String text) {
|
||||
List<BrandGatingImportRequest.Item> out = new ArrayList<>();
|
||||
if (text == null || text.isBlank()) {
|
||||
return out;
|
||||
}
|
||||
for (String rawLine : text.split("\\R")) {
|
||||
String line = rawLine.strip();
|
||||
if (line.startsWith("\uFEFF")) {
|
||||
line = line.substring(1).strip();
|
||||
}
|
||||
if (line.isEmpty() || line.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
List<String> parts = Arrays.stream(line.split("[,\\t;,;]"))
|
||||
.map(String::strip)
|
||||
.filter(part -> !part.isEmpty())
|
||||
.toList();
|
||||
if (parts.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
BrandGatingImportRequest.Item item = new BrandGatingImportRequest.Item();
|
||||
item.setBrand(parts.get(0));
|
||||
item.setCountry(parts.size() > 1 ? parts.get(1) : DEFAULT_COUNTRY);
|
||||
item.setGatingType(parts.size() > 2 ? parts.get(2) : DEFAULT_GATING_TYPE);
|
||||
item.setAsin(parts.size() > 3 ? parts.get(3) : "");
|
||||
item.setRemark(parts.size() > 4 ? parts.get(4) : "");
|
||||
out.add(item);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 私有工具
|
||||
|
||||
/** 桌面版 add/batch_add 的可选字段默认值:asin/shop_name 空→NULL、country/gating_type 空→默认、remark 空→空串。 */
|
||||
private void applyOptionalDefaults(BrandGatingEntity entity, String asin, String country,
|
||||
String gatingType, String remark) {
|
||||
entity.setAsin(trimToNull(asin));
|
||||
entity.setCountry(firstNonBlank(country, DEFAULT_COUNTRY));
|
||||
entity.setGatingType(firstNonBlank(gatingType, DEFAULT_GATING_TYPE));
|
||||
entity.setRemark(remark == null ? "" : remark.strip());
|
||||
}
|
||||
|
||||
private static String trimToEmpty(String value) {
|
||||
return value == null ? "" : value.strip();
|
||||
}
|
||||
|
||||
private static String trimToNull(String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
String trimmed = value.strip();
|
||||
return trimmed.isEmpty() ? null : trimmed;
|
||||
}
|
||||
|
||||
private static String firstNonBlank(String value, String fallback) {
|
||||
String trimmed = trimToNull(value);
|
||||
return trimmed == null ? fallback : trimmed;
|
||||
}
|
||||
|
||||
/** "1"→1、"0"→0,其余(含空串与「全部」)→ null = 不限。 */
|
||||
private static Integer parseStatusFilter(String status) {
|
||||
if ("1".equals(status)) {
|
||||
return 1;
|
||||
}
|
||||
if ("0".equals(status)) {
|
||||
return 0;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static long normalizePage(Long page) {
|
||||
if (page == null || page < 1) {
|
||||
return 1;
|
||||
}
|
||||
return page;
|
||||
}
|
||||
|
||||
private static long normalizePageSize(Long size) {
|
||||
if (size == null || size < 1) {
|
||||
return DEFAULT_PAGE_SIZE;
|
||||
}
|
||||
return Math.min(size, MAX_PAGE_SIZE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.nanri.aiimage.modules.brandgating.mapper.BrandGatingMapper">
|
||||
<!-- 品牌Gating库(biz_brand_gating)。行为口径对齐桌面版 gating_web_admin.py:
|
||||
列表筛选(list_gating L138-162)、统计(stats L165-178)、启停(set_status_gating L196-201)。
|
||||
动态筛选全部走 #{} 占位符,不做任何字符串拼接。 -->
|
||||
|
||||
<!-- 「全部」在桌面版里就是“不限”的哨兵值(也是表内真实取值),与空串一并放行不过滤 -->
|
||||
<sql id="filterConds">
|
||||
<if test="f.keyword != null and f.keyword != ''">
|
||||
AND (brand LIKE CONCAT('%', #{f.keyword}, '%') OR asin LIKE CONCAT('%', #{f.keyword}, '%'))
|
||||
</if>
|
||||
<if test="f.country != null and f.country != '' and f.country != '全部'">AND country = #{f.country}</if>
|
||||
<if test="f.gatingType != null and f.gatingType != '' and f.gatingType != '全部'">AND gating_type = #{f.gatingType}</if>
|
||||
<if test="f.status != null">AND status = #{f.status}</if>
|
||||
<if test="f.source != null and f.source != '' and f.source != '全部'">AND source = #{f.source}</if>
|
||||
</sql>
|
||||
|
||||
<select id="countByFilter" resultType="long">
|
||||
SELECT COUNT(*)
|
||||
FROM biz_brand_gating
|
||||
<where>
|
||||
<include refid="filterConds"/>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<select id="selectRows" resultType="com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingRowVo">
|
||||
SELECT id, brand, asin, country, gating_type, status, source, shop_name, remark,
|
||||
created_at, updated_at, disabled_at
|
||||
FROM biz_brand_gating
|
||||
<where>
|
||||
<include refid="filterConds"/>
|
||||
</where>
|
||||
ORDER BY id DESC
|
||||
LIMIT #{f.offset}, #{f.limit}
|
||||
</select>
|
||||
|
||||
<select id="countAll" resultType="long">
|
||||
SELECT COUNT(*) FROM biz_brand_gating
|
||||
</select>
|
||||
|
||||
<select id="countByStatus" resultType="long">
|
||||
SELECT COUNT(*) FROM biz_brand_gating WHERE status = #{status}
|
||||
</select>
|
||||
|
||||
<select id="countDistinctBrands" resultType="long">
|
||||
SELECT COUNT(DISTINCT brand) FROM biz_brand_gating WHERE status = 1
|
||||
</select>
|
||||
|
||||
<select id="countGroupByType" resultType="com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingNameCountRow">
|
||||
SELECT gating_type AS name, COUNT(*) AS count
|
||||
FROM biz_brand_gating
|
||||
WHERE status = 1
|
||||
GROUP BY gating_type
|
||||
ORDER BY count DESC
|
||||
</select>
|
||||
|
||||
<select id="countGroupByCountry" resultType="com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingNameCountRow">
|
||||
SELECT country AS name, COUNT(*) AS count
|
||||
FROM biz_brand_gating
|
||||
WHERE status = 1
|
||||
GROUP BY country
|
||||
ORDER BY count DESC
|
||||
</select>
|
||||
|
||||
<select id="countGroupBySource" resultType="com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingNameCountRow">
|
||||
SELECT source AS name, COUNT(*) AS count
|
||||
FROM biz_brand_gating
|
||||
WHERE status = 1
|
||||
GROUP BY source
|
||||
ORDER BY count DESC
|
||||
</select>
|
||||
|
||||
<!-- 启停:status=1 清 disabled_at、=0 记 disabled_at=NOW(),两种都刷 updated_at=NOW()
|
||||
(桌面版 set_status_gating 的两条 UPDATE 合一;IF 走 #{} 参数,不拼接) -->
|
||||
<update id="updateStatus">
|
||||
UPDATE biz_brand_gating
|
||||
SET status = #{status},
|
||||
disabled_at = IF(#{status} = 1, NULL, NOW()),
|
||||
updated_at = NOW()
|
||||
WHERE id = #{id}
|
||||
</update>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user