src/main/java/com/xcong/excoin/modules/otc/controller/OtcMsgController.java
New file @@ -0,0 +1,72 @@ package com.xcong.excoin.modules.otc.controller; import com.baomidou.mybatisplus.core.metadata.IPage; import com.xcong.excoin.common.response.Result; import com.xcong.excoin.modules.otc.dto.ChatBoxDto; import com.xcong.excoin.modules.otc.dto.MsgListDto; import com.xcong.excoin.modules.otc.dto.OrderListDto; import com.xcong.excoin.modules.otc.dto.SendMsgDto; import com.xcong.excoin.modules.otc.service.OtcMsgService; import com.xcong.excoin.modules.otc.vo.ChatBoxVo; import com.xcong.excoin.modules.otc.vo.MsgListVo; import com.xcong.excoin.modules.otc.vo.OrderListVo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @Slf4j @Validated @RestController @RequestMapping(value = "/api/otcMsg") @RequiredArgsConstructor @Api(value = "OtcMsgController", tags = "otc用户消息接口类") public class OtcMsgController { private final OtcMsgService otcMsgService; /** * 获取对话列表 */ @ApiOperation(value = "获取对话列表") @ApiResponses({ @ApiResponse(code = 200, message = "success", response = MsgListVo.class) }) @PostMapping(value = "/getMsgList") public Result getMsgList(@RequestBody MsgListDto msgListDto) { IPage<MsgListVo> page = otcMsgService.getMsgList(msgListDto); return Result.ok(page.getRecords()); } /** * 进入聊天框,获取聊天记录 */ @ApiOperation(value = "进入聊天框,获取聊天记录") @ApiResponses({ @ApiResponse(code = 200, message = "success", response = ChatBoxVo.class) }) @PostMapping(value = "/getChatBox") public Result getChatBox(@RequestBody ChatBoxDto chatBoxDto) { return otcMsgService.getChatBox(chatBoxDto); } /** * 发送消息 */ @ApiOperation(value = "发送消息") @PostMapping(value = "/sendMsg") public Result sendMsg(@RequestBody SendMsgDto sendMsgDto) { return otcMsgService.sendMsg(sendMsgDto); } } src/main/java/com/xcong/excoin/modules/otc/dao/OtcMsgHistoryDao.java
New file @@ -0,0 +1,14 @@ package com.xcong.excoin.modules.otc.dao; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.xcong.excoin.modules.otc.entity.OtcMsgHistoryEntity; import com.xcong.excoin.modules.otc.vo.ChatBoxVo; import org.apache.ibatis.annotations.Param; public interface OtcMsgHistoryDao extends BaseMapper<OtcMsgHistoryEntity> { IPage<ChatBoxVo> getChatBoxMsgList(Page<ChatBoxVo> page, @Param("record")OtcMsgHistoryEntity otcMsgHistoryEntity); } src/main/java/com/xcong/excoin/modules/otc/dao/OtcMsgUserListDao.java
New file @@ -0,0 +1,17 @@ package com.xcong.excoin.modules.otc.dao; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.xcong.excoin.modules.otc.entity.OtcMsgUserListEntity; import com.xcong.excoin.modules.otc.vo.MsgListVo; import org.apache.ibatis.annotations.Param; import java.util.List; public interface OtcMsgUserListDao extends BaseMapper<OtcMsgUserListEntity> { IPage<MsgListVo> getMsgList(@Param("record")OtcMsgUserListEntity otcMsgUserListEntity, Page<MsgListVo> page); List<OtcMsgUserListEntity> selectListByMemberIdAndTargetId(@Param("memberId")Long memberId, @Param("targetId")long targetId); } src/main/java/com/xcong/excoin/modules/otc/dto/ChatBoxDto.java
New file @@ -0,0 +1,19 @@ package com.xcong.excoin.modules.otc.dto; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @Data @ApiModel(value = "ChatBoxDto", description = "参数接收类") public class ChatBoxDto { @ApiModelProperty(value = "条数", example = "10") private Integer pageSize; @ApiModelProperty(value = "页码", example = "1") private Integer pageNum; @ApiModelProperty(value = "ID", example = "1") private long id; } src/main/java/com/xcong/excoin/modules/otc/dto/MsgListDto.java
New file @@ -0,0 +1,17 @@ package com.xcong.excoin.modules.otc.dto; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @Data @ApiModel(value = "MsgListDto", description = "参数接收类") public class MsgListDto { @ApiModelProperty(value = "条数", example = "10") private Integer pageSize; @ApiModelProperty(value = "页码", example = "1") private Integer pageNum; } src/main/java/com/xcong/excoin/modules/otc/dto/SendMsgDto.java
New file @@ -0,0 +1,22 @@ package com.xcong.excoin.modules.otc.dto; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @Data @ApiModel(value = "SendMsgDto", description = "参数接收类") public class SendMsgDto { @ApiModelProperty(value = "目标用户ID") private long targetId; //消息 @ApiModelProperty(value = "消息") private String msg; //消息类型 1-文本2-图片 @ApiModelProperty(value = "消息类型 1-文本2-图片") private Integer msgType; } src/main/java/com/xcong/excoin/modules/otc/entity/OtcMsgHistoryEntity.java
New file @@ -0,0 +1,23 @@ package com.xcong.excoin.modules.otc.entity; import com.baomidou.mybatisplus.annotation.TableName; import com.xcong.excoin.common.system.base.BaseEntity; import lombok.Data; //历史消息 @Data @TableName("otc_msg_history") public class OtcMsgHistoryEntity extends BaseEntity { //用户ID private long memberId; //对方用户ID private long targetId; //消息 private String msg; //消息类型 1-文本2-图片 private Integer msgType; } src/main/java/com/xcong/excoin/modules/otc/entity/OtcMsgUserListEntity.java
New file @@ -0,0 +1,30 @@ package com.xcong.excoin.modules.otc.entity; import com.baomidou.mybatisplus.annotation.TableName; import com.xcong.excoin.common.system.base.BaseEntity; import lombok.Data; import java.util.Date; //用户消息列表 @Data @TableName("otc_msg_user_list") public class OtcMsgUserListEntity extends BaseEntity { //用户ID private long memberId; //对方用户ID private long targetId; //是否已读 1:未读 2:已读 private Integer isRead; public static final Integer ISREAD_ONE = 1; public static final Integer ISREAD_TWO = 2; //最后聊天时间 private Date lastMsgTime; } src/main/java/com/xcong/excoin/modules/otc/service/OtcMsgService.java
New file @@ -0,0 +1,20 @@ package com.xcong.excoin.modules.otc.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; import com.xcong.excoin.common.response.Result; import com.xcong.excoin.modules.otc.dto.ChatBoxDto; import com.xcong.excoin.modules.otc.dto.MsgListDto; import com.xcong.excoin.modules.otc.dto.SendMsgDto; import com.xcong.excoin.modules.otc.entity.OtcMsgUserListEntity; import com.xcong.excoin.modules.otc.vo.ChatBoxVo; import com.xcong.excoin.modules.otc.vo.MsgListVo; public interface OtcMsgService extends IService<OtcMsgUserListEntity> { IPage<MsgListVo> getMsgList(MsgListDto msgListDto); Result getChatBox(ChatBoxDto chatBoxDto); Result sendMsg(SendMsgDto sendMsgDto); } src/main/java/com/xcong/excoin/modules/otc/service/impl/OtcMsgServiceImpl.java
New file @@ -0,0 +1,149 @@ package com.xcong.excoin.modules.otc.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.xcong.excoin.common.LoginUserUtils; import com.xcong.excoin.common.response.Result; import com.xcong.excoin.modules.member.dao.MemberDao; import com.xcong.excoin.modules.member.dao.MemberSettingDao; import com.xcong.excoin.modules.member.entity.MemberEntity; import com.xcong.excoin.modules.member.entity.MemberSettingEntity; import com.xcong.excoin.modules.otc.dao.OtcMsgHistoryDao; import com.xcong.excoin.modules.otc.dao.OtcMsgUserListDao; import com.xcong.excoin.modules.otc.dto.ChatBoxDto; import com.xcong.excoin.modules.otc.dto.MsgListDto; import com.xcong.excoin.modules.otc.dto.SendMsgDto; import com.xcong.excoin.modules.otc.entity.OtcMsgHistoryEntity; import com.xcong.excoin.modules.otc.entity.OtcMsgUserListEntity; import com.xcong.excoin.modules.otc.service.OtcMsgService; import com.xcong.excoin.modules.otc.vo.ChatBoxVo; import com.xcong.excoin.modules.otc.vo.MsgListVo; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.List; @Service @RequiredArgsConstructor public class OtcMsgServiceImpl extends ServiceImpl<OtcMsgUserListDao, OtcMsgUserListEntity> implements OtcMsgService { private final OtcMsgUserListDao otcMsgUserListDao; private final OtcMsgHistoryDao otcMsgHistoryDao; private final MemberSettingDao memberSettingDao; private final MemberDao memberDao; @Override public IPage<MsgListVo> getMsgList(MsgListDto msgListDto) { MemberEntity member = LoginUserUtils.getAppLoginUser(); Long memberId = member.getId(); // Long memberId = 444L; Page<MsgListVo> page = new Page<>(msgListDto.getPageNum(), msgListDto.getPageSize()); OtcMsgUserListEntity otcMsgUserListEntity = new OtcMsgUserListEntity(); otcMsgUserListEntity.setMemberId(memberId); return otcMsgUserListDao.getMsgList(otcMsgUserListEntity, page); } @Override @Transactional public Result getChatBox(ChatBoxDto chatBoxDto) { MemberEntity member = LoginUserUtils.getAppLoginUser(); //对话是否存在 long id = chatBoxDto.getId(); if(ObjectUtil.isEmpty(id)){ return Result.fail("请下拉刷新"); } OtcMsgUserListEntity otcMsgUserListEntity = otcMsgUserListDao.selectById(id); if(ObjectUtil.isEmpty(otcMsgUserListEntity)){ return Result.fail("请下拉刷新"); } //更新为已读 otcMsgUserListEntity.setIsRead(OtcMsgUserListEntity.ISREAD_TWO); otcMsgUserListDao.updateById(otcMsgUserListEntity); long memberId = otcMsgUserListEntity.getMemberId(); long targetId = otcMsgUserListEntity.getTargetId(); Page<ChatBoxVo> page = new Page<>(chatBoxDto.getPageNum(), chatBoxDto.getPageSize()); OtcMsgHistoryEntity otcMsgHistoryEntity = new OtcMsgHistoryEntity(); otcMsgHistoryEntity.setMemberId(memberId); otcMsgHistoryEntity.setTargetId(targetId); IPage<ChatBoxVo> chatBoxVos= otcMsgHistoryDao.getChatBoxMsgList(page,otcMsgHistoryEntity); return Result.ok(chatBoxVos); } @Override @Transactional public Result sendMsg(SendMsgDto sendMsgDto) { MemberEntity member = LoginUserUtils.getAppLoginUser(); Long memberId = member.getId(); // Long memberId = 445L; long targetId = sendMsgDto.getTargetId(); if(ObjectUtil.isEmpty(targetId)){ return Result.fail("对话用户不存在"); } MemberEntity memberEntity = memberDao.selectById(targetId); if(ObjectUtil.isEmpty(memberEntity)){ return Result.fail("对话用户不存在"); } String msg = sendMsgDto.getMsg(); if(StrUtil.isEmpty(msg)){ return Result.fail("请输入发送消息"); } Integer msgType = sendMsgDto.getMsgType(); if(ObjectUtil.isEmpty(msgType)){ return Result.fail("请输入发送消息"); } /** * 是否有记录 * 有就更新,没有就新增 * 在消息列表中增加一条记录 * 在历史记录中增加一条记录 * 增加一个提醒的 */ // QueryWrapper<OtcMsgHistoryEntity> objectQueryWrapper = new QueryWrapper<>(); // objectQueryWrapper.eq("member_id",memberId); // objectQueryWrapper.eq("target_id",targetId); // OtcMsgUserListEntity otcMsgUserListEntity = otcMsgUserListDao.selectById(objectQueryWrapper); List<OtcMsgUserListEntity> otcMsgUserListEntitys = otcMsgUserListDao.selectListByMemberIdAndTargetId(memberId,targetId); if(CollUtil.isEmpty(otcMsgUserListEntitys)){ OtcMsgUserListEntity otcMsgUserListEntity = new OtcMsgUserListEntity(); otcMsgUserListEntity.setMemberId(memberId); otcMsgUserListEntity.setTargetId(targetId); otcMsgUserListEntity.setIsRead(OtcMsgUserListEntity.ISREAD_ONE); otcMsgUserListEntity.setLastMsgTime(DateUtil.date()); otcMsgUserListDao.insert(otcMsgUserListEntity); }else{ OtcMsgUserListEntity otcMsgUserListEntity = otcMsgUserListEntitys.get(0); otcMsgUserListEntity.setMemberId(memberId); otcMsgUserListEntity.setTargetId(targetId); otcMsgUserListEntity.setIsRead(OtcMsgUserListEntity.ISREAD_ONE); otcMsgUserListEntity.setLastMsgTime(DateUtil.date()); otcMsgUserListDao.updateById(otcMsgUserListEntity); } //历史消息中增加新纪录 OtcMsgHistoryEntity otcMsgHistoryEntity = new OtcMsgHistoryEntity(); otcMsgHistoryEntity.setMemberId(memberId); otcMsgHistoryEntity.setTargetId(targetId); otcMsgHistoryEntity.setMsg(msg); otcMsgHistoryEntity.setMsgType(msgType); otcMsgHistoryDao.insert(otcMsgHistoryEntity); //增加一个提醒的 MemberSettingEntity memberSettingEntity = memberSettingDao.selectMemberSettingByMemberId(targetId); memberSettingEntity.setMessageReminder(1); memberSettingDao.updateById(memberSettingEntity); return Result.ok("发送成功"); } } src/main/java/com/xcong/excoin/modules/otc/vo/ChatBoxVo.java
New file @@ -0,0 +1,29 @@ package com.xcong.excoin.modules.otc.vo; import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import java.util.Date; @Data @ApiModel(value = "ChatBoxVo", description = "返回参数类") public class ChatBoxVo { @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @ApiModelProperty(value = "时间") private Date createTime; @ApiModelProperty(value = "对方用户ID") private long targetId; //消息 @ApiModelProperty(value = "消息") private String msg; //消息类型 1-文本2-图片 @ApiModelProperty(value = "消息类型 1-文本2-图片") private Integer msgType; } src/main/java/com/xcong/excoin/modules/otc/vo/MsgListVo.java
New file @@ -0,0 +1,27 @@ package com.xcong.excoin.modules.otc.vo; import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import java.util.Date; @Data @ApiModel(value = "MsgListVo", description = "返回参数类") public class MsgListVo { //用户ID @ApiModelProperty(value = "用户ID") private long id; //是否已读 1:未读 2:已读 @ApiModelProperty(value = "是否已读 1:未读 2:已读") private Integer isRead; //最后聊天时间 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @ApiModelProperty(value = "最后聊天时间") private Date lastMsgTime; } src/main/resources/mapper/otc/OtcMsgHistoryDao.xml
New file @@ -0,0 +1,24 @@ <?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.xcong.excoin.modules.otc.dao.OtcMsgHistoryDao"> <select id="getChatBoxMsgList" resultType="com.xcong.excoin.modules.otc.vo.ChatBoxVo"> select a.create_time createTime, a.msg msg, a.target_id targetId, a.msg_type msgType from otc_msg_history a <where> <if test="record!=null"> (a.member_id = ${record.memberId} and a.target_id = ${record.targetId}) or (a.member_id = ${record.targetId} and a.target_id = ${record.memberId}) </if> </where> order by a.create_time desc </select> </mapper> src/main/resources/mapper/otc/OtcMsgUserListDao.xml
New file @@ -0,0 +1,34 @@ <?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.xcong.excoin.modules.otc.dao.OtcMsgUserListDao"> <select id="getMsgList" resultType="com.xcong.excoin.modules.otc.vo.MsgListVo"> select a.id id, a.is_read isRead, a.last_msg_time lastMsgTime from otc_msg_user_list a <where> <if test="record!=null"> a.member_id = #{record.memberId} or a.target_id = #{record.memberId} </if> </where> order by a.create_time desc </select> <select id="selectListByMemberIdAndTargetId" resultType="com.xcong.excoin.modules.otc.entity.OtcMsgUserListEntity"> select * from otc_msg_user_list a <where> (a.member_id = #{memberId} and a.target_id = #{targetId}) or (a.member_id = #{targetId} and a.target_id = #{memberId}) </where> order by a.create_time desc </select> </mapper>