|
|
@ -5,21 +5,25 @@ import cn.hutool.core.lang.Snowflake; |
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.ccsens.health.bean.po.Department; |
|
|
|
import com.ccsens.health.bean.po.HealthAuth; |
|
|
|
import com.ccsens.health.bean.po.HealthAuthAgent; |
|
|
|
import com.ccsens.health.bean.po.*; |
|
|
|
import com.ccsens.health.persist.dao.DepartmentDao; |
|
|
|
import com.ccsens.health.persist.dao.DepartmentEmployeeDao; |
|
|
|
import com.ccsens.health.persist.dao.EmployeeDao; |
|
|
|
import com.ccsens.health.persist.mapper.HealthAuthAgentMapper; |
|
|
|
import com.ccsens.health.persist.mapper.HealthAuthMapper; |
|
|
|
import com.ccsens.util.RedisUtil; |
|
|
|
import com.ccsens.util.RestTemplateUtil; |
|
|
|
import com.ccsens.util.WebConstant; |
|
|
|
import com.ccsens.util.enterprisewx.WeiXinConstant; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.scheduling.annotation.Async; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.PlatformTransactionManager; |
|
|
|
import org.springframework.transaction.TransactionDefinition; |
|
|
|
import org.springframework.transaction.TransactionException; |
|
|
|
import org.springframework.transaction.TransactionStatus; |
|
|
|
import org.springframework.transaction.annotation.Propagation; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.springframework.transaction.support.DefaultTransactionDefinition; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.util.ArrayList; |
|
|
@ -38,8 +42,16 @@ public class WeiXinService implements IWeiXinService { |
|
|
|
private HealthAuthMapper healthAuthMapper; |
|
|
|
@Resource |
|
|
|
private HealthAuthAgentMapper healthAuthAgentMapper; |
|
|
|
@Autowired |
|
|
|
@Resource |
|
|
|
private RedisUtil redisUtil; |
|
|
|
@Resource |
|
|
|
private PlatformTransactionManager transactionManager; |
|
|
|
@Resource |
|
|
|
private DepartmentDao departmentDao; |
|
|
|
@Resource |
|
|
|
private EmployeeDao employeeDao; |
|
|
|
@Resource |
|
|
|
private DepartmentEmployeeDao departmentEmployeeDao; |
|
|
|
|
|
|
|
@Override |
|
|
|
public String getAccessToken(String suiteId, String corpId, String permanent_code ) { |
|
|
@ -120,14 +132,139 @@ public class WeiXinService implements IWeiXinService { |
|
|
|
public void initDepartment(String accessToken) { |
|
|
|
Map<String, Object> param = new HashMap<>(); |
|
|
|
param.put("access_token", accessToken); |
|
|
|
List departmentList = (List) RestTemplateUtil.getForEntity(WeiXinConstant.DEPARTMENT_LIST, param, List.class); |
|
|
|
log.info("部門信息:{}", departmentList); |
|
|
|
JSONObject result = (JSONObject) RestTemplateUtil.getForEntity(WeiXinConstant.DEPARTMENT_LIST, param, JSONObject.class); |
|
|
|
log.info("部門信息:{}", result); |
|
|
|
if (!WeiXinConstant.pageResult(result)) { |
|
|
|
//TODO 读取部门信息异常
|
|
|
|
return; |
|
|
|
} |
|
|
|
JSONArray departmentList = result.getJSONArray("department"); |
|
|
|
if (CollectionUtil.isEmpty(departmentList)) { |
|
|
|
return; |
|
|
|
} |
|
|
|
List<Department> departments = new ArrayList<>(); |
|
|
|
departmentList.forEach(obj -> { |
|
|
|
JSONObject json = (JSONObject) obj; |
|
|
|
Department department = json.toJavaObject(Department.class); |
|
|
|
departments.add(department); |
|
|
|
}); |
|
|
|
|
|
|
|
insertBatchDepartment(departments); |
|
|
|
//初始化員工信息
|
|
|
|
departments.forEach(department -> { |
|
|
|
initEmployee(accessToken, department); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 初始化员工信息 |
|
|
|
* @param accessToken |
|
|
|
* @param department |
|
|
|
*/ |
|
|
|
private void initEmployee(String accessToken, Department department) { |
|
|
|
// 查询员工信息
|
|
|
|
Map<String, Object> params = new HashMap<>(); |
|
|
|
params.put("access_token", accessToken); |
|
|
|
params.put("department_id", department.getId()); |
|
|
|
params.put("fetch_child", 0); |
|
|
|
JSONObject userResult = (JSONObject) RestTemplateUtil.getForEntity(WeiXinConstant.USER_LIST, params, JSONObject.class); |
|
|
|
if (WeiXinConstant.pageResult(userResult)) { |
|
|
|
log.error("查询员工信息异常:{},{}", params, userResult); |
|
|
|
return; |
|
|
|
} |
|
|
|
JSONArray userlist = userResult.getJSONArray("userlist"); |
|
|
|
if (CollectionUtil.isEmpty(userlist)) { |
|
|
|
return; |
|
|
|
} |
|
|
|
//解析员工信息
|
|
|
|
List<Employee> employees = new ArrayList<>(); |
|
|
|
List<DepartmentEmployee> des = new ArrayList<>(); |
|
|
|
userlist.forEach(obj -> { |
|
|
|
JSONObject user = (JSONObject)obj; |
|
|
|
Employee employee = user.toJavaObject(Employee.class); |
|
|
|
employee.setId(snowflake.nextId()); |
|
|
|
employees.add(employee); |
|
|
|
JSONArray departmentIds = user.getJSONArray("department"); |
|
|
|
JSONArray orders = user.getJSONArray("order"); |
|
|
|
for (int i = 0; i < departmentIds.size(); i++) { |
|
|
|
DepartmentEmployee de = new DepartmentEmployee(); |
|
|
|
de.setId(snowflake.nextId()); |
|
|
|
de.setEmployeeId(employee.getId()); |
|
|
|
de.setDepartmentId((Long)departmentIds.get(0)); |
|
|
|
de.setSort(i); |
|
|
|
des.add(de); |
|
|
|
} |
|
|
|
}); |
|
|
|
// 保存员工信息
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private void insertBatchEmployee(List<Employee> employees, List<DepartmentEmployee> des) { |
|
|
|
TransactionStatus status = getTransactionStatus(); |
|
|
|
int empSize = employees.size(); |
|
|
|
int deSize = des.size(); |
|
|
|
int once = 100; |
|
|
|
for (int i = 0; i < empSize; i+=once) { |
|
|
|
int end = i+once > empSize ? empSize : i+once; |
|
|
|
try { |
|
|
|
employeeDao.insertBatch(employees.subList(i, end)); |
|
|
|
//提交事务
|
|
|
|
transactionManager.commit( status ); |
|
|
|
} catch (TransactionException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
log.error("存储员工{}时发生异常:{}", employees.subList(i,end), e); |
|
|
|
transactionManager.rollback(status); |
|
|
|
} |
|
|
|
} |
|
|
|
for (int i = 0; i < deSize; i+=once) { |
|
|
|
int end = i+once > deSize ? deSize : i+once; |
|
|
|
try { |
|
|
|
departmentEmployeeDao.insertBatch(des.subList(i, end)); |
|
|
|
//提交事务
|
|
|
|
transactionManager.commit( status ); |
|
|
|
} catch (TransactionException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
log.error("存儲部門员工{}時發發生異常:{}", des.subList(i,end), e); |
|
|
|
transactionManager.rollback(status); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 批量保存部門信息 |
|
|
|
* @param departments |
|
|
|
*/ |
|
|
|
private void insertBatchDepartment(List<Department> departments) { |
|
|
|
TransactionStatus status = getTransactionStatus(); |
|
|
|
int size = departments.size(); |
|
|
|
int once = 100; |
|
|
|
for (int i = 0; i < size; i+=once) { |
|
|
|
int end = i+once > size ? size : i+once; |
|
|
|
try { |
|
|
|
departmentDao.insertBatch(departments.subList(i, end)); |
|
|
|
//提交事务
|
|
|
|
transactionManager.commit( status ); |
|
|
|
} catch (TransactionException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
log.error("存儲部門{}時發發生異常:{}", departments.subList(i,end), e); |
|
|
|
transactionManager.rollback(status); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 手动开启事务,获取事务状态 |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
private TransactionStatus getTransactionStatus() { |
|
|
|
//开启手动事务
|
|
|
|
DefaultTransactionDefinition def = new DefaultTransactionDefinition(); |
|
|
|
// 事务隔离级别,开启新事务
|
|
|
|
def.setPropagationBehavior( TransactionDefinition.PROPAGATION_REQUIRES_NEW ); |
|
|
|
//获取事务状态,并开启事务,相当于transation.begin();
|
|
|
|
return transactionManager.getTransaction( def ); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|