/**
 * 版权所有 ©2021 莱欧有限公司
 * 工程名：yearBooks
 * 文件名：LoginSignService.java
 * 创建日期：2021年11月11日 下午2:53:22
 * 作者：mt.W
 */
package com.lion.service;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lion.config.LoginUserInfoCache;
import com.lion.dao.LogDao;
import com.lion.dao.LoginDao;
import com.lion.dao.OrganizationTreeDao;
import com.lion.dao.UserDao;
import com.lion.entity.LoginUserInfo;
import com.lion.entity.User;
import com.lion.util.HttpUtil;
import com.lion.util.MD5Util;
import com.lion.util.TimeUtil;
import com.lion.util.TokenUtil;
import com.lion.util.Util;

import jodd.util.StringUtil;

/**
 * 类描述
 * @author mt.W
 * @since 2021年11月11日
 */
@Service
public class LoginSignService {

	@Autowired
	private LogDao logDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private LoginDao dao;
	@Autowired
	private OrganizationTreeDao oDao;
	public String login(HttpSession session,String code,String exiturl,String tokenUrl,String appkey,String appsecret){
		String rs = HttpUtil.sendHttpUrl(tokenUrl+"?appkey="+appkey+"&appsecret="+appsecret+"&code="+code, "");
		logDao.saveLog("发起单点登录请求:"+tokenUrl+"?appkey="+appkey+"&appsecret="+appsecret+"&code="+code+",返回结果:"+rs, "单点登录", "0");
		if(StringUtil.isNotEmpty(rs)){
			JSONObject json = new JSONObject(rs);
			JSONObject jsonValue = json.getJSONObject("values");
			String phone = jsonValue.getString("tel");
			int checkCount = userDao.getUserByPhone(phone);
			if(checkCount>0){//存在
				
			}else{//不存在,进行创建人员
				String otherid = jsonValue.getString("xzqhCode");
				String orgid = "";
				List<Map<String,Object>> orgdeptInfo = oDao.getOrgdeptInfoByOtherid(otherid);
				if(orgdeptInfo.size()>0){//区县存在
					orgid = orgdeptInfo.get(0).get("id").toString();
				}else{//区县不存在
					return Util.responseString("false", "");
				}
				User u = new User();
				u.setName(jsonValue.getString("nickname"));
				u.setUserAccount(jsonValue.getString("ssousername"));
				u.setSex("男");
				if(orgid.length()==2){
					u.setRoleid("1");
				}else{
					u.setRoleid("2");
				}
				u.setOrgid(orgid);
				u.setPhone(phone);
				u.setPwd("123456");
				userDao.addUser(u, "0");
			}
			List<Map<String,Object>> userList = userDao.getUserInfoByPhone(phone);
			String userId = userList.get(0).get("id").toString();
			String cookie=MD5Util.StringInMd5(userId+TimeUtil.getNowTimeTwo());
			int l=logDao.saveLoginInfo(userId, cookie);
			if(l>0){
				dao.deleteLoginStatus(userId);
				LoginUserInfo userInfo=new LoginUserInfo();
				userInfo.setUserId(userId);
				userInfo.setUserAccount(userList.get(0).get("name").toString());
				userInfo.setOrgid(userList.get(0).get("orgid").toString());
				userInfo.setRoleid(userList.get(0).get("roleid").toString());
				TokenUtil.setLoginUserInfo(cookie, userInfo);
				logDao.saveLog("用户登录:"+userList.get(0).get("name").toString(),"登录", userId);
				session.setAttribute("longinJOSONID", cookie);
				return Util.responseString("true", "操作成功");
			}
			return Util.responseString("false", "登录失败");
		}
		return Util.responseString("false", "登陆失败");
	}
	
	public String loginIn(HttpSession session){
		if(session.getAttribute("longinJOSONID")==null){
			return Util.responseString("false", "登录失败");
		}
		String cookie = session.getAttribute("longinJOSONID").toString();
		JSONObject json=new JSONObject();
		json.put("success","true");
		json.put("message",cookie);
		json.put("orgid", LoginUserInfoCache.getLoginUserInfoByToken(cookie).getOrgid());
		return json.toString();
	}
}
