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.ErpMaterialCheckVo;
import com.maintain.business.domain.bo.ErpMaterialCheckBo;
import com.maintain.business.service.IErpMaterialCheckService;
import com.maintain.common.core.page.TableDataInfo;

/**
 * 物资盘点
 *
 * @author liushuai
 * @date 2025-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/materialCheck")
public class ErpMaterialCheckController extends BaseController {

    private final IErpMaterialCheckService iErpMaterialCheckService;

    /**
     * 查询物资盘点列表
     */
    @SaCheckPermission("business:materialCheck:list")
    @GetMapping("/list")
    public TableDataInfo<ErpMaterialCheckVo> list(ErpMaterialCheckBo bo, PageQuery pageQuery) {
        return iErpMaterialCheckService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出物资盘点列表
     */
    @SaCheckPermission("business:materialCheck:export")
    @Log(title = "物资盘点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpMaterialCheckBo bo, HttpServletResponse response) {
        List<ErpMaterialCheckVo> list = iErpMaterialCheckService.queryList(bo);
        ExcelUtil.exportExcel(list, "物资盘点", ErpMaterialCheckVo.class, response);
    }

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

    /**
     * 新增物资盘点
     */
    @SaCheckPermission("business:materialCheck:add")
    @Log(title = "物资盘点", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpMaterialCheckBo bo) {
        return toAjax(iErpMaterialCheckService.insertByBo(bo));
    }

    /**
     * 修改物资盘点
     */
    @SaCheckPermission("business:materialCheck:edit")
    @Log(title = "物资盘点", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpMaterialCheckBo bo) {
        return toAjax(iErpMaterialCheckService.updateByBo(bo));
    }

    /**
     * 物资盘点
     */
    @SaCheckPermission("business:materialCheck:check")
    @Log(title = "物资盘点", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/check")
    public R<Void> check(@Validated(EditGroup.class) @RequestBody ErpMaterialCheckBo bo) {
        return toAjax(iErpMaterialCheckService.check(bo));
    }

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