package com.xly.erp.module.usr.controller;

import com.xly.erp.common.response.Result;
import com.xly.erp.module.usr.mapper.StaffMapper;
import com.xly.erp.module.usr.vo.StaffSearchVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/usr")
public class StaffController {

    private static final int DEFAULT_LIMIT = 20;
    private static final int MAX_LIMIT = 50;

    private final StaffMapper staffMapper;

    public StaffController(StaffMapper staffMapper) {
        this.staffMapper = staffMapper;
    }

    @GetMapping("/staffs")
    public Result<List<StaffSearchVO>> search(@RequestParam(required = false) String keyword,
                                              @RequestParam(required = false) Integer limit) {
        int n = (limit == null || limit < 1) ? DEFAULT_LIMIT : Math.min(limit, MAX_LIMIT);
        String kw = keyword == null ? "" : keyword.trim();
        return Result.ok(staffMapper.searchActive(kw, n));
    }
}
