package com.goldenwater.web.controller.yjjt;

import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.common.core.page.TableDataInfo;
import com.goldenwater.common.utils.poi.ExcelUtil;
import com.goldenwater.yjjt.domain.YjjtStation;
import com.goldenwater.yjjt.service.IYjjtStationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 引江济太站点管理Controller
 */
@RestController
@RequestMapping("/yjjt/station")
@Tag(name = "引江济太站点管理", description = "引江济太站点管理接口")
public class YjjtStationController extends BaseController {

    @Resource
    private IYjjtStationService yjjtStationService;

    /**
     * 查询引江济太站点列表
     */
    @GetMapping("/list")
    @Operation(summary = "查询站点列表", description = "查询引江济太站点列表")
    @PreAuthorize("@ss.hasPermi('yjjtgl:station:list')")
    public TableDataInfo list(YjjtStation yjjtStation) {
        startPage();
        List<YjjtStation> list = yjjtStationService.selectYjjtStationList(yjjtStation);
        return getDataTable(list);
    }

    /**
     * 查询引江济太站点详细
     */
    @GetMapping("/detail")
    @Operation(summary = "查询站点详情", description = "查询站点详细信息")
    @PreAuthorize("@ss.hasPermi('yjjtgl:station:query')")
    public AjaxResult getInfo(
            @Parameter(description = "测站编码") @RequestParam("stcd") String stcd,
            @Parameter(description = "年份") @RequestParam("year") String year) {
        return AjaxResult.success(yjjtStationService.selectYjjtStationByPk(stcd, year));
    }

    /**
     * 新增引江济太站点
     */
    @PostMapping
    @Operation(summary = "新增站点", description = "新增引江济太站点")
    @PreAuthorize("@ss.hasPermi('yjjtgl:station:add')")
    public AjaxResult add(@RequestBody YjjtStation yjjtStation) {
        return toAjax(yjjtStationService.insertYjjtStation(yjjtStation));
    }

    /**
     * 修改引江济太站点
     */
    @PutMapping
    @Operation(summary = "修改站点", description = "修改引江济太站点")
    @PreAuthorize("@ss.hasPermi('yjjtgl:station:edit')")
    public AjaxResult edit(@RequestBody YjjtStation yjjtStation) {
        return toAjax(yjjtStationService.updateYjjtStation(yjjtStation));
    }

    /**
     * 删除引江济太站点
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除站点", description = "删除引江济太站点")
    @PreAuthorize("@ss.hasPermi('yjjtgl:station:remove')")
    public AjaxResult remove(@RequestBody YjjtStation yjjtStation) {
        return toAjax(yjjtStationService.deleteYjjtStationByPk(yjjtStation.getStcd(), yjjtStation.getYear()));
    }

    /**
     * 批量删除引江济太站点
     */
    @DeleteMapping("/delete/batch")
    @Operation(summary = "批量删除站点", description = "批量删除引江济太站点")
    @PreAuthorize("@ss.hasPermi('yjjtgl:station:remove')")
    public AjaxResult removeBatch(@RequestBody List<YjjtStation> list) {
        return toAjax(yjjtStationService.deleteYjjtStationByPks(list));
    }

    /**
     * 导出引江济太站点
     */
    @GetMapping("/export")
    @Operation(summary = "导出站点", description = "导出引江济太站点数据")
    @PreAuthorize("@ss.hasPermi('yjjtgl:station:export')")
    public void export(HttpServletResponse response, YjjtStation yjjtStation) {
        List<YjjtStation> list = yjjtStationService.selectYjjtStationList(yjjtStation);
        ExcelUtil<YjjtStation> util = new ExcelUtil<YjjtStation>(YjjtStation.class);
        util.exportExcel(response, list, "引江济太站点数据");
    }

    /**
     * 导入引江济太站点
     */
    @PostMapping("/import")
    @Operation(summary = "导入站点", description = "导入引江济太站点数据")
    @PreAuthorize("@ss.hasPermi('yjjtgl:station:import')")
    public AjaxResult importData(@RequestParam("file") MultipartFile file) throws Exception {
        ExcelUtil<YjjtStation> util = new ExcelUtil<YjjtStation>(YjjtStation.class);
        List<YjjtStation> stationList = util.importExcel(file.getInputStream());
        if (stationList == null || stationList.isEmpty()) {
            return AjaxResult.error("导入数据为空");
        }
        yjjtStationService.importStation(stationList);
        return AjaxResult.success("导入成功，共导入" + stationList.size() + "条数据");
    }

    /**
     * 下载引江济太站点导入模板
     */
    @GetMapping("/importTemplate")
    @Operation(summary = "下载导入模板", description = "下载引江济太站点导入模板")
    @PreAuthorize("@ss.hasPermi('yjjtgl:station:import')")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<YjjtStation> util = new ExcelUtil<YjjtStation>(YjjtStation.class);
        util.exportExcel(response, new ArrayList<YjjtStation>(), "引江济太站点导入模板");
    }
}
