package com.zhongdao.jlr.business.service.impl;
import com.zhongdao.jlr.business.dao.BaseDao;
import com.zhongdao.jlr.business.service.BaseService;
import com.zhongdao.jlr.business.vo.LoginUserVo;
import com.zhongdao.jlr.pojo.*;
import jodd.util.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "baseServiceImpl")
public class BaseServiceImpl implements BaseService {

	@Autowired
	private BaseDao dao;

	@Override
	public Map login(HttpServletRequest request, Map result, String username, String password) {
		BaseUser entity = null;
		List<BaseUserRole> baseUserRoles=null;
		List<Integer> list=new ArrayList<>();
		try {
			if (username.length() > 0 && null != password) {
				//获取用户信息
				entity = (BaseUser) dao.getObjectHql("from BaseUser where delete_flag != 1 and login_name='" + username + "'");
				if (entity == null) {
					throw new Exception("登陆帐号不存在！");
				}
				//判断密码是否正确
				boolean ps=BCrypt.checkpw(password,entity.getPassword() );
				if (ps != true) {
					throw new Exception("密码错误！");
				}
			} else {

				throw new Exception("非法用户！");

			}
			LoginUserVo loginInfo = new LoginUserVo();
			loginInfo.setRole_id(entity.getRole_id());
			loginInfo.setId(entity.getId());
			loginInfo.setLogin_name(entity.getLogin_name());
			loginInfo.setName(entity.getName());
			loginInfo.setPhone(entity.getPhone());
			loginInfo.setEmail(entity.getEmail());
			loginInfo.setEnterpriseJlrId(entity.getEnterprise_id());
			request.getSession().setAttribute("logininfo", loginInfo);
			result.put("flag", "1");
			result.put("msg", "登录成功");
		} catch (Exception ex) {
			result.put("msg", ex.getMessage());
		}
		return result;
	}

	@Override
	public List<BaseMenu> getMenuInfo() {
		return dao.getMenuInfo();
	}

	@Override
	public List<BaseMenu> getParentMenuInfo() {
		return dao.getParentMenuInfo();
	}


}
