feat(appearance-patent): 外观专利数据库主管列+导出+分组筛选——后管只读页补「所属主管」读侧现算列(owner 是主管取自己、普通账户取创建人,铁律 30 读不到不猜名)、GET /export 与分页共用同一过滤面与正门出 xlsx(SXSSF,上限 5 万行超限拒)、分组筛选 group_id 按 selectUserIdsByGroupId 解析「组长本人+名下子账户」整集 SQL 下推(铁律 20,选项复用既有 GET /shop-manages/groups 不下新端点);前端加主管列/分组下拉/导出按钮(blob 落盘+业务错误信封解包),wiring 护栏补导出发送口钉与 watch token(25 处发送口);棘轮 1073→1074 一枚记名,「无侵权」白名单登记导出上限文案
This commit is contained in:
+51
-4
@@ -7,6 +7,7 @@ import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.common.security.DeviceSessionPolicy;
|
||||
import com.nanri.aiimage.common.service.DataReadScope;
|
||||
import com.nanri.aiimage.common.support.DownloadHeaderUtil;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentCleanItemPageVo;
|
||||
import com.nanri.aiimage.modules.appearancepatent.service.AppearancePatentCleanItemQueryService;
|
||||
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
|
||||
@@ -19,17 +20,26 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* 后管「外观专利无侵权数据」只读页(v1.3 R2/R3)。数据由出表成功点自动同步落表
|
||||
* ({@code AppearancePatentCleanSyncService}),本页只读——无写/删/导出接口,
|
||||
* 下载走既有结果直链/任务中心。
|
||||
* ({@code AppearancePatentCleanSyncService}),本页只读——无写/删接口,下载走既有结果直链/
|
||||
* 任务中心;另有 GET 导出端点(2026-11-11 客户需求「数据得导出来」):与分页查询同一过滤面
|
||||
* (含公司隔离正门),服务侧卡导出上限,主管列为读侧现算列,无写/删/导入;分组筛选同日落地
|
||||
* (客户口径「主管就是组长,可以筛选组长来导出,就是分组」)——group_id 参数(biz_shop_manage_group.id)
|
||||
* 在分页与导出同面生效,服务侧按 selectUserIdsByGroupId 解析「组长本人+名下子账户」整集后
|
||||
* SQL 侧下推;下拉选项复用既有 GET /shop-manages/groups(与去重页/店铺管理同源),不下新端点。
|
||||
*
|
||||
* <p>鉴权照去重页私有 {@code requireXxxAccess} 形状:JWT → 内部令牌回退 → 超管放行 →
|
||||
* 非超管菜单权限(column_key/route_path 双匹配)。菜单行由 V190 只插 columns 不预授权
|
||||
@@ -38,11 +48,14 @@ import java.time.LocalDate;
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/admin/appearance-patent-clean-items")
|
||||
@Tag(name = "外观专利无侵权数据", description = "后管只读页:出表成功自动同步的「结论=无侵权」明细,按分公司隔离分页查询。")
|
||||
@Tag(name = "外观专利无侵权数据", description = "后管只读页:出表成功自动同步的「结论=无侵权」明细,按分公司隔离分页查询与导出。")
|
||||
public class AdminAppearancePatentCleanItemsController {
|
||||
|
||||
private static final String CLEAN_ITEMS_COLUMN_KEY = "admin_appearance_patent_clean_items";
|
||||
private static final String CLEAN_ITEMS_ROUTE_PATH = "patent-center/clean-items";
|
||||
private static final String XLSX_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||||
/** 导出文件名时间戳(同 QueryAsinController 口径),中文文件名的 ASCII 回退由 DownloadHeaderUtil 兜底。 */
|
||||
private static final DateTimeFormatter EXPORT_FILENAME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
|
||||
|
||||
private final AppearancePatentCleanItemQueryService cleanItemQueryService;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
@@ -65,6 +78,8 @@ public class AdminAppearancePatentCleanItemsController {
|
||||
@RequestParam(name = "start_date", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
|
||||
@Parameter(description = "同步结束日期(包含)")
|
||||
@RequestParam(name = "end_date", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
|
||||
@Parameter(description = "分组 id:按该分组「组长本人+名下子账户」的用户集过滤(主管=组长)")
|
||||
@RequestParam(name = "group_id", required = false) Long groupId,
|
||||
@Parameter(description = "公司 ID") @RequestParam(name = "company_id", required = false) Long companyId,
|
||||
HttpServletRequest request) {
|
||||
RequestOperator operator = requireCleanItemsAccess(request);
|
||||
@@ -75,7 +90,39 @@ public class AdminAppearancePatentCleanItemsController {
|
||||
// 等于把整张表交给每一家(该方法 javadoc 点名的越权形状),不是本表该走的门。
|
||||
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(user, companyId);
|
||||
return ApiResponse.success(cleanItemQueryService.page(page, pageSize, taskId, asin, country,
|
||||
startDate, endDate, scopedCompanyId));
|
||||
startDate, endDate, groupId, scopedCompanyId));
|
||||
}
|
||||
|
||||
@GetMapping("/export")
|
||||
@Operation(summary = "导出外观专利数据库 Excel", description = "与分页查询同一过滤面(任务号/ASIN/国家/同步起止日期/分组),"
|
||||
+ "公司隔离走同一正门:company_id 只对站点超管生效(显式传才收窄,不传全量),非超管强制本公司。"
|
||||
+ "含所属主管列;超过导出上限按业务错误拒绝(QueryAsinController 导出 javadoc 点名的越权形状在此不成立:"
|
||||
+ "页签筛了公司、导出就只导该公司)。")
|
||||
public ResponseEntity<byte[]> export(
|
||||
@Parameter(description = "任务号") @RequestParam(name = "task_id", required = false) Long taskId,
|
||||
@Parameter(description = "ASIN 前缀") @RequestParam(name = "asin", required = false) String asin,
|
||||
@Parameter(description = "国家代码(如 DE、UK)") @RequestParam(name = "country", required = false) String country,
|
||||
@Parameter(description = "同步开始日期(包含)")
|
||||
@RequestParam(name = "start_date", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
|
||||
@Parameter(description = "同步结束日期(包含)")
|
||||
@RequestParam(name = "end_date", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
|
||||
@Parameter(description = "分组 id:按该分组「组长本人+名下子账户」的用户集过滤(主管=组长)")
|
||||
@RequestParam(name = "group_id", required = false) Long groupId,
|
||||
@Parameter(description = "公司 ID") @RequestParam(name = "company_id", required = false) Long companyId,
|
||||
HttpServletRequest request) {
|
||||
RequestOperator operator = requireCleanItemsAccess(request);
|
||||
AdminUserEntity user = operator.user();
|
||||
// 导出与分页共用 readCompanyIdOrOwn 正门(同 page 的口径注释):超管显式传才收窄、
|
||||
// 不传全量;非超管强制本公司(忽略请求传参),绝不让导出绕过页签的公司隔离。
|
||||
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(user, companyId);
|
||||
byte[] bytes = cleanItemQueryService.export(taskId, asin, country, startDate, endDate, groupId,
|
||||
scopedCompanyId);
|
||||
String filename = "外观专利数据库-" + LocalDateTime.now().format(EXPORT_FILENAME_FORMATTER) + ".xlsx";
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, DownloadHeaderUtil.contentDisposition(filename))
|
||||
.contentType(MediaType.parseMediaType(XLSX_CONTENT_TYPE))
|
||||
.contentLength(bytes.length)
|
||||
.body(bytes);
|
||||
}
|
||||
|
||||
private RequestOperator requireCleanItemsAccess(HttpServletRequest request) {
|
||||
|
||||
+7
-1
@@ -15,6 +15,10 @@ import java.util.List;
|
||||
* 覆盖式落表({@link #deleteByResultId} + {@link #insertBatch});后管只读页
|
||||
* (Phase 3)走 {@link #countCleanItems}/{@link #selectCleanItemPage}。SQL 全部落
|
||||
* {@code mapper/appearancepatent/AppearancePatentCleanItemMapper.xml}(铁律 1)。</p>
|
||||
*
|
||||
* <p>{@code userIds}(2026-11-11 分组筛选,主管=组长):服务侧由 {@code ShopManageGroupMapper
|
||||
* #selectUserIdsByGroupId} 按分组解析出「组长本人+名下子账户」的 id 集后整集传入,
|
||||
* <b>SQL 侧下推</b>——不得在 Java 侧对已取页做过滤(铁律 20:先 limit 再过滤会饿死另一档)。</p>
|
||||
*/
|
||||
@Mapper
|
||||
public interface AppearancePatentCleanItemMapper extends BaseMapper<CleanItemEntity> {
|
||||
@@ -36,7 +40,8 @@ public interface AppearancePatentCleanItemMapper extends BaseMapper<CleanItemEnt
|
||||
@Param("asin") String asin,
|
||||
@Param("country") String country,
|
||||
@Param("startDate") LocalDate startDate,
|
||||
@Param("endDate") LocalDate endDate);
|
||||
@Param("endDate") LocalDate endDate,
|
||||
@Param("userIds") List<Long> userIds);
|
||||
|
||||
/**
|
||||
* 只读页分页查询(synced_at 倒序)。
|
||||
@@ -51,6 +56,7 @@ public interface AppearancePatentCleanItemMapper extends BaseMapper<CleanItemEnt
|
||||
@Param("country") String country,
|
||||
@Param("startDate") LocalDate startDate,
|
||||
@Param("endDate") LocalDate endDate,
|
||||
@Param("userIds") List<Long> userIds,
|
||||
@Param("offset") long offset,
|
||||
@Param("limit") int limit);
|
||||
}
|
||||
|
||||
+7
-1
@@ -8,7 +8,9 @@ import java.time.LocalDateTime;
|
||||
/**
|
||||
* 外观专利无侵权数据明细行(后管只读页,v1.3 R3)。字段与 {@code biz_appearance_patent_clean_item}
|
||||
* 表列(V189)一一对应——行值是同步服务从 chunk payload 原样落的(不 trim、不转写,AC-1
|
||||
* 「与 payload 一致」),读侧只做搬运不做二次加工。
|
||||
* 「与 payload 一致」),读侧只搬运不做二次加工。唯一例外是 {@code supervisorName}:
|
||||
* 表里没有这一列,查询服务在读侧按「行属用户 → 归属主管」现算补上(2026-11-11 客户需求
|
||||
* 「显示哪一个主管的」),不落库、不回写同步链。
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "外观专利无侵权数据明细行")
|
||||
@@ -26,6 +28,10 @@ public class AppearancePatentCleanItemVo {
|
||||
@Schema(description = "任务 owner 用户 id")
|
||||
private Long userId;
|
||||
|
||||
@Schema(description = "所属主管(读侧现算:owner 是主管/公司超管=自己的用户名,普通账户=创建人的用户名;"
|
||||
+ "用户读不到为空,前端展示 -)")
|
||||
private String supervisorName;
|
||||
|
||||
@Schema(description = "物化归属分公司 id")
|
||||
private Long companyId;
|
||||
|
||||
|
||||
+249
-6
@@ -2,18 +2,32 @@ package com.nanri.aiimage.modules.appearancepatent.service;
|
||||
|
||||
import com.nanri.aiimage.common.api.PageDefaults;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.mapper.AdminUserMapper;
|
||||
import com.nanri.aiimage.common.mapper.ShopManageGroupMapper;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.security.DeviceSessionPolicy;
|
||||
import com.nanri.aiimage.modules.appearancepatent.mapper.AppearancePatentCleanItemMapper;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.entity.CleanItemEntity;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentCleanItemPageVo;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentCleanItemVo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 后管「外观专利无侵权数据」只读查询(v1.3 R2)。
|
||||
* 后管「外观专利无侵权数据」只读查询(v1.3 R2)+ 导出(2026-11-11 客户需求「数据得导出来」)。
|
||||
*
|
||||
* <p>分页钳制是本服务的职责(铁律 15):mapper 的 {@code offset}/{@code limit} 只收钳制后的值
|
||||
* (Phase 1 定下的单一出口,mapper 不做第二次换算)。钳制形状照关键词服务 CR-18 先例:
|
||||
@@ -24,7 +38,29 @@ import java.util.List;
|
||||
* <p>{@code scopedCompanyId} 由控制器经 {@code DataReadScope#readCompanyIdOrOwn} 算好传入
|
||||
* (本表与店铺密钥同形状:归属物化在行 {@code company_id} 列、可见集不靠账号/分组):
|
||||
* {@code null} = 超管没选公司、查全量;正数 = 只查该公司(非超管恒为自己的公司)。</p>
|
||||
*
|
||||
* <p><b>所属主管(supervisorName)</b>是唯一的读侧加工列(表 V189 没有该列,同步链一字不动):
|
||||
* 批量取行属用户后按 {@link DeviceSessionPolicy#resolveRole} 分档——owner 是 admin/公司超管/
|
||||
* 站点超管=主管本人,取自己的用户名;普通账户(role 为 null)=创建人({@code users.created_by_id},
|
||||
* 与密钥管理页「所属主管(创建人)」同口径,AccountExpiryService 也认这条为「普通账户的归属主管」)
|
||||
* 的用户名。用户读不到=展示为空(铁律 30:读不出≠没有,不猜名字),全程两次批量查询,无逐行查询。</p>
|
||||
*
|
||||
* <p><b>分组(主管)筛选</b>(2026-11-11 客户口径「主管就是组长,可以筛选组长来导出,就是分组」):
|
||||
* {@code groupId} 是分组管理里那一行的 {@code biz_shop_manage_group.id},服务侧经
|
||||
* {@link ShopManageGroupMapper#selectUserIdsByGroupId} 一次取出「该分组创建人(组长)本人+
|
||||
* 其名下子账户」的 id 集(与数据可见范围同源:组长旧字段 user_id/新字段 created_by_id 都算,
|
||||
* 成员表基本未使用)后<b>整集下推 SQL</b>({@code user_id IN},铁律 20:不得取页后 Java 过滤)。
|
||||
* 选项不下新端点:后管下拉直接复用既有 {@code GET /shop-manages/groups}(与去重页/店铺管理
|
||||
* 共用同一分组集)。行上「所属主管」列的展示口径与它同源:组长本人行=自己,名下子账户行=
|
||||
* 创建人(创建人即组长,AccountExpiryService 同一口径)。</p>
|
||||
*
|
||||
* <p><b>导出</b>与分页共用同一过滤面与 {@code selectCleanItemPage} 这一条 SQL(offset=0、
|
||||
* limit=导出上限,不另写导出查询),先 {@code countCleanItems} 卡 {@link #EXPORT_MAX_ROWS}
|
||||
* 上限防内存,行集经同一 {@code toVo}+主管富化后按后管页面的列序写 xlsx(SXSSF 流式,
|
||||
* 口径同 {@code QueryAsinService.export} 的 byte[] 形状)。国家列落中文标签,
|
||||
* 字典与前端 {@code asin-country.ts}(ASIN 数据中心共享国家字典)同表。</p>
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class AppearancePatentCleanItemQueryService {
|
||||
@@ -33,17 +69,35 @@ public class AppearancePatentCleanItemQueryService {
|
||||
private static final long MAX_PAGE = 10_000;
|
||||
/** 每页条数上限(蓝图 §3.2)。 */
|
||||
private static final long MAX_PAGE_SIZE = 100;
|
||||
/** 单次导出行数上限:超限按业务错误拒(提示缩小筛选面),不把无界内存写进一次 HTTP 响应。 */
|
||||
public static final long EXPORT_MAX_ROWS = 50_000;
|
||||
/** 导出同步时间列的展示格式。 */
|
||||
private static final DateTimeFormatter EXPORT_SYNCED_AT_FORMATTER =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
/** 导出列序(与后管页面同序,主管列插在结论后、同步时间前)。 */
|
||||
private static final List<String> EXPORT_HEADERS = List.of(
|
||||
"任务号", "ASIN", "国家", "SKU", "品牌", "价格", "标题", "结论", "所属主管", "同步时间");
|
||||
/** 国家码 → 中文标签:与前端 asin-country.ts 同表,不在字典的原样回显(读侧不加工未知值)。 */
|
||||
private static final Map<String, String> COUNTRY_LABELS = Map.of(
|
||||
"DE", "德国",
|
||||
"UK", "英国",
|
||||
"FR", "法国",
|
||||
"IT", "意大利",
|
||||
"ES", "西班牙");
|
||||
|
||||
private final AppearancePatentCleanItemMapper cleanItemMapper;
|
||||
private final AdminUserMapper adminUserMapper;
|
||||
private final ShopManageGroupMapper shopManageGroupMapper;
|
||||
|
||||
/**
|
||||
* 分页查询只读明细({@code synced_at} 倒序)。
|
||||
*
|
||||
* <p>过滤面:公司(物化列 eq)、任务号、ASIN 前缀(保 {@code idx_asin} 可用,同去重页口径)、
|
||||
* 国家、同步起止日期(闭区间,含 end 当天)。</p>
|
||||
* 国家、同步起止日期(闭区间,含 end 当天)、分组({@code user_id IN 组长名下用户集})。</p>
|
||||
*/
|
||||
public AppearancePatentCleanItemPageVo page(Long page, Long pageSize, Long taskId, String asin,
|
||||
String country, LocalDate startDate, LocalDate endDate, Long scopedCompanyId) {
|
||||
String country, LocalDate startDate, LocalDate endDate, Long groupId, Long scopedCompanyId) {
|
||||
if (startDate != null && endDate != null && startDate.isAfter(endDate)) {
|
||||
throw new BusinessException("开始日期不能晚于结束日期");
|
||||
}
|
||||
@@ -53,18 +107,207 @@ public class AppearancePatentCleanItemQueryService {
|
||||
Long companyId = scopedCompanyId != null && scopedCompanyId > 0 ? scopedCompanyId : null;
|
||||
String safeAsin = trimToNull(asin);
|
||||
String safeCountry = trimToNull(country);
|
||||
long total = cleanItemMapper.countCleanItems(companyId, taskId, safeAsin, safeCountry, startDate, endDate);
|
||||
List<Long> groupUserIds = groupUserIds(groupId);
|
||||
if (groupUserIds != null && groupUserIds.isEmpty()) {
|
||||
// 分组读不出任何用户(如分组已被删):结果必为空,短路不碰 mapper。
|
||||
return emptyPage(safePage, safeSize);
|
||||
}
|
||||
long total = cleanItemMapper.countCleanItems(companyId, taskId, safeAsin, safeCountry, startDate, endDate,
|
||||
groupUserIds);
|
||||
List<CleanItemEntity> rows = total <= 0 ? List.of()
|
||||
: cleanItemMapper.selectCleanItemPage(companyId, taskId, safeAsin, safeCountry, startDate, endDate,
|
||||
(safePage - 1) * safeSize, (int) safeSize);
|
||||
groupUserIds, (safePage - 1) * safeSize, (int) safeSize);
|
||||
List<AppearancePatentCleanItemVo> items = rows.stream().map(AppearancePatentCleanItemQueryService::toVo).toList();
|
||||
fillSupervisorNames(items);
|
||||
AppearancePatentCleanItemPageVo vo = new AppearancePatentCleanItemPageVo();
|
||||
vo.setItems(rows.stream().map(AppearancePatentCleanItemQueryService::toVo).toList());
|
||||
vo.setItems(items);
|
||||
vo.setTotal(total);
|
||||
vo.setPage(safePage);
|
||||
vo.setPageSize(safeSize);
|
||||
return vo;
|
||||
}
|
||||
|
||||
private static AppearancePatentCleanItemPageVo emptyPage(long page, long pageSize) {
|
||||
AppearancePatentCleanItemPageVo vo = new AppearancePatentCleanItemPageVo();
|
||||
vo.setItems(List.of());
|
||||
vo.setTotal(0L);
|
||||
vo.setPage(page);
|
||||
vo.setPageSize(pageSize);
|
||||
return vo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出当前过滤面全量明细为 xlsx 字节(控制器直接回 {@code ResponseEntity<byte[]>},
|
||||
* 口径同 {@code QueryAsinService.export})。
|
||||
*
|
||||
* <p>先 count 卡 {@link #EXPORT_MAX_ROWS} 上限再取数;取数复用 {@code selectCleanItemPage}
|
||||
* 这一条 SQL(offset=0、limit=上限),不另写导出查询——过滤面永远和分页页签同源,
|
||||
* 不会出现「页签筛了公司、导出全量」的越权形状(QueryAsinController 导出 javadoc 点名的语义)。</p>
|
||||
*/
|
||||
public byte[] export(Long taskId, String asin, String country, LocalDate startDate, LocalDate endDate,
|
||||
Long groupId, Long scopedCompanyId) {
|
||||
if (startDate != null && endDate != null && startDate.isAfter(endDate)) {
|
||||
throw new BusinessException("开始日期不能晚于结束日期");
|
||||
}
|
||||
Long companyId = scopedCompanyId != null && scopedCompanyId > 0 ? scopedCompanyId : null;
|
||||
String safeAsin = trimToNull(asin);
|
||||
String safeCountry = trimToNull(country);
|
||||
List<Long> groupUserIds = groupUserIds(groupId);
|
||||
if (groupUserIds != null && groupUserIds.isEmpty()) {
|
||||
// 与分页同口径:分组读不出用户=空结果,导出只给表头(不碰 mapper)。
|
||||
return writeExportWorkbook(List.of());
|
||||
}
|
||||
long total = cleanItemMapper.countCleanItems(companyId, taskId, safeAsin, safeCountry, startDate, endDate,
|
||||
groupUserIds);
|
||||
if (total > EXPORT_MAX_ROWS) {
|
||||
throw new BusinessException("无侵权数据共 " + total + " 行,超过导出上限 " + EXPORT_MAX_ROWS
|
||||
+ " 行,请缩小筛选范围后重试");
|
||||
}
|
||||
List<CleanItemEntity> rows = total <= 0 ? List.of()
|
||||
: cleanItemMapper.selectCleanItemPage(companyId, taskId, safeAsin, safeCountry, startDate, endDate,
|
||||
groupUserIds, 0L, (int) EXPORT_MAX_ROWS);
|
||||
List<AppearancePatentCleanItemVo> items = rows.stream().map(AppearancePatentCleanItemQueryService::toVo).toList();
|
||||
fillSupervisorNames(items);
|
||||
return writeExportWorkbook(items);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分组筛选 → 该分组「组长本人+名下子账户」的用户 id 集({@code null} = 没选分组、不过滤);
|
||||
* id 非法(≤0)按没选处理,不把它当全量之外的第三档。解析交给
|
||||
* {@link ShopManageGroupMapper#selectUserIdsByGroupId}(组长旧字段 user_id/新字段
|
||||
* created_by_id 都算,与数据可见范围同源),分组不存在/无成员返回空集由调用方短路。
|
||||
*/
|
||||
private List<Long> groupUserIds(Long groupId) {
|
||||
if (groupId == null || groupId <= 0) {
|
||||
return null;
|
||||
}
|
||||
List<Long> userIds = shopManageGroupMapper.selectUserIdsByGroupId(groupId);
|
||||
return userIds == null ? List.of() : userIds;
|
||||
}
|
||||
|
||||
private byte[] writeExportWorkbook(List<AppearancePatentCleanItemVo> items) {
|
||||
try (SXSSFWorkbook workbook = new SXSSFWorkbook(100);
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
||||
Sheet sheet = workbook.createSheet("外观专利数据库");
|
||||
Row header = sheet.createRow(0);
|
||||
for (int c = 0; c < EXPORT_HEADERS.size(); c++) {
|
||||
header.createCell(c).setCellValue(EXPORT_HEADERS.get(c));
|
||||
}
|
||||
for (int i = 0; i < items.size(); i++) {
|
||||
writeExportRow(sheet, i + 1, items.get(i));
|
||||
}
|
||||
// 标题列放宽,其余走默认窄列;与页面列宽观感对齐即可,不追求逐像素。
|
||||
sheet.setColumnWidth(6, 46 * 256);
|
||||
sheet.setColumnWidth(2, 10 * 256);
|
||||
workbook.write(outputStream);
|
||||
workbook.dispose();
|
||||
return outputStream.toByteArray();
|
||||
} catch (Exception ex) {
|
||||
// 落盘/序列化失败必须给可读业务错误(用户手里的导出请求不能变成 500 裸堆栈),
|
||||
// 细节进日志;CatchSilenceRatchetTest 基线随本支 +1(RETHROW 形状)。
|
||||
log.warn("[appearance-patent-clean] 导出 xlsx 失败 rows={} msg={}", items.size(), ex.getMessage());
|
||||
throw new BusinessException("导出外观专利数据库失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
private void writeExportRow(Sheet sheet, int rowIndex, AppearancePatentCleanItemVo item) {
|
||||
Row row = sheet.createRow(rowIndex);
|
||||
row.createCell(0).setCellValue(item.getTaskId() == null ? "" : String.valueOf(item.getTaskId()));
|
||||
row.createCell(1).setCellValue(safe(item.getAsin()));
|
||||
row.createCell(2).setCellValue(countryLabel(item.getCountry()));
|
||||
row.createCell(3).setCellValue(safe(item.getSku()));
|
||||
row.createCell(4).setCellValue(safe(item.getBrand()));
|
||||
row.createCell(5).setCellValue(safe(item.getPrice()));
|
||||
row.createCell(6).setCellValue(safe(item.getTitle()));
|
||||
row.createCell(7).setCellValue(safe(item.getConclusion()));
|
||||
row.createCell(8).setCellValue(safe(item.getSupervisorName()));
|
||||
row.createCell(9).setCellValue(item.getSyncedAt() == null
|
||||
? "" : EXPORT_SYNCED_AT_FORMATTER.format(item.getSyncedAt()));
|
||||
}
|
||||
|
||||
private static String countryLabel(String country) {
|
||||
if (country == null || country.isBlank()) {
|
||||
return "";
|
||||
}
|
||||
return COUNTRY_LABELS.getOrDefault(country.trim(), country.trim());
|
||||
}
|
||||
|
||||
private static String safe(String value) {
|
||||
return value == null ? "" : value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量补「所属主管」:行属用户一次取齐;普通账户的创建人再一次取齐(全程两次批量查询,
|
||||
* 同密钥管理页 fillRowGroups 的「用户→创建人→创建人用户名」形状)。读不到的用户/创建人
|
||||
* 不猜名字,supervisorName 留空由前端展示 -(铁律 30)。
|
||||
*/
|
||||
private void fillSupervisorNames(List<AppearancePatentCleanItemVo> items) {
|
||||
List<Long> userIds = items.stream()
|
||||
.map(AppearancePatentCleanItemVo::getUserId)
|
||||
.filter(id -> id != null)
|
||||
.distinct()
|
||||
.toList();
|
||||
if (userIds.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Map<Long, AdminUserEntity> ownersById = new LinkedHashMap<>();
|
||||
for (AdminUserEntity owner : adminUserMapper.selectBatchIds(userIds)) {
|
||||
if (owner.getId() != null) {
|
||||
ownersById.put(owner.getId(), owner);
|
||||
}
|
||||
}
|
||||
Map<Long, String> supervisorByUserId = new LinkedHashMap<>();
|
||||
Set<Long> creatorIds = new LinkedHashSet<>();
|
||||
for (Long userId : userIds) {
|
||||
AdminUserEntity owner = ownersById.get(userId);
|
||||
if (owner == null) {
|
||||
continue;
|
||||
}
|
||||
String username = trimmed(owner.getUsername());
|
||||
if (DeviceSessionPolicy.resolveRole(owner.getRole(), owner.getIsAdmin(), owner.getCreatedById()) != null) {
|
||||
// 主管本人(admin/公司超管/站点超管)跑的单:主管就是自己,不外指创建人。
|
||||
if (!username.isEmpty()) {
|
||||
supervisorByUserId.put(userId, username);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
Long creatorId = owner.getCreatedById();
|
||||
if (creatorId == null || creatorId <= 0) {
|
||||
continue;
|
||||
}
|
||||
creatorIds.add(creatorId);
|
||||
}
|
||||
if (!creatorIds.isEmpty()) {
|
||||
Map<Long, String> creatorNames = new LinkedHashMap<>();
|
||||
for (AdminUserEntity creator : adminUserMapper.selectBatchIds(creatorIds)) {
|
||||
String username = trimmed(creator.getUsername());
|
||||
if (creator.getId() != null && !username.isEmpty()) {
|
||||
creatorNames.put(creator.getId(), username);
|
||||
}
|
||||
}
|
||||
for (Long userId : userIds) {
|
||||
AdminUserEntity owner = ownersById.get(userId);
|
||||
if (owner == null || creatorNames.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
Long creatorId = owner.getCreatedById();
|
||||
if (creatorId != null && creatorNames.containsKey(creatorId)
|
||||
&& !supervisorByUserId.containsKey(userId)) {
|
||||
supervisorByUserId.put(userId, creatorNames.get(creatorId));
|
||||
}
|
||||
}
|
||||
}
|
||||
for (AppearancePatentCleanItemVo item : items) {
|
||||
if (item.getUserId() != null) {
|
||||
item.setSupervisorName(supervisorByUserId.get(item.getUserId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String trimmed(String value) {
|
||||
return value == null ? "" : value.trim();
|
||||
}
|
||||
|
||||
private static String trimToNull(String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user