admin
2019-01-22 0e552c5dba18d27622116a7d7a6e04fc99f8e8a7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.yeshi.fanli.service.impl.msg;
 
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.mybatis.msg.UserSystemMsgMapper;
import com.yeshi.fanli.entity.bus.msg.UserSystemMsg;
import com.yeshi.fanli.entity.bus.msg.UserSystemMsgTypeEnum;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.system.SystemZnx;
import com.yeshi.fanli.exception.msg.UserSystemMsgException;
import com.yeshi.fanli.service.inter.msg.UserMsgReadStateService;
import com.yeshi.fanli.service.inter.msg.UserSystemMsgService;
import com.yeshi.fanli.util.StringUtil;
 
@Service
public class UserSystemMsgServiceImpl implements UserSystemMsgService {
 
    @Resource
    private UserSystemMsgMapper userSystemMsgMapper;
 
    @Resource
    private UserMsgReadStateService userMsgReadStateService;
 
    @Override
    public void addUserSystemMsg(Long uid, UserSystemMsgTypeEnum type, String title, String content, int timeTag,
            SystemZnx sz) throws UserSystemMsgException {
        if (sz == null && (StringUtil.isNullOrEmpty(title) || StringUtil.isNullOrEmpty(content) || uid == null))
            throw new UserSystemMsgException(1, "信息不完整");
        UserSystemMsg msg = new UserSystemMsg();
        if (sz != null) {
            if (userSystemMsgMapper.selectBySystemZNXId(sz.getId()) == null) {
                msg.setCreateTime(new Date(sz.getCreateTime()));
                msg.setRead(false);
                msg.setSolved(false);
                msg.setSystemZNX(sz);
                msg.setUser(new UserInfo(uid));
                msg.setType(type);
                msg.setTimeTag(timeTag);
            } else
                throw new UserSystemMsgException(1, "消息已存在");
        } else {
            msg.setCreateTime(new Date());
            msg.setRead(false);
            msg.setSolved(false);
            msg.setContent(content);
            msg.setTitle(title);
            msg.setUser(new UserInfo(uid));
            msg.setType(type);
            msg.setTimeTag(timeTag);
        }
        userSystemMsgMapper.insertSelective(msg);
        userMsgReadStateService.addSystemMsgUnReadCount(uid, 1);
    }
 
    @Override
    public UserSystemMsg getLatestUserSystemMsg(Long uid) {
        UserSystemMsg msg = userSystemMsgMapper.selectLatestUserSystemMsg(uid);
        if (msg != null)
            if (msg.getSystemZNX() != null) {
                msg.setTitle(msg.getSystemZNX().getTitle());
                msg.setContent(msg.getSystemZNX().getContent());
            }
        return msg;
    }
 
    @Override
    public List<UserSystemMsg> listUserSystemMsg(Long uid, int page, int pageSize) {
        List<UserSystemMsg> list = userSystemMsgMapper.listByUid(uid, (page - 1) * pageSize, pageSize);
        if (list != null)
            for (UserSystemMsg msg : list) {
                if (msg.getSystemZNX() != null) {
                    msg.setTitle(msg.getSystemZNX().getTitle());
                    msg.setContent(msg.getSystemZNX().getContent());
                }
            }
 
        return list;
    }
 
    @Override
    public long countUserSystemMsg(Long uid) {
        return userSystemMsgMapper.countByUid(uid);
    }
 
    @Override
    public void setSystemMsgSolved(Long uid, Long id) throws UserSystemMsgException {
        UserSystemMsg msg = userSystemMsgMapper.selectByPrimaryKey(id);
        if (msg.getUser().getId() != uid.longValue())
            throw new UserSystemMsgException(2, "不是自己的消息");
        UserSystemMsg update = new UserSystemMsg();
        update.setId(id);
        update.setSolved(true);
        update.setUpdateTime(new Date());
        userSystemMsgMapper.updateByPrimaryKeySelective(update);
    }
 
    @Override
    public void readMsgByUid(Long uid) {
        userSystemMsgMapper.setMsgReadByUid(uid);
    }
 
}