增加公告同步信息

This commit is contained in:
459816669@qq.com 2021-03-14 16:26:29 +08:00
parent a6fb60a9e8
commit bb95bb5906
11 changed files with 279 additions and 83 deletions

View File

@ -8,12 +8,14 @@
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
<form class="form-horizontal m" id="form-notice-edit" th:object="${notice}">
<input id="noticeId" name="noticeId" th:field="*{noticeId}" type="hidden">
<input id="blackboardId" name="blackboardId" th:field="*{blackboardId}" type="hidden">
<div class="form-group">
<label class="col-sm-2 control-label is-required">公告标题:</label>
<div class="col-sm-10">
<input id="noticeTitle" name="noticeTitle" th:field="*{noticeTitle}" class="form-control" type="text" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">公告类型:</label>
<div class="col-sm-10">

View File

@ -31,7 +31,14 @@ public enum DingTalkListenerType {
WORK_RECORD_UPDATE(3,20,"更新待办"),
ASYNCSEND_V2(10,20,"发送钉钉消息")
ASYNCSEND_V2(10,20,"发送钉钉消息"),
BLACKBOARD_CREATE(1,30,"公告创建"),
BLACKBOARD_UPDATE(2,30,"公告更新"),
BLACKBOARD_DELETE(3,30,"公告删除")
;

View File

@ -87,4 +87,19 @@ public class BaseConstantUrl {
*/
public static final String ASYNCSEND_V2="https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2";
/**
* 创建公告
*/
public static final String BLACKBOARD_CREATE="https://oapi.dingtalk.com/topapi/blackboard/create";
/**
* 更新公告
*/
public static final String BLACKBOARD_UPDATE="https://oapi.dingtalk.com/topapi/blackboard/update";
/**
* 删除公告
*/
public static final String BLACKBOARD_DELETE="https://oapi.dingtalk.com/topapi/blackboard/delete";
}

View File

@ -6,18 +6,10 @@ import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.snow.common.constant.Constants;
import com.snow.common.enums.BusinessStatus;
import com.snow.common.enums.BusinessType;
import com.snow.common.enums.DingTalkSyncType;
import com.snow.common.utils.ServletUtils;
import com.snow.common.utils.StringUtils;
import com.snow.common.utils.spring.SpringUtils;
import com.snow.framework.util.ShiroUtils;
import com.snow.system.domain.SysDingtalkSyncLog;
import com.snow.system.domain.SysOperLog;
import com.snow.system.domain.SysUser;
import com.snow.system.service.ISysConfigService;
import com.snow.system.service.ISysOperLogService;
import com.snow.system.service.impl.SysConfigServiceImpl;
import com.snow.system.service.impl.SysDingtalkSyncLogServiceImpl;
import com.snow.system.service.impl.SysOperLogServiceImpl;
@ -39,7 +31,6 @@ public class BaseService {
private SysOperLogServiceImpl iSysOperLogService=SpringUtils.getBean("sysOperLogServiceImpl");
private SysDingtalkSyncLogServiceImpl sysDingtalkSyncLogService=SpringUtils.getBean("sysDingtalkSyncLogServiceImpl");
/**
* 获取token
* @return

View File

@ -0,0 +1,47 @@
package com.snow.dingtalk.listener;
import com.alibaba.fastjson.JSON;
import com.snow.common.enums.DingTalkListenerType;
import com.snow.common.utils.spring.SpringUtils;
import com.snow.dingtalk.model.WorkrecordAddRequest;
import com.snow.dingtalk.service.impl.BlackboardServiceImpl;
import com.snow.framework.web.domain.common.SysSendMessageDTO;
import com.snow.system.domain.SysNotice;
import com.snow.system.event.SyncEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @program: snow
* @description
* @author: 没用的阿吉
* @create: 2021-03-14 15:33
**/
@Component
@Slf4j
public class BlackboardService implements ISyncDingTalkInfo {
private BlackboardServiceImpl blackboardService=SpringUtils.getBean(BlackboardServiceImpl.class);
@Override
public void syncDingTalkInfoEvent(SyncEvent syncEvent) {
log.info("调用公告管理传入的原始参数:{}",JSON.toJSONString(syncEvent));
DingTalkListenerType eventType =(DingTalkListenerType) syncEvent.getT();
Integer code = eventType.getCode();
if(code.equals(DingTalkListenerType.BLACKBOARD_CREATE.getCode())){
SysNotice sysNotice=(SysNotice)syncEvent.getSource();
blackboardService.createBlackboard(sysNotice);
}
else if(code.equals(DingTalkListenerType.BLACKBOARD_UPDATE.getCode())){
SysNotice sysNotice=(SysNotice)syncEvent.getSource();
blackboardService.updateBlackboard(sysNotice);
}
else if(code.equals(DingTalkListenerType.BLACKBOARD_DELETE.getCode())){
String id=(String) syncEvent.getSource();
blackboardService.deleteBlackboard(id);
}
}
}

View File

@ -19,6 +19,7 @@ public class SyncDingTalkInfoFactory {
DingTalkListenerType dingTalkEnum = (DingTalkListenerType) syncEvent.getT();
Integer type = dingTalkEnum.getType();
if(type.equals(DingTalkListenerType.DEPARTMENT_CREATE.getType())){
return new DepartmentEventService();
}
@ -31,7 +32,9 @@ public class SyncDingTalkInfoFactory {
else if(type.equals(DingTalkListenerType.WORK_RECODE_CREATE.getType())){
return new WorkRecodeService();
}
else if(type.equals(DingTalkListenerType.BLACKBOARD_CREATE.getType())){
return new BlackboardService();
}
else {
throw new RuntimeException("不存在的监听类型");

View File

@ -0,0 +1,33 @@
package com.snow.dingtalk.service;
import com.snow.system.domain.SysNotice;
/**
* @program: snow
* @description
* @author: 没用的阿吉
* @create: 2021-03-14 15:16
**/
public interface BlackboardService {
/**
* 更新
* @param sysNotice
*/
String createBlackboard(SysNotice sysNotice);
/**
* 删除
* @param blackboardId
*/
void deleteBlackboard(String blackboardId);
/**
* 更新
* @param sysNotice
*/
void updateBlackboard(SysNotice sysNotice);
}

View File

@ -0,0 +1,134 @@
package com.snow.dingtalk.service.impl;
import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiBlackboardCreateRequest;
import com.dingtalk.api.request.OapiBlackboardDeleteRequest;
import com.dingtalk.api.request.OapiBlackboardUpdateRequest;
import com.dingtalk.api.response.OapiBlackboardCreateResponse;
import com.dingtalk.api.response.OapiBlackboardDeleteResponse;
import com.dingtalk.api.response.OapiBlackboardUpdateResponse;
import com.snow.common.exception.SyncDataException;
import com.snow.common.utils.spring.SpringUtils;
import com.snow.dingtalk.common.BaseConstantUrl;
import com.snow.dingtalk.common.BaseService;
import com.snow.dingtalk.service.BlackboardService;
import com.snow.system.domain.SysNotice;
import com.snow.system.domain.SysUser;
import com.snow.system.mapper.SysNoticeMapper;
import com.snow.system.service.impl.SysUserServiceImpl;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
/**
* @program: snow
* @description
* @author: 没用的阿吉
* @create: 2021-03-14 15:18
**/
@Service
@Slf4j
public class BlackboardServiceImpl extends BaseService implements BlackboardService {
private SysUserServiceImpl sysUserService=SpringUtils.getBean(SysUserServiceImpl.class);
private SysNoticeMapper sysNoticeService=SpringUtils.getBean(SysNoticeMapper.class);
@Override
public String createBlackboard(SysNotice sysNotice) {
DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.BLACKBOARD_CREATE);
OapiBlackboardCreateRequest req = new OapiBlackboardCreateRequest();
OapiBlackboardCreateRequest.OapiCreateBlackboardVo boardVoObj = new OapiBlackboardCreateRequest.OapiCreateBlackboardVo();
//设置公告管理员先写死
boardVoObj.setOperationUserid("manager4480");
//作者先写死
boardVoObj.setAuthor("没用的阿吉");
//保密等级0普通公告 20保密公告
boardVoObj.setPrivateLevel(0L);
//是否发送应用内钉提醒
boardVoObj.setDing(true);
OapiBlackboardCreateRequest.BlackboardReceiverOpenVo receiverOpenVoObj = new OapiBlackboardCreateRequest.BlackboardReceiverOpenVo();
receiverOpenVoObj.setUseridList(getUserDingId());
boardVoObj.setBlackboardReceiver(receiverOpenVoObj);
boardVoObj.setTitle(sysNotice.getNoticeTitle());
boardVoObj.setPushTop(true);
boardVoObj.setContent(sysNotice.getNoticeContent());
req.setCreateRequest(boardVoObj);
try {
OapiBlackboardCreateResponse response = client.execute(req, getDingTalkToken());
if(response.getErrcode()!=0){
throw new SyncDataException(JSON.toJSONString(req),response.getErrmsg());
}else {
//更新boardId 注意这个requestId不是公告id这个钉钉是真的垃圾添加成功不返回id还需要去查就先这样写吧后面再改
sysNotice.setBlackboardId(response.getRequestId());
sysNoticeService.updateNotice(sysNotice);
}
return response.getRequestId();
} catch (ApiException e) {
log.error("新增公告deleteBlackboard异常{}",e.getMessage());
throw new SyncDataException(JSON.toJSONString(req),e.getErrMsg());
}
}
@Override
public void deleteBlackboard(String blackboardId) {
DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.DEPARTMENT_DELETE);
OapiBlackboardDeleteRequest req = new OapiBlackboardDeleteRequest();
req.setBlackboardId(blackboardId);
req.setOperationUserid("manager4480");
try {
OapiBlackboardDeleteResponse response = client.execute(req, getDingTalkToken());
if(response.getErrcode()!=0){
throw new SyncDataException(JSON.toJSONString(req),response.getErrmsg());
}
} catch (ApiException e) {
log.error("删除公告deleteBlackboard异常{}",e.getMessage());
throw new SyncDataException(JSON.toJSONString(req),e.getErrMsg());
}
}
@Override
public void updateBlackboard(SysNotice sysNotice) {
DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.DEPARTMENT_UPDATE);
OapiBlackboardUpdateRequest req = new OapiBlackboardUpdateRequest();
OapiBlackboardUpdateRequest.OapiUpdateBlackboardVo boardVoObj = new OapiBlackboardUpdateRequest.OapiUpdateBlackboardVo();
boardVoObj.setAuthor("没用的阿吉");
boardVoObj.setDing(true);
boardVoObj.setBlackboardId(sysNotice.getBlackboardId());
boardVoObj.setTitle(sysNotice.getNoticeTitle());
boardVoObj.setContent(sysNotice.getNoticeContent());
//修改后是否再次通知接收人
boardVoObj.setNotify(true);
//操作人userid必须是公告管理员
boardVoObj.setOperationUserid("manager4480");
req.setUpdateRequest(boardVoObj);
try {
OapiBlackboardUpdateResponse response = client.execute(req, getDingTalkToken());
if(response.getErrcode()!=0){
throw new SyncDataException(JSON.toJSONString(req),response.getErrmsg());
}
} catch (ApiException e) {
log.error("修改公告deleteBlackboard异常{}",e.getMessage());
throw new SyncDataException(JSON.toJSONString(req),e.getErrMsg());
}
}
/**
* 获取系统用户的dingId
* @return
*/
private List<String> getUserDingId(){
SysUser sysUser=new SysUser();
sysUser.setStatus("0");
List<SysUser> userList = sysUserService.selectUserList(sysUser);
return userList.stream().map(SysUser::getDingUserId).collect(Collectors.toList());
}
}

View File

@ -1,15 +1,14 @@
package com.snow.system.domain;
import javax.validation.constraints.*;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.snow.common.core.domain.BaseEntity;
import lombok.Data;
/**
* 通知公告表 sys_notice
*
* @author snow
*/
@Data
public class SysNotice extends BaseEntity
{
private static final long serialVersionUID = 1L;
@ -29,71 +28,9 @@ public class SysNotice extends BaseEntity
/** 公告状态0正常 1关闭 */
private String status;
public Long getNoticeId()
{
return noticeId;
}
/**
* 钉钉的公告id
*/
private String blackboardId;
public void setNoticeId(Long noticeId)
{
this.noticeId = noticeId;
}
public void setNoticeTitle(String noticeTitle)
{
this.noticeTitle = noticeTitle;
}
@NotBlank(message = "公告标题不能为空")
@Size(min = 0, max = 50, message = "公告标题不能超过50个字符")
public String getNoticeTitle()
{
return noticeTitle;
}
public void setNoticeType(String noticeType)
{
this.noticeType = noticeType;
}
public String getNoticeType()
{
return noticeType;
}
public void setNoticeContent(String noticeContent)
{
this.noticeContent = noticeContent;
}
public String getNoticeContent()
{
return noticeContent;
}
public void setStatus(String status)
{
this.status = status;
}
public String getStatus()
{
return status;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("noticeId", getNoticeId())
.append("noticeTitle", getNoticeTitle())
.append("noticeType", getNoticeType())
.append("noticeContent", getNoticeContent())
.append("status", getStatus())
.append("createBy", getCreateBy())
.append("createTime", getCreateTime())
.append("updateBy", getUpdateBy())
.append("updateTime", getUpdateTime())
.append("remark", getRemark())
.toString();
}
}

View File

@ -1,13 +1,20 @@
package com.snow.system.service.impl;
import java.util.Date;
import java.util.List;
import com.snow.common.enums.DingTalkListenerType;
import com.snow.system.event.SyncEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import com.snow.common.core.text.Convert;
import com.snow.system.domain.SysNotice;
import com.snow.system.mapper.SysNoticeMapper;
import com.snow.system.service.ISysNoticeService;
import javax.annotation.Resource;
/**
* 公告 服务层实现
*
@ -20,6 +27,9 @@ public class SysNoticeServiceImpl implements ISysNoticeService
@Autowired
private SysNoticeMapper noticeMapper;
@Resource
private ApplicationContext applicationContext;
/**
* 查询公告信息
*
@ -53,7 +63,12 @@ public class SysNoticeServiceImpl implements ISysNoticeService
@Override
public int insertNotice(SysNotice notice)
{
return noticeMapper.insertNotice(notice);
notice.setCreateTime(new Date());
int i = noticeMapper.insertNotice(notice);
//同步钉钉数据
SyncEvent syncEvent = new SyncEvent(notice, DingTalkListenerType.BLACKBOARD_CREATE);
applicationContext.publishEvent(syncEvent);
return i;
}
/**
@ -65,6 +80,8 @@ public class SysNoticeServiceImpl implements ISysNoticeService
@Override
public int updateNotice(SysNotice notice)
{
SyncEvent syncEvent = new SyncEvent(notice, DingTalkListenerType.BLACKBOARD_UPDATE);
applicationContext.publishEvent(syncEvent);
return noticeMapper.updateNotice(notice);
}
@ -77,6 +94,7 @@ public class SysNoticeServiceImpl implements ISysNoticeService
@Override
public int deleteNoticeByIds(String ids)
{
//删除通知钉钉删除
return noticeMapper.deleteNoticeByIds(Convert.toStrArray(ids));
}
}

View File

@ -15,10 +15,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
<result property="remark" column="remark" />
<result property="blackboardId" column="blackboard_id" />
</resultMap>
<sql id="selectNoticeVo">
select notice_id, notice_title, notice_type, notice_content, status, create_by, create_time, update_by, update_time, remark
select notice_id, notice_title,blackboard_id, notice_type, notice_content, status, create_by, create_time, update_by, update_time, remark
from sys_notice
</sql>
@ -39,10 +41,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="createBy != null and createBy != ''">
AND create_by like concat('%', #{createBy}, '%')
</if>
<if test="blackboardId != null and blackboardId != ''">
AND blackboard_id = #{blackboardId}
</if>
</where>
</select>
<insert id="insertNotice" parameterType="SysNotice">
<insert id="insertNotice" parameterType="SysNotice" keyProperty="noticeId" useGeneratedKeys="true" >
insert into sys_notice (
<if test="noticeTitle != null and noticeTitle != '' ">notice_title, </if>
<if test="noticeType != null and noticeType != '' ">notice_type, </if>
@ -50,6 +56,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="status != null and status != '' ">status, </if>
<if test="remark != null and remark != ''">remark,</if>
<if test="createBy != null and createBy != ''">create_by,</if>
<if test="blackboardId != null and blackboardId != ''">blackboard_id,</if>
create_time
)values(
<if test="noticeTitle != null and noticeTitle != ''">#{noticeTitle}, </if>
@ -58,6 +65,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="status != null and status != ''">#{status}, </if>
<if test="remark != null and remark != ''">#{remark},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if>
<if test="blackboardId != null and blackboardId != ''">#{blackboardId},</if>
sysdate()
)
</insert>
@ -70,6 +78,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="noticeContent != null">notice_content = #{noticeContent}, </if>
<if test="status != null and status != ''">status = #{status}, </if>
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
<if test="blackboardId != null and blackboardId != ''">blackboard_id=#{blackboardId},</if>
update_time = sysdate()
</set>
where notice_id = #{noticeId}