package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaLinkService;
import com.gxwebsoft.oa.entity.OaLink;
import com.gxwebsoft.oa.param.OaLinkParam;
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:42
 */
@Tag(name = "常用链接管理")
@RestController
@RequestMapping("/api/oa/oa-link")
public class OaLinkController extends BaseController {
    @Resource
    private OaLinkService oaLinkService;

    @Operation(summary = "分页查询常用链接")
    @GetMapping("/page")
    public ApiResult<PageResult<OaLink>> page(OaLinkParam param) {
        // 使用关联查询
        return success(oaLinkService.pageRel(param));
    }

    @Operation(summary = "查询全部常用链接")
    @GetMapping()
    public ApiResult<List<OaLink>> list(OaLinkParam param) {
        PageParam<OaLink, OaLinkParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(oaLinkService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(oaLinkService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaLink:list')")
    @OperationLog
    @Operation(summary = "根据id查询常用链接")
    @GetMapping("/{id}")
    public ApiResult<OaLink> get(@PathVariable("id") Integer id) {
        return success(oaLinkService.getById(id));
        // 使用关联查询
        //return success(oaLinkService.getByIdRel(id));
    }

    @Operation(summary = "添加常用链接")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaLink oaLink) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          oaLink.setUserId(loginUser.getUserId());
        }
        if (oaLinkService.save(oaLink)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "修改常用链接")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaLink oaLink) {
        if (oaLinkService.updateById(oaLink)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "删除常用链接")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (oaLinkService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @Operation(summary = "批量添加常用链接")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OaLink> list) {
        if (oaLinkService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "批量修改常用链接")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaLink> batchParam) {
        if (batchParam.update(oaLinkService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "批量删除常用链接")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (oaLinkService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
