package com.CUST.brain.service.impl;

import com.CUST.brain.common.annotation.RedisCache;
import com.CUST.brain.common.constant.RedisKeyConstants;
import com.CUST.brain.dao.domain.business.TalentBasic;
import com.CUST.brain.dao.mapper.TalentBasicMapper;
import com.CUST.brain.service.TalentBasicService;
import com.CUST.brain.common.utils.RedisUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 人才基本信息服务实现类
 */
@Service
public class TalentBasicServiceImpl extends ServiceImpl<TalentBasicMapper, TalentBasic> implements TalentBasicService {

    @Resource
    private RedisUtils redisUtils;

    @Override
    @RedisCache(key = RedisKeyConstants.TALENT_BASIC + "${#name}", expire = 3600)
    public List<TalentBasic> getByName(String name) {
        LambdaQueryWrapper<TalentBasic> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(TalentBasic::getName, name)
                .eq(TalentBasic::getFlag, 0)
                .orderByDesc(TalentBasic::getCreateTime);
        return list(wrapper);
    }

    @Override
    @RedisCache(key = RedisKeyConstants.TALENT_UNIT + "${#unitId}", expire = 3600)
    public List<TalentBasic> getByUnitId(String unitId) {
        LambdaQueryWrapper<TalentBasic> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TalentBasic::getUnitId, unitId)
                .eq(TalentBasic::getFlag, 0)
                .orderByDesc(TalentBasic::getCreateTime);
        return list(wrapper);
    }

    @Override
    @RedisCache(key = RedisKeyConstants.TALENT_INFO + "title:${#title}", expire = 3600)
    public List<TalentBasic> getByTitle(String title) {
        LambdaQueryWrapper<TalentBasic> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TalentBasic::getTitle, title)
                .eq(TalentBasic::getFlag, 0)
                .orderByDesc(TalentBasic::getCreateTime);
        return list(wrapper);
    }

    @Override
    @RedisCache(key = RedisKeyConstants.TALENT_INFO + "grade:${#classifyGrade}", expire = 3600)
    public List<TalentBasic> getByClassifyGrade(String classifyGrade) {
        LambdaQueryWrapper<TalentBasic> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TalentBasic::getClassifyGrade, classifyGrade)
                .eq(TalentBasic::getFlag, 0)
                .orderByDesc(TalentBasic::getCreateTime);
        return list(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTalent(TalentBasic talent) {
        boolean result = updateById(talent);
        if (result) {
            // 删除相关缓存
            redisUtils.delete(RedisKeyConstants.TALENT_BASIC + talent.getName());
            redisUtils.delete(RedisKeyConstants.TALENT_UNIT + talent.getUnitId());
            redisUtils.delete(RedisKeyConstants.TALENT_INFO + "title:" + talent.getTitle());
            redisUtils.delete(RedisKeyConstants.TALENT_INFO + "grade:" + talent.getClassifyGrade());
        }
        return result;
    }
} 