package com.rf.psychological.user.service.impl;

import com.rf.psychological.user.dao.model.UserRepeatNameNumEntity;
import com.rf.psychological.user.dao.repository.UserRepeatNameNumRepository;
import com.rf.psychological.user.service.UserRepeatNameNumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lpf
 * @description:
 * @date 17/12/2021下午7:22
 */
@Service
public class UserRepeatNameNumServiceImpl implements UserRepeatNameNumService {

    @Autowired
    private UserRepeatNameNumRepository userRepeatNameNumRepository;

    @Override
    public List<UserRepeatNameNumEntity> findAllByInstitutionNo(String institutionNo) {
        return userRepeatNameNumRepository.findAllByInstitutionNo(institutionNo);
    }

    @Override
    public UserRepeatNameNumEntity findUserRepeatNameNumEntityByNamePinyinAndInstitutionNo(String namePinyin, String institutionNo) {
        return userRepeatNameNumRepository.findUserRepeatNameNumEntityByNamePinyinAndInstitutionNo(namePinyin, institutionNo);
    }

    @Override
    public int updateNum(String namePinyin, String institutionNo, int num) {
        return userRepeatNameNumRepository.updateNum(namePinyin, institutionNo, num);
    }

    @Override
    public UserRepeatNameNumEntity save(UserRepeatNameNumEntity userRepeatNameNumEntity) {
        return userRepeatNameNumRepository.save(userRepeatNameNumEntity);
    }

    @Override
    public int testOrNotNamePinyin(String institutionNo, String namePinyin) {
        UserRepeatNameNumEntity userRepeatNameNumEntityByNamePinyinAndInstitutionNo = userRepeatNameNumRepository.findUserRepeatNameNumEntityByNamePinyinAndInstitutionNo(namePinyin, institutionNo);
        if (userRepeatNameNumEntityByNamePinyinAndInstitutionNo == null) {
            userRepeatNameNumRepository.save(new UserRepeatNameNumEntity(namePinyin, institutionNo, 0));
            return 0;
        } else {
            userRepeatNameNumRepository.updateNum(namePinyin, institutionNo, userRepeatNameNumEntityByNamePinyinAndInstitutionNo.getNum() + 1);
            return userRepeatNameNumEntityByNamePinyinAndInstitutionNo.getNum();
        }
    }
}
