forked from baskbuddy/zhangshun
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
4.6 KiB
102 lines
4.6 KiB
package com.mydreamjob.bidirectionalmatch;
|
|
|
|
import com.mydreamjob.auth.User;
|
|
import com.mydreamjob.auth.UserRepository;
|
|
import com.mydreamjob.common.exception.BusinessException;
|
|
import com.mydreamjob.common.exception.ErrorCode;
|
|
import com.mydreamjob.company.Company;
|
|
import com.mydreamjob.company.CompanyRepository;
|
|
import com.mydreamjob.company.VerifiedCompanyGuard;
|
|
import com.mydreamjob.security.UserRoleGuard;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* 匹配阈值配置:企业 requiredCandidateScore / 学生 requiredHrScore。
|
|
* 规则配置不存在时读取返回默认 60;首次更新时创建。
|
|
*/
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class MatchRuleService {
|
|
|
|
private final UserRoleGuard userRoleGuard;
|
|
private final CompanyMatchRuleRepository companyRuleRepository;
|
|
private final StudentMatchRuleRepository studentRuleRepository;
|
|
private final CompanyRepository companyRepository;
|
|
private final UserRepository userRepository;
|
|
|
|
// ── 企业 ──────────────────────────────────────────────────
|
|
|
|
@Transactional(readOnly = true)
|
|
public Map<String, Object> getCompanyRule(Long userId) {
|
|
Company company = requireCompany(userId);
|
|
VerifiedCompanyGuard.requireVerified(company);
|
|
CompanyMatchRule rule = companyRuleRepository.findByCompanyId(company.getId()).orElse(null);
|
|
int required = rule != null ? rule.getRequiredCandidateScore()
|
|
: CompanyMatchRule.DEFAULT_REQUIRED_CANDIDATE_SCORE;
|
|
return companyView(company.getId(), required);
|
|
}
|
|
|
|
@Transactional
|
|
public Map<String, Object> updateCompanyRule(Long userId, int requiredCandidateScore) {
|
|
Company company = requireCompany(userId);
|
|
VerifiedCompanyGuard.requireVerified(company);
|
|
CompanyMatchRule rule = companyRuleRepository.findByCompanyId(company.getId())
|
|
.orElseGet(() -> CompanyMatchRule.builder().companyId(company.getId()).build());
|
|
rule.setRequiredCandidateScore(requiredCandidateScore);
|
|
rule = companyRuleRepository.save(rule);
|
|
return companyView(company.getId(), rule.getRequiredCandidateScore());
|
|
}
|
|
|
|
// ── 学生 ──────────────────────────────────────────────────
|
|
|
|
@Transactional(readOnly = true)
|
|
public Map<String, Object> getStudentRule(Long userId) {
|
|
userRoleGuard.requireStudent(userId);
|
|
StudentMatchRule rule = studentRuleRepository.findByStudentUserId(userId).orElse(null);
|
|
int required = rule != null ? rule.getRequiredHrScore()
|
|
: StudentMatchRule.DEFAULT_REQUIRED_HR_SCORE;
|
|
return studentView(userId, required);
|
|
}
|
|
|
|
@Transactional
|
|
public Map<String, Object> updateStudentRule(Long userId, int requiredHrScore) {
|
|
userRoleGuard.requireStudent(userId);
|
|
StudentMatchRule rule = studentRuleRepository.findByStudentUserId(userId)
|
|
.orElseGet(() -> StudentMatchRule.builder().studentUserId(userId).build());
|
|
rule.setRequiredHrScore(requiredHrScore);
|
|
rule = studentRuleRepository.save(rule);
|
|
return studentView(userId, rule.getRequiredHrScore());
|
|
}
|
|
|
|
// ── 视图 / 私有 ───────────────────────────────────────────
|
|
|
|
private Map<String, Object> companyView(Long companyId, int required) {
|
|
Map<String, Object> v = new HashMap<>();
|
|
v.put("companyId", companyId);
|
|
v.put("requiredCandidateScore", required);
|
|
return v;
|
|
}
|
|
|
|
private Map<String, Object> studentView(Long studentUserId, int required) {
|
|
Map<String, Object> v = new HashMap<>();
|
|
v.put("studentUserId", studentUserId);
|
|
v.put("requiredHrScore", required);
|
|
return v;
|
|
}
|
|
|
|
private Company requireCompany(Long userId) {
|
|
User user = userRepository.findById(userId)
|
|
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
|
|
boolean isCompany = user.getRoles() != null
|
|
&& user.getRoles().stream().anyMatch(r -> r == User.Role.COMPANY);
|
|
if (!isCompany) throw new BusinessException(ErrorCode.FORBIDDEN);
|
|
return companyRepository.findByOwnerUserId(userId)
|
|
.orElseThrow(() -> new BusinessException(ErrorCode.COMPANY_NOT_FOUND));
|
|
}
|
|
|
|
}
|
|
|