package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.ShopStoreService;
import com.gxwebsoft.shop.entity.ShopStore;
import com.gxwebsoft.shop.param.ShopStoreParam;
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 15:00:25
 */
@Tag(name = "门店管理")
@RestController
@RequestMapping("/api/shop/shop-store")
public class ShopStoreController extends BaseController {
    @Resource
    private ShopStoreService shopStoreService;

    @Operation(summary = "分页查询门店")
    @GetMapping("/page")
    public ApiResult<PageResult<ShopStore>> page(ShopStoreParam param) {
        // 使用关联查询
        return success(shopStoreService.pageRel(param));
    }

    @Operation(summary = "查询全部门店")
    @GetMapping()
    public ApiResult<List<ShopStore>> list(ShopStoreParam param) {
        // 使用关联查询
        return success(shopStoreService.listRel(param));
    }

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

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

    @PreAuthorize("hasAuthority('shop:shopStore:update')")
    @OperationLog
    @Operation(summary = "修改门店")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ShopStore shopStore) {
        if (shopStoreService.updateById(shopStore)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
