package com.yihu.hos.system.service;

import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.system.model.SystemUser;
import com.yihu.hos.system.service.intf.ISystemManager;
import com.yihu.hos.system.service.intf.IUserManager;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * Created by HZP on 2016/02/26.
 */
@Service("systemManager")
public class SystemManager implements ISystemManager {

    @Autowired
    private IUserManager userManager;

    /*
    登录操作
     */
    @Override
    public  Result loginAction(HttpSession session,String user,String password) throws Exception
    {
        //特殊账户
        if(user.equals("admin") && password.equals("JKZL"))
        {
            SystemUser userInfo = new SystemUser();
            userInfo.setLoginCode("admin");
            userInfo.setUserName("管理员");
            session.setAttribute("userInfo",userInfo);
            return Result.success("登录成功！");
        }

        //根据用户名/密码到总平台校验
        SystemUser userInfo = userManager.getUserByLoginCode(user);
        if(userInfo==null)
        {
            return Result.error("登录失败！用户不存在！");
        }
        else{
            String saltValue =userInfo.getSaltValue();
            String userPassword = userInfo.getPassword();
            if(userPassword.equals(DigestUtils.md5Hex(password + saltValue)))
            {
                session.setAttribute("userInfo",userInfo);
                return Result.success("登录成功！");
            }
            else{
                return Result.error("登录失败！密码错误！");
            }
        }

    }
}
