Skip to content

Instantly share code, notes, and snippets.

@lysu
Last active December 10, 2015 02:58
Show Gist options
  • Save lysu/4371411 to your computer and use it in GitHub Desktop.
Save lysu/4371411 to your computer and use it in GitHub Desktop.
genSql.pl
#! /usr/bin/perl
#
# Generate init hotel bind user data sql.
# author: robiplus
#
# usage:
#
use strict;
use warnings;
sub gen_sql;
sub gen_relation_ship;
my ($userInfo_file, $hotel_file, $flag) = @ARGV;
$flag = ($flag or 0);
open(my $userInfo, "<", $userInfo_file);
open(my $hotel, "<", $hotel_file);
my @users = <$userInfo>;
my @hotels = <$hotel>;
close($hotel);
close($userInfo);
my @hotelInfos = map {chomp; my @item = split(/\t/, $_); ({id => $item[0], name => $item[1]}) x 4 } @hotels;
my @userInfos = map {chomp; my @infos = split(/:|\s/, $_); {id => $infos[1], pwd => $infos[3]}} @users;
if ($flag == 0){
for my $i (0 .. $#hotelInfos) {
&gen_sql($userInfos[$i]{'id'}, $hotelInfos[$i]{'id'})
}
} else {
for my $j (0 .. $#hotelInfos) {
&gen_relation_ship($hotelInfos[$j]{'id'}, $hotelInfos[$j]{'name'}, $userInfos[$j]{'id'}, $userInfos[$j]{'pwd'})
}
}
sub gen_sql {
my ($userId, $hotelId) = @_;
print "insert into hotel_sub_account (user_id, hotel_id, role_type, parent_group_id, parent_group_name, create_time, update_time) ".
"select '$userId', '$hotelId', 'com.qunar.hotel.hms.hotel_owner.model.GroupTypeEnum.HOTEL_OWNER_CUSTOMER', g.id, g.name, now(), now() ".
"from AUDIT_RBAC_GROUP g join AUDIT_RBAC_DATA_PRIV d on g.id = d.group_id ".
"where d.data_id = '$hotelId' and g.deleted = 0 and d.data_type = 'com.qunar.hotel.hms.hotel.model.HotelInfo';\n";
}
sub gen_relation_ship {
my ($hotelId, $hotelName, $userId, $pwd) = @_;
print "$hotelId\t$hotelName\t$userId\t$pwd\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment