package com.brframework.webapppatch.service.impl;

import com.brframework.commondb.core.AbstractEntityService;
import com.brframework.commondb.core.CommonRepository;
import com.brframework.commonweb.exception.HandleException;
import com.brframework.webapppatch.dao.PatchGrayUserDao;
import com.brframework.webapppatch.entity.PatchGrayUser;
import com.brframework.webapppatch.service.PatchGrayUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 灰度用户
 *
 * @author xu
 * @date 2019/11/14 10:32
 */
@Service
public class PatchGrayUserServiceImpl extends AbstractEntityService<PatchGrayUser, Long, Object>
        implements PatchGrayUserService {

    @Autowired
    PatchGrayUserDao patchGrayUserDao;

    @Override
    public CommonRepository<Long, PatchGrayUser> getRepository() {
        return patchGrayUserDao;
    }

    @Override
    public void createGrayUser(String username) {
        PatchGrayUser byUsername = patchGrayUserDao.findByUsername(username);
        if(byUsername != null){
            throw new HandleException("当前灰度用户已存在");
        }
        PatchGrayUser grayUser = new PatchGrayUser();
        grayUser.setCreateDate(LocalDateTime.now());
        grayUser.setUsername(username);
        save(grayUser);
    }

    @Override
    public boolean isGrayUser(String username) {
        return patchGrayUserDao.findByUsername(username) != null;
    }
}
