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.common.mybatis.core.page.TableDataInfo;
import org.dromara.web.domain.bo.StorageLocationBo;
import org.dromara.web.domain.vo.StorageLocationVo;
import org.dromara.web.service.IStorageLocationService;
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;

/**
 * 库位管理
 *
 * @author Lion Li
 * @date 2025-07-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/warehouse/storageLocation")
public class StorageLocationController extends BaseController {

    private final IStorageLocationService storageLocationService;

    /**
     * 查询库位管理列表
     */
    @SaCheckPermission("warehouse:storageLocation:list")
    @GetMapping("/list")
    public TableDataInfo<StorageLocationVo> list(StorageLocationBo bo, PageQuery pageQuery) {
        return storageLocationService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出库位管理列表
     */
    @SaCheckPermission("warehouse:storageLocation:export")
    @Log(title = "库位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StorageLocationBo bo, HttpServletResponse response) {
        List<StorageLocationVo> list = storageLocationService.queryList(bo);
        ExcelUtil.exportExcel(list, "库位管理", StorageLocationVo.class, response);
    }

    /**
     * 获取库位管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("warehouse:storageLocation:query")
    @GetMapping("/{id}")
    public R<StorageLocationVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(storageLocationService.queryById(id));
    }

    /**
     * 新增库位管理
     */
    @SaCheckPermission("warehouse:storageLocation:add")
    @Log(title = "库位管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StorageLocationBo bo) {
        return toAjax(storageLocationService.insertByBo(bo));
    }

    /**
     * 修改库位管理
     */
    @SaCheckPermission("warehouse:storageLocation:edit")
    @Log(title = "库位管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StorageLocationBo bo) {
        return toAjax(storageLocationService.updateByBo(bo));
    }

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