package com.yingpai.stock.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yingpai.stock.domain.bo.StockInfoBo;
import com.yingpai.stock.domain.vo.StockInfoVo;
import com.yingpai.stock.service.IStockInfoService;
import com.yingpai.stock.task.StockSyncTask;
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/info")
public class StockInfoController extends BaseController {

    private final IStockInfoService stockInfoService;
    private final StockSyncTask stockSyncTask;

    /**
     * 查询股票信息列表
     */
    @SaCheckPermission("stock:info:list")
    @GetMapping("/list")
    public TableDataInfo<StockInfoVo> list(StockInfoBo bo, PageQuery pageQuery) {
        return stockInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出股票信息列表
     */
    @SaCheckPermission("stock:info:export")
    @Log(title = "股票信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StockInfoBo bo, HttpServletResponse response) {
        List<StockInfoVo> list = stockInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "股票信息数据", StockInfoVo.class, response);
    }

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

    /**
     * 新增股票信息
     */
    @SaCheckPermission("stock:info:add")
    @Log(title = "股票信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody StockInfoBo bo) {
        return toAjax(stockInfoService.insertByBo(bo));
    }

    /**
     * 修改股票信息
     */
    @SaCheckPermission("stock:info:edit")
    @Log(title = "股票信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody StockInfoBo bo) {
        return toAjax(stockInfoService.updateByBo(bo));
    }

    /**
     * 删除股票信息
     */
    @SaCheckPermission("stock:info:remove")
    @Log(title = "股票信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(stockInfoService.deleteWithValidByIds(List.of(ids)));
    }

    /**
     * 手动同步股票信息
     */
    @SaCheckPermission("stock:info:sync")
    @Log(title = "股票信息", businessType = BusinessType.OTHER)
    @PostMapping("/sync")
    public R<String> sync() {
        log.info("[股票同步] 管理员手动触发同步");
        // 异步执行，避免接口超时
        new Thread(() -> stockSyncTask.manualSync()).start();
        return R.ok("同步任务已启动，请稍后刷新查看结果");
    }
}
