Commit 0849373c by 徐高华

企微托管

parent 377f8a69
......@@ -122,5 +122,11 @@ public interface HaobanTimerApiService {
public void halfTimer(String wxEnterpriseId) ;
/**
* 校验是否登录中
* @param params
*/
void openStaffTimer(String params) ;
}
package com.gic.haoban.manage.service.dao.mapper;
import com.gic.haoban.manage.api.dto.OpenStaffDTO;
import com.gic.haoban.manage.api.dto.OpenStaffListDTO;
import com.gic.haoban.manage.api.dto.StaffClerkRelationDTO;
import com.gic.haoban.manage.api.qdto.OpenStaffPageQDTO;
......@@ -48,11 +46,14 @@ public interface OpenStaffMapper {
List<StaffClerkRelationDTO> listOpenStaffByStore(@Param("wxEnterpriseId")String wxEnterpriseId, @Param("enterpriseId")String enterpriseId, @Param("list")List<String> storeIdList);
List<String> listAll(@Param("wxEnterpriseId")String wxEnterpriseId, @Param("enterpriseId")String enterpriseId) ;
List<TabOpenStaff> listAllOnline(@Param("wxEnterpriseId")String wxEnterpriseId, @Param("enterpriseId")String enterpriseId) ;
List<String> listOpenStoreId(@Param("wxEnterpriseId")String wxEnterpriseId, @Param("enterpriseId")String enterpriseId);
void updateStopTime(@Param("id") Long openStaffId, @Param("type") int type, @Param("time") int time);
int getNum(@Param("wxEnterpriseId")String wxEnterpriseId, @Param("enterpriseId")String enterpriseId , @Param("status")int status) ;
int updateByAutoLogin(@Param("id") Long openStaffId ,@Param("uuid") String uuid) ;
}
\ No newline at end of file
......@@ -4,6 +4,8 @@ import java.io.Serializable;
public class QwOpenResultInitBO implements Serializable {
private boolean is_login ;
private String uuid ;
private String qrcode ;
......@@ -12,6 +14,14 @@ public class QwOpenResultInitBO implements Serializable {
private String qrcode_data ;
public boolean isIs_login() {
return is_login;
}
public void setIs_login(boolean is_login) {
this.is_login = is_login;
}
public String getQrcode_data() {
return qrcode_data;
}
......
......@@ -20,4 +20,14 @@ public interface OpenStaffService {
ServiceResponse<Page<OpenStaffListDTO>> page(OpenStaffPageQDTO qdto, BasePageInfo basePageInfo);
List<String> listAllStaffId(String wxEnterpriseId) ;
/**
* 自动登录 返回新的uuid
* @param openStaffId
* @param qwUserId
* @return
*/
String autoLogin(Long openStaffId , Long qwUserId) ;
void openStaffTimer();
}
......@@ -16,6 +16,7 @@ import com.gic.haoban.manage.api.dto.OpenStaffListDTO;
import com.gic.haoban.manage.api.dto.StaffClerkRelationDTO;
import com.gic.haoban.manage.api.dto.StoreClerkDTO;
import com.gic.haoban.manage.api.enums.NoticeMessageTypeEnum;
import com.gic.haoban.manage.api.enums.OpenStaffOfflineEnum;
import com.gic.haoban.manage.api.qdto.OpenStaffPageQDTO;
import com.gic.haoban.manage.api.service.notify.NoticeMessageApiService;
import com.gic.haoban.manage.api.util.notify.NoticeMessageUtil;
......@@ -23,6 +24,7 @@ import com.gic.haoban.manage.service.dao.mapper.OpenStaffMapper;
import com.gic.haoban.manage.service.dao.mapper.StaffMapper;
import com.gic.haoban.manage.service.entity.TabHaobanStaff;
import com.gic.haoban.manage.service.entity.TabOpenStaff;
import com.gic.haoban.manage.service.pojo.QwOpenResultInitBO;
import com.gic.haoban.manage.service.service.OpenStaffService;
import com.gic.haoban.manage.service.service.StaffClerkRelationService;
import com.gic.haoban.manage.service.util.OpenUtils;
......@@ -51,11 +53,11 @@ public class OpenStaffServiceImpl implements OpenStaffService {
@Autowired
private StoreService storeService;
@Autowired
private ClerkService clerkService ;
private ClerkService clerkService;
@Autowired
private NoticeMessageApiService noticeMessageApiService ;
private NoticeMessageApiService noticeMessageApiService;
@Autowired
private StaffMapper staffMapper ;
private StaffMapper staffMapper;
@Override
public TabOpenStaff save(TabOpenStaff tab) {
......@@ -82,7 +84,7 @@ public class OpenStaffServiceImpl implements OpenStaffService {
}
@Override
public void logout(String uuid, String loginRemark, int step , String noticeReason) {
public void logout(String uuid, String loginRemark, int step, String noticeReason) {
TabOpenStaff openStaff = this.getByUUID(uuid);
if (null == openStaff) {
return;
......@@ -101,26 +103,26 @@ public class OpenStaffServiceImpl implements OpenStaffService {
this.openStaffMapper.update(openStaff);
// 发送通知
if(StringUtils.isNotBlank(noticeReason)) {
log.info("发送退出登录通知,staffId={}",openStaff.getStaffId());
TabHaobanStaff staff = this.staffMapper.selectByPrimaryKey(openStaff.getStaffId()) ;
this.noticeMessageApiService.sendMessageForOpenStaff(openStaff.getEnterpriseId(),staff.getStaffName() , noticeReason);
if (StringUtils.isNotBlank(noticeReason)) {
log.info("发送退出登录通知,staffId={}", openStaff.getStaffId());
TabHaobanStaff staff = this.staffMapper.selectByPrimaryKey(openStaff.getStaffId());
this.noticeMessageApiService.sendMessageForOpenStaff(openStaff.getEnterpriseId(), staff.getStaffName(), noticeReason);
// 发送通知
Map<String, String> map = new HashMap<String, String>();
map.put("reason", noticeReason);
int messageType = NoticeMessageTypeEnum.HAOBAN_OPEN_STAFF_LOGOUT.getType();
NoticeMessageUtil.sendNoticeMessageByStaff(openStaff.getEnterpriseId(),openStaff.getStaffId(),"-1",messageType,null,map,null);
NoticeMessageUtil.sendNoticeMessageByStaff(openStaff.getEnterpriseId(), openStaff.getStaffId(), "-1", messageType, null, map, null);
}
}
@Override
public ServiceResponse<Page<OpenStaffListDTO>> page(OpenStaffPageQDTO qdto, BasePageInfo basePageInfo) {
log.info("分页查询条件={}",JSONObject.toJSONString(qdto));
log.info("分页查询条件={}", JSONObject.toJSONString(qdto));
String wxEnterpriseId = qdto.getWxEnterpriseId();
String enterpriseId = qdto.getEnterpriseId();
List<String> storeIdList = null;
if(CollectionUtils.isNotEmpty(qdto.getStoreGroupIdList())) {
storeIdList = this.getStoreIdList(enterpriseId,qdto.getStoreGroupIdList()) ;
if (CollectionUtils.isNotEmpty(qdto.getStoreGroupIdList())) {
storeIdList = this.getStoreIdList(enterpriseId, qdto.getStoreGroupIdList());
}
if (StringUtils.isNotBlank(qdto.getClerkId()) || CollectionUtils.isNotEmpty(storeIdList)) {
List<String> staffIds = staffClerkRelationService.listAllStaffIdForOpenStaff(enterpriseId, wxEnterpriseId, qdto.getClerkId(), storeIdList);
......@@ -131,48 +133,48 @@ public class OpenStaffServiceImpl implements OpenStaffService {
}
PageHelper.startPage(basePageInfo);
List<OpenStaffListDTO> list = this.openStaffMapper.list(qdto);
if(CollectionUtils.isEmpty(list)) {
if (CollectionUtils.isEmpty(list)) {
return ServiceResponse.success(new Page<>());
}
Set<String> staffIdList = list.stream().map(dto -> dto.getStaffId()).collect(Collectors.toSet());
List<StaffClerkRelationDTO> relationDTOList = staffClerkRelationService.listByWxEnterpriseIdAndStaffId(wxEnterpriseId, staffIdList);
relationDTOList = relationDTOList.stream().filter(o->o.getEnterpriseId().equals(enterpriseId)).collect(Collectors.toList()) ;
Map<String, ClerkDTO> clerkMap = new HashMap<>() ;
if(CollectionUtils.isNotEmpty(relationDTOList)) {
Set<String> clerkIdSet = relationDTOList.stream().map(o->o.getClerkId()).collect(Collectors.toSet()) ;
if(CollectionUtils.isNotEmpty(clerkIdSet)) {
relationDTOList = relationDTOList.stream().filter(o -> o.getEnterpriseId().equals(enterpriseId)).collect(Collectors.toList());
Map<String, ClerkDTO> clerkMap = new HashMap<>();
if (CollectionUtils.isNotEmpty(relationDTOList)) {
Set<String> clerkIdSet = relationDTOList.stream().map(o -> o.getClerkId()).collect(Collectors.toSet());
if (CollectionUtils.isNotEmpty(clerkIdSet)) {
List<ClerkDTO> clerkDTOS = this.clerkService.getClerkByIds(clerkIdSet);
clerkMap.putAll(clerkDTOS.stream().collect(Collectors.toMap(o -> o.getClerkId(), o -> o, (k1, k2) -> k1)));
}
}
log.info("导购={}", JSONObject.toJSONString(clerkMap));
for(OpenStaffListDTO item : list) {
String staffId = item.getStaffId() ;
List<StoreClerkDTO> clerkList = new ArrayList<>() ;
for (StaffClerkRelationDTO clerkRelation : relationDTOList){
if (clerkRelation.getStaffId().equals(staffId)){
String storeId = clerkRelation.getStoreId() ;
String clerkId = clerkRelation.getClerkId() ;
ClerkDTO clerkDTO = clerkMap.get(clerkId) ;
if(null != clerkDTO) {
for (OpenStaffListDTO item : list) {
String staffId = item.getStaffId();
List<StoreClerkDTO> clerkList = new ArrayList<>();
for (StaffClerkRelationDTO clerkRelation : relationDTOList) {
if (clerkRelation.getStaffId().equals(staffId)) {
String storeId = clerkRelation.getStoreId();
String clerkId = clerkRelation.getClerkId();
ClerkDTO clerkDTO = clerkMap.get(clerkId);
if (null != clerkDTO) {
StoreClerkDTO cs = new StoreClerkDTO();
cs.setClerkId(clerkId);
cs.setClerkName(clerkDTO.getClerkName());
cs.setClerkCode(clerkDTO.getClerkCode());
StoreDTO store = this.storeService.getStore(storeId);
if(null != store) {
if (null != store) {
cs.setStoreId(storeId);
cs.setStoreName(store.getStoreName());
cs.setStoreCode(store.getStoreCode());
}
clerkList.add(cs) ;
clerkList.add(cs);
}
}
}
item.setClerkList(clerkList);
}
Page<OpenStaffListDTO> retPage = PageHelperUtils.changePageHelperToCurrentPage(list, OpenStaffListDTO.class);
log.info("分页查询结果={}",JSONObject.toJSONString(retPage,true));
log.info("分页查询结果={}", JSONObject.toJSONString(retPage, true));
return ServiceResponse.success(retPage);
}
......@@ -201,4 +203,52 @@ public class OpenStaffServiceImpl implements OpenStaffService {
return searchStoreIds;
}
@Override
public String autoLogin(Long openStaffId, Long qwUserId) {
ServiceResponse<QwOpenResultInitBO> initResp = OpenUtils.init(qwUserId, false);
log.info("自动登录-初始化={},{}", qwUserId, JSONObject.toJSONString(initResp));
if (initResp.isSuccess() && StringUtils.isNotBlank(initResp.getResult().getUuid())) {
QwOpenResultInitBO bo = initResp.getResult();
String newUuid = bo.getUuid();
if (bo.isIs_login()) {
this.openStaffMapper.updateByAutoLogin(qwUserId, newUuid);
return newUuid;
}
boolean autLogin = OpenUtils.autoLogin(newUuid);
if (autLogin) {
this.openStaffMapper.updateByAutoLogin(qwUserId, newUuid);
return newUuid;
}
}
return null;
}
/**
* 1. 首次登录流程
* 初始化 扫码登录
* <p>
* 2. 自动重连流程
* 如果登录了很多号,我结束服务,重启服务后
* 初始化接口 vid传登陆过的账号userid 然后 初始化接口返回有个is_login=true 代表连上了如果是false就走自动登录接口
* 如果自动登录失败,获取二维码扫码登录
* <p>
* 3. 自动登录
* 如果一个号登录了,调用了退出接口,下次初始化接口vid填这个号的id然后调用自动登录接口,登录失败 走扫码
*/
@Override
public void openStaffTimer() {
log.info("定时检查托管状态");
List<TabOpenStaff> list = this.openStaffMapper.listAllOnline(null, null);
for (TabOpenStaff item : list) {
String uuid = item.getUuid();
boolean isLogin = OpenUtils.isLogin(uuid);
// 未登录
if (!isLogin) {
String autLogin = this.autoLogin(item.getOpenStaffId(), item.getQwUserId());
if (null == autLogin) {
this.logout(uuid, "定时检查异", 0, OpenStaffOfflineEnum.OFFLINE2.getDesc());
}
}
}
}
}
......@@ -7,7 +7,7 @@ import com.gic.haoban.manage.service.dao.mapper.TabHaobanExternalClerkRelatedMap
import com.gic.haoban.manage.service.dao.mapper.hm.WxUserAddLogMapper;
import com.gic.haoban.manage.service.entity.TabHaobanExternalClerkRelated;
import com.gic.haoban.manage.service.entity.hm.TabWxUserAddLog;
import com.gic.haoban.manage.service.service.QywxErrorLogService;
import com.gic.haoban.manage.service.service.*;
import com.gic.haoban.manage.service.service.chat.GroupChatDataService;
import com.gic.haoban.manage.service.util.DingUtils;
import com.gic.haoban.manage.service.util.HBQwMonitorUtils;
......@@ -16,16 +16,12 @@ import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.joda.time.DateTime;
import org.redisson.api.RMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.haoban.manage.api.service.HaobanTimerApiService;
import com.gic.haoban.manage.service.service.ExternalClerkRelatedService;
import com.gic.haoban.manage.service.service.ExternalMemberService;
import com.gic.haoban.manage.service.service.StaffDepartmentRelatedService;
import com.gic.haoban.manage.service.service.chat.GroupChatService;
import com.gic.haoban.manage.service.service.hm.HmQrcodeTempService;
......@@ -56,6 +52,8 @@ public class HaobanTimerApiServiceImpl implements HaobanTimerApiService {
private WxUserAddLogMapper wxUserAddLogMapper ;
@Autowired
private HaobanCommonMQApiService haobanCommonMQApiService ;
@Autowired
private OpenStaffService openStaffService ;
@Override
......@@ -178,4 +176,9 @@ public class HaobanTimerApiServiceImpl implements HaobanTimerApiService {
DingUtils.send("halfTimer告警,wxEnterpriseId="+wxEnterpriseId, null, false);
log.info("结束单向数据={}",wxEnterpriseId);
}
@Override
public void openStaffTimer(String params) {
this.openStaffService.openStaffTimer() ;
}
}
......@@ -30,6 +30,7 @@ import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
* 企微托管账号
......@@ -200,6 +201,18 @@ public class OpenStaffApiServiceImpl implements OpenStaffApiService {
qwUserId = openStaff.getQwUserId();
log.info("重新初始化");
this.openStaffService.logout(openStaff.getUuid(), "重新初始化", 0, null);
// 自动登录
String newUuid = this.openStaffService.autoLogin(openStaff.getOpenStaffId(),openStaff.getQwUserId()) ;
if(null != newUuid) {
log.info("自动登录成功");
openStaff.setAuthTime(new Date());
openStaff.setStatusFlag(1);
openStaff.setUuid(newUuid);
openStaff.setLoginRemark("自动登录成功");
this.openStaffMapper.update(openStaff);
OpenStaffDTO dto = EntityUtil.changeEntityByJSON(OpenStaffDTO.class, openStaff);
return ServiceResponse.success(dto);
}
}
if (null == openStaff) {
openStaff = new TabOpenStaff();
......@@ -207,7 +220,7 @@ public class OpenStaffApiServiceImpl implements OpenStaffApiService {
openStaff.setWxEnterpriseId(qdto.getWxEnterpriseId());
openStaff.setEnterpriseId(qdto.getEnterpriseId());
}
ServiceResponse<QwOpenResultInitBO> resp = OpenUtils.init(qwUserId);
ServiceResponse<QwOpenResultInitBO> resp = OpenUtils.init(qwUserId,true);
log.info("resp={}", JSONObject.toJSONString(resp));
QwOpenResultInitBO init = resp.getResult();
openStaff.setUuid(init.getUuid());
......@@ -276,13 +289,14 @@ public class OpenStaffApiServiceImpl implements OpenStaffApiService {
@Override
public List<String> listAllOnlineStaff() {
return this.openStaffMapper.listAll(null,null);
return this.listAllOnlineStaff(null, null);
}
@Override
public List<String> listAllOnlineStaff(String wxEnterpriseId, String enterpriseId) {
return this.openStaffMapper.listAll(wxEnterpriseId,enterpriseId);
List<TabOpenStaff> list = this.openStaffMapper.listAllOnline(wxEnterpriseId, enterpriseId);
return list.stream().map(o -> o.getStaffId()).collect(Collectors.toList());
}
@Override
......
......@@ -20,13 +20,13 @@ public class OpenUtils {
private static final String openHost = "http://47.94.7.218:8089/wxwork/%s";
public static ServiceResponse<QwOpenResultInitBO> init(Long qwUserId) {
public static ServiceResponse<QwOpenResultInitBO> init(Long qwUserId, boolean qrcodeFlag) {
JSONObject initJson = new JSONObject();
if (null != qwUserId) {
initJson.put("vid", qwUserId);
}
String initStr = HttpClient.sendPostJSON(String.format(openHost, "init"), initJson.toJSONString(), "utf-8");
logger.info("init={}", initStr);
logger.info("企微初始化,req={},{}", initJson.toJSONString(), initStr);
QwOpenResultBO initResult = result(initStr);
if (!initResult.isOk()) {
return ServiceResponse.failure("9999", initResult.getErrmsg());
......@@ -35,11 +35,17 @@ public class OpenUtils {
String uuid = initBO.getUuid();
// 设置回调
setCallback(uuid);
if (initBO.isIs_login()) {
logger.info("客户端是登录中的={}", qwUserId);
return ServiceResponse.success(initBO);
}
if (qrcodeFlag) {
// 获取二维码
ServiceResponse<QwOpenResultInitBO> response = getQrcode(uuid, initBO, 1);
if (!response.isSuccess()) {
return response;
}
}
return ServiceResponse.success(initBO);
}
......@@ -101,12 +107,31 @@ public class OpenUtils {
}
/**
* 自动登录
*
* @param uuid
*/
public static boolean autoLogin(String uuid) {
JSONObject json = new JSONObject();
json.put("uuid", uuid);
String backJson = HttpClient.sendPostJSON(String.format(openHost, "automaticLogin"), json.toJSONString(), "utf-8");
logger.info("自动登录={},uuid={}", backJson, uuid);
JSONObject resultJson = JSONObject.parseObject(backJson);
int errcode = resultJson.getIntValue("errcode");
if (errcode == 0) {
logger.info("自动登录成功={}");
return true;
}
return false;
}
/**
* 判断账号是否登录中
*
* @param uuid
* @return
*/
private static boolean isLogin(String uuid) {
public static boolean isLogin(String uuid) {
JSONObject json = new JSONObject();
json.put("uuid", uuid);
String backJson = HttpClient.sendPostJSON(String.format(openHost, "GetProfile"), json.toJSONString(), "utf-8");
......
......@@ -111,6 +111,10 @@
where open_staff_id = #{id}
</update>
<update id="updateByAutoLogin">
update tab_haoban_open_staff set uuid=#{uuid} , login_remark = '自动登录成功' where open_staff_id = #{id}
</update>
<delete id="deleteByPrimaryKey">
update tab_haoban_open_staff
set delete_flag = 1,
......@@ -229,8 +233,8 @@
and a.delete_flag = 0
</select>
<select id="listAll" resultType="java.lang.String">
select staff_id from tab_haoban_open_staff where
<select id="listAllOnline" resultMap="BaseResultMap">
select <include refid="Base_Column_List" /> from tab_haoban_open_staff where
status_flag = 1 and delete_flag = 0
<if test="null !=wxEnterpriseId">
and wx_enterprise_id = #{wxEnterpriseId}
......
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