package lingtao.net.service;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lingtao.net.bean.LoginIp;
import lingtao.net.bean.Msg;
import lingtao.net.bean.SysUser;
import lingtao.net.dao.LoginIpMapper;

@Service
public class LoginIpService {

	@Autowired
	private LoginIpMapper loginIpMapper;

	// 列表
	public List<LoginIp> getLoginIpList(LoginIp loginIp) {
		return loginIpMapper.getLoginIpList(loginIp);
	}

	// 新增
	public void addIp(LoginIp loginIp) {
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		loginIp.setCreateBy(user.getRealname());
		loginIpMapper.addIp(loginIp);
	}
	
	// 修改
	public void updateIp(LoginIp loginIp) {
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		loginIp.setUpdateBy(user.getRealname());
		loginIpMapper.updateIp(loginIp);
	}

	// 删除
	public int deleteIpById(Integer id) {
		return loginIpMapper.deleteIpById(id);
	}

	// 批量删除
	public Msg deleteBatch(int[] ints) {
		try {
			loginIpMapper.deleteBatch(ints);
			return Msg.success();
		} catch (Exception e) {
			return Msg.fail();
		}
	}

	public Msg checkIP(String agreeIp) {
		LoginIp loginIp = loginIpMapper.checkIP(agreeIp);
		if (loginIp != null) {
			return Msg.fail();
		}
		return Msg.success();
	}

}
