|
|
@@ -0,0 +1,87 @@
|
|
|
+package com.etotem.cfc.controller;
|
|
|
+
|
|
|
+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.CfTransferRecord;
|
|
|
+import com.etotem.cfc.entity.User;
|
|
|
+import com.etotem.cfc.mapper.CfTransferRecordMapper;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
+import com.etotem.cfc.service.PlatformPointsService;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestAttribute;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/cf/transfer")
|
|
|
+public class CfTransferController {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private PlatformPointsService platformPointsService;
|
|
|
+ @Resource
|
|
|
+ private CfTransferRecordMapper cfTransferRecordMapper;
|
|
|
+ @Resource
|
|
|
+ private UserMapper userMapper;
|
|
|
+
|
|
|
+ /** 成员间转让 CF(需同家庭) */
|
|
|
+ @PostMapping("/send")
|
|
|
+ @Transactional
|
|
|
+ public Result<String> send(@RequestAttribute("userId") Long fromUserId,
|
|
|
+ @RequestBody Map<String, Object> params) {
|
|
|
+ Long toUserId = ((Number) params.get("toUserId")).longValue();
|
|
|
+ int amount = ((Number) params.get("amount")).intValue();
|
|
|
+ if (amount <= 0) {
|
|
|
+ return Result.error("CF值必须为正数");
|
|
|
+ }
|
|
|
+ User from = userMapper.selectById(fromUserId);
|
|
|
+ User to = userMapper.selectById(toUserId);
|
|
|
+ if (from == null || to == null) {
|
|
|
+ return Result.error("用户不存在");
|
|
|
+ }
|
|
|
+ if (from.getFamilyId() == null || !from.getFamilyId().equals(to.getFamilyId())) {
|
|
|
+ return Result.error("仅限同一家庭成员间转让");
|
|
|
+ }
|
|
|
+ // 每次转让使用唯一 refId(避免 earn 幂等键 (ref_type, ref_id) 碰撞)
|
|
|
+ Long transferRefId = java.util.UUID.randomUUID().getMostSignificantBits();
|
|
|
+ if (transferRefId == null || transferRefId == 0) transferRefId = System.currentTimeMillis();
|
|
|
+ // 转出扣减 + 转入增加(同一事务)
|
|
|
+ platformPointsService.spend(fromUserId, amount, "cf_transfer_out", transferRefId,
|
|
|
+ "转给成员: " + (to.getNickname() == null ? toUserId : to.getNickname()));
|
|
|
+ platformPointsService.earn(toUserId, amount, "cf_transfer_in", transferRefId,
|
|
|
+ "收到成员转让: " + (from.getNickname() == null ? fromUserId : from.getNickname()));
|
|
|
+
|
|
|
+ CfTransferRecord record = new CfTransferRecord();
|
|
|
+ record.setFromUserId(fromUserId);
|
|
|
+ record.setToUserId(toUserId);
|
|
|
+ record.setFamilyId(from.getFamilyId());
|
|
|
+ record.setAmount(amount);
|
|
|
+ record.setType("transfer");
|
|
|
+ record.setRemark("成员间转让");
|
|
|
+ record.setCreatedAt(new Date());
|
|
|
+ cfTransferRecordMapper.insert(record);
|
|
|
+ return Result.success("转让成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 转让记录(分页,按当前用户家庭) */
|
|
|
+ @PostMapping("/list")
|
|
|
+ public Result<Page<CfTransferRecord>> list(@RequestAttribute("userId") Long userId,
|
|
|
+ @RequestBody Map<String, Integer> params) {
|
|
|
+ int page = params.getOrDefault("page", 1);
|
|
|
+ int size = params.getOrDefault("size", 20);
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
+ Long familyId = user == null ? null : user.getFamilyId();
|
|
|
+ LambdaQueryWrapper<CfTransferRecord> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ if (familyId != null) {
|
|
|
+ wrapper.eq(CfTransferRecord::getFamilyId, familyId);
|
|
|
+ }
|
|
|
+ wrapper.orderByDesc(CfTransferRecord::getCreatedAt);
|
|
|
+ return Result.success(cfTransferRecordMapper.selectPage(new Page<>(page, size), wrapper));
|
|
|
+ }
|
|
|
+}
|