public static void importMembers(ObjectId id,String msg){
render(id,msg);
}
public static void generateMembers(File file,ObjectId id) throws Exception{
if(session.get("companyid") == null)
login();
String msg = "";
Group group = Group.findById(id);
Map<String,Member> members = group.members;
if(members==null){
members = new HashMap<String, Member>();
}
long updatedtime = System.currentTimeMillis();
if(file == null){
msg = "请选择所要上传的文件";
importMembers(id,msg);
}
String fileName = file.getName();
//String serverPath = Play.configuration.getProperty("fileupload.path", "attachments");
if(!file.isFile()){
msg= "不是文件";
importMembers(id,msg);
}else {
int totalrows =0;
int totalimported=0;
int totalexisted=0;
int totaltmp = 0;
List<BasicDBObject> list = new ArrayList<BasicDBObject>();
List<String> uuids = new ArrayList<String>();
FileInputStream is = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(is);
HSSFSheet childSheet = wb.getSheetAt(0);
int rowNum = childSheet.getLastRowNum();
String line = null;
for(int jj = 0; jj < rowNum + 1; jj++){
totalrows++;
if(totalrows == 1)//skip first row
continue;
HSSFRow row = childSheet.getRow(jj);
String cols[] = new String[14];
int ccol = row.getLastCellNum();
for(int k = 0;k < ccol; k++){
cols[k]= row.getCell((short) k).getStringCellValue().trim();
}
int i = 0;
String name = "";
if(ccol >= 1)
name = cols[i++].trim();
String position = "";
if(ccol >= 2)
position = cols[i++].trim();
String logo = "";
if(ccol >= 3 )
logo = cols[i++].trim();
String mobile = "";
if(ccol >= 4 )
mobile = cols[i++].trim();
String tel = "";
if(ccol >= 5 )
tel = cols[i++].trim();
String email = "";
if(ccol >= 6 )
email = cols[i++].trim();
String website = "";
if(ccol >= 7 )
website = cols[i++].trim();
String address = "";
if(ccol >= 8 )
address = cols[i++].trim();
String company = "";
if(ccol >= 9 )
company = cols[i++].trim();
String department = "";
if(ccol >= 10)
department = cols[i++].trim();
String industry = "";
if(ccol >= 11)
industry=cols[i++].trim();
String description = "";
if(ccol >= 12)
description = cols[i++].trim();
String fax = "";
if(ccol >= 13)
fax=cols[i++].trim();
String zipcode = "";
if(ccol >= 14){
zipcode=cols[i++].trim();
}
if(name == null)
continue;
if(mobile == null)
continue;
if(mobile == null|| "".equals(mobile))
continue;
name.replaceAll(" ", "");
mobile = CommonUtils.formatPhoneNo(mobile);
address = address.replace(" ", "");
if("".equals(mobile))
continue;
List<User> users = User.q().filter("mobile", mobile).filter("innerversion", 4).asList();
int count =0;
boolean isrealjoined = false;
List<String> needjoinuids = new ArrayList<String>();
for(int f = 0; f < users.size(); f++){
count++;
User user = users.get(f);
if(user.istmp != null){
//privacy
Map<String ,Integer> map = new HashMap<String ,Integer>();
Contact contact = new Contact();
user.contact = contact;
map.put("p1", 1);
map.put("p2", 0);
map.put("p3", 0);
map.put("p4", 0);
map.put("p5", 0);
map.put("p6", 0);
user.contact.privacy = map;
user = CommonUtils.setContact(user, company, name, mobile, updatedtime, tel, fax, website, email, department, industry, position, description, address, zipcode);
user.save();
totaltmp++;
}
if(user.groups != null && user.groups.get("groupid") != null){
//已经加入到组中了
totalexisted++;
if(user.istmp == null)
isrealjoined = true;
}else
needjoinuids.add(user.getId().toString());
}
if(isrealjoined==false && needjoinuids.size()>0){
uuids.addAll(needjoinuids);
}
List<User> findUserList = User.q().filter("mobile", mobile).filter("name", name).filter("istmp", 1).asList();
if(count == 0 && findUserList.size() == 0){
//set user info
User user = new User();
user.imei = "";
user.mobile = mobile;
user.innerversion = 4;
user.istmp = 1;
user.name = name;
user.created = new Date();
user.password = "";
user.device = "";
user.deviceid = "";
user.os = "";
user.password = "";
user.time = System.currentTimeMillis();
user.token = "";
user.updatedtime = System.currentTimeMillis();
user.version = "";
user.save();
Map<String ,Integer> map = new HashMap<String ,Integer>();
Contact contact = new Contact();
user.contact = contact;
map.put("p1", 1);
map.put("p2", 0);
map.put("p3", 0);
map.put("p4", 0);
map.put("p5", 0);
map.put("p6", 0);
user.contact.privacy = map;
user = CommonUtils.setContact(user, company, name, mobile, updatedtime, tel, fax, website, email, department, industry, position, description, address, zipcode);
user.save();
uuids.add(user.getId().toString());
}
for(String uuid:uuids){
//TODO groupid
//用户加入群组
User user = User.findById(new ObjectId(uuid));
Member m = new Member();
m.uuid = user.contact.uuid;
m.joinstatus = "ok";
m.jointime = System.currentTimeMillis();
m.groupid = id.toString();
m.email="";
m.verifycode="";
if(user.groups == null){
user.groups = new HashMap<String, Member>();
}
user.groups.put(id.toString(),m);
user.save();
//群组添加成员
members.put(m.uuid, m);
group.members = members;
group.updatedtime ++;
group.lasttime = System.currentTimeMillis();
group.save();
totalimported++;
}
}
}
file.delete();
showMemberList(id.toString(), null);
}