package com.gxwebsoft.tower.controller;

import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.tower.entity.TowerPlaceStandard;
import com.gxwebsoft.tower.param.TowerPlaceStandardParam;
import com.gxwebsoft.tower.service.TowerPlaceStandardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 检查标准附件表控制器
 *
 * @author 科技小王子
 * @since 2023-06-08 15:37:00
 */
@Api(tags = "检查标准附件表管理")
@RestController
@RequestMapping("/api/tower/tower-place-standard")
public class TowerPlaceStandardController extends BaseController {
    @Resource
    private TowerPlaceStandardService towerPlaceStandardService;

    @PreAuthorize("hasAuthority('tower:towerPlaceStandard:list')")
    @OperationLog
    @ApiOperation("分页查询检查标准附件表")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerPlaceStandard>> page(TowerPlaceStandardParam param) {
        PageParam<TowerPlaceStandard, TowerPlaceStandardParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerPlaceStandardService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(towerPlaceStandardService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerPlaceStandard:list')")
    @OperationLog
    @ApiOperation("查询全部检查标准附件表")
    @GetMapping()
    public ApiResult<List<TowerPlaceStandard>> list(TowerPlaceStandardParam param) {
        PageParam<TowerPlaceStandard, TowerPlaceStandardParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerPlaceStandardService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerPlaceStandardService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerPlaceStandard:list')")
    @OperationLog
    @ApiOperation("根据id查询检查标准附件表")
    @GetMapping("/{id}")
    public ApiResult<TowerPlaceStandard> get(@PathVariable("id") Integer id) {
        return success(towerPlaceStandardService.getById(id));
        // 使用关联查询
        //return success(towerPlaceStandardService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerPlaceStandard:save')")
    @OperationLog
    @ApiOperation("添加检查标准附件表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerPlaceStandard towerPlaceStandard) {
        if (towerPlaceStandardService.save(towerPlaceStandard)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPlaceStandard:update')")
    @OperationLog
    @ApiOperation("修改检查标准附件表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerPlaceStandard towerPlaceStandard) {
        if (towerPlaceStandardService.updateById(towerPlaceStandard)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPlaceStandard:remove')")
    @OperationLog
    @ApiOperation("删除检查标准附件表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerPlaceStandardService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPlaceStandard:save')")
    @OperationLog
    @ApiOperation("批量添加检查标准附件表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerPlaceStandard> list) {
        if (towerPlaceStandardService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPlaceStandard:update')")
    @OperationLog
    @ApiOperation("批量修改检查标准附件表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerPlaceStandard> batchParam) {
        if (batchParam.update(towerPlaceStandardService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPlaceStandard:remove')")
    @OperationLog
    @ApiOperation("批量删除检查标准附件表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (towerPlaceStandardService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
