package org.dromara.materials.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.materials.domain.req.materialsinventory.MaterialsInventoryCreateReq;
import org.dromara.materials.domain.req.materialsinventory.MaterialsInventoryQueryReq;
import org.dromara.materials.domain.req.materialsinventory.MaterialsInventoryUpdateReq;
import org.dromara.materials.domain.vo.BusMaterialsInventoryVo;
import org.dromara.materials.service.IBusMaterialsInventoryService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 材料出/入库
 *
 * @author lcj
 * @date 2025-03-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/materials/materialsInventory")
public class BusMaterialsInventoryController extends BaseController {

    private final IBusMaterialsInventoryService busMaterialsInventoryService;

    /**
     * 查询材料出/入库列表
     */
    @SaCheckPermission("materials:materialsInventory:list")
    @GetMapping("/list")
    public TableDataInfo<BusMaterialsInventoryVo> list(MaterialsInventoryQueryReq req, PageQuery pageQuery) {
        return busMaterialsInventoryService.queryPageList(req, pageQuery);
    }

    /**
     * 导出材料出/入库列表
     */
    @SaCheckPermission("materials:materialsInventory:export")
    @Log(title = "材料出/入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MaterialsInventoryQueryReq req, HttpServletResponse response) {
        List<BusMaterialsInventoryVo> list = busMaterialsInventoryService.queryList(req);
        ExcelUtil.exportExcel(list, "材料出/入库", BusMaterialsInventoryVo.class, response);
    }

    /**
     * 获取材料出/入库详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("materials:materialsInventory:query")
    @GetMapping("/{id}")
    public R<BusMaterialsInventoryVo> getInfo(@NotNull(message = "主键不能为空")
                                              @PathVariable Long id) {
        return R.ok(busMaterialsInventoryService.queryById(id));
    }

    /**
     * 新增材料出/入库
     */
    @SaCheckPermission("materials:materialsInventory:add")
    @Log(title = "材料出/入库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody MaterialsInventoryCreateReq req) {
        return R.ok(busMaterialsInventoryService.insertByBo(req));
    }

    /**
     * 修改材料出/入库
     */
    @SaCheckPermission("materials:materialsInventory:edit")
    @Log(title = "材料出/入库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MaterialsInventoryUpdateReq req) {
        return toAjax(busMaterialsInventoryService.updateByBo(req));
    }

    /**
     * 删除材料出/入库
     *
     * @param ids 主键串
     */
    @SaCheckPermission("materials:materialsInventory:remove")
    @Log(title = "材料出/入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(busMaterialsInventoryService.deleteWithValidByIds(List.of(ids), true));
    }
}
