func (p *UserService) deleteUserById(userId string) (err error) {
	if !bson.IsObjectIdHex(userId) {
		logrus.Errorln("invalid object id for deleteUserById: ", userId)
		err = errors.New("invalid object id for deleteUserById")
		return
	}

	selector := bson.M{}
	selector["_id"] = bson.ObjectIdHex(userId)

	err = dao.HandleDelete(p.userCollectionName, true, selector)
	return
}
func (p *TenantService) deleteTenantByName(tenantname string) (err error) {
	query := strings.Join([]string{"{\"tenantname\": \"", tenantname, "\"}"}, "")

	selector := make(bson.M)
	err = json.Unmarshal([]byte(query), &selector)
	if err != nil {
		return
	}
	selector, err = mejson.Unmarshal(selector)
	if err != nil {
		return
	}

	return dao.HandleDelete(p.collectionName, true, selector)
}
func (p *TenantService) deleteTenantById(tenantid string) (err error) {
	if !bson.IsObjectIdHex(tenantid) {
		err = errors.New("invalide ObjectId.")
		return
	}

	var selector = bson.M{}
	selector["_id"] = bson.ObjectIdHex(tenantid)

	err = dao.HandleDelete(p.collectionName, true, selector)
	if err != nil {
		logrus.Errorf("delete tenant [tenantid=%v] error is %v", tenantid, err)
	}
	return

}
func (p *UserService) UserDelete(token string, userId string) (errorCode string, err error) {
	if !bson.IsObjectIdHex(userId) {
		logrus.Errorln("invalid object id for UserDelete: ", userId)
		err = errors.New("invalid object id for UserDelete")
		return USER_ERROR_DELETE, err
	}

	code, err := GetTokenService().TokenValidate(token)
	if err != nil {
		return code, err
	}

	if authorized := GetAuthService().Authorize("delete_user", token, userId, p.userCollectionName); !authorized {
		logrus.Errorln("required opertion is not allowed!")
		return COMMON_ERROR_UNAUTHORIZED, errors.New("Required opertion is not authorized!")
	}

	clusters, errquery := GetClusterByUser(userId, token)
	if errquery != nil {
		logrus.Errorf("query cluster err is %v", errquery)
		return "", errors.New("query cluster is err")
	}
	if len(clusters) != 0 {
		logrus.Errorf("user has unterminated cluster")
		return USER_ERROR_EXISTCLUSTER, errors.New("Please terminated cluster first!")
	}

	selector := bson.M{}
	selector["_id"] = bson.ObjectIdHex(userId)

	user, err := p.GetUserById(userId)
	tenantid := user.TenantId

	err = dao.HandleDelete(p.userCollectionName, true, selector)
	if err != nil {
		logrus.Warnln("delete user error %v", err)
		return USER_ERROR_DELETE, err
	}

	err = GetTenantService().deleteTenantById(tenantid)
	if err != nil {
		logrus.Warnln("delete tenant error %v", err)
		return TENANT_ERROR_DELETE, err
	}

	return "", nil
}