|
|
@@ -4,15 +4,17 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.etotem.cfc.common.Result;
|
|
|
import com.etotem.cfc.entity.Coupon;
|
|
|
-import com.etotem.cfc.entity.CouponGrantLog;
|
|
|
+import com.etotem.cfc.entity.Family;
|
|
|
import com.etotem.cfc.entity.FamilyCoupon;
|
|
|
import com.etotem.cfc.entity.FamilyCouponGrantLog;
|
|
|
-import com.etotem.cfc.mapper.CouponGrantLogMapper;
|
|
|
-import com.etotem.cfc.service.CouponGiftService;
|
|
|
import com.etotem.cfc.mapper.CouponMapper;
|
|
|
+import com.etotem.cfc.service.CouponGiftService;
|
|
|
import com.etotem.cfc.mapper.FamilyCouponGrantLogMapper;
|
|
|
import com.etotem.cfc.mapper.FamilyCouponMapper;
|
|
|
+import com.etotem.cfc.mapper.FamilyMapper;
|
|
|
import com.etotem.cfc.service.CouponService;
|
|
|
+import com.etotem.cfc.util.ParamUtils;
|
|
|
+import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
@@ -23,8 +25,6 @@ import java.util.LinkedHashSet;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
-import com.etotem.cfc.util.SortUtil;
|
|
|
-import com.etotem.cfc.util.ParamUtils;
|
|
|
|
|
|
@RestController
|
|
|
@RequestMapping("/api/admin/coupon")
|
|
|
@@ -36,9 +36,6 @@ public class AdminCouponController {
|
|
|
@Resource
|
|
|
private CouponMapper couponMapper;
|
|
|
|
|
|
- @Resource
|
|
|
- private CouponGrantLogMapper couponGrantLogMapper;
|
|
|
-
|
|
|
@Resource
|
|
|
private FamilyCouponGrantLogMapper familyCouponGrantLogMapper;
|
|
|
|
|
|
@@ -48,25 +45,35 @@ public class AdminCouponController {
|
|
|
@Resource
|
|
|
private CouponGiftService couponGiftService;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private FamilyMapper familyMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 优惠券模板分页列表(COUPON-P2 ①:支持 keyword 搜索 + 真实分页 + 排序)。
|
|
|
+ * 请求: { page, size, keyword?, sort? }
|
|
|
+ * 返回: Result<Page<Coupon>>(records/total/current/size)
|
|
|
+ */
|
|
|
@PostMapping("/list")
|
|
|
- public Result<List<Coupon>> list(@RequestBody(required = false) Map<String, Object> params,
|
|
|
- @RequestAttribute("role") String role) {
|
|
|
- if (!"admin".equals(role)) {
|
|
|
- return Result.error("无权限");
|
|
|
- }
|
|
|
+ @PreAuthorize("hasRole('admin')")
|
|
|
+ public Result<Page<Coupon>> list(@RequestBody(required = false) Map<String, Object> params) {
|
|
|
+ String keyword = params != null && params.get("keyword") != null
|
|
|
+ ? params.get("keyword").toString().trim() : null;
|
|
|
+ int page = params != null && params.get("page") != null
|
|
|
+ ? Integer.parseInt(params.get("page").toString()) : 1;
|
|
|
+ int size = params != null && params.get("size") != null
|
|
|
+ ? Integer.parseInt(params.get("size").toString()) : 20;
|
|
|
+ if (page < 1) page = 1;
|
|
|
+ if (size < 1) size = 20;
|
|
|
+ if (size > 999) size = 999;
|
|
|
@SuppressWarnings("unchecked")
|
|
|
List<Map<String, String>> sortSpecs = params != null && params.get("sort") != null
|
|
|
- ? (List<Map<String, String>>) params.get("sort")
|
|
|
- : null;
|
|
|
- return Result.success(couponService.listAll(sortSpecs));
|
|
|
+ ? (List<Map<String, String>>) params.get("sort") : null;
|
|
|
+ return Result.success(couponService.pageCoupons(keyword, page, size, sortSpecs));
|
|
|
}
|
|
|
|
|
|
@PostMapping("/create")
|
|
|
- public Result<String> create(@RequestBody Coupon coupon,
|
|
|
- @RequestAttribute("role") String role) {
|
|
|
- if (!"admin".equals(role)) {
|
|
|
- return Result.error("无权限");
|
|
|
- }
|
|
|
+ @PreAuthorize("hasRole('admin')")
|
|
|
+ public Result<String> create(@RequestBody Coupon coupon) {
|
|
|
if (coupon.getType() == null) coupon.setType("FIXED");
|
|
|
if (coupon.getMinSpend() == null) coupon.setMinSpend(0);
|
|
|
if (coupon.getApplicableTo() == null) coupon.setApplicableTo("ALL");
|
|
|
@@ -77,21 +84,15 @@ public class AdminCouponController {
|
|
|
}
|
|
|
|
|
|
@PostMapping("/update")
|
|
|
- public Result<String> update(@RequestBody Coupon coupon,
|
|
|
- @RequestAttribute("role") String role) {
|
|
|
- if (!"admin".equals(role)) {
|
|
|
- return Result.error("无权限");
|
|
|
- }
|
|
|
+ @PreAuthorize("hasRole('admin')")
|
|
|
+ public Result<String> update(@RequestBody Coupon coupon) {
|
|
|
couponMapper.updateById(coupon);
|
|
|
return Result.success("更新成功");
|
|
|
}
|
|
|
|
|
|
@PostMapping("/issue")
|
|
|
- public Result<String> issue(@RequestBody Map<String, Object> params,
|
|
|
- @RequestAttribute("role") String role) {
|
|
|
- if (!"admin".equals(role)) {
|
|
|
- return Result.error("无权限");
|
|
|
- }
|
|
|
+ @PreAuthorize("hasRole('admin')")
|
|
|
+ public Result<String> issue(@RequestBody Map<String, Object> params) {
|
|
|
Long couponId = ParamUtils.getLong(params.get("couponId"));
|
|
|
Object familyIdsObj = params.get("familyIds");
|
|
|
if (familyIdsObj instanceof List) {
|
|
|
@@ -105,11 +106,8 @@ public class AdminCouponController {
|
|
|
|
|
|
/** 单家庭发放(家庭管理页使用) */
|
|
|
@PostMapping("/issue-family")
|
|
|
- public Result<String> issueFamily(@RequestBody Map<String, Object> params,
|
|
|
- @RequestAttribute("role") String role) {
|
|
|
- if (!"admin".equals(role)) {
|
|
|
- return Result.error("无权限");
|
|
|
- }
|
|
|
+ @PreAuthorize("hasRole('admin')")
|
|
|
+ public Result<String> issueFamily(@RequestBody Map<String, Object> params) {
|
|
|
Long familyId = ParamUtils.getLong(params.get("familyId"));
|
|
|
Long couponId = ParamUtils.getLong(params.get("couponId"));
|
|
|
Integer quantity = params.get("quantity") == null ? 1 : Integer.valueOf(params.get("quantity").toString());
|
|
|
@@ -130,11 +128,8 @@ public class AdminCouponController {
|
|
|
* 后端自动创建会员专属券模板并发放到该用户所在家庭。
|
|
|
*/
|
|
|
@PostMapping("/grant-user")
|
|
|
- public Result<Map<String, Object>> grantUser(@RequestBody Map<String, Object> params,
|
|
|
- @RequestAttribute("role") String role) {
|
|
|
- if (!"admin".equals(role)) {
|
|
|
- return Result.error("无权限");
|
|
|
- }
|
|
|
+ @PreAuthorize("hasRole('admin')")
|
|
|
+ public Result<Map<String, Object>> grantUser(@RequestBody Map<String, Object> params) {
|
|
|
Long userId = ParamUtils.getLong(params.get("userId"));
|
|
|
Integer amountFen = null;
|
|
|
if (params.get("amount") != null) {
|
|
|
@@ -165,22 +160,16 @@ public class AdminCouponController {
|
|
|
}
|
|
|
|
|
|
@PostMapping("/delete")
|
|
|
- public Result<String> delete(@RequestBody Map<String, Object> params,
|
|
|
- @RequestAttribute("role") String role) {
|
|
|
- if (!"admin".equals(role)) {
|
|
|
- return Result.error("无权限");
|
|
|
- }
|
|
|
+ @PreAuthorize("hasRole('admin')")
|
|
|
+ public Result<String> delete(@RequestBody Map<String, Object> params) {
|
|
|
Long id = ParamUtils.getLong(params.get("id"));
|
|
|
couponMapper.deleteById(id);
|
|
|
return Result.success("删除成功");
|
|
|
}
|
|
|
|
|
|
@PostMapping("/toggle-status")
|
|
|
- public Result<String> toggleStatus(@RequestBody Map<String, Object> params,
|
|
|
- @RequestAttribute("role") String role) {
|
|
|
- if (!"admin".equals(role)) {
|
|
|
- return Result.error("无权限");
|
|
|
- }
|
|
|
+ @PreAuthorize("hasRole('admin')")
|
|
|
+ public Result<String> toggleStatus(@RequestBody Map<String, Object> params) {
|
|
|
Long id = ParamUtils.getLong(params.get("id"));
|
|
|
String status = params.get("status") == null ? null : params.get("status").toString();
|
|
|
if (id == null || !("ACTIVE".equals(status) || "DISABLED".equals(status))) {
|
|
|
@@ -195,12 +184,13 @@ public class AdminCouponController {
|
|
|
return Result.success("ACTIVE".equals(status) ? "启用成功" : "停用成功");
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 发券流水(COUPON-P2 ②:批量填充家庭名,消除前端逐个请求 getFamilyDetail 的 N+1)。
|
|
|
+ * 返回 List<FamilyCouponGrantLog>,每条带 familyName(batch 一次查 families 表)。
|
|
|
+ */
|
|
|
@PostMapping("/grant-log")
|
|
|
- public Result<List<FamilyCouponGrantLog>> grantLog(@RequestBody Map<String, Object> params,
|
|
|
- @RequestAttribute("role") String role) {
|
|
|
- if (!"admin".equals(role)) {
|
|
|
- return Result.error("无权限");
|
|
|
- }
|
|
|
+ @PreAuthorize("hasRole('admin')")
|
|
|
+ public Result<List<FamilyCouponGrantLog>> grantLog(@RequestBody Map<String, Object> params) {
|
|
|
Long familyId = ParamUtils.getLong(params.get("familyId"));
|
|
|
Long couponId = ParamUtils.getLong(params.get("couponId"));
|
|
|
String grantType = params.get("grantType") == null ? null : params.get("grantType").toString();
|
|
|
@@ -219,7 +209,29 @@ public class AdminCouponController {
|
|
|
wrapper.eq(FamilyCouponGrantLog::getSource, source);
|
|
|
}
|
|
|
wrapper.orderByDesc(FamilyCouponGrantLog::getCreatedAt);
|
|
|
- return Result.success(familyCouponGrantLogMapper.selectList(wrapper));
|
|
|
+ List<FamilyCouponGrantLog> logs = familyCouponGrantLogMapper.selectList(wrapper);
|
|
|
+ fillFamilyNames(logs);
|
|
|
+ return Result.success(logs);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 批量填充发券流水中的家庭名(单次 selectBatchIds,替代逐条查询) */
|
|
|
+ private void fillFamilyNames(List<FamilyCouponGrantLog> logs) {
|
|
|
+ if (logs == null || logs.isEmpty()) return;
|
|
|
+ Set<Long> familyIds = new LinkedHashSet<>();
|
|
|
+ for (FamilyCouponGrantLog log : logs) {
|
|
|
+ if (log.getFamilyId() != null) familyIds.add(log.getFamilyId());
|
|
|
+ }
|
|
|
+ Map<Long, String> nameMap = new HashMap<>();
|
|
|
+ if (!familyIds.isEmpty()) {
|
|
|
+ List<Family> families = familyMapper.selectBatchIds(familyIds);
|
|
|
+ for (Family f : families) {
|
|
|
+ nameMap.put(f.getId(), f.getName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (FamilyCouponGrantLog log : logs) {
|
|
|
+ String name = log.getFamilyId() == null ? null : nameMap.get(log.getFamilyId());
|
|
|
+ log.setFamilyName(name != null ? name : "家庭" + log.getFamilyId());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/** 会员券转赠记录(分页,支持按赠送方/接收方家庭、状态、券模板过滤) */
|
|
|
@@ -240,11 +252,8 @@ public class AdminCouponController {
|
|
|
|
|
|
/** 家庭券列表(family_coupon 分页,JOIN coupon 取模板信息) */
|
|
|
@PostMapping("/family-coupons")
|
|
|
- public Result<Page<Map<String, Object>>> familyCoupons(@RequestBody Map<String, Object> params,
|
|
|
- @RequestAttribute("role") String role) {
|
|
|
- if (!"admin".equals(role)) {
|
|
|
- return Result.error("无权限");
|
|
|
- }
|
|
|
+ @PreAuthorize("hasRole('admin')")
|
|
|
+ public Result<Page<Map<String, Object>>> familyCoupons(@RequestBody Map<String, Object> params) {
|
|
|
Long familyId = ParamUtils.getLong(params.get("familyId"));
|
|
|
String status = params.get("status") == null ? null : params.get("status").toString();
|
|
|
int page = params.get("page") == null ? 1 : Integer.parseInt(params.get("page").toString());
|
|
|
@@ -303,11 +312,8 @@ public class AdminCouponController {
|
|
|
|
|
|
/** 家庭券使用记录(status=USED,JOIN coupon 取模板信息) */
|
|
|
@PostMapping("/family-coupon-usage")
|
|
|
- public Result<Page<Map<String, Object>>> familyCouponUsage(@RequestBody Map<String, Object> params,
|
|
|
- @RequestAttribute("role") String role) {
|
|
|
- if (!"admin".equals(role)) {
|
|
|
- return Result.error("无权限");
|
|
|
- }
|
|
|
+ @PreAuthorize("hasRole('admin')")
|
|
|
+ public Result<Page<Map<String, Object>>> familyCouponUsage(@RequestBody Map<String, Object> params) {
|
|
|
Long familyId = ParamUtils.getLong(params.get("familyId"));
|
|
|
int page = params.get("page") == null ? 1 : Integer.parseInt(params.get("page").toString());
|
|
|
int size = params.get("size") == null ? 20 : Integer.parseInt(params.get("size").toString());
|