package com.palmnest.dao.estateserve.impl;

import cn.jiguang.common.utils.StringUtils;

import com.palmnest.application.ElementConstant;
import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.application.dto.AppUserBindInfoDto;
import com.palmnest.dao.estateserve.AppUserDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.User;
import com.palmnest.model.estateserve.AppUser;
import com.palmnest.model.estateserve.WxUser;

import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.*;

import lombok.extern.slf4j.Slf4j;

/**
 * 会员用户信息底层Dao数据实现类
 *
 * @author yubing
 * @since 2017-1-14
 */
@Slf4j
@Repository("appUserDao")
public class AppUserDaoImpl extends GenericDaoHibernate<AppUser, Integer> implements AppUserDao {

	public AppUserDaoImpl() {
		super(AppUser.class);
	}

	/**
	 * 根据手机号码查询用户
	 *
	 * @param number
	 * @return
	 */
	@Override
	public AppUser getAppUsersByPhone(String userMobile) {
		StringBuffer strHql = new StringBuffer("from AppUser where userMobile=? and isEnabled=?");
		List<AppUser> list = getSession().createQuery(strHql.toString())
				.setString(0, userMobile)
				.setString(1, ElementConstant.YESORNO_1).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public AppUser getAppUserByOpenId(String developId, String openId) {
		String strHQL = "from WxUser where developId=? and openId=?";
		List<WxUser> wxUsers = getSession().createQuery(strHQL.toString()).setString(0, developId).setString(1, openId).list();
		if (!CollectionUtils.isEmpty(wxUsers)) {
			if (wxUsers.get(0).getUserId() != null) {
				AppUser appUser = get(wxUsers.get(0).getUserId());
				if (appUser.getIsEnabled().equals("1")) {
					return appUser;
				}
			}
		}
		return null;
	}

	@Override
	public Page queryAppUserList(int userId, int virtualId, int pageNo, int pageSize) {
		StringBuffer strSQL = new StringBuffer("SELECT DISTINCT(t1.id) as id,t1.userName,t1.userMobile,t2.headimgUrl,t1.`status`,t1.roleType,t1.imageUrl01,t1.imageUrl02,t1.imageUrl03,t3.imageUrl");
		strSQL.append(" FROM tb_user t1");
		strSQL.append(" LEFT JOIN tb_wxuser t2 ON t1.ID = t2.userId");
		strSQL.append(" LEFT JOIN tb_userimageinfo t3 on t1.ID=t3.userid");
		strSQL.append(" WHERE t1.virtualArchId =" + virtualId + " AND t1.isEnabled = '1' AND t1. STATUS != '2' AND t1.id !=" + userId);
		strSQL.append(" ORDER BY t1. STATUS ASC,t1.createTime DESC");
		return pagedQueryByJdbcTemplate(strSQL.toString(), pageNo, pageSize);
	}


	@Override
	public Integer getAppUserNonCheckedCount(String virtualArchId, String status) {
		String strSQL = "SELECT count(1) from tb_user t1 where t1.virtualArchId=" + virtualArchId + " and t1.`status`=" + status + " and t1.isEnabled=1";
		return this.getJdbcTemplate().queryForInt(strSQL);
	}


	@Override
	public List<AppUser> getAppUserByRoleType(String roleType, int virtualId) {
		String strHQL = "from AppUser where roleType=? and virtualArchId=? and isEnabled='1' ";
		List<AppUser> list = getSession().createQuery(strHQL).setString(0, roleType).setInteger(1, virtualId).list();
		return list;
	}

	@Override
	public List<AppUser> getAppUserByVirId(int virtualId) {
		String strHQL = "from AppUser where virtualArchId=? and isEnabled='1' ";
		List<AppUser> list = getSession().createQuery(strHQL).setInteger(0, virtualId).list();
		return list;
	}

	/**
	 * 获取该项目下所有角色类型用户
	 *
	 * @param orgId
	 * @return
	 */
	@Override
	public List<AppUser> getUserByOrgIdAndRoleType(String orgId,
												   String roleTypes) {
		StringBuilder strHQL = new StringBuilder("from AppUser where isEnabled='1'  and organizationId=? ");
		if (StringUtils.isNotEmpty(roleTypes)) {
			strHQL.append(" and roleType in (" + roleTypes + ")");
		}
		List<AppUser> list = getSession().createQuery(strHQL.toString()).setInteger(0, Integer.parseInt(orgId)).list();
		return list;
	}

	/**
	 * 获取用户账号列表
	 */
	@Override
	public Page findList(Map<String, Object> paramets, int pageSize, int pageNo) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" select u.id,u.userName,v.name virName,u.userMobile,v.address,u.roleType,u.integral,u.organizationId,date_format(u.createTime,'%Y-%m-%d %T') createTime,u.isEnabled    ");
		sql.append(" from tb_user u left join tb_virtualarchitecture v on v.id=u.virtualArchId ");
		sql.append(" where 1=1  ");
		if (StringUtils.isNotEmpty(paramets.get("orgId") == null ? "" : paramets.get("orgId").toString())) {
			sql.append(" and u.organizationId in (" + paramets.get("orgId") == null ? "" : paramets.get("orgId").toString() + ")  ");
		}
		if (StringUtils.isNotEmpty(paramets.get("userName") == null ? "" : paramets.get("userName").toString())) {
			sql.append(" and u.userName like '%" + paramets.get("userName") == null ? "" : paramets.get("userName").toString() + "%'  ");
		}
		if (StringUtils.isNotEmpty(paramets.get("roleType") == null ? "" : paramets.get("roleType").toString())) {
			sql.append(" and u.roleType='" + paramets.get("roleType") == null ? "" : paramets.get("roleType").toString() + "'  ");
		}

		if (StringUtils.isNotEmpty(paramets.get("mobile") == null ? "" : paramets.get("mobile").toString())) {
			sql.append(" and u.userMobile= '" + paramets.get("mobile") == null ? "" : paramets.get("mobile").toString() + "'  ");
		}
		if (StringUtils.isNotEmpty(paramets.get("virName") == null ? "" : paramets.get("virName").toString())) {
			sql.append(" and u.virName='" + paramets.get("virName") == null ? "" : paramets.get("virName").toString() + "'  ");
		}
		sql.append(" order by u.createTime desc ");
		return pagedQueryByJdbcTemplate(sql.toString(), pageNo, pageSize);
	}

//	@Override
//	public AppUser getAUByOpenId(String orgId, String openId) {
//		String strHQL = "from WxUser where organizationId=? and openId=?";
//		List<WxUser> wxUsers=getSession().createQuery(strHQL.toString()).setString(0, orgId).setString(1,openId).list();
//		if(!CollectionUtils.isEmpty(wxUsers)){
//			if(wxUsers.get(0).getUserId()!=null){
//				AppUser appUser=get(wxUsers.get(0).getUserId());
//				if(appUser.getIsEnabled().equals("1")){
//					return appUser;
//				}
//			}
//		}
//		return null;
//	}

	@Override
	public Integer getUserId() {
		String sql = "SELECT nextval('bw_user_no') FROM dual";
		return (Integer) getSession().createSQLQuery(sql).uniqueResult();
	}

	@Override
	public Integer getUserNumer() {
		String sql = "SELECT nextval('bw_user_number') FROM dual";
		return (Integer) getSession().createSQLQuery(sql).uniqueResult();
	}

	@Override
	public AppUser getAppUsersByPhoneAndOrg(String userMobile, Integer orgId) {
		StringBuffer strHql = new StringBuffer("from AppUser where userMobile=? and isEnabled=? and organizationId=? ");
		List<AppUser> list = getSession().createQuery(strHql.toString())
				.setString(0, userMobile)
				.setString(1, ElementConstant.YESORNO_1)
				.setInteger(2, orgId)
				.list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public AppUser getAppUserByWxUserAndOrg(Integer wxUserId, Integer orgId) {

		System.out.println("======>getAppUserByWxUserAndOrg旧框架 微信用户Id和项目Id 找user[wxUserId:"+wxUserId+",orgId:"+orgId+"]");
		if(null == wxUserId || null == orgId){
			return null;
		}
		// 这里查询条件之所以为 (isEnabled=1 or userType='2');因为被禁用的白名单用户(userType='2'),它的isEnable为0;
		// 但是这类人(被禁用的)还可以使用部分功能(维修服务/报事报修/访客功能..),所以需要能查询到此用户;
		StringBuffer strHql = new StringBuffer("from AppUser where wxUserId=? and organizationId=? and (isEnabled=? or userType='2') and isLogin=1 ");
		List<AppUser> list = getSession().createQuery(strHql.toString())
				.setInteger(0, wxUserId)
				.setInteger(1, orgId)
				.setString(2, ElementConstant.YESORNO_1)
				.list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public AppUser getAppUsersByPhoneAndOrg(String userMobile, String orgId,
											String isEnabled) {
		StringBuffer strHql = new StringBuffer("from AppUser where userMobile=?   ");
		if (StringUtils.isNotEmpty(orgId)) {
			strHql.append(" and organizationId=" + orgId + " ");
		}
		if (StringUtils.isNotEmpty(isEnabled)) {
			strHql.append(" and isEnabled=" + isEnabled + " ");
		}
		List<AppUser> list = getSession().createQuery(strHql.toString()).setString(0, userMobile).list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public void updateAppUser(Integer userId, Integer wxUserId, Integer orgId) {
		String sql = "UPDATE tb_user SET wxUserId=NULL,isBinding=0,isLogin=0 WHERE wxUserId=" + wxUserId + " AND organizationId=" + orgId;
		if (userId != null) {
			sql += " AND ID <> " + userId;
		}
		int update = getSession().createSQLQuery(sql).executeUpdate();
	}

	@Override
	public List<AppUser> getWxUserByOrgIds(String orgIds) {
		StringBuilder sql = new StringBuilder(" from AppUser ");
		sql.append(" where  isEnabled='1'  and isBinding=1 and wxUserId is not null  ");
		if (StringUtils.isNotEmpty(orgIds)) {
			sql.append(" and organizationId in (" + orgIds + ")  ");
			return getSession().createQuery(sql.toString()).list();
		}
		return null;
	}

	@Override
	public List<AppUser> getAppUserByOrgId(Integer orgId) {
		String sql = "from AppUser where organizationId=? and isEnabled='1' ";
		return getSession().createQuery(sql).setInteger(0, orgId).list();
	}

	@Override
	public int updateAppUserByUserIds(Map<String, String> map) {
		String sql = "UPDATE tb_user SET integral=" + map.get("integral") + ",modifyTime=SYSDATE(),modifier='" + map.get("userId") + "'   WHERE id in (" + map.get("userIds") + ") ";
		int update = getSession().createSQLQuery(sql).executeUpdate();
		return update;
	}

	@Override
	public AppUser getAppUserByLingLingId(String lingLingId) {
		String sql = "from AppUser where lingLingId = ?";
		List<AppUser> list = getSession().createQuery(sql).setString(0, lingLingId).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public AppUser getUserByVicIdAndMobile(Integer virtualArchId, String mobile) {
		String sql = "from AppUser where virtualArchId=? and userMobile = ?";
		List<AppUser> list = getSession().createQuery(sql).setInteger(0, virtualArchId).setString(1, mobile).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getWhiteUserByMobileAndOrg2(String mobile, Integer publicId) {

		String sqlStr = "SELECT u.* " +
				" FROM tb_user u " +
				" WHERE u.isEnabled=1 AND userType=0 ";

		if (org.apache.commons.lang3.StringUtils.isNotBlank(mobile)) {
			sqlStr += " AND u.userMobile='" + mobile + "'";
		}

		if (publicId != null) {
			sqlStr += " AND u.organizationId IN (SELECT two.organizationId FROM tr_wxpublicno_org two WHERE two.publicNoId=" + publicId + " GROUP BY two.organizationId)";
		}
		sqlStr += " GROUP BY u.ID";
		return getJdbcTemplate().queryForList(sqlStr);


	}

	@Override
	public List<Map<String, Object>> getWeiXiaoAppUsersByIds(String userIds, Integer orgId) {

		String sql = "SELECT u.ID userId,u.userNumber card_number,u.userName name,u.userMobile telephone,u.userSex gender,CONCAT(tc.`name`,'/',g.`name`,'/',tv.`name`,'/',dl.itemname) organization FROM tb_user u " +
				" LEFT JOIN tb_organization g ON u.organizationId=g.ID " +
				" LEFT JOIN tb_virtualarchitecture tv on tv.id=u.virtualArchId" +
				" LEFT JOIN tb_dictionary_item dl on dl.id=u.departmentId" +
				" LEFT JOIN tr_company_org tco on tco.org_id=u.organizationId" +
				" LEFT JOIN tb_company tc on tco.company_id=tc.id" +
				" WHERE u.userType=0 AND u.isEnabled=1 AND u.organizationId=" + orgId;

		if (org.apache.commons.lang3.StringUtils.isNotBlank(userIds)) {
			sql += " AND u.ID IN (" + userIds + ")";
		}
		sql += " GROUP BY u.ID";
		return getJdbcTemplate().queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> getUserMobileAndDepName(Integer orgId) {

		String sql = "select u.userMobile,IF(IFNULL(dep.shortName,'') <> '',dep.shortName,dep.name) depName from tb_user u left join tb_department dep on u.departmentId=dep.id " +
				" where organizationId= " + orgId;


		return getJdbcTemplate().queryForList(sql);
	}

	@Override
	public List<AppUser> getAppUserByIds(String userIds) {
		String[] split = userIds.split(",");
		List<Integer> ids = new ArrayList<>();
		for (String id : split) {
			ids.add(Integer.parseInt(id));
		}
		String sql = " from AppUser where id in (:ids)";
		return getSession().createQuery(sql).setParameterList("ids", ids).list();
	}

	@Override
	public List<AppUser> getWhiteUserByMobile(String mobile) {
		String sql = " from AppUser where userType=0 and isEnabled=1 and userMobile=? ";
		return getSession().createQuery(sql).setString(0, mobile).list();
	}

	@Override
	public List<AppUser> countOrgWhiteUser(String[] orgIds) {
		String hql = "from AppUser where userType=0 and isEnabled=1";
		return getSession().createQuery(hql).list();
	}

	@Override
	public String getUserMealcard(String userMobile) {
		String sql = "SELECT " +
				" meal_card " +
				"FROM " +
				" tb_virtualarchitecture " +
				"WHERE " +
				" meal_card IS NOT NULL " +
				"AND id IN ( " +
				" SELECT " +
				" virtualArchId " +
				" FROM " +
				" tb_user " +
				" WHERE " +
				" userMobile =? " +
				" AND isEnabled = 1 " +
				" AND virtualArchId IS NOT NULL " +
				") order by modifyTime desc " +
				" LIMIT 1";
		return (String) getSession().createSQLQuery(sql).setString(0, userMobile).uniqueResult();
	}

	@Override
	public User getByAccount(String account) {
		String sql = "from User where username=? ";
		List<User> list = getSession().createQuery(sql).setString(0, account).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public AppUser getWhiteUserByMobileAndVirt(Integer virId, String employeePhone) {
		String sql = "from AppUser where isEnabled='1' and userType='0' ";
		if (null != virId) {
			sql = sql + " and virtualArchId=" + virId;
		}
		if(StringUtils.isNotEmpty(employeePhone)){
			sql = sql + " and userMobile=" + employeePhone;
		}
		List<AppUser> list = getSession().createQuery(sql).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public AppUser getWhiteUserByMobileAndOrgId(Integer orgId, String employeePhone) {
		String sql = "from AppUser where isEnabled='1' and userType='0' ";
		
		if (null != orgId) {
			sql = sql + " and organizationId=" + orgId;
		}
		if(StringUtils.isNotEmpty(employeePhone)){
			sql = sql + " and ( userMobile='" + employeePhone + "' or  userName = '" + employeePhone + "')";
		}
		List<AppUser> list = getSession().createQuery(sql).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getVirtDeptUser(Integer virtId, String name) {
		StringBuilder sql = new StringBuilder();
		sql.append(" select u.id,u.userName,u.userMobile,u.departmentId,d.name deptName  ");
		sql.append(" ,u.virtualArchId,v.name virName,wu.openId from tb_user u  ");
		sql.append(" left join tb_virtualarchitecture v on v.id=u.virtualArchId  ");
		sql.append(" left join tb_department d on d.id=u.departmentId  ");
		sql.append(" left join tb_wxuser wu on wu.id=u.wxUserId and u.isLogin=1  ");
		sql.append(" where u.userType=0 and u.isEnabled=1  ");
		if (null != virtId) {
			sql.append(" and u.virtualArchId=" + virtId);
		}
		if (StringUtils.isNotEmpty(name)) {
			sql.append(" and (u.userName like '%" + name + "%' or u.userMobile like '%" + name + "%')  ");
		}
		sql.append(" order by u.virtualArchId,u.departmentId   ");
		return getJdbcTemplate().queryForList(sql.toString());
	}
	
	@Override
	public List<Map<String, Object>> getGroupUser(Integer groupId, Integer orgId,String name) {
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT  ");
		sql.append(" u.id,	u.userName,	u.userMobile,	u.departmentId,	d. NAME deptName,	u.virtualArchId,	v. NAME virName  ");
		sql.append(" FROM  ");
		sql.append(" 	tr_perm_group_user gu  ");
		sql.append(" LEFT JOIN	tb_user u on u.id=gu.user_id  ");
		sql.append(" LEFT JOIN tb_virtualarchitecture v ON v.id = u.virtualArchId  ");	
		sql.append(" LEFT JOIN tb_department d ON d.id = u.departmentId  ");
		sql.append("  WHERE  ");
		sql.append("  	gu.device_perm_group_id =  "+groupId );
		sql.append("    and v.organizationId = "+orgId);		
		sql.append("    and u.organizationId = "+orgId);	
		sql.append(" AND v.isEnabled = '1' ");
		sql.append(" AND u.isEnabled = '1' ");
		sql.append(" and u.userType=0  ");
		if (StringUtils.isNotEmpty(name)) {			
			sql.append(" and (u.userName like '%" + name + "%' or v.NAME like '%" + name + "%'  or u.userMobile like '%" + name + "%')  ");
		}
		return getJdbcTemplate().queryForList(sql.toString());
	}
	
	
	@Override
	public List<Map<String, Object>> getNotGroupUser(Integer groupId, Integer orgId,String name) {
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT  ");
		sql.append(" u.id,	u.userName,	u.userMobile,	u.departmentId,	d. NAME deptName,	u.virtualArchId,	v. NAME virName  ");
		sql.append(" FROM  ");
		sql.append(" 	tb_user  u  ");
		sql.append(" LEFT JOIN tb_virtualarchitecture v ON v.id = u.virtualArchId  ");
		sql.append(" LEFT JOIN tb_department d ON d.id = u.departmentId  ");	
		sql.append("  WHERE  ");
		sql.append("  (u.userType <> '-1' OR u.userType IS NULL) and u.userMobile is not null and u.isEnabled='1'  ");
		sql.append("    and v.organizationId = "+orgId);
		sql.append("    and u.organizationId = "+orgId);
		sql.append(" AND v.isEnabled = '1' ");
		sql.append("    AND NOT EXISTS (SELECT gu.user_id FROM tr_perm_group_user gu WHERE device_perm_group_id = "+groupId+" and gu.user_id = u.ID)  ");
	
		if (StringUtils.isNotEmpty(name)) {			
			sql.append(" and (u.userName like '%" + name + "%' or v.NAME like '%" + name + "%'  or u.userMobile like '%" + name + "%')  ");
		}
		return getJdbcTemplate().queryForList(sql.toString());
	}

	/**
	 * 根据项目、openid获取绑定的openid
	 */
	@Override
	public AppUserBindInfoDto getByOrgIdAndOpenId(Integer orgId, String openId) {
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT id,organizationId,userName,userMobile,openId,bind_openid bindOpenid,bind_developid bindDevelopid  ");
		sql.append(" FROM tb_user  ");
		sql.append(" WHERE isLogin = 1  ");
		sql.append(" AND organizationId =  "+orgId);
		sql.append(" AND openId = '"+openId+"'  ");
		List<Map<String, Object>> list=getJdbcTemplate().queryForList(sql.toString());
		if(CollectionUtils.isEmpty(list)){
			return null;
		}
		Map<String, Object> map=list.get(0);
		if(null == map || !map.containsKey("id") || null == map.get("id")){
			return null;
		}
		AppUserBindInfoDto dto=new AppUserBindInfoDto();
		dto.setId(Integer.parseInt(map.get("id").toString()));
		if(map.containsKey("userName") && null != map.get("userName")){
			dto.setUserName(map.get("userName").toString());
		}
		if(map.containsKey("userMobile") && null != map.get("userMobile")){
			dto.setUserMobile(map.get("userMobile").toString());
		}
		if(map.containsKey("openId") && null != map.get("openId")){
			dto.setOpenId(map.get("openId").toString());
		}
		if(map.containsKey("organizationId") && null != map.get("organizationId")){
			dto.setOrganizationId(Integer.parseInt(map.get("organizationId").toString()));
		}
		if(map.containsKey("bindDevelopid") && null != map.get("bindDevelopid")){
			dto.setBindDevelopid(map.get("bindDevelopid").toString());
		}
		if(map.containsKey("bindOpenid") && null != map.get("bindOpenid")){
			dto.setBindOpenid(map.get("bindOpenid").toString());
		}
		return dto;
	}

	@Override
	public AppUser findUserByNewFramework(Integer userId,String database) {
		StringBuilder sql=new StringBuilder("SELECT id ID, user_name userName, nick_name nickName, user_mobile userMobile, user_type userType");
		sql.append(",org_id organizationId, virt_id virtualArchId, enabled isEnabled, department_id departmentId, is_login isLogin, `status` ");
		sql.append("FROM "+database+"tb_user WHERE 1=1 ");
		sql.append("AND id ="+userId);
		log.info("findUserByNewFramework-sql:{}",sql);
		List<Map<String, Object>> list=getJdbcTemplate().queryForList(sql.toString());
		if(CollectionUtils.isEmpty(list)){
			return null;
			
		}
		Map<String, Object> map=list.get(0);
		if(null == map || !map.containsKey("ID") || null == map.get("ID") || StringUtils.isEmpty(map.get("ID").toString())){
			return null;
		}
		AppUser user=new AppUser();
		if(map.containsKey("userType") && null != map.get("userType") && StringUtils.isNotEmpty(map.get("userType").toString())){
			user.setUserType(map.get("userType").toString());
		}
		if(map.containsKey("organizationId") && null != map.get("organizationId") && StringUtils.isNotEmpty(map.get("organizationId").toString())){
			user.setOrganizationId(Integer.parseInt(map.get("organizationId").toString()));
		}
		if(map.containsKey("virtualArchId") && null != map.get("virtualArchId") && StringUtils.isNotEmpty(map.get("virtualArchId").toString())){
			user.setVirtualArchId(Integer.parseInt(map.get("virtualArchId").toString()));
		}
		user.setId(Integer.parseInt(map.get("ID").toString()));
		if(map.containsKey("userName") && null != map.get("userName") && StringUtils.isNotEmpty(map.get("userName").toString())){
			user.setUserName(map.get("userName").toString());
		}
		if(map.containsKey("nickName") && null != map.get("nickName") && StringUtils.isNotEmpty(map.get("nickName").toString())){
			user.setNickName(map.get("nickName").toString());
		}
		if(map.containsKey("userMobile") && null != map.get("userMobile") && StringUtils.isNotEmpty(map.get("userMobile").toString())){
			user.setUserMobile(map.get("userMobile").toString());
		}
		if(map.containsKey("isEnabled") && null != map.get("isEnabled") && StringUtils.isNotEmpty(map.get("isEnabled").toString())){
			user.setIsEnabled(map.get("isEnabled").toString());
		}
		if(map.containsKey("departmentId") && null != map.get("departmentId") && StringUtils.isNotEmpty(map.get("departmentId").toString())){
			user.setDepartmentId(Integer.parseInt(map.get("departmentId").toString()));
		}
		if(map.containsKey("isLogin") && null != map.get("isLogin") && StringUtils.isNotEmpty(map.get("isLogin").toString())){
			user.setIsLogin(Integer.parseInt(map.get("isLogin").toString()));
		}
		if(map.containsKey("status") && null != map.get("status") && StringUtils.isNotEmpty(map.get("status").toString())){
			user.setStatus(map.get("status").toString());
		}
		return user;
//		List<AppUser> userList=getSession().createSQLQuery(sql.toString()).addEntity(AppUser.class).list();
//		if(CollectionUtils.isEmpty(userList)){
//			return null;
//		}
		
	}

	@Override
	public List<Map<String, Object>> getAllUserByOrgIds(String orgIds,String userIds,String openIds) {
		if(StringUtils.isNotEmpty(orgIds) && ",".equals(orgIds.substring(orgIds.length()-1,orgIds.length()))){
			orgIds=orgIds.substring(0,orgIds.length()-1);
		}
		if(StringUtils.isNotEmpty(userIds) && ",".equals(userIds.substring(userIds.length()-1,userIds.length()))){
			userIds=userIds.substring(0,userIds.length()-1);
		}
		if(StringUtils.isNotEmpty(openIds) && ",".equals(openIds.substring(openIds.length()-1,openIds.length()))){
			openIds=openIds.substring(0,openIds.length()-1);
		}
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT tp.*, ( CASE WHEN ( tp.deptInfo IS NOT NULL AND tp.deptInfo <> '' ) THEN CONCAT( tp.orgName, '-', tp.virtName, '-', tp.deptInfo ) ELSE CONCAT( tp.orgName, '-', tp.virtName ) END ) deptName  ");
		sql.append(" FROM (  ");
		sql.append(" 	SELECT t.*, GROUP_CONCAT( DISTINCT dt. NAME ORDER BY dt.levels SEPARATOR '-' ) deptInfo  ");
		sql.append(" 	FROM (  ");
		sql.append(" 		SELECT u.id userId, userName, userMobile, userType, u.organizationId, o.`name` orgName, virtualArchId, v.`name` virtName, departmentId ");
		sql.append(" 		, ifnull(bind_openid, openId) openId, REPLACE ( SUBSTRING( d.rank, LOCATE('-', d.rank) + 1 ), '-', ',' ) rankInfo  ");
		sql.append(" 		FROM tb_user u LEFT JOIN tb_organization o ON o.id = u.organizationId  ");
		sql.append(" 		LEFT JOIN tb_virtualarchitecture v ON v.id = u.virtualArchId AND v.isEnabled = 1  ");
		sql.append(" 		LEFT JOIN tb_department d ON d.id = u.departmentId AND d.isEnabled = 1  ");
		sql.append(" 		WHERE u.isEnabled = 1 AND u.`status` = 1 AND u.organizationId IN ("+orgIds+")  ");
		sql.append(" 	) t  ");
		sql.append(" 	LEFT JOIN ( SELECT p.* FROM tb_department p WHERE p.orgId IN ("+orgIds+") ORDER BY p.levels ) dt ON FIND_IN_SET(dt.id, t.rankInfo) GROUP BY t.userId  ");
		sql.append(" ) tp ");
		sql.append(" where 1=1 ");
		if(StringUtils.isNotEmpty(userIds)){
			sql.append(" and tp.userId in ("+userIds+") ");
		}
		if(StringUtils.isNotEmpty(openIds)){
			sql.append(" and tp.openId in ("+openIds+") ");
		}
		log.info("推送公告消息获取用户的SQL，orgIds:{};userIds:{};openIds:{};sql:{}",orgIds,userIds,openIds,sql.toString());
		List<Map<String, Object>> list=getJdbcTemplate().queryForList(sql.toString());
		if(!CollectionUtils.isEmpty(list)){
			log.info("推送公告消息获取用户的SQL，orgIds:{};userIds:{};openIds:{},list.size:{}",orgIds,userIds,openIds,list.size());
		}
		return list;
	}

	@Override
	public AppUser findLoginUserByOpenId(Integer orgId, String openId) {
		System.out.println("======>findLoginUserByOpenId旧框架 openId和项目Id 找user[openId:"+openId+",orgId:"+orgId+"]");
		if(StringUtils.isEmpty(openId) || null == orgId){
			return null;
		}
		// 这里查询条件之所以为 (isEnabled=1 or userType='2');因为被禁用的白名单用户(userType='2'),它的isEnable为0;
		// 但是这类人(被禁用的)还可以使用部分功能(维修服务/报事报修/访客功能..),所以需要能查询到此用户;
		StringBuffer strHql = new StringBuffer("from AppUser where 1=1 and (openId=? or bind_openid=?) and organizationId=? and (isEnabled=? or userType='2')");
		List<AppUser> list = getSession().createQuery(strHql.toString())
				.setString(0, openId)
				.setString(1, openId)
				.setInteger(2, orgId)
				.setString(3, ElementConstant.YESORNO_1)
				.list();
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	
}
