package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.ShopCommunityService;
import com.gxwebsoft.shop.entity.ShopCommunity;
import com.gxwebsoft.shop.param.ShopCommunityParam;
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-29 20:48:32
 */
@Tag(name = "小区管理")
@RestController
@RequestMapping("/api/shop/shop-community")
public class ShopCommunityController extends BaseController {
    @Resource
    private ShopCommunityService shopCommunityService;

    @Operation(summary = "分页查询小区")
    @GetMapping("/page")
    public ApiResult<PageResult<ShopCommunity>> page(ShopCommunityParam param) {
        // 使用关联查询
        return success(shopCommunityService.pageRel(param));
    }

    @Operation(summary = "查询全部小区")
    @GetMapping()
    public ApiResult<List<ShopCommunity>> list(ShopCommunityParam param) {
        // 使用关联查询
        return success(shopCommunityService.listRel(param));
    }

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

    @PreAuthorize("hasAuthority('shop:shopCommunity:save')")
    @OperationLog
    @Operation(summary = "添加小区")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ShopCommunity shopCommunity) {
        if (shopCommunityService.save(shopCommunity)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:shopCommunity:update')")
    @OperationLog
    @Operation(summary = "修改小区")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ShopCommunity shopCommunity) {
        if (shopCommunityService.updateById(shopCommunity)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
