/*
 * 该类主要负责系统主要业务逻辑的实现,如多表处理的事务操作、权限控制等
 * 该类根据具体的业务逻辑来调用该实体对应的Dao或者多个Dao来实现数据库操作
 * 实际的数据库操作在对应的Dao或其他Dao中实现
 */

package com.blueland.bpm.identity.service;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.blueland.bpm.identity.UserService;
import com.blueland.bpm.identity.dao.UserQueryDao;
import com.blueland.bpm.identity.model.Org;
import com.blueland.bpm.identity.model.Role;
import com.blueland.bpm.identity.model.User;
import com.blueland.bpm.identity.util.IdentityException;
import com.blueland.bpm.identity.util.UserServiceRestImplCondition;
import com.google.common.collect.Maps;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Service;

import java.net.HttpCookie;
import java.util.List;
import java.util.Map;


/**
 * @author lw
 * @version 1.0
 * @since
 */
@Service("userServiceImpl")
@Conditional(UserServiceRestImplCondition.class)
public class UserServiceRestImpl implements UserService {

	private static final Log logger = LogFactory.getLog(UserServiceRestImpl.class);
	
    private static final String GET_USER_BY_ID_URL = "/user/getUserById";
    
    private static final String GET_CENTER_USER_BY_ID_URL = "/user/getCenterUserById";
    
    private static final String PAGE_QUERY_USER_BY_ATTR_URL = "/user/pageQueryUserByAttr";
   
    private static final String FIND_USER_BY_ORG_ID_URL = "/user/findUserByOrgId";
    
    private static final String QUERY_EMPLOYEEJOBIDENTITY_BY_IDS_URL = "/user/queryEmployeeJobIdentityByIds";
    
    private static final String QUERY_EMPLOYOFORG_BY_EMPLOYJOBIDENTIFYIDS_AND_ORGNODEID_URL = "/user/queryEmployOfOrgByEmployJobIdentifyIdsAndOrgNodeId";
    
    private static final String GET_ORG_UNIT_URL = "/user/getOrgUnit";

    private static final String SSOLOGIN_URL = "/ssologin";	
    
	private String orgBizCode = "";
	
	@Value("${rzdata.bpmcRestUrl}")
    String bpmcRestUrl;
	
	@Autowired
	// User对应的DAO类,主要用于数据库的增删改查等操作
	private UserQueryDao userQueryDao;
	
	@Override
	public User getUserById(String userId) {
		User user = null;
		try {
			JSONObject paramsJson = new JSONObject();
			paramsJson.put("orgBizCode", orgBizCode);
			paramsJson.put("userId", userId);
			String url = bpmcRestUrl + GET_USER_BY_ID_URL;
			System.out.println("getUserById===>url=====" + url);
			String result = HttpUtil.post(url, JSONUtil.toJsonStr(paramsJson));
			System.out.println("result=====" + result);
			user = JSONUtil.parseObj(result).toBean(User.class);
			System.out.println("user=====" + JSONUtil.toJsonStr(user));
		}catch(Exception e) {
			return new User();
		}
		return user;
	}
	
	@Override
	public User getCenterUserById(String userId) {
		User user = null;
		try {
			JSONObject paramsJson = new JSONObject();
			paramsJson.put("userId", userId);
			String url = bpmcRestUrl + GET_CENTER_USER_BY_ID_URL;
			String result = HttpUtil.post(url, JSONUtil.toJsonStr(paramsJson));
			user = JSONUtil.parseObj(result).toBean(User.class);
		}catch(Exception e) {
			return new User();
		}
		return user;
	}
	
	@Override
	public Map<String, Object> pageQueryUserByAttr(String userType, String userName, int pageNumber, int pageSize) {
		Map<String, Object> resultMap = null;
		try {
			JSONObject paramsJson = new JSONObject();
			paramsJson.put("userType", userType);
			paramsJson.put("userName", userName);
			paramsJson.put("pageNumber", pageNumber);
			paramsJson.put("pageSize", pageSize);
			String url = bpmcRestUrl + PAGE_QUERY_USER_BY_ATTR_URL;
			String result = HttpUtil.post(url, JSONUtil.toJsonStr(paramsJson));
			resultMap = JSONUtil.parseObj(result).toBean(Map.class);
		}catch(Exception e) {
			return Maps.newHashMap();
		}
		return resultMap;
	}

	@Override
	public User getUserByCode(String userCode, String orgId) {
		// TODO Auto-generated method stub
		return getUserById(userCode);
	}

	@Override
	public User getUserByCode(String userCode) {
		// TODO Auto-generated method stub
		return getUserById(userCode);
	}

	@Override
	public List<User> findUserByOrgId(String orgId) {
		List<User> userList = null;
		try {
			JSONObject paramsJson = new JSONObject();
			paramsJson.put("orgId", orgId);
			String url = bpmcRestUrl + FIND_USER_BY_ORG_ID_URL;
			String result = HttpUtil.post(url, JSONUtil.toJsonStr(paramsJson));
			userList = JSONUtil.parseArray(result).toList(User.class);
		}catch(Exception e) {
			return null;
		}
		return userList;
	}

	@Override
	public List<User> findUserByOrgIds(List<Org> orgs) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> findUserTreeByOrgId(String orgId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> findUserByEntitiyCriteria(User paramEntity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean hasUser(String orgId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<User> getUserByUserNameOrUserCode(String userName, String userCode) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isUserInOrg(String userId, String orgId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isUserInRole(String userId, String roleId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public User getUserByDeptId(String deptId, String userCode) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> findUserMobiles(List<String> userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> findUserByRoleId(String roleId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> findAllUserByRole(String roleId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> findUserIdByRoleCascade(String roleId) {
		List<User> users = null;
		try {
			Role params = new Role();
			params.setRoleId(roleId);
			users = userQueryDao.findUserIdByRoleCascade(params);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return users;
	}

	@Override
	public List<User> findUserByRole(String roleId, String companyId, String deptId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> findUserByRoleCascade(String roleId, String orgId, Integer flag) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> findUser(String roleId, String orgId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> findUserIdByRoleId(String roleId) {
		List<String> users = null;
		try {
			Role params = new Role();
			params.setRoleId(roleId);
			users = userQueryDao.findUserIdByRole(roleId);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return users;
	}

	@Override
	public Map<String, Object> ssoLogin(String bwUcLocalCookieValue){
		Map<String, Object> principalInfoMap = null;
		try {
			HttpCookie cookie = new HttpCookie("BW_UC_LOCAL_COOKIE_KEY", bwUcLocalCookieValue);
			String result = HttpRequest.get(this.bpmcRestUrl + SSOLOGIN_URL)
					.cookie(cookie)
					.timeout(20000)//超时，毫秒
					.execute().body();
			principalInfoMap = JSONUtil.parseObj(JSONUtil.parseObj(result)
					.get("data")).toBean(Map.class);
		} catch(Exception e) {
			return null;
		}
		return principalInfoMap;
	}
	
}
