package cn.iselab.mooctest.user.service.impl;

import cn.iselab.mooctest.user.mapper.MedalMapper;
import cn.iselab.mooctest.user.mapper.User2MedalDao;
import cn.iselab.mooctest.user.model.Medal;
import cn.iselab.mooctest.user.model.User2Medal;
import cn.iselab.mooctest.user.service.MedalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author ROKG
 * @Description
 * @Date: Created in 下午6:52 2018/2/13
 * @Modified By:
 */
@Service
public class MedalServiceImpl implements MedalService {

    @Autowired
    MedalMapper medalMapper;

    @Autowired
    User2MedalDao user2MedalDao;

    @Override
    public Medal saveMedal(Medal medal){
        medalMapper.save(medal);
        return medal;
    }

    @Override
    public Medal updateMedal(Medal medal){
        medalMapper.update(medal);
        return medal;
    }

    @Override
    public Medal findById(long id){
        return medalMapper.findById(id);
    }

    @Override
    public User2Medal saveUser2Medal(User2Medal medal){
        return user2MedalDao.save(medal);
    }

    @Override
    public User2Medal findByUserIdAndMedalId(long userId, long medalId){
        List<User2Medal> user2Medals = user2MedalDao.findByUserIdAndMedalId(userId,medalId);
        if(user2Medals!=null && user2Medals.size()>0){
            return user2Medals.get(0);
        }else {
            return null;
        }
    }

    @Override
    public List<User2Medal> findByPage(long userId){
        return user2MedalDao.findByUserId(userId);
    }

    @Override
    public List<Medal> findByKeyword(String word){
        if (word==null) {
            word = "";
        }
        return medalMapper.findAll(word);
    }

}
