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 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.GxExchangeUnit;
import com.goldenwater.gx.service.IGxExchangeUnitService;

/**
 * 数据交换单位Controller
 */
@RestController
@RequestMapping("/gx/unit")
public class GxExchangeUnitController extends BaseController
{
    @Autowired
    private IGxExchangeUnitService gxExchangeUnitService;

    @PreAuthorize("@ss.hasPermi('gx:unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(GxExchangeUnit unit)
    {
        startPage();
        List<GxExchangeUnit> list = gxExchangeUnitService.selectGxExchangeUnitList(unit);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('gx:unit:export')")
    @Log(title = "单位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxExchangeUnit unit)
    {
        List<GxExchangeUnit> list = gxExchangeUnitService.selectGxExchangeUnitList(unit);
        ExcelUtil<GxExchangeUnit> util = new ExcelUtil<GxExchangeUnit>(GxExchangeUnit.class);
        util.exportExcel(response, list, "单位数据");
    }

    @PreAuthorize("@ss.hasPermi('gx:unit:query')")
    @GetMapping(value = { "/", "/{id}" })
    public AjaxResult getInfo(@PathVariable(value = "id", required = false) Long id)
    {
        if (id != null)
        {
            return success(gxExchangeUnitService.selectGxExchangeUnitById(id));
        }
        return success();
    }

    @PreAuthorize("@ss.hasPermi('gx:unit:add')")
    @Log(title = "单位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxExchangeUnit unit)
    {
        unit.setCreateBy(getUsername());
        return toAjax(gxExchangeUnitService.insertGxExchangeUnit(unit));
    }

    @PreAuthorize("@ss.hasPermi('gx:unit:edit')")
    @Log(title = "单位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxExchangeUnit unit)
    {
        unit.setUpdateBy(getUsername());
        return toAjax(gxExchangeUnitService.updateGxExchangeUnit(unit));
    }

    @PreAuthorize("@ss.hasPermi('gx:unit:remove')")
    @Log(title = "单位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gxExchangeUnitService.deleteGxExchangeUnitByIds(ids));
    }

    @Anonymous
    @GetMapping("/front/list")
    public AjaxResult frontList()
    {
        GxExchangeUnit unit = new GxExchangeUnit();
        return success(gxExchangeUnitService.selectGxExchangeUnitList(unit));
    }
}
