package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.service.ShopStoreWarehouseService;
import com.gxwebsoft.shop.entity.ShopStoreWarehouse;
import com.gxwebsoft.shop.param.ShopStoreWarehouseParam;
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-02-07 18:10:25
 */
@Tag(name = "仓库管理")
@RestController
@RequestMapping("/api/shop/shop-store-warehouse")
public class ShopStoreWarehouseController extends BaseController {
    @Resource
    private ShopStoreWarehouseService shopStoreWarehouseService;

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

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

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

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

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

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

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

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

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

}
