yujian
2020-05-09 7e7db2fa55a9a3af46d4fd8ede0dee147f101d64
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
108
109
110
111
112
113
114
115
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 org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.mybatis.msg.MsgDeviceReadStateMapper;
import com.yeshi.fanli.entity.bus.msg.MsgDeviceReadState;
import com.yeshi.fanli.service.inter.msg.MsgDeviceReadStateService;
 
@Service
public class MsgDeviceReadStateServiceImpl implements MsgDeviceReadStateService {
 
    @Resource
    private MsgDeviceReadStateMapper msgDeviceReadStateMapper;
 
    @Override
    public void setDeviceMsgRead(String type, String device, int platform) {
        MsgDeviceReadState state = msgDeviceReadStateMapper.selectByDeviceAndPlatformAndType(device, platform, type);
        if (state != null) {
            MsgDeviceReadState update = new MsgDeviceReadState();
            update.setId(state.getId());
            update.setReadTime(new Date());
            update.setUnReadCount(0);
            update.setUpdateTime(new Date());
            msgDeviceReadStateMapper.updateByPrimaryKeySelective(update);
        }
    }
 
    @Override
    public void addUnreadDeviceMsg(String type, String device, int platform, int msgCount, String msg, Date msgTime) {
        MsgDeviceReadState state = msgDeviceReadStateMapper.selectByDeviceAndPlatformAndType(device, platform, type);
        if (state != null) {
            MsgDeviceReadState update = new MsgDeviceReadState();
            update.setId(state.getId());
            update.setUnReadCount(state.getUnReadCount() + msgCount);
            update.setUpdateTime(new Date());
            update.setLatestContent(msg);
            update.setLatestContentTime(msgTime);
            msgDeviceReadStateMapper.updateByPrimaryKeySelective(update);
        } else {
            state = new MsgDeviceReadState();
            state.setCreateTime(new Date());
            state.setDevice(device);
            state.setPlatform(platform);
            state.setType(type);
            state.setUnReadCount(msgCount);
            state.setUpdateTime(new Date());
            state.setLatestContent(msg);
            state.setLatestContentTime(msgTime);
            msgDeviceReadStateMapper.insertSelective(state);
        }
    }
 
    @Transactional
    @Override
    public MsgDeviceReadState getByDeviceAndPlatformAndType(String type, String device, int platform) {
        MsgDeviceReadState state = msgDeviceReadStateMapper.selectByDeviceAndPlatformAndTypeForUpdate(device, platform,
                type);
        if (state == null) {
            state = new MsgDeviceReadState();
            state.setCreateTime(new Date());
            state.setDevice(device);
            state.setPlatform(platform);
            state.setType(type);
            state.setUnReadCount(0);
            state.setUpdateTime(new Date());
            msgDeviceReadStateMapper.insertSelective(state);
        }
        return state;
    }
 
    @Override
    public void setAllMsgRead(String device, int platform) {
        msgDeviceReadStateMapper.setAllMsgRead(device, platform);
    }
 
    @Override
    public void initReadState(String device, int platform, String type) {
        MsgDeviceReadState state = getByDeviceAndPlatformAndType(type, device, platform);
        if (state == null) {
            state = new MsgDeviceReadState();
            state.setCreateTime(new Date());
            state.setDevice(device);
            state.setPlatform(platform);
            state.setReadTime(null);
            state.setType(type);
            state.setUnReadCount(0);
            msgDeviceReadStateMapper.insertSelective(state);
        }
    }
 
    @Override
    public int getUnReadCount(String device, int platform) {
        int totalCount = 0;
        List<MsgDeviceReadState> stateList = msgDeviceReadStateMapper.listByDeviceAndPlatform(device, platform);
        for (MsgDeviceReadState state : stateList) {
            totalCount += state.getUnReadCount();
        }
        return totalCount;
    }
 
    @Override
    public int getUnReadCount(String device, int platform, String type) {
        MsgDeviceReadState state = getByDeviceAndPlatformAndType(type, device, platform);
        if (state == null)
            return 0;
        return state.getUnReadCount() == null ? 0 : state.getUnReadCount();
    }
 
}