import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  Patch,
  Query,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { TokenData } from 'src/common/decorator/token-data.decorator';
import { CreateOrgDto } from '../dto/create-org.dto';
import { UpdateOrgDto } from '../dto/update-org.dto';
import { TokenDataEntity } from '../../common/entities/token-data.entity';
import { OrgService } from '../service/org.service';
import { NoAuthToken } from 'src/common/decorator/no-token.decorator';

@ApiTags('系统管理 - 组织机构')
@ApiBearerAuth()
@Controller('system/org')
export class OrgController {
  constructor(private readonly orgService: OrgService) {}

  @Post()
  async create(
    @Body() createOrgDto: CreateOrgDto,
    @TokenData() token: TokenDataEntity,
  ) {
    createOrgDto.creator = createOrgDto.updater = token.userId;
    return this.orgService.create(createOrgDto);
  }

  @Get()
  @NoAuthToken()
  async findAll() {
    return this.orgService.findAll();
  }

  @Get('findOrgByUserId')
  async findOrgByUserId(@TokenData() token: TokenDataEntity, @Query() query) {
    return this.orgService.findOrgByUserId(query.userId || token.userId);
  }

  @Patch(':id')
  async update(@Param('id') id: string, @Body() updateOrgDto: UpdateOrgDto) {
    return this.orgService.update(+id, updateOrgDto);
  }

  @Delete(':id')
  async remove(@Param('id') id: string) {
    return this.orgService.remove(+id);
  }
}
