package com.maintain.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.maintain.business.domain.bo.ErpMaterialInventoryRecordBo;
import com.maintain.business.domain.vo.ErpMaterialInventoryRecordVo;
import com.maintain.business.service.IErpMaterialInventoryRecordService;
import com.maintain.common.annotation.Log;
import com.maintain.common.annotation.RepeatSubmit;
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.page.TableDataInfo;
import com.maintain.common.core.validate.AddGroup;
import com.maintain.common.core.validate.EditGroup;
import com.maintain.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 物料库存记录
 *
 * @author liushuai
 * @date 2025-07-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/materialInventoryRecord")
public class ErpMaterialInventoryRecordController extends BaseController {

    private final IErpMaterialInventoryRecordService iErpMaterialInventoryRecordService;

    /**
     * 查询物料库存汇总记录列表
     */
    @GetMapping("/list")
    public TableDataInfo<ErpMaterialInventoryRecordVo> list(ErpMaterialInventoryRecordBo bo, PageQuery pageQuery) {
        return iErpMaterialInventoryRecordService.customQueryPageList(bo, pageQuery);
    }

    /**
     * 查询物料库存明细记录列表
     */
    @GetMapping("/inventoryList")
    public TableDataInfo<ErpMaterialInventoryRecordVo> inventoryList(ErpMaterialInventoryRecordBo bo, PageQuery pageQuery) {
        return iErpMaterialInventoryRecordService.customQueryPageInventoryList(bo, pageQuery);
    }

    /**
     * 查询物料库存记录列表
     */
    @GetMapping("/listByMaterialId")
    public R<List<ErpMaterialInventoryRecordVo>> listByMaterialId(ErpMaterialInventoryRecordBo bo) {
        return R.ok(iErpMaterialInventoryRecordService.queryListByMaterialId(bo));
    }

    /**
     * 获取物料库存记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:materialInventoryRecord:query")
    @GetMapping("/{id}")
    public R<ErpMaterialInventoryRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iErpMaterialInventoryRecordService.queryById(id));
    }

    /**
     * 新增物料库存记录
     */
    @SaCheckPermission("business:materialInventoryRecord:add")
    @Log(title = "物料库存记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpMaterialInventoryRecordBo bo) {
        return toAjax(iErpMaterialInventoryRecordService.insertByBo(bo));
    }

    /**
     * 修改物料库存记录
     */
    @SaCheckPermission("business:materialInventoryRecord:edit")
    @Log(title = "物料库存记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpMaterialInventoryRecordBo bo) {
        return toAjax(iErpMaterialInventoryRecordService.updateByBo(bo));
    }

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