|
|
@ -5,21 +5,28 @@ 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.dto.EmployeeDto; |
|
|
|
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.CodeEnum; |
|
|
|
import com.ccsens.util.RedisUtil; |
|
|
|
import com.ccsens.util.RestTemplateUtil; |
|
|
|
import com.ccsens.util.WebConstant; |
|
|
|
import com.ccsens.util.enterprisewx.WeiXinConstant; |
|
|
|
import com.ccsens.util.exception.BaseException; |
|
|
|
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 +45,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 ) { |
|
|
@ -64,7 +79,26 @@ public class WeiXinService implements IWeiXinService { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void getSuiteToken(String suiteId, String suiteTicket) { |
|
|
|
public void bindUser(EmployeeDto.Bind bind) { |
|
|
|
log.info("用户绑定:{}", bind); |
|
|
|
EmployeeExample example = new EmployeeExample(); |
|
|
|
example.createCriteria().andUseridEqualTo(bind.getUserid()); |
|
|
|
List<Employee> employees = employeeDao.selectByExample(example); |
|
|
|
log.info("有无用户信息"); |
|
|
|
if (CollectionUtil.isEmpty(employees)) { |
|
|
|
throw new BaseException(CodeEnum.PARAM_ERROR); |
|
|
|
} |
|
|
|
Employee employee = employees.get(0); |
|
|
|
if (employee.getTallUserId() == null || employee.getTallUserId() == 0) { |
|
|
|
Employee bindEmployee = new Employee(); |
|
|
|
bindEmployee.setId(employee.getId()); |
|
|
|
bindEmployee.setTallUserId(bind.getTallUserId()); |
|
|
|
employeeDao.updateByPrimaryKeySelective(bindEmployee); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public String getSuiteToken(String suiteId, String suiteTicket) { |
|
|
|
log.info("获取suiteToken, suiteId:{}, suiteTicket:{}", suiteId, suiteTicket); |
|
|
|
String key = WeiXinConstant.getSuiteAccessToken(suiteId); |
|
|
|
String suiteAccessToken = (String)redisUtil.get(key); |
|
|
@ -75,7 +109,7 @@ public class WeiXinService implements IWeiXinService { |
|
|
|
int expireTime = 1200; |
|
|
|
if (expire > expireTime) { |
|
|
|
log.info("有效时长大于1200,无需更新"); |
|
|
|
return; |
|
|
|
return suiteAccessToken; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -89,23 +123,26 @@ public class WeiXinService implements IWeiXinService { |
|
|
|
if (!WeiXinConstant.pageResult(result)) { |
|
|
|
log.info("获取suite_ticket异常"); |
|
|
|
// TODO
|
|
|
|
return; |
|
|
|
return null; |
|
|
|
} |
|
|
|
redisUtil.set(key, result.getString("suite_access_token"), result.getInteger("expires_in")); |
|
|
|
return result.getString("suite_access_token"); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void savePermanentCode(String authCode) { |
|
|
|
public HealthAuth savePermanentCode(String authCode, String suiteAccessToken) { |
|
|
|
//授权成功通知
|
|
|
|
Map<String, String> params = new HashMap<>(); |
|
|
|
params.put("auth_code", authCode); |
|
|
|
String result = RestTemplateUtil.postBody(WeiXinConstant.GET_PERMANENT_CODE, params); |
|
|
|
String url = WeiXinConstant.GET_PERMANENT_CODE + " ?suite_access_token=" + suiteAccessToken; |
|
|
|
log.info("获取永久授权参数, auth_code:{}, suite:{}", authCode, suiteAccessToken); |
|
|
|
String result = RestTemplateUtil.postBody(url, params); |
|
|
|
log.info("获取永久授权返回:{}", result); |
|
|
|
JSONObject json = JSONObject.parseObject(result); |
|
|
|
if (json.getInteger(WeiXinConstant.ERR_CODE).intValue() != 0) { |
|
|
|
if (json.getInteger(WeiXinConstant.ERR_CODE) !=null && json.getIntValue(WeiXinConstant.ERR_CODE) != 0) { |
|
|
|
log.error("获取永久授权码异常:{}", result); |
|
|
|
//TODO 异常如何处理
|
|
|
|
return; |
|
|
|
return null; |
|
|
|
} |
|
|
|
HealthAuth auth = initAuth(json); |
|
|
|
HealthAuthAgent agent = initAuthAgent(json); |
|
|
@ -114,22 +151,148 @@ public class WeiXinService implements IWeiXinService { |
|
|
|
if (agent != null) { |
|
|
|
healthAuthAgentMapper.insertSelective(agent); |
|
|
|
} |
|
|
|
return auth; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
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); |
|
|
|
} |
|
|
|
}); |
|
|
|
// 保存员工信息
|
|
|
|
insertBatchEmployee(employees, des); |
|
|
|
} |
|
|
|
|
|
|
|
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 ); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 初始化永久授权信息 |
|
|
|
* @param json |
|
|
|