forked from github/cool-admin-java
feat: 大A记录模块后端 - 股票交易记录CRUD与数据隔离
- 新增StockRecord实体,含userId字段实现用户数据隔离 - Controller查询自动按userId过滤,归档/恢复增加用户校验 - Service新增时自动注入当前用户ID - 支持归档/恢复操作
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package com.cool.modules.stock.controller.admin;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.cool.core.annotation.CoolRestController;
|
||||
import com.cool.core.base.BaseController;
|
||||
import com.cool.core.request.R;
|
||||
import com.cool.core.util.CoolSecurityUtil;
|
||||
import com.cool.modules.stock.entity.StockRecordEntity;
|
||||
import com.cool.modules.stock.service.StockRecordService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
import static com.cool.modules.stock.entity.table.StockRecordEntityTableDef.STOCK_RECORD_ENTITY;
|
||||
|
||||
@Tag(name = "股票交易记录", description = "股票交易记录")
|
||||
@CoolRestController(api = {"add", "delete", "update", "page", "list", "info"})
|
||||
public class AdminStockRecordController extends BaseController<StockRecordService, StockRecordEntity> {
|
||||
@Override
|
||||
protected void init(HttpServletRequest request, JSONObject requestParams) {
|
||||
setPageOption(createOp().fieldEq(STOCK_RECORD_ENTITY.USER_ID, STOCK_RECORD_ENTITY.ARCHIVED, STOCK_RECORD_ENTITY.STOCK_NAME));
|
||||
setListOption(createOp().fieldEq(STOCK_RECORD_ENTITY.USER_ID, STOCK_RECORD_ENTITY.ARCHIVED, STOCK_RECORD_ENTITY.STOCK_NAME));
|
||||
}
|
||||
|
||||
@Operation(summary = "归档")
|
||||
@PostMapping("/archive")
|
||||
public R<Void> archive(@RequestBody JSONObject params) {
|
||||
Long id = params.getLong("id");
|
||||
service.archive(id, CoolSecurityUtil.getCurrentUserId());
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
@Operation(summary = "恢复")
|
||||
@PostMapping("/restore")
|
||||
public R<Void> restore(@RequestBody JSONObject params) {
|
||||
Long id = params.getLong("id");
|
||||
service.restore(id, CoolSecurityUtil.getCurrentUserId());
|
||||
return R.ok();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.cool.modules.stock.entity;
|
||||
|
||||
import com.cool.core.annotation.ColumnDefine;
|
||||
import com.cool.core.base.BaseEntity;
|
||||
import com.mybatisflex.annotation.Column;
|
||||
import com.mybatisflex.annotation.Table;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Table(value = "stock_record", comment = "股票交易记录")
|
||||
public class StockRecordEntity extends BaseEntity<StockRecordEntity> {
|
||||
|
||||
@ColumnDefine(comment = "用户ID", notNull = true)
|
||||
private Long userId;
|
||||
|
||||
@ColumnDefine(comment = "股票名称", notNull = true)
|
||||
private String stockName;
|
||||
|
||||
@ColumnDefine(comment = "买入价", type = "decimal", length = 12, decimalLength = 2)
|
||||
private BigDecimal buyPrice;
|
||||
|
||||
@ColumnDefine(comment = "买入数量", defaultValue = "100")
|
||||
private Integer buyQuantity;
|
||||
|
||||
@ColumnDefine(comment = "卖出价", type = "decimal", length = 12, decimalLength = 2)
|
||||
private BigDecimal sellPrice;
|
||||
|
||||
@ColumnDefine(comment = "卖出数量", defaultValue = "100")
|
||||
private Integer sellQuantity;
|
||||
|
||||
@ColumnDefine(comment = "是否已归档", defaultValue = "0")
|
||||
private Boolean archived;
|
||||
|
||||
@ColumnDefine(comment = "归档时间")
|
||||
private java.util.Date archivedTime;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.cool.modules.stock.mapper;
|
||||
|
||||
import com.mybatisflex.core.BaseMapper;
|
||||
import com.cool.modules.stock.entity.StockRecordEntity;
|
||||
|
||||
public interface StockRecordMapper extends BaseMapper<StockRecordEntity> {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.cool.modules.stock.service;
|
||||
|
||||
import com.cool.core.base.BaseService;
|
||||
import com.cool.modules.stock.entity.StockRecordEntity;
|
||||
|
||||
public interface StockRecordService extends BaseService<StockRecordEntity> {
|
||||
void archive(Long id, Long userId);
|
||||
void restore(Long id, Long userId);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.cool.modules.stock.service.impl;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.cool.core.base.BaseServiceImpl;
|
||||
import com.cool.core.base.ModifyEnum;
|
||||
import com.cool.core.util.CoolSecurityUtil;
|
||||
import com.cool.modules.stock.entity.StockRecordEntity;
|
||||
import com.cool.modules.stock.mapper.StockRecordMapper;
|
||||
import com.cool.modules.stock.service.StockRecordService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Service
|
||||
public class StockRecordServiceImpl extends BaseServiceImpl<StockRecordMapper, StockRecordEntity>
|
||||
implements StockRecordService {
|
||||
|
||||
@Override
|
||||
public void modifyBefore(JSONObject requestParams, StockRecordEntity entity, ModifyEnum type) {
|
||||
if (type == ModifyEnum.ADD) {
|
||||
entity.setUserId(CoolSecurityUtil.getCurrentUserId());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void archive(Long id, Long userId) {
|
||||
StockRecordEntity entity = getById(id);
|
||||
if (entity != null && entity.getUserId().equals(userId)) {
|
||||
entity.setArchived(true);
|
||||
entity.setArchivedTime(new Date());
|
||||
updateById(entity);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restore(Long id, Long userId) {
|
||||
StockRecordEntity entity = getById(id);
|
||||
if (entity != null && entity.getUserId().equals(userId)) {
|
||||
entity.setArchived(false);
|
||||
entity.setArchivedTime(null);
|
||||
updateById(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user