package com.goldenwater.web.controller.gx;

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 org.springframework.web.multipart.MultipartFile;
import com.goldenwater.common.annotation.Anonymous;
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.gx.domain.GxExchangeStation;
import com.goldenwater.gx.service.IGxExchangeStationService;
import com.goldenwater.gx.domain.GxExchangeBiz;
import com.goldenwater.gx.service.IGxExchangeBizService;

/**
 * 数据交换站点Controller
 */
@RestController
@RequestMapping("/gx/station")
public class GxExchangeStationController extends BaseController
{
    @Autowired
    private IGxExchangeStationService gxExchangeStationService;

    @Autowired
    private IGxExchangeBizService gxExchangeBizService;

    @PreAuthorize("@ss.hasPermi('gx:station:list')")
    @GetMapping("/list")
    public TableDataInfo list(GxExchangeStation station)
    {
        startPage();
        List<GxExchangeStation> list = gxExchangeStationService.selectGxExchangeStationList(station);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('gx:station:export')")
    @Log(title = "站点管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxExchangeStation station)
    {
        List<GxExchangeStation> list = gxExchangeStationService.selectGxExchangeStationList(station);
        ExcelUtil<GxExchangeStation> util = new ExcelUtil<GxExchangeStation>(GxExchangeStation.class);
        util.exportExcel(response, list, "站点数据");
    }

    @Log(title = "站点管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('gx:station:add')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<GxExchangeStation> util = new ExcelUtil<GxExchangeStation>(GxExchangeStation.class);
        List<GxExchangeStation> stationList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = gxExchangeStationService.importStation(stationList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<GxExchangeStation> util = new ExcelUtil<GxExchangeStation>(GxExchangeStation.class);
        util.importTemplateExcel(response, "站点数据");
    }

    @PreAuthorize("@ss.hasPermi('gx: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, gxExchangeStationService.selectGxExchangeStationById(id));
        }
        // 返回所有业务域供下拉选择
        GxExchangeBiz bizQuery = new GxExchangeBiz();
        ajax.put("bizList", gxExchangeBizService.selectGxExchangeBizList(bizQuery));
        return ajax;
    }

    @PreAuthorize("@ss.hasPermi('gx:station:add')")
    @Log(title = "站点管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxExchangeStation station)
    {
        station.setCreateBy(getUsername());
        return toAjax(gxExchangeStationService.insertGxExchangeStation(station));
    }

    @PreAuthorize("@ss.hasPermi('gx:station:edit')")
    @Log(title = "站点管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxExchangeStation station)
    {
        station.setUpdateBy(getUsername());
        return toAjax(gxExchangeStationService.updateGxExchangeStation(station));
    }

    @PreAuthorize("@ss.hasPermi('gx:station:remove')")
    @Log(title = "站点管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gxExchangeStationService.deleteGxExchangeStationByIds(ids));
    }

    /**
     * 根据业务编码查询站点列表（不分页）
     */
    @PreAuthorize("@ss.hasPermi('gx:station:list')")
    @GetMapping("/listByBizCode/{bizCode}")
    public AjaxResult listByBizCode(@PathVariable String bizCode)
    {
        return success(gxExchangeStationService.selectStationByBizCode(bizCode));
    }

    @Anonymous
    @GetMapping("/front/listByBizCode/{bizCode}")
    public AjaxResult frontListByBizCode(@PathVariable String bizCode)
    {
        return success(gxExchangeStationService.selectStationByBizCode(bizCode));
    }
}
