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.TowerModel;
import com.gxwebsoft.tower.param.TowerModelParam;
import com.gxwebsoft.tower.service.TowerModelService;
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-05-29 14:07:46
 */
@Api(tags = "设备型号管理表管理")
@RestController
@RequestMapping("/api/tower/tower-model")
public class TowerModelController extends BaseController {
    @Resource
    private TowerModelService towerModelService;

    @PreAuthorize("hasAuthority('tower:towerModel:list')")
    @OperationLog
    @ApiOperation("分页查询设备型号管理表")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerModel>> page(TowerModelParam param) {
        // 使用关联查询
        return success(towerModelService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerModel:list')")
    @OperationLog
    @ApiOperation("查询全部设备型号管理表")
    @GetMapping()
    public ApiResult<List<TowerModel>> list(TowerModelParam param) {
        // 使用关联查询
        return success(towerModelService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerModel:list')")
    @OperationLog
    @ApiOperation("根据id查询设备型号管理表")
    @GetMapping("/{id}")
    public ApiResult<TowerModel> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(towerModelService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerModel:save')")
    @OperationLog
    @ApiOperation("添加设备型号管理表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerModel towerModel) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          towerModel.setUserId(loginUser.getUserId());
        }
        if (towerModelService.save(towerModel)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerModel:update')")
    @OperationLog
    @ApiOperation("修改设备型号管理表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerModel towerModel) {
        if (towerModelService.updateById(towerModel)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerModel:remove')")
    @OperationLog
    @ApiOperation("删除设备型号管理表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerModelService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('tower:towerModel:save')")
    @OperationLog
    @ApiOperation("批量添加设备型号管理表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerModel> list) {
        if (towerModelService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerModel:update')")
    @OperationLog
    @ApiOperation("批量修改设备型号管理表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerModel> batchParam) {
        if (batchParam.update(towerModelService, "category_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerModel:remove')")
    @OperationLog
    @ApiOperation("批量删除设备型号管理表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (towerModelService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
