package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.ShopWarehouseService;
import com.gxwebsoft.shop.entity.ShopWarehouse;
import com.gxwebsoft.shop.param.ShopWarehouseParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 仓库控制器
 *
 * @author 科技小王子
 * @since 2026-01-30 17:46:48
 */
@Tag(name = "仓库管理")
@RestController
@RequestMapping("/api/shop/shop-warehouse")
public class ShopWarehouseController extends BaseController {
    @Resource
    private ShopWarehouseService shopWarehouseService;

    @Operation(summary = "分页查询仓库")
    @GetMapping("/page")
    public ApiResult<PageResult<ShopWarehouse>> page(ShopWarehouseParam param) {
        // 使用关联查询
        return success(shopWarehouseService.pageRel(param));
    }

    @Operation(summary = "查询全部仓库")
    @GetMapping()
    public ApiResult<List<ShopWarehouse>> list(ShopWarehouseParam param) {
        // 使用关联查询
        return success(shopWarehouseService.listRel(param));
    }

    @Operation(summary = "根据id查询仓库")
    @GetMapping("/{id}")
    public ApiResult<ShopWarehouse> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(shopWarehouseService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:shopWarehouse:save')")
    @OperationLog
    @Operation(summary = "添加仓库")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ShopWarehouse shopWarehouse) {
        // 记录当前登录用户id
        // User loginUser = getLoginUser();
        // if (loginUser != null) {
        // shopWarehouse.setUserId(loginUser.getUserId());
        // }
        if (shopWarehouseService.save(shopWarehouse)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:shopWarehouse:update')")
    @OperationLog
    @Operation(summary = "修改仓库")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ShopWarehouse shopWarehouse) {
        if (shopWarehouseService.updateById(shopWarehouse)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:shopWarehouse:remove')")
    @OperationLog
    @Operation(summary = "删除仓库")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (shopWarehouseService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:shopWarehouse:save')")
    @OperationLog
    @Operation(summary = "批量添加仓库")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ShopWarehouse> list) {
        if (shopWarehouseService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:shopWarehouse:update')")
    @OperationLog
    @Operation(summary = "批量修改仓库")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ShopWarehouse> batchParam) {
        if (batchParam.update(shopWarehouseService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:shopWarehouse:remove')")
    @OperationLog
    @Operation(summary = "批量删除仓库")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (shopWarehouseService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
