package com.tmerclub.cloud.warehouse.controller.admin;

import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.warehouse.model.dto.WarehouseDTO;
import com.tmerclub.cloud.warehouse.model.vo.WarehouseVO;
import com.tmerclub.cloud.warehouse.service.WarehouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

/**
 * 仓库
 *
 * @author : frank
 * @create : 2025-04-14
 */
@Tag(name = "后管-仓库")
@RestController("adminWarehouseController")
@RequestMapping("/admin/warehouse")
public class WarehouseController {
    @Resource
    WarehouseService warehouseService;

    /**
     * 分页查询
     *
     * @param pageDTO      分页参数
     * @param warehouseDTO 查询参数
     * @return 分页数据
     */
    @GetMapping("/page")
    @Operation(summary = "查询仓库分页列表", description = "查询仓库分页列表")
    public ServerResponseEntity<PageVO<WarehouseVO>> page(@Valid PageDTO pageDTO, WarehouseDTO warehouseDTO) {
        PageVO<WarehouseVO> warehousePage = warehouseService.page(pageDTO, warehouseDTO);
        return ServerResponseEntity.success(warehousePage);
    }


    /**
     * 根据主键id获取仓库
     *
     * @param id 主键id
     * @return WarehouseVO
     */
    @GetMapping
    @Operation(summary = "根据id获取仓库", description = "根据id获取仓库")
    public ServerResponseEntity<WarehouseVO> getById(@RequestParam Long id) {
        WarehouseVO warehouse = warehouseService.getById(id);
        return ServerResponseEntity.success(warehouse);
    }

    /**
     * 新增仓库
     *
     * @param warehouseDTO WarehouseDTO
     * @return ServerResponseEntity
     */
    @PostMapping
    @Operation(summary = "新增仓库", description = "新增仓库")
    public ServerResponseEntity<Void> save(@Valid @RequestBody WarehouseDTO warehouseDTO) {
        int insert = warehouseService.save(warehouseDTO);
        return insert > 0 ? ServerResponseEntity.success() : ServerResponseEntity.showFailMsg("保存失败");
    }

    /**
     * 修改仓库
     *
     * @param warehouseDTO WarehouseDTO
     * @return ServerResponseEntity
     */
    @PutMapping
    @Operation(summary = "修改仓库", description = "修改仓库")
    public ServerResponseEntity<Void> update(@Valid @RequestBody WarehouseDTO warehouseDTO) {
        int update = warehouseService.update(warehouseDTO);
        return update > 0 ? ServerResponseEntity.success() : ServerResponseEntity.showFailMsg("修改失败");
    }

    /**
     * 删除仓库
     *
     * @param id 主键id
     * @return ServerResponseEntity
     */
    @DeleteMapping
    @Operation(summary = "删除仓库", description = "删除仓库")
    public ServerResponseEntity<Void> delete(@RequestParam Long id) {
        int delete = warehouseService.delete(id);
        return delete > 0 ? ServerResponseEntity.success() : ServerResponseEntity.showFailMsg("删除失败");
    }
}