package org.dromara.zhishu.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.zhishu.domain.WarehouseSettings;
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;
import org.dromara.zhishu.domain.vo.WarehouseSettingsVo;
import org.dromara.zhishu.domain.bo.WarehouseSettingsBo;
import org.dromara.zhishu.service.IWarehouseSettingsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 设置
 *
 * @author yxy
 * @date 2025-12-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhishu/warehouseSettings")
public class WarehouseSettingsController extends BaseController {

    private final IWarehouseSettingsService warehouseSettingsService;

    /**
     * 查询设置列表
     */
    @SaCheckPermission("zhishu:warehouseSettings:list")
    @GetMapping("/list")
    public TableDataInfo<WarehouseSettings> list(WarehouseSettingsBo bo, PageQuery pageQuery) {
        return warehouseSettingsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出设置列表
     */
    @SaCheckPermission("zhishu:warehouseSettings:export")
    @Log(title = "设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WarehouseSettingsBo bo, HttpServletResponse response) {
        List<WarehouseSettingsVo> list = warehouseSettingsService.queryList(bo);
        ExcelUtil.exportExcel(list, "设置", WarehouseSettingsVo.class, response);
    }

    /**
     * 获取设置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("zhishu:warehouseSettings:query")
    @GetMapping("/{id}")
    public R<WarehouseSettingsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(warehouseSettingsService.queryById(id));
    }

    /**
     * 新增设置
     */
    @SaCheckPermission("zhishu:warehouseSettings:add")
    @Log(title = "设置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WarehouseSettingsBo bo) {
        return toAjax(warehouseSettingsService.insertByBo(bo));
    }

    /**
     * 修改设置
     */
    @SaCheckPermission("zhishu:warehouseSettings:edit")
    @Log(title = "设置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WarehouseSettingsBo bo) {
        return toAjax(warehouseSettingsService.updateByBo(bo));
    }

    @SaCheckPermission("zhishu:warehouseSettings:edit")
    @Log(title = "设置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/editStatus")
    public R<Void> editStatus(@Validated(EditGroup.class) @RequestBody WarehouseSettingsBo bo) {
        return toAjax(warehouseSettingsService.updateStatus(bo));
    }

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