package com.yihu.hos.system.service;

import com.yihu.hos.common.constants.ContextAttributes;
import com.yihu.hos.core.datatype.StringUtil;
import com.yihu.hos.interceptor.LocalContext;
import com.yihu.hos.system.model.SystemUser;
import com.yihu.hos.tenant.model.TenantSession;
import com.yihu.hos.web.framework.model.ActionResult;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

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

    public static final String BEAN_ID = "systemManager";

    @Resource(name = UserManager.BEAN_ID)
    private UserManager userManager;

    @Value("${hos.administrators}")
    private String saasAdmin;

    /*
    登录操作
     */
  
    public  ActionResult loginAction(HttpSession session,String user,String password) throws Exception
    {
        ActionResult result = null;
        TenantSession tenantSession = (TenantSession)session.getAttribute(ContextAttributes.TENANT_SESSION);
        if (StringUtil.isEmpty(saasAdmin)){
            System.out.println("saasAdmin 为空！");
        }
        if (tenantSession == null){
            result = new ActionResult(false,"登录失败！用户登录超时！");
            return result;
        }

        if (saasAdmin.equals(tenantSession.getTenant())){
            tenantSession.setRole("admin");//标识为中心管理员角色
        }
        //特殊账户
        if(user.equals("admin") && password.equals("JKZL"))
        {
            SystemUser userInfo = new SystemUser();
            userInfo.setLoginCode("admin");
            userInfo.setUserName("管理员");
            session.setAttribute("userInfo",userInfo);
            tenantSession.setUserCode("admin");//设置租户code
            session.setAttribute(ContextAttributes.TENANT_SESSION, tenantSession);
            LocalContext.getContext().setAttachment(ContextAttributes.TENANT_NAME,tenantSession.getTenant());
            LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA, tenantSession.getSchema());

            result = new ActionResult(true,"登录成功!");
            result.setData(tenantSession);
            return result;
        }

        //根据用户名/密码到总平台校验
        SystemUser userInfo = userManager.getUserByLoginCode(user);
        if(userInfo==null && !user.equals("admin"))
        {
            result = new ActionResult(false,"登录失败！用户不存在！");
            return result;
        }
        else{
            String saltValue =userInfo.getSaltValue();
            String userPassword = userInfo.getPassword();
            if(userPassword.equals(DigestUtils.md5Hex(password + saltValue)))
            {
                tenantSession.setUserCode(user);
                session.setAttribute(ContextAttributes.TENANT_SESSION, tenantSession);
                session.setAttribute("userInfo",userInfo);
                result = new ActionResult(true,"登录成功!");
                result.setData(tenantSession);
                return result;
            }
            else{
                result = new ActionResult(false,"登录失败！密码错误！");
                return result;
            }
        }

    }
}
