package com.yingpai.stock.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yingpai.stock.domain.bo.StockPoolHistoryBo;
import com.yingpai.stock.domain.vo.StockPoolHistoryVo;
import com.yingpai.stock.service.IStockPoolHistoryService;
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.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 org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 股票池历史数据管理
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/stock/history")
public class StockPoolHistoryController extends BaseController {

    private final IStockPoolHistoryService stockPoolHistoryService;

    /**
     * 查询历史数据列表
     */
    @SaCheckPermission("stock:history:list")
    @GetMapping("/list")
    public TableDataInfo<StockPoolHistoryVo> list(StockPoolHistoryBo bo, PageQuery pageQuery) {
        return stockPoolHistoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取历史数据详细信息
     */
    @SaCheckPermission("stock:history:query")
    @GetMapping("/{id}")
    public R<StockPoolHistoryVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(stockPoolHistoryService.queryById(id));
    }

    /**
     * 新增历史数据
     */
    @SaCheckPermission("stock:history:add")
    @Log(title = "历史数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Void> add(@Validated @RequestBody StockPoolHistoryBo bo) {
        return toAjax(stockPoolHistoryService.insertByBo(bo));
    }

    /**
     * 修改历史数据
     */
    @SaCheckPermission("stock:history:edit")
    @Log(title = "历史数据", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public R<Void> edit(@Validated @RequestBody StockPoolHistoryBo bo) {
        return toAjax(stockPoolHistoryService.updateByBo(bo));
    }

    /**
     * 删除历史数据
     */
    @SaCheckPermission("stock:history:remove")
    @Log(title = "历史数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(stockPoolHistoryService.deleteWithValidByIds(List.of(ids)));
    }

    /**
     * 导入历史数据（Excel）
     * @param file Excel文件
     * @param recordDate 管理员指定的记录日期
     * @param updateSupport 是否更新已存在的数据
     */
    @SaCheckPermission("stock:history:import")
    @Log(title = "历史数据", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public R<Void> importData(
            @RequestParam("file") MultipartFile file,
            @RequestParam("recordDate") String recordDate,
            @RequestParam(value = "updateSupport", defaultValue = "true") boolean updateSupport) throws Exception {
        String message = stockPoolHistoryService.importData(file, recordDate, updateSupport);
        return R.ok(message);
    }
}
