package com.goldenwater.web.controller.yjjt;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.goldenwater.common.annotation.Log;
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.enums.BusinessType;
import com.goldenwater.common.utils.poi.ExcelUtil;
import com.goldenwater.yjjt.domain.YjjtStation;
import com.goldenwater.yjjt.service.IYjjtStationService;

/**
 * 引江济太站点Controller
 */
@RestController
@RequestMapping("/yjjt/station")
public class YjjtStationController extends BaseController
{
    @Autowired
    private IYjjtStationService yjjtStationService;

    /**
     * 查询站点列表
     */
    @PreAuthorize("@ss.hasPermi('yjjt:station:list')")
    @GetMapping("/list")
    public TableDataInfo list(YjjtStation station)
    {
        startPage();
        List<YjjtStation> list = yjjtStationService.selectYjjtStationList(station);
        return getDataTable(list);
    }

    /**
     * 导出站点列表
     */
    @PreAuthorize("@ss.hasPermi('yjjt:station:export')")
    @Log(title = "引江济太站点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YjjtStation station)
    {
        List<YjjtStation> list = yjjtStationService.selectYjjtStationList(station);
        ExcelUtil<YjjtStation> util = new ExcelUtil<YjjtStation>(YjjtStation.class);
        util.exportExcel(response, list, "站点数据");
    }

    /**
     * 获取站点详细信息
     */
    @PreAuthorize("@ss.hasPermi('yjjt:station:query')")
    @GetMapping(value = { "/", "/{id}" })
    public AjaxResult getInfo(@PathVariable(value = "id", required = false) Long id)
    {
        AjaxResult ajax = AjaxResult.success();
        if (id != null)
        {
            ajax.put(AjaxResult.DATA_TAG, yjjtStationService.selectYjjtStationById(id));
        }
        return ajax;
    }

    /**
     * 新增站点
     */
    @PreAuthorize("@ss.hasPermi('yjjt:station:add')")
    @Log(title = "引江济太站点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YjjtStation station)
    {
        // 检查同站编码+年份是否已存在
        if (station.getStcd() != null && station.getYear() != null)
        {
            YjjtStation existing = yjjtStationService.selectByStcd(station.getStcd());
            if (existing != null)
            {
                // 已存在的站点追加年份
                String existYear = existing.getYear();
                if (existYear != null && !existYear.contains(station.getYear()))
                {
                    existing.setYear(existYear + "," + station.getYear());
                    existing.setUpdateBy(getUsername());
                    yjjtStationService.updateYjjtStation(existing);
                }
                return success();
            }
        }
        station.setCreateBy(getUsername());
        return toAjax(yjjtStationService.insertYjjtStation(station));
    }

    /**
     * 修改站点
     */
    @PreAuthorize("@ss.hasPermi('yjjt:station:edit')")
    @Log(title = "引江济太站点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YjjtStation station)
    {
        station.setUpdateBy(getUsername());
        return toAjax(yjjtStationService.updateYjjtStation(station));
    }

    /**
     * 删除站点
     */
    @PreAuthorize("@ss.hasPermi('yjjt:station:remove')")
    @Log(title = "引江济太站点", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yjjtStationService.deleteYjjtStationByIds(ids));
    }
}
