package com.storlead.user.api;

import com.storlead.framework.common.result.Result;
import com.storlead.user.pojo.entity.TenantUserEntity;
import com.storlead.user.service.ITenantUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 租户用户（企业-账号关系）HTTP 接口
 */
@RestController
@RequestMapping("/sys/tenant/user")
@Api(tags = "租户: 租户用户")
public class TenantUserApiController {

    @Resource
    private ITenantUserService tenantUserService;

    @GetMapping("/{id}")
    @ApiOperation("按主键查询")
    public Result<TenantUserEntity> getById(@PathVariable Long id) {
        return Result.result(tenantUserService.getById(id));
    }

    @GetMapping("/by-tenant-user")
    @ApiOperation("按租户ID + 用户ID查询")
    public Result<TenantUserEntity> getByTenantIdAndUserId(
            @ApiParam(value = "租户ID", required = true) @RequestParam Long tenantId,
            @ApiParam(value = "用户ID", required = true) @RequestParam Long userId) {
        return Result.result(tenantUserService.getByTenantIdAndUserId(tenantId, userId));
    }

    @GetMapping("/list-by-enterprise")
    @ApiOperation("按企业主键列出关系")
    public Result<List<TenantUserEntity>> listByEnterpriseId(
            @ApiParam(value = "企业主键 enterprise_id", required = true) @RequestParam Long enterpriseId) {
        return Result.result(tenantUserService.listByEnterpriseId(enterpriseId));
    }

    @PostMapping("/save")
    @ApiOperation("新增或更新（有主键则更新）")
    public Result<Boolean> save(@RequestBody TenantUserEntity entity) {
        return Result.result(tenantUserService.saveOrUpdate(entity));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("逻辑删除")
    public Result<Boolean> remove(@PathVariable Long id) {
        return Result.result(tenantUserService.lgDelete(id));
    }
}
