package com.bonus.hn.bearing.capacity.backstage.manager.service.impl;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

import com.bonus.hn.bearing.capacity.backstage.manager.dao.PermissionDao;
import com.bonus.hn.bearing.capacity.backstage.manager.dao.UtilDao;
import com.bonus.hn.bearing.capacity.backstage.manager.entity.LoginUser;
import com.bonus.hn.bearing.capacity.backstage.manager.model.Permission;
import com.bonus.hn.bearing.capacity.backstage.manager.model.SysUser;
import com.bonus.hn.bearing.capacity.backstage.manager.service.UserService;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.UserUtil;
import com.bonus.hn.bearing.capacity.backstage.utils.Aes;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * spring security登陆处理<br>
 * <p>
 * 密码校验请看文档（02 框架及配置），第三章第4节
 *
 * @author 小威老师 xiaoweijiagou@163.com
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

	@Autowired
	private UserService userService;
	@Autowired
	private PermissionDao permissionDao;
	@Autowired
	private UtilDao utilDao;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		username= Aes.aesDecrypt(username);
		SysUser sysUser = userService.getUser(username);
		if (sysUser == null) {
			throw new AuthenticationCredentialsNotFoundException("用户名不存在");
		} else if (sysUser.getStatus() == SysUser.Status.LOCKED) {
			throw new LockedException("用户被锁定,请联系管理员");
		} else if (sysUser.getStatus() == SysUser.Status.DISABLED) {
			throw new DisabledException("用户已作废");
		}

		LoginUser loginUser = new LoginUser();
		BeanUtils.copyProperties(sysUser, loginUser);

		List<Permission> permissions = permissionDao.listByUserId(sysUser.getId());
		loginUser.setPermissions(permissions);
		try {
			// 获取当前IP地址
			InetAddress ipAddress = InetAddress.getLocalHost();
			String ip = ipAddress.getHostAddress();

			// 获取系统名称
			String systemName = System.getProperty("os.name");

			// 获取所在位置（主机名）
			String hostName = ipAddress.getHostName();

			LocalDate currentYearMonth = LocalDate.now();
			DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
			String formattedYearMonth = currentYearMonth.format(formatter);

//			String id= UserUtil.getLoginUser().getId().toString();

			// 创建SysUser对象并设置值
			SysUser b = new SysUser();
			b.setIpAddress(ip);
			b.setSystemName(systemName);
			b.setHostName(hostName);
			b.setMonth(formattedYearMonth);
			b.setUserId(sysUser.getId().toString());

			utilDao.insertLoginInfo(b);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}

        return loginUser;
	}

}
