package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.oa.service.OaCompanyUserService;
import com.gxwebsoft.oa.entity.OaCompanyUser;
import com.gxwebsoft.oa.param.OaCompanyUserParam;
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-20 12:33:12
 */
@Tag(name = "成员管理管理")
@RestController
@RequestMapping("/api/oa/oa-company-user")
public class OaCompanyUserController extends BaseController {
    @Resource
    private OaCompanyUserService oaCompanyUserService;

    @Operation(summary = "分页查询成员管理")
    @GetMapping("/page")
    public ApiResult<PageResult<OaCompanyUser>> page(OaCompanyUserParam param) {
        // 使用关联查询
        return success(oaCompanyUserService.pageRel(param));
    }

    @Operation(summary = "查询全部成员管理")
    @GetMapping()
    public ApiResult<List<OaCompanyUser>> list(OaCompanyUserParam param) {
        PageParam<OaCompanyUser, OaCompanyUserParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(oaCompanyUserService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(oaCompanyUserService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaCompanyUser:list')")
    @OperationLog
    @Operation(summary = "根据id查询成员管理")
    @GetMapping("/{id}")
    public ApiResult<OaCompanyUser> get(@PathVariable("id") Integer id) {
        return success(oaCompanyUserService.getById(id));
        // 使用关联查询
        //return success(oaCompanyUserService.getByIdRel(id));
    }

    @Operation(summary = "添加成员管理")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaCompanyUser oaCompanyUser) {
        if (oaCompanyUserService.save(oaCompanyUser)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "修改成员管理")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaCompanyUser oaCompanyUser) {
        if (oaCompanyUserService.updateById(oaCompanyUser)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "删除成员管理")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (oaCompanyUserService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @Operation(summary = "批量添加成员管理")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OaCompanyUser> list) {
        if (oaCompanyUserService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "批量修改成员管理")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaCompanyUser> batchParam) {
        if (batchParam.update(oaCompanyUserService, "company_user_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
