package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaAssetsMysqlService;
import com.gxwebsoft.oa.entity.OaAssetsMysql;
import com.gxwebsoft.oa.param.OaAssetsMysqlParam;
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.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 2024-10-18 19:00:20
 */
@Tag(name = "云数据库管理")
@RestController
@RequestMapping("/api/oa/oa-assets-mysql")
public class OaAssetsMysqlController extends BaseController {
    @Resource
    private OaAssetsMysqlService oaAssetsMysqlService;

    @PreAuthorize("hasAuthority('oa:oaAssetsMysql:list')")
    @Operation(summary = "分页查询云数据库")
    @GetMapping("/page")
    public ApiResult<PageResult<OaAssetsMysql>> page(OaAssetsMysqlParam param) {
        // 使用关联查询
        return success(oaAssetsMysqlService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsMysql:list')")
    @Operation(summary = "查询全部云数据库")
    @GetMapping()
    public ApiResult<List<OaAssetsMysql>> list(OaAssetsMysqlParam param) {
        // 使用关联查询
        return success(oaAssetsMysqlService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsMysql:list')")
    @Operation(summary = "根据id查询云数据库")
    @GetMapping("/{id}")
    public ApiResult<OaAssetsMysql> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(oaAssetsMysqlService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsMysql:save')")
    @Operation(summary = "添加云数据库")
    @OperationLog
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaAssetsMysql oaAssetsMysql) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          oaAssetsMysql.setUserId(loginUser.getUserId());
        }
        if (oaAssetsMysqlService.save(oaAssetsMysql)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsMysql:update')")
    @Operation(summary = "修改云数据库")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaAssetsMysql oaAssetsMysql) {
        if (oaAssetsMysqlService.updateById(oaAssetsMysql)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsMysql:remove')")
    @Operation(summary = "删除云数据库")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (oaAssetsMysqlService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }
    @PreAuthorize("hasAuthority('oa:oaAssetsMysql:save')")
    @Operation(summary = "批量添加云数据库")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OaAssetsMysql> list) {
        if (oaAssetsMysqlService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsMysql:update')")
    @Operation(summary = "批量修改云数据库")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaAssetsMysql> batchParam) {
        if (batchParam.update(oaAssetsMysqlService, "mysql_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }
    @PreAuthorize("hasAuthority('oa:oaAssetsMysql:remove')")
    @Operation(summary = "批量删除云数据库")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (oaAssetsMysqlService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
