package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaAssetsDomainService;
import com.gxwebsoft.oa.entity.OaAssetsDomain;
import com.gxwebsoft.oa.param.OaAssetsDomainParam;
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 18:27:02
 */
@Tag(name = "域名管理")
@RestController
@RequestMapping("/api/oa/oa-assets-domain")
public class OaAssetsDomainController extends BaseController {
    @Resource
    private OaAssetsDomainService oaAssetsDomainService;

    @PreAuthorize("hasAuthority('oa:oaAssetsDomain:list')")
    @Operation(summary = "分页查询域名")
    @GetMapping("/page")
    public ApiResult<PageResult<OaAssetsDomain>> page(OaAssetsDomainParam param) {
        // 使用关联查询
        return success(oaAssetsDomainService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsDomain:list')")
    @Operation(summary = "查询全部域名")
    @GetMapping()
    public ApiResult<List<OaAssetsDomain>> list(OaAssetsDomainParam param) {
        // 使用关联查询
        return success(oaAssetsDomainService.listRel(param));
    }

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

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

    @PreAuthorize("hasAuthority('oa:oaAssetsDomain:update')")
    @Operation(summary = "修改域名")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaAssetsDomain oaAssetsDomain) {
        if (oaAssetsDomainService.updateById(oaAssetsDomain)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsDomain:remove')")
    @Operation(summary = "删除域名")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (oaAssetsDomainService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsDomain:save')")
    @Operation(summary = "批量添加域名")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OaAssetsDomain> list) {
        if (oaAssetsDomainService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsDomain:update')")
    @Operation(summary = "批量修改域名")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaAssetsDomain> batchParam) {
        if (batchParam.update(oaAssetsDomainService, "domain_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsDomain:remove')")
    @Operation(summary = "批量删除域名")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (oaAssetsDomainService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
