package com.yingpai.stock.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yingpai.stock.domain.bo.StockPoolBo;
import com.yingpai.stock.domain.vo.StockPoolVo;
import com.yingpai.stock.service.IStockPoolService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 股票池管理
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/stock/pool")
public class StockPoolController extends BaseController {

    private final IStockPoolService stockPoolService;

    /**
     * 查询股票池列表（带实时数据，自动刷新）
     */
    @SaCheckPermission("stock:pool:list")
    @GetMapping("/list")
    public TableDataInfo<StockPoolVo> list(StockPoolBo bo, PageQuery pageQuery) {
        return stockPoolService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出股票池列表（带实时数据）
     */
    @SaCheckPermission("stock:pool:export")
    @Log(title = "股票池", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StockPoolBo bo, HttpServletResponse response) {
        List<StockPoolVo> list = stockPoolService.queryList(bo);
        ExcelUtil.exportExcel(list, "股票池数据", StockPoolVo.class, response);
    }

    /**
     * 获取股票池详细信息
     */
    @SaCheckPermission("stock:pool:query")
    @GetMapping("/{id}")
    public R<StockPoolVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(stockPoolService.queryById(id));
    }

    /**
     * 添加股票到指定池（超短池/强势池）
     */
    @SaCheckPermission("stock:pool:add")
    @Log(title = "股票池", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Void> addToPool(@Validated @RequestBody StockPoolBo bo) {
        return toAjax(stockPoolService.insertByBo(bo));
    }

    /**
     * 从股票池移除（软删除，设置状态为无效）
     */
    @SaCheckPermission("stock:pool:remove")
    @Log(title = "股票池", businessType = BusinessType.UPDATE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(stockPoolService.softDeleteByIds(List.of(ids)));
    }

    /**
     * 根据股票代码和池类型移除
     */
    @SaCheckPermission("stock:pool:remove")
    @Log(title = "股票池", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeByCode")
    public R<Void> removeByCode(@RequestParam String stockCode, @RequestParam Integer poolType) {
        return toAjax(stockPoolService.removeByCodeAndType(stockCode, poolType));
    }
}
