package com.uas.account.dao;

import com.uas.account.entity.UserLock;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

/**
 * Created by wangmh on 2017/11/17.
 */
@Repository
public class UserLockDaoImpl extends BaseDao implements UserLockDao {

    @Override
    public void save(UserLock userLock) {
        int count = count("from ac$lock where username=?", userLock.getUsername());
        if (count == 0) {
            jdbcTemplate.update("insert into ac$lock (username, errorCount) VALUES (?,?)", userLock.getUsername(), userLock.getErrorCount());
        } else {
            jdbcTemplate.update("update ac$lock set errorCount=? where username=?", userLock.getErrorCount(), userLock.getUsername());

        }
    }

    @Override
    public UserLock getOneByUsername(String username) {
        int count = count("from ac$lock where username=?", username);
        if (count == 0) {
            return null;
        }
        return jdbcTemplate.queryForObject("select * from ac$lock where username=?", new BeanPropertyRowMapper<>(UserLock.class), username);
    }

    @Override
    public void deleteAll() {
        jdbcTemplate.update("delete from ac$lock");
    }
}
