package com.ruoyi.web.controller.app;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.app.domain.request.ReqSupplierList;
import com.ruoyi.system.domain.SysPost;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.app.domain.Supplier;
import com.ruoyi.app.service.ISupplierService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供应商Controller
 * 
 * @author coede
 * @date 2025-03-19
 */
@Api("供应商信息管理")
@RestController
@RequestMapping("/app/supplier")
public class SupplierController extends BaseController
{
    @Autowired
    private ISupplierService supplierService;

    /**
     * 查询供应商列表
     */
    @ApiOperation("查询供应商列表")
    @GetMapping("/list")
    public TableDataInfo list(ReqSupplierList req)
    {
        startPage();
        List<Supplier> list = supplierService.selectSupplierList(req);
        return getDataTable(list);
    }

    /**
     * 获取供应商选择框列表
     */
    @ApiOperation("获取供应商选择框列表")
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<Supplier> list = supplierService.selectAllSupplierList();
        return success(list);
    }

    /**
     * 导出供应商列表
     */
    @PreAuthorize("@ss.hasPermi('app:supplier:export')")
    @Log(title = "供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReqSupplierList req)
    {
        List<Supplier> list = supplierService.selectSupplierList(req);
        ExcelUtil<Supplier> util = new ExcelUtil<Supplier>(Supplier.class);
        util.exportExcel(response, list, "供应商数据");
    }

    /**
     * 获取供应商详细信息
     */
    @ApiOperation("获取供应商详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(supplierService.selectSupplierById(id));
    }

    /**
     * 新增供应商
     */
    @ApiOperation("新增供应商")
    @PreAuthorize("@ss.hasPermi('app:supplier:add')")
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated  @RequestBody Supplier supplier)
    {
        if (!supplierService.checkSupplierNameUnique(supplier))
        {
            return error("新增供应商'" + supplier.getSupplierName() + "'失败，列表中存在重复的供应商名称");
        }
        if (!supplierService.checkSupplierNoUnique(supplier))
        {
            return error("新增供应商'" + supplier.getSupplierName() + "'失败，列表中存在重复的供应商编号");
        }
        supplier.setCreateBy(getUsername());
        return toAjax(supplierService.insertSupplier(supplier));
    }

    /**
     * 修改供应商
     */
    @ApiOperation("修改供应商")
    @PreAuthorize("@ss.hasPermi('app:supplier:edit')")
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Supplier supplier)
    {
        if (!supplierService.checkSupplierNameUnique(supplier))
        {
            return error("修改供应商'" + supplier.getSupplierName() + "'失败，列表中存在重复的供应商名称");
        }
        if (!supplierService.checkSupplierNoUnique(supplier))
        {
            return error("修改供应商'" + supplier.getSupplierName() + "'失败，列表中存在重复的供应商编号");
        }
        supplier.setUpdateBy(getUsername());
        return toAjax(supplierService.updateSupplier(supplier));
    }

    /**
     * 删除供应商
     */
    @ApiOperation("删除供应商")
    @PreAuthorize("@ss.hasPermi('app:supplier:remove')")
    @Log(title = "供应商", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(supplierService.deleteSupplierByIds(ids));
    }
}
