 package com.huatek.busi.service.impl.login;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.mapper.LoginMapper;
import com.huatek.busi.service.login.LoginService;
import com.huatek.frame.base.dto.UserDTO;
import com.huatek.frame.base.util.Constants;
import com.huatek.frame.base.vo.UserVO;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.util.jwt.TokenMgr;
import com.huatek.torch.util.md5.Md5;
import com.huatek.torch.util.uuid.Uuid;
@Component
@Service(interfaceClass = LoginService.class)
@Transactional(rollbackFor = Exception.class)
public class LoginServiceImpl implements LoginService {
    
    @Autowired
    private LoginMapper loginMapper;
    @Override
    public Map<String, Object> login(UserDTO user,long time) {
        UserVO userLogin = loginMapper.login(user);
        if (userLogin == null) {
            return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "LOGIN.NAMEORPWDW.EEOR", null);
        }
        String password = Md5.getMD5Code(userLogin.getPasswordSalt() + user.getPassword());
        String longinPassword = userLogin.getPassword();
        if (longinPassword.equals(password)) {
            //登录成功
        	Map<String, Object> param = new HashMap<String, Object>(2);
    		param.put("id", userLogin.getId());
    		param.put("deleted", Constants.DEFAULT_NO);
        	String  groupName = loginMapper.findByUserGroup(userLogin.getId());
        	userLogin.setGroupName(groupName);
        	String  roleName = loginMapper.findByUserRoles(userLogin.getId());
        	userLogin.setRoles(roleName);
			// 登陆验证通过后--用户信息
			LocalUser sub = new LocalUser(userLogin.getId(), userLogin.getUserName(), userLogin.getUserMobile(),
					userLogin.getUserEmail(), userLogin.getUserPhone(), userLogin.getLeader(), userLogin.getGroupId(),
					userLogin.getLanguageType(), time);
        	String token = TokenMgr.createJWT(Uuid.getUUID(), ConstantUtil.JWT_ISS, TokenMgr.generalSubject(sub), time);
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", userLogin, token);
        }
        else{
            return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "LOGIN.NAMEORPWDW.EEOR", null);
        }
        
    }

}
