package com.gxwebsoft.hjm.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.hjm.service.HjmFenceService;
import com.gxwebsoft.hjm.entity.HjmFence;
import com.gxwebsoft.hjm.param.HjmFenceParam;
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 com.gxwebsoft.common.system.entity.User;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 黄家明_电子围栏控制器
 *
 * @author 科技小王子
 * @since 2025-06-03 02:08:03
 */
@Tag(name = "黄家明_电子围栏管理")
@RestController
@RequestMapping("/api/hjm/hjm-fence")
public class HjmFenceController extends BaseController {
    @Resource
    private HjmFenceService hjmFenceService;

    @Operation(summary = "分页查询黄家明_电子围栏")
    @GetMapping("/page")
    public ApiResult<PageResult<HjmFence>> page(HjmFenceParam param) {
        // 使用关联查询
        return success(hjmFenceService.pageRel(param));
    }

    @Operation(summary = "查询全部黄家明_电子围栏")
    @GetMapping()
    public ApiResult<List<HjmFence>> list(HjmFenceParam param) {
        // 使用关联查询
        return success(hjmFenceService.listRel(param));
    }

    @Operation(summary = "根据id查询黄家明_电子围栏")
    @GetMapping("/{id}")
    public ApiResult<HjmFence> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(hjmFenceService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('hjm:hjmFence:save')")
    @OperationLog
    @Operation(summary = "添加黄家明_电子围栏")
    @PostMapping()
    public ApiResult<?> save(@RequestBody HjmFence hjmFence) {
        if (hjmFenceService.save(hjmFence)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('hjm:hjmFence:update')")
    @OperationLog
    @Operation(summary = "修改黄家明_电子围栏")
    @PutMapping()
    public ApiResult<?> update(@RequestBody HjmFence hjmFence) {
        if (hjmFenceService.updateById(hjmFence)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('hjm:hjmFence:remove')")
    @OperationLog
    @Operation(summary = "删除黄家明_电子围栏")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (hjmFenceService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('hjm:hjmFence:save')")
    @OperationLog
    @Operation(summary = "批量添加黄家明_电子围栏")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<HjmFence> list) {
        if (hjmFenceService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('hjm:hjmFence:update')")
    @OperationLog
    @Operation(summary = "批量修改黄家明_电子围栏")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<HjmFence> batchParam) {
        if (batchParam.update(hjmFenceService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('hjm:hjmFence:remove')")
    @OperationLog
    @Operation(summary = "批量删除黄家明_电子围栏")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (hjmFenceService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
