package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaAppUrlService;
import com.gxwebsoft.oa.entity.OaAppUrl;
import com.gxwebsoft.oa.param.OaAppUrlParam;
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-09-10 20:57:41
 */
@Tag(name = "项目域名管理")
@RestController
@RequestMapping("/api/oa/oa-app-url")
public class OaAppUrlController extends BaseController {
    @Resource
    private OaAppUrlService oaAppUrlService;

    @Operation(summary = "分页查询项目域名")
    @GetMapping("/page")
    public ApiResult<PageResult<OaAppUrl>> page(OaAppUrlParam param) {
        // 使用关联查询
        return success(oaAppUrlService.pageRel(param));
    }

    @Operation(summary = "查询全部项目域名")
    @GetMapping()
    public ApiResult<List<OaAppUrl>> list(OaAppUrlParam param) {
        PageParam<OaAppUrl, OaAppUrlParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(oaAppUrlService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(oaAppUrlService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAppUrl:list')")
    @OperationLog
    @Operation(summary = "根据id查询项目域名")
    @GetMapping("/{id}")
    public ApiResult<OaAppUrl> get(@PathVariable("id") Integer id) {
        return success(oaAppUrlService.getById(id));
        // 使用关联查询
        //return success(oaAppUrlService.getByIdRel(id));
    }

    @Operation(summary = "添加项目域名")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaAppUrl oaAppUrl) {
        if (oaAppUrlService.save(oaAppUrl)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "修改项目域名")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaAppUrl oaAppUrl) {
        if (oaAppUrlService.updateById(oaAppUrl)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
