package com.th.demo.service.impl.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.th.demo.mapping.system.SysUserMapper;
import com.th.demo.model.system.SysUser;
import com.th.demo.service.system.SysLoginService;
import com.th.demo.tools.TokenTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("SysLoginService")
public class SysLoginServiceImpl implements SysLoginService {
    @Autowired
    public SysUserMapper sysUserMapper;
    public String login(String username, String password) {
        SysUser user = sysUserMapper.selectUserByLogin(username);
        String result = "";
        if (user != null) {
            if (user.getPassword().equals(password)) {
                JSONObject object = new JSONObject();
                object.put("id",user.getId());
                object.put("userDesc",user.getUserDesc());
                object.put("belongId",user.getBelongId());
                String token = TokenTools.sign(user, 600L* 1000L* 30L);
                object.put("token",token);
        		result = JSON.toJSONString(object);
            } else {
                result = "password";
            }
        } else {
            result = "username";
        }
        return result;

    }
}
