Browse Source

企业微信

master
zhizhi wu 5 years ago
parent
commit
1783603f4f
  1. 15
      health/src/main/java/com/ccsens/health/persist/dao/DepartmentDao.java
  2. 14
      health/src/main/java/com/ccsens/health/persist/dao/DepartmentEmployeeDao.java
  3. 8
      health/src/main/java/com/ccsens/health/persist/dao/EmployeeDao.java
  4. 153
      health/src/main/java/com/ccsens/health/service/WeiXinService.java
  5. 17
      health/src/main/resources/mapper_dao/DepartmentDao.xml
  6. 17
      health/src/main/resources/mapper_dao/DepartmentEmployeeDao.xml
  7. 19
      health/src/main/resources/mapper_dao/EmployeeDao.xml
  8. 1
      util/src/main/java/com/ccsens/util/enterprisewx/WeiXinConstant.java

15
health/src/main/java/com/ccsens/health/persist/dao/DepartmentDao.java

@ -0,0 +1,15 @@
package com.ccsens.health.persist.dao;
import com.ccsens.health.bean.po.Department;
import com.ccsens.health.persist.mapper.DepartmentMapper;
import java.util.List;
public interface DepartmentDao extends DepartmentMapper {
/**
* 批量添加部門信息
* @param departmentList
*/
void insertBatch(List<Department> departmentList);
}

14
health/src/main/java/com/ccsens/health/persist/dao/DepartmentEmployeeDao.java

@ -0,0 +1,14 @@
package com.ccsens.health.persist.dao;
import com.ccsens.health.bean.po.DepartmentEmployee;
import com.ccsens.health.persist.mapper.DepartmentEmployeeMapper;
import java.util.List;
public interface DepartmentEmployeeDao extends DepartmentEmployeeMapper {
/**
* 批量添加部門员工信息
* @param des
*/
void insertBatch(List<DepartmentEmployee> des);
}

8
health/src/main/java/com/ccsens/health/persist/dao/EmployeeDao.java

@ -1,8 +1,16 @@
package com.ccsens.health.persist.dao;
import com.ccsens.health.bean.po.Employee;
import com.ccsens.health.persist.mapper.EmployeeMapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface EmployeeDao extends EmployeeMapper {
/**
* 批量添加部門信息
* @param employees
*/
void insertBatch(List<Employee> employees);
}

153
health/src/main/java/com/ccsens/health/service/WeiXinService.java

@ -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 );
}
/**

17
health/src/main/resources/mapper_dao/DepartmentDao.xml

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ccsens.health.persist.dao.DepartmentDao">
<insert id="insertBatch" parameterType="ArrayList">
insert into t_department (id, name,
name_en, parentid, order
)
values
<foreach collection="list" item="item" separator=",">
(#{item.id,jdbcType=BIGINT}, #{item.name,jdbcType=VARCHAR},#{item.nameEn,jdbcType=VARCHAR},
#{item.parentid,jdbcType=VARCHAR}, #{item.order,jdbcType=INTEGER}
)
</foreach>
</insert>
</mapper>

17
health/src/main/resources/mapper_dao/DepartmentEmployeeDao.xml

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ccsens.health.persist.dao.DepartmentEmployeeDao">
<insert id="insertBatch" parameterType="ArrayList">
insert into t_department_employee (id, department_id, employee_id,
order, sort
)
values
<foreach collection="list" item="item" separator=",">
(#{item.id,jdbcType=BIGINT}, #{item.departmentId,jdbcType=BIGINT},
#{item.employeeId,jdbcType=BIGINT},
#{item.order,jdbcType=INTEGER}, #{item.sort,jdbcType=INTEGER})
</foreach>
</insert>
</mapper>

19
health/src/main/resources/mapper_dao/EmployeeDao.xml

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ccsens.health.persist.dao.EmployeeDao">
<insert id="insertBatch" parameterType="ArrayList">
insert into t_employee (id, userid, name,
gender, status, address,
hide_mobile, english_name, tall_user_id
)
values
<foreach collection="list" item="item" separator=",">
(#{item.id,jdbcType=BIGINT}, #{item.userid,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR},
#{item.gender,jdbcType=TINYINT}, #{item.status,jdbcType=TINYINT}, #{item.address,jdbcType=VARCHAR},
#{item.hideMobile,jdbcType=TINYINT}, #{item.englishName,jdbcType=VARCHAR}, #{item.tallUserId,jdbcType=BIGINT}
)
</foreach>
</insert>
</mapper>

1
util/src/main/java/com/ccsens/util/enterprisewx/WeiXinConstant.java

@ -27,6 +27,7 @@ public class WeiXinConstant {
public static final String GET_SUITE_TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/service/get_suite_token";
public static final String GET_CORP_TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/service/get_corp_token";
public static final String DEPARTMENT_LIST = "https://qyapi.weixin.qq.com/cgi-bin/department/list";
public static final String USER_LIST = "https://qyapi.weixin.qq.com/cgi-bin/user/list";
/**

Loading…
Cancel
Save