package org.dromara.web.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.web.domain.bo.ProductManufacturerBo;
import org.dromara.web.domain.vo.ProductManufacturerVo;
import org.dromara.web.service.IProductManufacturerService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
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.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;

import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 生产厂商
 *
 * @author Lion Li
 * @date 2025-07-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/warehouse/productManufacturer")
public class ProductManufacturerController extends BaseController {

    private final IProductManufacturerService productManufacturerService;

    /**
     * 查询生产厂商列表
     */
    @SaCheckPermission("warehouse:productManufacturer:list")
    @GetMapping("/list")
    public TableDataInfo<ProductManufacturerVo> list(ProductManufacturerBo bo, PageQuery pageQuery) {
        return productManufacturerService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出生产厂商列表
     */
    @SaCheckPermission("warehouse:productManufacturer:export")
    @Log(title = "生产厂商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductManufacturerBo bo, HttpServletResponse response) {
        List<ProductManufacturerVo> list = productManufacturerService.queryList(bo);
        ExcelUtil.exportExcel(list, "生产厂商", ProductManufacturerVo.class, response);
    }

    /**
     * 获取生产厂商详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("warehouse:productManufacturer:query")
    @GetMapping("/{id}")
    public R<ProductManufacturerVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(productManufacturerService.queryById(id));
    }

    /**
     * 新增生产厂商
     */
    @SaCheckPermission("warehouse:productManufacturer:add")
    @Log(title = "生产厂商", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductManufacturerBo bo) {
        return toAjax(productManufacturerService.insertByBo(bo));
    }

    /**
     * 修改生产厂商
     */
    @SaCheckPermission("warehouse:productManufacturer:edit")
    @Log(title = "生产厂商", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductManufacturerBo bo) {
        return toAjax(productManufacturerService.updateByBo(bo));
    }

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