上个小节我们写好了异步线程任务类,接下来应该编写Web方法、Service方法这些内容,本小节咱们把这些代码写完,然后用Swagger测试一下。
一、编写持久层代码
用户在弹窗页面填写的会议申请信息需要保存到数据表里面,所以我们要在TbMeetingDao.xml
文件中定义SQL语句。
<insert id="insert" parameterType="com.example.emos.api.db.pojo.TbMeeting">
INSERT INTO tb_meeting
SET title = #{title},
<if test="uuid!=null">
uuid = #{uuid},
</if>
`date` = #{date},
creator_id = #{creatorId},
<if test="place!=null">
place = #{place},
</if>
`start` = #{start},
`end` = #{end},
`type` = #{type},
members = #{members},
`desc` = #{desc},
instance_id = #{instanceId},
status = #{status},
create_time = NOW()
</insert>
在TbMeetingDao.java
接口中,定义DAO方法。
public interface TbMeetingDao {
……
public int insert(TbMeeting meeting);
}
二、编写业务层代码
在MeetingService.java
接口中,定义抽象方法。
public interface MeetingService {
……
public int insert(TbMeeting meeting);
}
在MeetingServiceImpl.java
类中,实现抽象方法。
public class MeetingServiceImpl implements MeetingService {
……
@Override
public int insert(TbMeeting meeting) {
int rows = meetingDao.insert(meeting);
if (rows != 1) {
throw new EmosException("会议添加失败");
}
meetingWorkflowTask.startMeetingWorkflow(meeting.getUuid(), meeting.getCreatorId(), meeting.getTitle(), meeting.getDate(), meeting.getStart() + ":00", "线下会议");
return rows;
}
}
三、编写Web层代码
创建InsertMeetingForm.java
类,封装Ajax提交的数据。
@Data
@Schema(description = "添加会议表单")
public class InsertMeetingForm {
@NotBlank(message = "title不能为空")
@Pattern(regexp = "^[a-zA-Z0-9\\u4e00-\\u9fa5]{2,30}$", message = "title内容不正确")
@Schema(description = "主题")
private String title;
@NotBlank(message = "date不能为空")
@Pattern(regexp = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$", message = "date内容不正确")
@Schema(description = "日期")
private String date;
@Pattern(regexp = "^[a-zA-Z0-9\\u4e00-\\u9fa5]{2,20}$", message = "place内容不正确")
@Schema(description = "会议地点")
private String place;
@NotBlank(message = "start不能为空")
@Pattern(regexp = "^([01]?[0-9]|2[0-3]):(00|30)$", message = "start内容不正确")
@Schema(description = "起始时间")
private String start;
@NotBlank(message = "end不能为空")
@Pattern(regexp = "^([01]?[0-9]|2[0-3]):(00|30)$", message = "end内容不正确")
@Schema(description = "结束时间")
private String end;
@NotNull(message = "type不能为空")
@Range(min = 1, max = 2, message = "type内容不正确")
@Schema(description = "会议类型")
private Byte type;
@NotBlank(message = "members不能为空")
@Schema(description = "参会人")
private String members;
@NotBlank(message = "desc不能为空")
@Length(min = 1, max = 200)
@Schema(description = "内容")
private String desc;
}
在MeetingController.java
类中,定义web方法,然后用Swagger测试一下。
public class MeetingController {
……
@PostMapping("/insert")
@Operation(summary = "添加会议")
@SaCheckLogin
public R insert(@Valid @RequestBody InsertMeetingForm form) {
DateTime start = DateUtil.parse(form.getDate() + " " + form.getStart());
DateTime end = DateUtil.parse(form.getDate() + " " + form.getEnd());
if (start.isAfterOrEquals(end)) {
return R.error("结束时间必须大于开始时间");
} else if (new DateTime().isAfterOrEquals(start)) {
return R.error("会议开始时间不能早于当前时间");
}
TbMeeting meeting = JSONUtil.parse(form).toBean(TbMeeting.class);
meeting.setUuid(UUID.randomUUID().toString(true));
meeting.setCreatorId(StpUtil.getLoginIdAsInt());
meeting.setStatus((short) 1);
int rows = meetingService.insert(meeting);
return R.ok().put("rows", rows);
}
}
创建RecieveNotifyForm.java
类,封装工作流项目发送的数据。
@Data
public class RecieveNotifyForm {
@NotBlank
private String processId;
@NotBlank
private String uuid;
@NotBlank
private String result;
}
在MeetingController.java
类中,定义接收会议审批结果的Web方法。
public class MeetingController {
……
@PostMapping("/recieveNotify")
@Operation(summary = "接收工作流通知")
public R recieveNotify(@Valid @RequestBody RecieveNotifyForm form){
if(form.getResult().equals("同意")){
log.debug(form.getUuid()+"的会议审批通过");
}
else{
log.debug(form.getUuid()+"的会议审批不通过");
}
return R.ok();
}
}