package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.oa.service.AppUrlService;
import com.gxwebsoft.oa.entity.AppUrl;
import com.gxwebsoft.oa.param.AppUrlParam;
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 com.gxwebsoft.common.system.entity.User;
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-04-15 13:01:34
 */
@Api(tags = "项目域名管理")
@RestController
@RequestMapping("/api/oa/app-url")
public class AppUrlController extends BaseController {
    @Resource
    private AppUrlService appUrlService;

    @ApiOperation("分页查询项目域名")
    @GetMapping("/page")
    public ApiResult<PageResult<AppUrl>> page(AppUrlParam param) {
        // 使用关联查询
        return success(appUrlService.pageRel(param));
    }

    @ApiOperation("查询全部项目域名")
    @GetMapping()
    public ApiResult<List<AppUrl>> list(AppUrlParam param) {
        // 使用关联查询
        return success(appUrlService.listRel(param));
    }

    @ApiOperation("根据id查询项目域名")
    @GetMapping("/{id}")
    public ApiResult<AppUrl> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(appUrlService.getByIdRel(id));
    }

    @ApiOperation("添加项目域名")
    @PostMapping()
    public ApiResult<?> save(@RequestBody AppUrl appUrl) {
        if (appUrlService.save(appUrl)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改项目域名")
    @PutMapping()
    public ApiResult<?> update(@RequestBody AppUrl appUrl) {
        if (appUrlService.updateById(appUrl)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除项目域名")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (appUrlService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加项目域名")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<AppUrl> list) {
        if (appUrlService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改项目域名")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<AppUrl> batchParam) {
        if (batchParam.update(appUrlService, "app_url_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除项目域名")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (appUrlService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
