package com.gxwebsoft.tower.controller;

import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.tower.entity.TowerPlaceSafety;
import com.gxwebsoft.tower.param.TowerPlaceSafetyParam;
import com.gxwebsoft.tower.service.TowerPlaceSafetyService;
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 11:00:17
 */
@Api(tags = "安全巡检部位管理表管理")
@RestController
@RequestMapping("/api/tower/tower-place-safety")
public class TowerPlaceSafetyController extends BaseController {
    @Resource
    private TowerPlaceSafetyService towerPlaceSafetyService;

    @PreAuthorize("hasAuthority('tower:towerPlaceSafety:list')")
    @OperationLog
    @ApiOperation("分页查询安全巡检部位管理表")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerPlaceSafety>> page(TowerPlaceSafetyParam param) {
        PageParam<TowerPlaceSafety, TowerPlaceSafetyParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerPlaceSafetyService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(towerPlaceSafetyService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerPlaceSafety:list')")
    @OperationLog
    @ApiOperation("查询全部安全巡检部位管理表")
    @GetMapping()
    public ApiResult<List<TowerPlaceSafety>> list(TowerPlaceSafetyParam param) {
        PageParam<TowerPlaceSafety, TowerPlaceSafetyParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerPlaceSafetyService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerPlaceSafetyService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerPlaceSafety:list')")
    @OperationLog
    @ApiOperation("根据id查询安全巡检部位管理表")
    @GetMapping("/{id}")
    public ApiResult<TowerPlaceSafety> get(@PathVariable("id") Integer id) {
        return success(towerPlaceSafetyService.getById(id));
        // 使用关联查询
        //return success(towerPlaceSafetyService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerPlaceSafety:save')")
    @OperationLog
    @ApiOperation("添加安全巡检部位管理表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerPlaceSafety towerPlaceSafety) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          towerPlaceSafety.setUserId(loginUser.getUserId());
        }
        if (towerPlaceSafetyService.save(towerPlaceSafety)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPlaceSafety:update')")
    @OperationLog
    @ApiOperation("修改安全巡检部位管理表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerPlaceSafety towerPlaceSafety) {
        if (towerPlaceSafetyService.updateById(towerPlaceSafety)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPlaceSafety:remove')")
    @OperationLog
    @ApiOperation("删除安全巡检部位管理表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerPlaceSafetyService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPlaceSafety:save')")
    @OperationLog
    @ApiOperation("批量添加安全巡检部位管理表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerPlaceSafety> list) {
        if (towerPlaceSafetyService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPlaceSafety:update')")
    @OperationLog
    @ApiOperation("批量修改安全巡检部位管理表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerPlaceSafety> batchParam) {
        if (batchParam.update(towerPlaceSafetyService, "place_safety_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerPlaceSafety:remove')")
    @OperationLog
    @ApiOperation("批量删除安全巡检部位管理表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (towerPlaceSafetyService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
