package org.dromara.web.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.web.domain.bo.ProductSupplierBo;
import org.dromara.web.domain.vo.ProductSupplierVo;
import org.dromara.web.service.IProductSupplierService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 供应商
 *
 * @author Lion Li
 * @date 2025-07-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/warehouse/productSupplier")
public class ProductSupplierController extends BaseController {

    private final IProductSupplierService productSupplierService;

    /**
     * 查询供应商列表
     */
    @SaCheckPermission("warehouse:productSupplier:list")
    @GetMapping("/list")
    public TableDataInfo<ProductSupplierVo> list(ProductSupplierBo bo, PageQuery pageQuery) {
        return productSupplierService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出供应商列表
     */
    @SaCheckPermission("warehouse:productSupplier:export")
    @Log(title = "供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductSupplierBo bo, HttpServletResponse response) {
        List<ProductSupplierVo> list = productSupplierService.queryList(bo);
        ExcelUtil.exportExcel(list, "供应商", ProductSupplierVo.class, response);
    }

    /**
     * 获取供应商详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("warehouse:productSupplier:query")
    @GetMapping("/{id}")
    public R<ProductSupplierVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(productSupplierService.queryById(id));
    }

    /**
     * 新增供应商
     */
    @SaCheckPermission("warehouse:productSupplier:add")
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductSupplierBo bo) {
        return toAjax(productSupplierService.insertByBo(bo));
    }

    /**
     * 修改供应商
     */
    @SaCheckPermission("warehouse:productSupplier:edit")
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductSupplierBo bo) {
        return toAjax(productSupplierService.updateByBo(bo));
    }

    /**
     * 删除供应商
     *
     * @param ids 主键串
     */
    @SaCheckPermission("warehouse:productSupplier:remove")
    @Log(title = "供应商", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(productSupplierService.deleteWithValidByIds(List.of(ids), true));
    }
}
