package com.maintain.business.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.maintain.common.annotation.RepeatSubmit;
import com.maintain.common.annotation.Log;
import com.maintain.common.core.controller.BaseController;
import com.maintain.common.core.domain.PageQuery;
import com.maintain.common.core.domain.R;
import com.maintain.common.core.validate.AddGroup;
import com.maintain.common.core.validate.EditGroup;
import com.maintain.common.enums.BusinessType;
import com.maintain.common.utils.poi.ExcelUtil;
import com.maintain.business.domain.vo.ErpMaterialVendorVo;
import com.maintain.business.domain.bo.ErpMaterialVendorBo;
import com.maintain.business.service.IErpMaterialVendorService;
import com.maintain.common.core.page.TableDataInfo;

/**
 * 物资供应商
 *
 * @author liushuai
 * @date 2025-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/materialVendor")
public class ErpMaterialVendorController extends BaseController {

    private final IErpMaterialVendorService iErpMaterialVendorService;

    /**
     * 查询物资供应商列表
     */
    @SaCheckPermission("business:materialVendor:list")
    @GetMapping("/list")
    public TableDataInfo<ErpMaterialVendorVo> list(ErpMaterialVendorBo bo, PageQuery pageQuery) {
        return iErpMaterialVendorService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询物资供应商列表
     */
    @GetMapping("/listAll")
    public R listAll(ErpMaterialVendorBo bo) {
        return R.ok(iErpMaterialVendorService.queryList(bo));
    }

    /**
     * 导出物资供应商列表
     */
    @SaCheckPermission("business:materialVendor:export")
    @Log(title = "物资供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpMaterialVendorBo bo, HttpServletResponse response) {
        List<ErpMaterialVendorVo> list = iErpMaterialVendorService.queryList(bo);
        ExcelUtil.exportExcel(list, "物资供应商", ErpMaterialVendorVo.class, response);
    }

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

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

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

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