package com.leromro.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leromro.core.domain.VolunteerInfo;
import com.leromro.core.mapper.VolunteerInfoMapper;
import com.leromro.core.service.IVolunteerInfoService;
import com.leromro.core.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 志愿者信息表 服务实现类
 * </p>
 *
 * @author lr
 * @since 2025-04-07
 */
@Service
public class VolunteerInfoServiceImpl extends ServiceImpl<VolunteerInfoMapper, VolunteerInfo> implements IVolunteerInfoService {

    @Autowired
    private VolunteerInfoMapper volunteerInfoMapper;

    /**
     * 新增志愿者信息
     * @param volunteerInfo
     * @param userId
     * @param serviceCategory
     */
    @Override
    public void newVolunteerInfoByuserId(VolunteerInfo volunteerInfo, Long userId, Long serviceCategory) {
        volunteerInfo.setUserId(userId);
        volunteerInfo.setServiceCategory(serviceCategory);
        volunteerInfo.setCreateTime(DateTimeUtil.getNowTime());
        QueryWrapper<VolunteerInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId).eq("service_category",serviceCategory);

        Integer i = volunteerInfoMapper.selectCount(wrapper);
        if(i>0){
            throw new RuntimeException("此用户已有志愿者信息，不可重复创建，请前往修改");
        }else {
            volunteerInfoMapper.newVolunteerByUserId(volunteerInfo);
        }
    }

    /**
     * 志愿者注册信息回显
     * @param serviceCategory
     * @param userId
     * @return
     */
    @Override
    public VolunteerInfo selectByUserID(Long serviceCategory, Long userId) {
        QueryWrapper<VolunteerInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId).eq("service_category",serviceCategory);
        VolunteerInfo infos = volunteerInfoMapper.selectOne(wrapper);
        return infos;
    }

    /**
     * @param volunteerInfo
     * @return
     */
    @Override
    public List<VolunteerInfo> selectVolunteerInfoList(VolunteerInfo volunteerInfo) {

        List<VolunteerInfo> infos = volunteerInfoMapper.selectVolunteerInfoSimple(volunteerInfo);
        return infos;
    }

    /**
     * @param volunteerId
     * @return
     */
    @Override
    public VolunteerInfo selectLVolunteerInfoByVolunteerId(Long volunteerId) {
        VolunteerInfo info = volunteerInfoMapper.selectLVolunteerInfoByVolunteerId(volunteerId);
        return info;
    }
}
