package com.gxwebsoft.tower.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.tower.service.TowerRentService;
import com.gxwebsoft.tower.entity.TowerRent;
import com.gxwebsoft.tower.param.TowerRentParam;
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.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 2024-02-05 15:04:19
 */
@Api(tags = "标准节租金管理")
@RestController
@RequestMapping("/api/tower/tower-rent")
public class TowerRentController extends BaseController {
    @Resource
    private TowerRentService towerRentService;

    @PreAuthorize("hasAuthority('tower:towerRent:list')")
    @OperationLog
    @ApiOperation("分页查询标准节租金")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerRent>> page(TowerRentParam param) {
        // 使用关联查询
        return success(towerRentService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerRent:list')")
    @OperationLog
    @ApiOperation("查询全部标准节租金")
    @GetMapping()
    public ApiResult<List<TowerRent>> list(TowerRentParam param) {
        PageParam<TowerRent, TowerRentParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(towerRentService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(towerRentService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerRent:list')")
    @OperationLog
    @ApiOperation("根据id查询标准节租金")
    @GetMapping("/{id}")
    public ApiResult<TowerRent> get(@PathVariable("id") Integer id) {
        return success(towerRentService.getById(id));
        // 使用关联查询
        //return success(towerRentService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerRent:save')")
    @OperationLog
    @ApiOperation("添加标准节租金")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerRent towerRent) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          towerRent.setUserId(loginUser.getUserId());
        }
        if (towerRentService.save(towerRent)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerRent:update')")
    @OperationLog
    @ApiOperation("修改标准节租金")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerRent towerRent) {
        if (towerRentService.updateById(towerRent)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerRent:remove')")
    @OperationLog
    @ApiOperation("删除标准节租金")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerRentService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('tower:towerRent:save')")
    @OperationLog
    @ApiOperation("批量添加标准节租金")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerRent> list) {
        if (towerRentService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerRent:update')")
    @OperationLog
    @ApiOperation("批量修改标准节租金")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerRent> batchParam) {
        if (batchParam.update(towerRentService, "rent_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerRent:remove')")
    @OperationLog
    @ApiOperation("批量删除标准节租金")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (towerRentService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
