package com.YuyeTech.TPlat.game.userinfo.service;

import com.YuyeTech.TPlat.common.service.CommonServiceImpl;
import com.YuyeTech.TPlat.enums.UserEnum;
import com.YuyeTech.TPlat.exception.UserException;
import com.YuyeTech.TPlat.game.userinfo.projo.MainInfo;
import com.YuyeTech.TPlat.game.userinfo.repository.MainInfoRepository;
import com.YuyeTech.TPlat.game.userinfo.service.MainInfoService;
import com.YuyeTech.TPlat.game.userinfo.vo.MainInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

/**
 * @author:slambb
 * @date:2019/12/31
 */
@Service
@Slf4j
public class MainInfoServiceImpl extends CommonServiceImpl<MainInfoVo, MainInfo, String> implements MainInfoService {

    @Autowired
    private MainInfoRepository mainInfoRepository;


    @Override
    public MainInfo findMainInfoById(String userId) {
       MainInfo mainInfo= mainInfoRepository.findById(userId).orElse(null);
       if(mainInfo==null){
           throw  new UserException(UserEnum.USER_NOT_EXIST.getCode(),UserEnum.USER_NOT_EXIST.getMessage());
       }
       return mainInfo;
    }

    @Override
    @Transactional
    public String deleteMainInfoById(String userId) {
        try {
            mainInfoRepository.deleteById(userId);
        }catch (UserException e){
            return "error";
        }
        return "success";
    }

    @Override
    public MainInfo addMainInfo(MainInfo mainInfo) {
        return mainInfoRepository.save(mainInfo);
    }

    @Override
    public MainInfo findMainInfoByPhoneNumberAndPassword(String telephoneNumber, String password) {
        MainInfo mainInfo= mainInfoRepository.findByTelephoneNumberAndPassword(telephoneNumber,password).orElse(null);
        return mainInfo;
    }

    @Override
    public MainInfo findMainInfoByEmailAndPassword(String email, String password) {
        MainInfo mainInfo= mainInfoRepository.findByEmailAndPassword(email,password).orElse(null);
        return mainInfo;
    }

    @Override
    public MainInfo findMainInfoByEmail(String mail) {
        MainInfo mainInfo= mainInfoRepository.findByEmail(mail).orElse(null);
        return mainInfo;
    }

    @Override
    public MainInfo findMainInfoByPhoneNumber(String telephoneNumber) {
        MainInfo mainInfo = mainInfoRepository.findByTelephoneNumber(telephoneNumber).orElse(null);
        return mainInfo;
    }
}
