package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaAppUserService;
import com.gxwebsoft.oa.entity.OaAppUser;
import com.gxwebsoft.oa.param.OaAppUserParam;
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-user")
public class OaAppUserController extends BaseController {
    @Resource
    private OaAppUserService oaAppUserService;

    @Operation(summary = "分页查询应用成员")
    @GetMapping("/page")
    public ApiResult<PageResult<OaAppUser>> page(OaAppUserParam param) {
        // 使用关联查询
        return success(oaAppUserService.pageRel(param));
    }

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

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

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

    @Operation(summary = "修改应用成员")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaAppUser oaAppUser) {
        if (oaAppUserService.updateById(oaAppUser)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

    @Operation(summary = "批量修改应用成员")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaAppUser> batchParam) {
        if (batchParam.update(oaAppUserService, "app_user_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
