feat: 新增拼多多店铺日推广费和开放接口

This commit is contained in:
2026-08-16 11:10:46 +08:00
parent c21dbc2d5c
commit 2f32d955eb
25 changed files with 1813 additions and 1 deletions
@@ -0,0 +1,75 @@
package top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.xyquan.ecomdmbbackend.common.annotation.RequirePermission;
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto.BatchImportPddGoodsAdFeeDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto.CreatePddGoodsAdFeeDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto.PddGoodsAdFeeFilterDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto.UpdatePddGoodsAdFeeDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.vo.BatchImportPddGoodsAdFeeResultVO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.vo.PddGoodsAdFeeVO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.service.PddGoodsAdFeeService;
@Tag(name = "拼多多-商品广告费", description = "拼多多商品按日广告费增删改查")
@RestController
@RequestMapping("/api/channel/pdd/goods/adfee")
@RequiredArgsConstructor
public class PddGoodsAdFeeController {
private final PddGoodsAdFeeService pddGoodsAdFeeService;
@Operation(summary = "创建")
@PostMapping("/create")
@RequirePermission(value = "biz:pdd:ad-fee:manage", description = "创建拼多多商品广告费", allowSuper = true)
public ApiResponse<PddGoodsAdFeeVO> create(@Valid @RequestBody CreatePddGoodsAdFeeDTO dto) {
return ApiResponse.success("创建成功", pddGoodsAdFeeService.create(dto));
}
@Operation(summary = "更新")
@PutMapping("/update/{id}")
@RequirePermission(value = "biz:pdd:ad-fee:manage", description = "更新拼多多商品广告费", allowSuper = true)
public ApiResponse<PddGoodsAdFeeVO> update(
@Parameter(description = "记录ID") @PathVariable Long id,
@Valid @RequestBody UpdatePddGoodsAdFeeDTO dto) {
return ApiResponse.success("更新成功", pddGoodsAdFeeService.update(id, dto));
}
@Operation(summary = "删除")
@DeleteMapping("/delete/{id}")
@RequirePermission(value = "biz:pdd:ad-fee:manage", description = "删除拼多多商品广告费", allowSuper = true)
public ApiResponse<Void> delete(@Parameter(description = "记录ID") @PathVariable Long id) {
pddGoodsAdFeeService.delete(id);
return ApiResponse.success("删除成功");
}
@Operation(summary = "详情")
@GetMapping("/detail/{id}")
@RequirePermission(value = "biz:pdd:ad-fee:read", description = "查询拼多多商品广告费详情", allowSuper = true)
public ApiResponse<PddGoodsAdFeeVO> detail(@Parameter(description = "记录ID") @PathVariable Long id) {
return ApiResponse.success("获取成功", pddGoodsAdFeeService.getDetail(id));
}
@Operation(summary = "分页列表")
@PostMapping("/list")
@RequirePermission(value = "biz:pdd:ad-fee:read", description = "查询拼多多商品广告费列表", allowSuper = true)
public ApiResponse<PageResultVO<PddGoodsAdFeeVO>> list(
@Valid @RequestBody PageQueryDTO<PddGoodsAdFeeFilterDTO> pageQueryDTO) {
return ApiResponse.success("获取成功", pddGoodsAdFeeService.getPage(pageQueryDTO));
}
@Operation(summary = "批量导入/覆盖", description = "按 goods_id+日期 upsert,店铺从商品表反查")
@PostMapping("/batch-import")
@RequirePermission(value = "biz:pdd:ad-fee:manage", description = "批量导入拼多多商品广告费", allowSuper = true)
public ApiResponse<BatchImportPddGoodsAdFeeResultVO> batchImport(
@Valid @RequestBody BatchImportPddGoodsAdFeeDTO dto) {
return ApiResponse.success("批量导入完成", pddGoodsAdFeeService.batchImport(dto));
}
}
@@ -0,0 +1,9 @@
package top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.po.PddGoodsAdFeeDailyPO;
@Mapper
public interface PddGoodsAdFeeDailyMapper extends BaseMapper<PddGoodsAdFeeDailyPO> {
}
@@ -0,0 +1,18 @@
package top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;
import java.util.List;
@Data
@Schema(description = "批量导入拼多多商品广告费")
public class BatchImportPddGoodsAdFeeDTO {
@NotEmpty(message = "广告费记录不能为空")
@Valid
@Schema(description = "广告费记录列表", requiredMode = Schema.RequiredMode.REQUIRED)
private List<BatchImportPddGoodsAdFeeItemDTO> items;
}
@@ -0,0 +1,30 @@
package top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDate;
@Data
@Schema(description = "批量导入拼多多商品广告费单条")
public class BatchImportPddGoodsAdFeeItemDTO {
@NotNull(message = "渠道店铺ID不能为空")
@Schema(description = "渠道店铺ID(channel_pdd_shop.id)", requiredMode = Schema.RequiredMode.REQUIRED)
private Long channelShopId;
@NotNull(message = "统计日期不能为空")
@Schema(description = "广告费统计日期", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDate feeDate;
@NotBlank(message = "商品ID不能为空")
@Schema(description = "拼多多商品ID(goods_id)", requiredMode = Schema.RequiredMode.REQUIRED)
private String goodsId;
@NotNull(message = "广告费金额不能为空")
@Schema(description = "广告费金额(元)", requiredMode = Schema.RequiredMode.REQUIRED)
private BigDecimal feeAmount;
}
@@ -0,0 +1,30 @@
package top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDate;
@Data
@Schema(description = "创建拼多多商品广告费")
public class CreatePddGoodsAdFeeDTO {
@NotNull(message = "渠道店铺ID不能为空")
@Schema(description = "渠道店铺ID(channel_pdd_shop.id)", requiredMode = Schema.RequiredMode.REQUIRED)
private Long channelShopId;
@NotBlank(message = "商品ID不能为空")
@Schema(description = "拼多多商品ID", requiredMode = Schema.RequiredMode.REQUIRED)
private String goodsId;
@NotNull(message = "统计日期不能为空")
@Schema(description = "广告费统计日期", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDate feeDate;
@NotNull(message = "广告费金额不能为空")
@Schema(description = "广告费金额(元)", requiredMode = Schema.RequiredMode.REQUIRED)
private BigDecimal feeAmount;
}
@@ -0,0 +1,23 @@
package top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDate;
@Data
@Schema(description = "拼多多商品广告费过滤条件")
public class PddGoodsAdFeeFilterDTO {
@Schema(description = "渠道店铺ID(channel_pdd_shop.id)")
private Long channelShopId;
@Schema(description = "拼多多商品ID")
private String goodsId;
@Schema(description = "统计日期起")
private LocalDate feeDateStart;
@Schema(description = "统计日期止")
private LocalDate feeDateEnd;
}
@@ -0,0 +1,30 @@
package top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDate;
@Data
@Schema(description = "更新拼多多商品广告费")
public class UpdatePddGoodsAdFeeDTO {
@NotNull(message = "渠道店铺ID不能为空")
@Schema(description = "渠道店铺ID(channel_pdd_shop.id)", requiredMode = Schema.RequiredMode.REQUIRED)
private Long channelShopId;
@NotBlank(message = "商品ID不能为空")
@Schema(description = "拼多多商品ID", requiredMode = Schema.RequiredMode.REQUIRED)
private String goodsId;
@NotNull(message = "统计日期不能为空")
@Schema(description = "广告费统计日期", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDate feeDate;
@NotNull(message = "广告费金额不能为空")
@Schema(description = "广告费金额(元)", requiredMode = Schema.RequiredMode.REQUIRED)
private BigDecimal feeAmount;
}
@@ -0,0 +1,51 @@
package top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.po;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@TableName("channel_pdd_goods_ad_fee_daily")
public class PddGoodsAdFeeDailyPO {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@TableField("company_id")
private Long companyId;
/** 渠道店铺ID(channel_pdd_shop.id) */
@TableField("channel_shop_id")
private Long channelShopId;
@TableField("mall_id")
private Long mallId;
@TableField("goods_id")
private String goodsId;
@TableField("fee_date")
private LocalDate feeDate;
@TableField("fee_amount")
private BigDecimal feeAmount;
@TableField("raw_response")
private String rawResponse;
@TableField("last_sync_time")
private LocalDateTime lastSyncTime;
@TableField(value = "create_time", fill = FieldFill.INSERT)
private LocalDateTime createTime;
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
}
@@ -0,0 +1,26 @@
package top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.List;
@Data
@Schema(description = "拼多多商品广告费批量导入结果")
public class BatchImportPddGoodsAdFeeResultVO {
private Integer totalCount;
private Integer successCount;
private Integer failureCount;
private List<Long> successIds;
private List<ImportFailureRecord> failureRecords;
@Data
@Schema(description = "导入失败记录")
public static class ImportFailureRecord {
private Integer rowNumber;
private Long channelShopId;
private String goodsId;
private String failureReason;
}
}
@@ -0,0 +1,26 @@
package top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@Schema(description = "拼多多商品广告费 VO")
public class PddGoodsAdFeeVO {
private Long id;
private Long companyId;
private Long channelShopId;
private String channelShopName;
private Long mallId;
private String goodsId;
private String goodsName;
private LocalDate feeDate;
private BigDecimal feeAmount;
private LocalDateTime lastSyncTime;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
@@ -0,0 +1,28 @@
package top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.service;
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto.BatchImportPddGoodsAdFeeDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto.CreatePddGoodsAdFeeDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto.PddGoodsAdFeeFilterDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto.UpdatePddGoodsAdFeeDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.vo.BatchImportPddGoodsAdFeeResultVO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.vo.PddGoodsAdFeeVO;
public interface PddGoodsAdFeeService {
PddGoodsAdFeeVO create(CreatePddGoodsAdFeeDTO dto);
PddGoodsAdFeeVO update(Long id, UpdatePddGoodsAdFeeDTO dto);
void delete(Long id);
PddGoodsAdFeeVO getDetail(Long id);
PageResultVO<PddGoodsAdFeeVO> getPage(PageQueryDTO<PddGoodsAdFeeFilterDTO> pageQueryDTO);
/**
* 按 channelShopId + goods_id + fee_date + fee_amount 批量写入(存在则覆盖金额)。
*/
BatchImportPddGoodsAdFeeResultVO batchImport(BatchImportPddGoodsAdFeeDTO dto);
}
@@ -0,0 +1,305 @@
package top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
import top.xyquan.ecomdmbbackend.common.utils.BeanCopyUtils;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.mapper.PddGoodsAdFeeDailyMapper;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto.BatchImportPddGoodsAdFeeDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto.BatchImportPddGoodsAdFeeItemDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto.CreatePddGoodsAdFeeDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto.PddGoodsAdFeeFilterDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto.UpdatePddGoodsAdFeeDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.po.PddGoodsAdFeeDailyPO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.vo.BatchImportPddGoodsAdFeeResultVO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.vo.PddGoodsAdFeeVO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.service.PddGoodsAdFeeService;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.mapper.PddGoodsMapper;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.po.PddGoodsPO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.mapper.PddShopMapper;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.model.po.PddShopPO;
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
public class PddGoodsAdFeeServiceImpl implements PddGoodsAdFeeService {
private final PddGoodsAdFeeDailyMapper adFeeMapper;
private final PddShopMapper pddShopMapper;
private final PddGoodsMapper pddGoodsMapper;
private final CurrentUserContext currentUserContext;
@Override
@Transactional(rollbackFor = Exception.class)
public PddGoodsAdFeeVO create(CreatePddGoodsAdFeeDTO dto) {
Long companyId = currentUserContext.getCurrentCompanyId();
PddShopPO shop = requireOwnedShop(dto.getChannelShopId(), companyId);
assertGoodsInShop(companyId, shop.getId(), dto.getGoodsId());
assertUnique(shop.getId(), dto.getGoodsId(), dto.getFeeDate(), null);
assertFeeAmount(dto.getFeeAmount());
PddGoodsAdFeeDailyPO po = new PddGoodsAdFeeDailyPO();
po.setCompanyId(companyId);
po.setChannelShopId(shop.getId());
po.setMallId(shop.getMallId());
po.setGoodsId(dto.getGoodsId().trim());
po.setFeeDate(dto.getFeeDate());
po.setFeeAmount(dto.getFeeAmount());
po.setLastSyncTime(LocalDateTime.now());
adFeeMapper.insert(po);
return toVo(po);
}
@Override
@Transactional(rollbackFor = Exception.class)
public PddGoodsAdFeeVO update(Long id, UpdatePddGoodsAdFeeDTO dto) {
Long companyId = currentUserContext.getCurrentCompanyId();
PddGoodsAdFeeDailyPO existing = requireOwnedRecord(id, companyId);
PddShopPO shop = requireOwnedShop(dto.getChannelShopId(), companyId);
assertGoodsInShop(companyId, shop.getId(), dto.getGoodsId());
assertUnique(shop.getId(), dto.getGoodsId(), dto.getFeeDate(), id);
assertFeeAmount(dto.getFeeAmount());
existing.setChannelShopId(shop.getId());
existing.setMallId(shop.getMallId());
existing.setGoodsId(dto.getGoodsId().trim());
existing.setFeeDate(dto.getFeeDate());
existing.setFeeAmount(dto.getFeeAmount());
existing.setLastSyncTime(LocalDateTime.now());
adFeeMapper.updateById(existing);
return toVo(existing);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(Long id) {
Long companyId = currentUserContext.getCurrentCompanyId();
requireOwnedRecord(id, companyId);
adFeeMapper.deleteById(id);
}
@Override
public PddGoodsAdFeeVO getDetail(Long id) {
Long companyId = currentUserContext.getCurrentCompanyId();
return toVo(requireOwnedRecord(id, companyId));
}
@Override
public PageResultVO<PddGoodsAdFeeVO> getPage(PageQueryDTO<PddGoodsAdFeeFilterDTO> pageQueryDTO) {
Long companyId = currentUserContext.getCurrentCompanyId();
PddGoodsAdFeeFilterDTO filters =
pageQueryDTO.getFilters() != null ? pageQueryDTO.getFilters() : new PddGoodsAdFeeFilterDTO();
LambdaQueryWrapper<PddGoodsAdFeeDailyPO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(PddGoodsAdFeeDailyPO::getCompanyId, companyId);
if (filters.getChannelShopId() != null) {
wrapper.eq(PddGoodsAdFeeDailyPO::getChannelShopId, filters.getChannelShopId());
}
if (StringUtils.hasText(filters.getGoodsId())) {
wrapper.eq(PddGoodsAdFeeDailyPO::getGoodsId, filters.getGoodsId().trim());
}
if (filters.getFeeDateStart() != null) {
wrapper.ge(PddGoodsAdFeeDailyPO::getFeeDate, filters.getFeeDateStart());
}
if (filters.getFeeDateEnd() != null) {
wrapper.le(PddGoodsAdFeeDailyPO::getFeeDate, filters.getFeeDateEnd());
}
wrapper.orderByDesc(PddGoodsAdFeeDailyPO::getFeeDate)
.orderByDesc(PddGoodsAdFeeDailyPO::getId);
Page<PddGoodsAdFeeDailyPO> page = new Page<>(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
IPage<PddGoodsAdFeeDailyPO> result = adFeeMapper.selectPage(page, wrapper);
List<PddGoodsAdFeeVO> records = enrich(result.getRecords());
return PageResultVO.create(records, result.getTotal(),
(int) pageQueryDTO.getPage(), (int) pageQueryDTO.getPageSize());
}
@Override
@Transactional(rollbackFor = Exception.class)
public BatchImportPddGoodsAdFeeResultVO batchImport(BatchImportPddGoodsAdFeeDTO dto) {
Long companyId = currentUserContext.getCurrentCompanyId();
List<BatchImportPddGoodsAdFeeItemDTO> items = dto.getItems();
BatchImportPddGoodsAdFeeResultVO result = new BatchImportPddGoodsAdFeeResultVO();
result.setTotalCount(items.size());
result.setSuccessIds(new ArrayList<>());
result.setFailureRecords(new ArrayList<>());
int success = 0;
for (int i = 0; i < items.size(); i++) {
BatchImportPddGoodsAdFeeItemDTO item = items.get(i);
int rowNumber = i + 1;
try {
Long id = upsertByChannelShopAndGoods(companyId, item);
result.getSuccessIds().add(id);
success++;
} catch (Exception e) {
BatchImportPddGoodsAdFeeResultVO.ImportFailureRecord failure =
new BatchImportPddGoodsAdFeeResultVO.ImportFailureRecord();
failure.setRowNumber(rowNumber);
failure.setChannelShopId(item.getChannelShopId());
failure.setGoodsId(item.getGoodsId());
failure.setFailureReason(e.getMessage() != null ? e.getMessage() : "导入失败");
result.getFailureRecords().add(failure);
}
}
result.setSuccessCount(success);
result.setFailureCount(items.size() - success);
return result;
}
private Long upsertByChannelShopAndGoods(Long companyId, BatchImportPddGoodsAdFeeItemDTO item) {
if (item.getChannelShopId() == null) {
throw new BusinessException("渠道店铺ID不能为空");
}
if (item.getFeeAmount() == null || item.getFeeAmount().compareTo(BigDecimal.ZERO) < 0) {
throw new BusinessException("广告费金额不能为空且须大于等于0");
}
if (!StringUtils.hasText(item.getGoodsId())) {
throw new BusinessException("商品ID不能为空");
}
if (item.getFeeDate() == null) {
throw new BusinessException("统计日期不能为空");
}
PddShopPO shop = requireOwnedShop(item.getChannelShopId(), companyId);
String goodsId = item.getGoodsId().trim();
assertGoodsInShop(companyId, shop.getId(), goodsId);
PddGoodsAdFeeDailyPO existing = adFeeMapper.selectOne(new LambdaQueryWrapper<PddGoodsAdFeeDailyPO>()
.eq(PddGoodsAdFeeDailyPO::getChannelShopId, shop.getId())
.eq(PddGoodsAdFeeDailyPO::getGoodsId, goodsId)
.eq(PddGoodsAdFeeDailyPO::getFeeDate, item.getFeeDate())
.last("LIMIT 1"));
LocalDateTime now = LocalDateTime.now();
if (existing != null) {
existing.setFeeAmount(item.getFeeAmount());
existing.setMallId(shop.getMallId());
existing.setLastSyncTime(now);
adFeeMapper.updateById(existing);
return existing.getId();
}
PddGoodsAdFeeDailyPO po = new PddGoodsAdFeeDailyPO();
po.setCompanyId(companyId);
po.setChannelShopId(shop.getId());
po.setMallId(shop.getMallId());
po.setGoodsId(goodsId);
po.setFeeDate(item.getFeeDate());
po.setFeeAmount(item.getFeeAmount());
po.setLastSyncTime(now);
adFeeMapper.insert(po);
return po.getId();
}
private PddShopPO requireOwnedShop(Long channelShopId, Long companyId) {
PddShopPO shop = pddShopMapper.selectById(channelShopId);
if (shop == null || !Objects.equals(shop.getCompanyId(), companyId)) {
throw new BusinessException("渠道店铺不存在或无权访问");
}
return shop;
}
private PddGoodsAdFeeDailyPO requireOwnedRecord(Long id, Long companyId) {
PddGoodsAdFeeDailyPO po = adFeeMapper.selectById(id);
if (po == null || !Objects.equals(po.getCompanyId(), companyId)) {
throw new BusinessException("广告费记录不存在或无权访问");
}
return po;
}
private void assertGoodsInShop(Long companyId, Long channelShopId, String goodsId) {
Long count = pddGoodsMapper.selectCount(new LambdaQueryWrapper<PddGoodsPO>()
.eq(PddGoodsPO::getCompanyId, companyId)
.eq(PddGoodsPO::getShopId, channelShopId)
.eq(PddGoodsPO::getGoodsId, goodsId.trim()));
if (count == null || count == 0) {
throw new BusinessException("该渠道店铺下不存在商品 goods_id=" + goodsId);
}
}
private void assertUnique(Long channelShopId, String goodsId, java.time.LocalDate feeDate, Long excludeId) {
LambdaQueryWrapper<PddGoodsAdFeeDailyPO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(PddGoodsAdFeeDailyPO::getChannelShopId, channelShopId)
.eq(PddGoodsAdFeeDailyPO::getGoodsId, goodsId.trim())
.eq(PddGoodsAdFeeDailyPO::getFeeDate, feeDate);
if (excludeId != null) {
wrapper.ne(PddGoodsAdFeeDailyPO::getId, excludeId);
}
if (adFeeMapper.selectCount(wrapper) > 0) {
throw new BusinessException("该商品在该日期已有广告费记录");
}
}
private void assertFeeAmount(BigDecimal feeAmount) {
if (feeAmount == null || feeAmount.compareTo(BigDecimal.ZERO) < 0) {
throw new BusinessException("广告费金额不能为空且须大于等于0");
}
}
private List<PddGoodsAdFeeVO> enrich(List<PddGoodsAdFeeDailyPO> records) {
if (records == null || records.isEmpty()) {
return List.of();
}
Set<Long> shopIds = new HashSet<>();
Set<String> goodsIds = new HashSet<>();
for (PddGoodsAdFeeDailyPO po : records) {
shopIds.add(po.getChannelShopId());
goodsIds.add(po.getGoodsId());
}
Map<Long, String> shopNameMap = new HashMap<>();
if (!shopIds.isEmpty()) {
List<PddShopPO> shops = pddShopMapper.selectBatchIds(shopIds);
for (PddShopPO shop : shops) {
shopNameMap.put(shop.getId(), shop.getMallName());
}
}
Map<String, String> goodsNameMap = new HashMap<>();
if (!goodsIds.isEmpty()) {
List<PddGoodsPO> goodsList = pddGoodsMapper.selectList(new LambdaQueryWrapper<PddGoodsPO>()
.eq(PddGoodsPO::getCompanyId, currentUserContext.getCurrentCompanyId())
.in(PddGoodsPO::getGoodsId, goodsIds));
for (PddGoodsPO goods : goodsList) {
goodsNameMap.putIfAbsent(goods.getGoodsId(), goods.getGoodsName());
}
}
return records.stream().map(po -> {
PddGoodsAdFeeVO vo = toVoPlain(po);
vo.setChannelShopName(shopNameMap.get(po.getChannelShopId()));
vo.setGoodsName(goodsNameMap.get(po.getGoodsId()));
return vo;
}).collect(Collectors.toList());
}
private PddGoodsAdFeeVO toVo(PddGoodsAdFeeDailyPO po) {
return enrich(List.of(po)).get(0);
}
private PddGoodsAdFeeVO toVoPlain(PddGoodsAdFeeDailyPO po) {
PddGoodsAdFeeVO vo = new PddGoodsAdFeeVO();
BeanCopyUtils.copyProperties(po, vo);
return vo;
}
}
@@ -15,6 +15,8 @@ public final class OpenApiScopeConstants {
public static final String PROMOTION_FEE_READ = "open:scope:promotion_fee:read";
public static final String IMPORT_TASK_READ = "open:scope:import_task:read";
public static final String META_READ = "open:scope:meta:read";
public static final String PDD_GOODS_AD_FEE_IMPORT = "open:scope:pdd_goods_ad_fee:import";
public static final String PDD_SHOP_READ = "open:scope:pdd_shop:read";
public static final List<String> ALL = List.of(
ORDER_IMPORT,
@@ -22,7 +24,9 @@ public final class OpenApiScopeConstants {
PROMOTION_FEE_IMPORT,
PROMOTION_FEE_READ,
IMPORT_TASK_READ,
META_READ
META_READ,
PDD_GOODS_AD_FEE_IMPORT,
PDD_SHOP_READ
);
private OpenApiScopeConstants() {
@@ -0,0 +1,34 @@
package top.xyquan.ecomdmbbackend.modules.open.api.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
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.RestController;
import top.xyquan.ecomdmbbackend.common.annotation.RequireOpenScope;
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.dto.BatchImportPddGoodsAdFeeDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.model.vo.BatchImportPddGoodsAdFeeResultVO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.adfee.service.PddGoodsAdFeeService;
import top.xyquan.ecomdmbbackend.modules.open.api.constants.OpenApiScopeConstants;
@RestController
@RequestMapping("/api/open/v1/channel/pdd/goods/adfee")
@RequiredArgsConstructor
@Tag(name = "开放接口-拼多多商品广告费", description = "API Key 认证,批量写入按日广告费")
public class OpenPddGoodsAdFeeController {
private final PddGoodsAdFeeService pddGoodsAdFeeService;
@PostMapping("/batch-import")
@RequireOpenScope(value = OpenApiScopeConstants.PDD_GOODS_AD_FEE_IMPORT, description = "批量导入拼多多商品广告费")
@Operation(summary = "批量导入拼多多商品广告费",
description = "每条需 channelShopId、feeDate、goodsId、feeAmount;同店同商品同日覆盖金额")
public ApiResponse<BatchImportPddGoodsAdFeeResultVO> batchImport(
@Valid @RequestBody BatchImportPddGoodsAdFeeDTO dto) {
return ApiResponse.success("批量导入完成", pddGoodsAdFeeService.batchImport(dto));
}
}
@@ -0,0 +1,36 @@
package top.xyquan.ecomdmbbackend.modules.open.api.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
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.RestController;
import top.xyquan.ecomdmbbackend.common.annotation.RequireOpenScope;
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.model.dto.PddShopFilterDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.model.vo.PddShopVO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.service.PddShopService;
import top.xyquan.ecomdmbbackend.modules.open.api.constants.OpenApiScopeConstants;
@RestController
@RequestMapping("/api/open/v1/channel/pdd/shop")
@RequiredArgsConstructor
@Tag(name = "开放接口-拼多多渠道店铺", description = "API Key 认证,查询本公司渠道店铺")
public class OpenPddShopController {
private final PddShopService pddShopService;
@PostMapping("/list")
@RequireOpenScope(value = OpenApiScopeConstants.PDD_SHOP_READ, description = "查询拼多多渠道店铺列表")
@Operation(summary = "分页查询拼多多渠道店铺", description = "镜像 POST /channel/pdd/shop/list,返回 id(channel_shop_id)、mallId、mallName 等")
public ApiResponse<PageResultVO<PddShopVO>> getPddShopPage(
@Valid @RequestBody PageQueryDTO<PddShopFilterDTO> pageQueryDTO) {
PageResultVO<PddShopVO> result = pddShopService.getPddShopPage(pageQueryDTO);
return ApiResponse.success("获取渠道店铺列表成功", result);
}
}