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

import com.rf.psychological.institution.model.InstitutionUserEntity;
import com.rf.psychological.institution.repository.InstitutionUserRepository;
import com.rf.psychological.institution.service.InstitutionUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InstitutionUserServiceImpl implements InstitutionUserService {

    @Autowired
    private InstitutionUserRepository institutionUserRepository;

    @Override
    public InstitutionUserEntity findInstitutionByNo(String institutionNo) {
        return this.institutionUserRepository.findInstitutionByNo(institutionNo);
    }

    @Override
    public InstitutionUserEntity getOne(String institutionId) {
        return this.institutionUserRepository.findById(institutionId).get();
    }

    @Override
    public InstitutionUserEntity findByPhoneAndPassword(String phone, String password) {
        return this.institutionUserRepository.findByPhoneAndPassword(phone, password);
    }

    @Override
    public InstitutionUserEntity findByPhone(String phone) {
        return this.institutionUserRepository.findByPhone(phone);
    }

    @Override
    public InstitutionUserEntity findByInstitutionNoAndPhone(String institutionNo, String phone) {
        return this.institutionUserRepository.findByInstitutionNoAndPhone(institutionNo, phone);
    }

    @Override
    public void save(InstitutionUserEntity institutionUserEntity) {
        this.institutionUserRepository.save(institutionUserEntity);
    }


    @Override
    public void updatePassword(String phone, String password, String institutionNo) {
        this.institutionUserRepository.updatePassword(phone, password, institutionNo);
    }

    @Override
    public List<InstitutionUserEntity> findAllByName(int pageNum, int pageSize, String searchKey) {
        int beginIndex = (pageNum - 1) * pageSize;
        if (searchKey != null && !"".equals(searchKey)) {
            return this.institutionUserRepository.findAllByName(beginIndex, pageSize, searchKey);
        } else {
            return this.institutionUserRepository.findAll(beginIndex, pageSize);
        }
    }

    @Override
    public int allNum(String searchKey) {
        return this.institutionUserRepository.allNum(searchKey);
    }

    @Override
    public List<InstitutionUserEntity> findAllInAdminByInNo(int pageNum, int pageSize, String institutionNo, String searchKey) {
        return this.institutionUserRepository.findAllByInNo((pageNum - 1) * pageSize, pageSize, institutionNo, searchKey);
    }

    @Override
    public int numAllInAdminByInNo(String institutionNo, String searchKey) {
        return this.institutionUserRepository.numAllByInNo(institutionNo, searchKey);
    }

    @Override
    public void deleteByPhoneAndInstitutionNo(String phone, String institutionNo) {
        this.institutionUserRepository.deleteByPhoneAndInstitutionNo(phone, institutionNo);
    }

    @Override
    public void updateUserStatusByPhone(String phone, String userStatus, String institutionNo) {
        this.institutionUserRepository.updateUserStatusByPhone(phone, userStatus, institutionNo);
    }

}
