Commit 0a101d99 by java-李谡

格式化

parent 949bf94e
package com.foc.hrm.service;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
......@@ -23,12 +10,18 @@ import com.foc.hrm.entity.UserEntity;
import com.foc.ods.entity.OdsChangesEntity;
import com.foc.office.entity.OfficeEntity;
import com.foc.office.service.OfficeService;
import com.foc.util.DBHandler;
import com.foc.util.DBHelper;
import com.foc.util.DateUtil;
import com.foc.util.IdWorker;
import com.foc.util.Util;
import com.foc.util.*;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.lang3.StringUtils;
import java.sql.SQLException;
import java.util.*;
/**
* @author lisu
*/
public class HRMService {
QueryRunner run = null;
......@@ -37,7 +30,7 @@ public class HRMService {
public static final int SALT_SIZE = 8;
private OfficeService officeService;
public HRMService() {
try {
this.run = new QueryRunner(DBHelper.getDataSource());
......@@ -53,10 +46,10 @@ public class HRMService {
JSONObject dataJson = (JSONObject) result.get("data");
JSONArray jsonArray = (JSONArray) dataJson.get("apiResp");
if (jsonArray != null && jsonArray.size()>0) {
if (jsonArray != null && jsonArray.size() > 0) {
int index = 0;
int size = jsonArray.size();
for (int i=0;i<size;i++) {
for (int i = 0; i < size; i++) {
index++;
try {
JSONObject jsonObject = jsonArray.getJSONObject(i);
......@@ -64,42 +57,29 @@ public class HRMService {
String userId = (String) jsonObject.get("employeeId");
String account = (String) jsonObject.get("account");
String deptFullname = (String) jsonObject.get("deptFullname");
System.out.println("【HRM】第【"+index+"/"+size+"】条【"+userId+"】【"+username+"】【"+account+"】【"+deptFullname+"】");
if (StringUtils.isBlank(userId) || StringUtils.isBlank(account) || StringUtils.isBlank(deptFullname)){// 只导入有账号信息的用户
System.out.println("【HRM】第【" + index + "/" + size + "】条【" + userId + "】【" + username + "】【" + account + "】【" + deptFullname + "】");
// 只导入有账号信息的用户
if (StringUtils.isBlank(userId) || StringUtils.isBlank(account) || StringUtils.isBlank(deptFullname)) {
System.out.println("【跳过】");
continue;
}
// if(StringUtils.isBlank(account)){// 导入所有数据,包括没有账号和部门信息的
//
// jsonObject.put("account", "");
// }
// if (StringUtils.isBlank(userId)){// 导入所有数据,包括没有账号和部门信息的
// System.out.println("【跳过】");
// continue;
// }
System.out.println("【正常】");
UserEntity user = checkUserExist(userId);
if (user != null){
// System.out.println("修改用户【"+userId+"】【"+username+"】");
if (user != null) {
update(user, jsonObject, userId, modified);
} else{
// System.out.println("新增用户【"+userId+"】【"+username+"】");
} else {
insert(jsonObject, modified);
}
if (checkUserProfileExist(userId)){
if (checkUserProfileExist(userId)) {
updateUserProfile(jsonObject);
} else {
insertUserProfile(jsonObject);
}
// System.out.println("【正常】");
} catch (Exception e) {
// TODO: handle exception
// System.out.println("【异常】");
}
}
}
......@@ -109,41 +89,21 @@ public class HRMService {
String deptFullname = (String) jsonObject.get("deptFullname");
if (StringUtils.isNotBlank(deptFullname)){
/*
Map<MapType, OfficeEntity> resultOfficeEntityMap = new HashMap<MapType, OfficeEntity>();
insertOfficeByRecursive(resultOfficeEntityMap, deptFullname, null, null);
OfficeEntity resultOfficeEntity = resultOfficeEntityMap.get(MapType.resultOfficeEntityType);
if (resultOfficeEntity == null) {
throw new IllegalStateException("OfficeEntity 不应为空");
}
String companyId = null;
if (isCompany(resultOfficeEntity.getParentId()))
companyId = resultOfficeEntity.getId();
else if (isDepart(resultOfficeEntity.getParentId()))
companyId = resultOfficeEntity.getParentIds().split(",")[1];
String officeId = resultOfficeEntity.getId();
*/
if (StringUtils.isNotBlank(deptFullname)) {
OfficeEntity office = this.officeService.addOffice(deptFullname);
String fullName = deptFullname.replaceAll("[\\+|\\s+]", "");
String[] species = fullName.split("-");
OfficeEntity company = this.officeService.getOfficeByName(species[0], null);
if(office == null || company == null)
return ;
// System.out.println("公司【"+company.getId()+"】部门【"+office.getId()+"】");
if (office == null || company == null) {
return;
}
insertUser(jsonObject, company.getId(), office.getId(), modified);
String userId = (String) jsonObject.get("employeeId");
insertUser2RoleData(userId);
}
// String loginName = (String) jsonObject.get("mobile");
// String userId = (String) jsonObject.get("employeeId");
// insertUser2RoleData(userId);
}
private void insertUser2RoleData(String userId) {
......@@ -151,8 +111,9 @@ public class HRMService {
Map<MapType, UserEntity> userEntityMap = new HashMap<MapType, UserEntity>();
if (checkUserExist(userEntityMap, userId)) {
UserEntity userEntity = userEntityMap.get(MapType.userEntityType);
if (!existUser2RoleWithUserId(userEntity.getId()))
if (!existUser2RoleWithUserId(userEntity.getId())) {
insertUser2Role(userEntity.getId(), GConstants.getValue("innerRoleId"));
}
}
}
......@@ -168,19 +129,22 @@ public class HRMService {
e.printStackTrace();
}
if (list != null && list.size() > 0)
if (list != null && list.size() > 0) {
return true;
}
return false;
}
private void insertUser2Role(String userId, String roleId) {
if (StringUtils.isBlank(userId))
if (StringUtils.isBlank(userId)) {
throw new IllegalArgumentException("userId 不能为空");
}
if (StringUtils.isBlank(roleId))
if (StringUtils.isBlank(roleId)) {
throw new IllegalArgumentException("roleId 不能为空");
}
List<Map<String, Object>> columnMapList = new ArrayList<Map<String, Object>>();
......@@ -213,28 +177,9 @@ public class HRMService {
e.printStackTrace();
}
// if (list != null && list.size() > 1)
// throw new IllegalRecordException("记录应该只有一条,但存在多条", UserEntity.class);
//
// if (list != null && list.size() == 1) {
// userEntityMap.put(MapType.userEntityType, list.get(0));
// return true;
// }
return false;
}
// private boolean isCompany(String parentId) {
// if ("0".equals(parentId))
// return true;
// return false;
// }
//
// private boolean isDepart(String parentId) {
// if (!"0".equals(parentId))
// return true;
// return false;
// }
private void insertUser(JSONObject jsonObject, String companyId, String officeId, final String modified) throws SQLException {
......@@ -246,15 +191,14 @@ public class HRMService {
columnMap.put("id", jsonObject.get("employeeId"));
columnMap.put("no", jsonObject.get("employeeId"));
columnMap.put("full_office_name", jsonObject.get("deptFullname"));
// columnMap.put("id", IdWorker.getNextId());
columnMap.put("name", jsonObject.get("name"));
columnMap.put("mobile", jsonObject.get("mobile"));
String workPhone = jsonObject.getString("workPhone");
columnMap.put("phone", getWorkPhone(workPhone));
columnMap.put("phone_number", Util.formatedWorkPhone(workPhone));
columnMap.put("email", jsonObject.get("email"));
columnMap.put("update_date", jsonObject.get("operatorDate"));
columnMap.put("office_id", officeId);
......@@ -262,55 +206,51 @@ public class HRMService {
columnMap.put("company_id", companyId);
columnMap.put("login_name", jsonObject.get("account"));
// String plain = GConstants.getValue("initPasswd");
// byte[] salt = com.foc.util.Digests.generateSalt(SALT_SIZE);
// byte[] hashPassword = com.foc.util.Digests.sha1(plain.getBytes(), salt, HASH_INTERATIONS);
columnMap.put("password", "");//Encodes.encodeHex(salt) + Encodes.encodeHex(hashPassword));
columnMap.put("password", "");
columnMap.put("create_by", "1");
columnMap.put("create_date", DateUtil.dateToStr(new Date(), DateUtil.TIMEF_FORMAT));
columnMap.put("update_by", "1");
columnMap.put("del_flag", "0");
columnMap.put("user_type", "2");
columnMap.put("photo", this.getPhoto(jsonObject.getString("sex")));
// columnMap.put("update_date", modified);
columnMapList.add(columnMap);
dBHandler.batchInsert(Constants.SYS_USER, columnMapList);
}
public String getPhoto(String sex){
if("0".equals(sex) || "F".equalsIgnoreCase(sex)){// 1:男,0:女
return GConstants.getValue("female.photo");// F:女
public String getPhoto(String sex) {
// 1:男,0:女
if ("0".equals(sex) || "F".equalsIgnoreCase(sex)) {
// F:女
return GConstants.getValue("female.photo");
}
return GConstants.getValue("male.photo");
}
private Map<String, Object> getUserProfile(JSONObject jsonObject) {
Map<String, Object> columnMap = new TreeMap<String, Object>();
columnMap.put("user_id", jsonObject.get("employeeId"));
columnMap.put("fax", jsonObject.get("fax"));
columnMap.put("fax", jsonObject.get("fax"));
String sex = jsonObject.getString("sex");
if("0".equals(sex) || "F".equalsIgnoreCase(sex)){// 1:男,0:女
columnMap.put("sex", "F");// F:女
// 1:男,0:女
if ("0".equals(sex) || "F".equalsIgnoreCase(sex)) {
// F:女
columnMap.put("sex", "F");
} else {
columnMap.put("sex", "M");// M:男
// M:男
columnMap.put("sex", "M");
}
// System.out.println(jsonObject);
columnMap.put("duty_name", "");//
if(jsonObject.containsKey("empPosts")){
columnMap.put("duty_name", "");
if (jsonObject.containsKey("empPosts")) {
JSONArray empPosts = jsonObject.getJSONArray("empPosts");
if(empPosts != null && empPosts.size()>0){
if (empPosts != null && empPosts.size() > 0) {
UserEmpPostEntity lastEmpPost = null;
List<UserEmpPostEntity> empPostList = JSON.parseArray(empPosts.toJSONString(), UserEmpPostEntity.class);
if(empPostList != null && empPostList.size()>0){
for(UserEmpPostEntity userEmpPost:empPostList){
if (empPostList != null && empPostList.size() > 0) {
for (UserEmpPostEntity userEmpPost : empPostList) {
try {
if(lastEmpPost == null
|| lastEmpPost.getUpdatedTime().getTime()< userEmpPost.getUpdatedTime().getTime()){
if (lastEmpPost == null
|| lastEmpPost.getUpdatedTime().getTime() < userEmpPost.getUpdatedTime().getTime()) {
lastEmpPost = userEmpPost;
}
} catch (Exception e) {
......@@ -318,14 +258,15 @@ public class HRMService {
}
}
}
if(lastEmpPost != null && StringUtils.isNotEmpty(lastEmpPost.getPostName())){
if (lastEmpPost != null && StringUtils.isNotEmpty(lastEmpPost.getPostName())) {
columnMap.put("duty_name", lastEmpPost.getPostName());
}
}
}
return columnMap;
}
private void insertUserProfile(JSONObject jsonObject) throws SQLException {
DBHandler dBHandler = new DBHandler();
......@@ -336,7 +277,7 @@ public class HRMService {
dBHandler.batchInsert(Constants.SYS_USER_PROFILES, columnMapList);
}
private void updateUserProfile(JSONObject jsonObject) throws SQLException {
DBHandler dBHandler = new DBHandler();
......@@ -349,19 +290,17 @@ public class HRMService {
}
/**
*
* @param resultOfficeEntity
* 返回最后需要关联到用户的OfficeEntity
* @param deptName
* @param parentId
* @param parentIds
* @throws SQLException
*/
public void insertOfficeByRecursive(Map<MapType, OfficeEntity> resultOfficeEntityMap, String deptName,
String parentId, String parentIds) throws SQLException {
String parentId, String parentIds) throws SQLException {
if (StringUtils.isBlank(deptName))
if (StringUtils.isBlank(deptName)) {
return;
}
String firstDeptName = null;
String subDeptName = null;
......@@ -394,16 +333,17 @@ public class HRMService {
insertedOfficeEntity.getParentIds() + "," + insertedOfficeEntity.getId());
}
}
private boolean isOfficeExistLastDept(String firstDeptName, String subDeptName) {
if (StringUtils.isNotBlank(firstDeptName) && StringUtils.isBlank(subDeptName))
if (StringUtils.isNotBlank(firstDeptName) && StringUtils.isBlank(subDeptName)) {
return true;
}
return false;
}
private void setResultOfficeEntityMap(Map<MapType, OfficeEntity> resultOfficeEntityMap, OfficeEntity officeEntity) {
if (officeEntity == null) {
// System.out.println(1);
}
resultOfficeEntityMap.put(MapType.resultOfficeEntityType, officeEntity);
}
......@@ -428,7 +368,7 @@ public class HRMService {
columnMap.put("type", "1");
} else {
columnMap.put("parent_id", parentId);
columnMap.put("parent_ids", parentIds+",");
columnMap.put("parent_ids", parentIds + ",");
columnMap.put("type", "2");
}
columnMap.put("area_id", GConstants.getValue("default.area.id", "1"));
......@@ -449,7 +389,7 @@ public class HRMService {
}
private boolean checkOfficeExist(Map<MapType, OfficeEntity> officeEntityMap, String firstDeptName,
String parentId) {
String parentId) {
OfficeEntity resultOfficeEntity = null;
try {
......@@ -472,17 +412,16 @@ public class HRMService {
ResultSetHandler<List<OfficeEntity>> handler = new BeanListHandler<OfficeEntity>(OfficeEntity.class);
String query = "SELECT *, parent_id as parentId, parent_ids as parentIds FROM sys_office WHERE 1 = 1";
if (parentId == null)
parentId = "0"; // 查询顶级部门
if (parentId == null) {
// 查询顶级部门
parentId = "0";
}
query += " and parent_id ='" + parentId + "' and name = '" + firstDeptName + "'";
// System.out.println(query);
List<OfficeEntity> list = this.run.query(query, handler);
if (list.isEmpty())
if (list.isEmpty()) {
return null;
// if (list.size() > 1)
// throw new IllegalRecordException("记录应该只有一条,但存在多条", OfficeEntity.class);
}
return list.get(0);
}
......@@ -490,124 +429,46 @@ public class HRMService {
String deptFullname = (String) jsonObject.get("deptFullname");
if (StringUtils.isNotBlank(deptFullname)){
/*
Map<MapType, OfficeEntity> resultOfficeEntityMap = new HashMap<MapType, OfficeEntity>();
insertOfficeByRecursive(resultOfficeEntityMap, deptFullname, null, null);
OfficeEntity resultOfficeEntity = resultOfficeEntityMap.get(MapType.resultOfficeEntityType);
if (resultOfficeEntity == null) {
throw new IllegalStateException("OfficeEntity 不应为空");
}
String companyId = null;
if (isCompany(resultOfficeEntity.getParentId()))
companyId = resultOfficeEntity.getId();
else if (isDepart(resultOfficeEntity.getParentId()))
companyId = resultOfficeEntity.getParentIds().split(",")[1];
String officeId = resultOfficeEntity.getId();
*/
if (StringUtils.isNotBlank(deptFullname)) {
OfficeEntity office = this.officeService.addOffice(deptFullname);
String fullName = deptFullname.replaceAll("[\\+|\\s+]", "");
String[] species = fullName.split("-");
OfficeEntity company = this.officeService.getOfficeByName(species[0], null);
if(office == null || company == null)
return ;
System.out.println("公司【"+company.getId()+"】部门【"+office.getId()+"】");
if (office == null || company == null) {
return;
}
System.out.println("公司【" + company.getId() + "】部门【" + office.getId() + "】");
updateUser(lastUserInfo, jsonObject, company.getId(), office.getId(), userId, modified);
}
// insertUser2RoleData(loginName);
}
// private void mergeOffice(JSONObject jsonObject) throws SQLException {
//
// String officeId = (String)jsonObject.get("nodeId");
// if(checkOfficeExist(officeId))
// updateOffice(jsonObject);
// else
// insertOffice(jsonObject);
//
// }
// private void insertOffice(JSONObject jsonObject) {
//
// List<Map<String,Object>> columnMapList = new
// ArrayList<Map<String,Object>>();
//
// Map<String,Object> columnMap = new TreeMap<String,Object>();
// //字段和值绑定
// columnMap.put("id", jsonObject.get("nodeId"));
// columnMap.put("name", jsonObject.get("deptFullname"));
// columnMap.put("parent_id", "0");
// columnMap.put("parent_ids", "0,");
//
// columnMap.put("sort", "30");
// columnMap.put("type", "1");
// columnMap.put("grade", "1");
// columnMap.put("create_by", "1");
// columnMap.put("create_date", DateUtil.dateToStr(new Date(),
// DateUtil.TIMEF_FORMAT));
// columnMap.put("update_by", "1");
// columnMap.put("update_date", DateUtil.dateToStr(new Date(),
// DateUtil.TIMEF_FORMAT));
// columnMap.put("del_flag", "0");
//
// columnMapList.add(columnMap);
//
// //批量插入数据
// DBHandler dBHandler = new DBHandler();
// dBHandler.batchInsert("sys_office", columnMapList);
// }
// private void updateOffice(JSONObject jsonObject) {
//
// DBHandler dBHandler = new DBHandler();
// final String tableName = "sys_office";
// Map<String, Object> columnMap = new TreeMap<String, Object>();
// Map<String, Object> whereMap = new TreeMap<String, Object>();
//
// // 字段和值绑定
// columnMap.put("name", jsonObject.get("deptFullname"));
//
// whereMap.put("id", jsonObject.get("nodeId"));
//
// dBHandler.update(tableName, columnMap, whereMap);
// }
// private boolean checkOfficeExist(String officeId) throws SQLException {
// ResultSetHandler<List<OfficeEntity>> handler = new
// BeanListHandler<OfficeEntity>(OfficeEntity.class);
// String query = "SELECT * FROM sys_office WHERE id ='" + officeId + "'";
// List<OfficeEntity> list = this.run.query(query, handler);
//
// if (list != null && list.size() > 0)
// return true;
// return false;
// }
public String getWorkPhone(String workPhone) {
if(StringUtils.isNotBlank(workPhone)){
if (StringUtils.isNotBlank(workPhone)) {
workPhone = workPhone.replaceAll("[^\\d|+]+", "");
if(workPhone.length() < 7 || workPhone.matches("0+")){
if (workPhone.length() < 7 || workPhone.matches("0+")) {
workPhone = "";
} else if(workPhone.startsWith("0") == false){// 非0开头的号码需要处理
if(workPhone.startsWith("1") && workPhone.length() == 11){
// 非0开头的号码需要处理
} else if (workPhone.startsWith("0") == false) {
if (workPhone.startsWith("1") && workPhone.length() == 11) {
workPhone = "";
}
}
if(workPhone.startsWith("+") == false){// 外国号码前面加拨3个000
// 外国号码前面加拨3个000
if (workPhone.startsWith("+") == false) {
workPhone = workPhone.replace("+", "");
}
} else {
workPhone = "";
}
return workPhone;
}
private void updateUser(UserEntity lastUserInfo, JSONObject jsonObject, String companyId, String officeId, String userId, final String modified) {
DBHandler dBHandler = new DBHandler();
Map<String, Object> columnMap = new TreeMap<String, Object>();
......@@ -619,8 +480,9 @@ public class HRMService {
columnMap.put("full_office_name", jsonObject.get("deptFullname"));
columnMap.put("name", jsonObject.get("name"));
columnMap.put("mobile", jsonObject.get("mobile"));
if(0 == lastUserInfo.getHuaweiUnicall()){// 座机号与HRM的需要同步
// 座机号与HRM的需要同步
if (0 == lastUserInfo.getHuaweiUnicall()) {
String workPhone = jsonObject.getString("workPhone");
columnMap.put("phone", getWorkPhone(workPhone));
columnMap.put("phone_number", Util.formatedWorkPhone(workPhone));
......@@ -632,15 +494,10 @@ public class HRMService {
columnMap.put("company_id", companyId);
columnMap.put("login_name", jsonObject.get("account"));
// columnMap.put("photo", GConstants.getValue("photo"));
if(StringUtils.isEmpty(lastUserInfo.getPhoto())){
if (StringUtils.isEmpty(lastUserInfo.getPhoto())) {
columnMap.put("photo", this.getPhoto(jsonObject.getString("sex")));
}
// columnMap.put("update_date", modified);
whereMap.put("id", userId);
dBHandler.update(Constants.SYS_USER, columnMap, whereMap);
}
......@@ -649,11 +506,11 @@ public class HRMService {
ResultSetHandler<List<UserEntity>> handler = new BeanListHandler<UserEntity>(UserEntity.class);
String query = "SELECT *,huawei_unicall AS huaweiUnicall FROM sys_user WHERE id ='" + userId + "'";
// System.out.println(query);
List<UserEntity> list = this.run.query(query, handler);
if (list != null && list.size() > 0)
if (list != null && list.size() > 0) {
return list.get(0);
}
return null;
}
......@@ -667,6 +524,7 @@ public class HRMService {
return true;
return false;
}
private enum MapType {
resultOfficeEntityType, existOfficeEntityType, userEntityType;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment