admin
2021-09-13 8ce7c720e4e7a604b0ff770349b5556f39d37759
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.yeshi.fanli.service.impl.order.msg;
 
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import com.yeshi.fanli.service.inter.msg.MsgOverViewsService;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.factory.msg.MsgOverViewsFactory;
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.mybatis.msg.MsgOrderDetailMapper;
import com.yeshi.fanli.entity.bus.msg.MsgExtra;
import com.yeshi.fanli.entity.bus.msg.MsgOrderDetail;
import com.yeshi.fanli.entity.bus.msg.MsgOrderDetail.MsgTypeOrderTypeEnum;
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.exception.msg.MsgOrderDetailException;
import com.yeshi.fanli.service.inter.msg.MsgExtraService;
import com.yeshi.fanli.service.inter.msg.UserMsgReadStateService;
import com.yeshi.fanli.service.inter.order.msg.MsgOrderDetailService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
 
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
 
@Service
public class MsgOrderDetailServiceImpl implements MsgOrderDetailService {
 
    @Resource
    private MsgOrderDetailMapper msgOrderDetailMapper;
 
    @Resource
    private UserMsgReadStateService userMsgReadStateService;
 
    @Resource
    private RedisManager redisManager;
 
    @Resource
    private MsgExtraService msgExtraService;
 
    @Resource
    private MsgOverViewsService msgOverViewsService;
 
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addMsgOrderDetail(MsgOrderDetail detail, boolean needNotify) throws MsgOrderDetailException {
        if (detail == null)
            throw new MsgOrderDetailException(1, "消息为空");
        if (detail.getOrderId() == null || detail.getType() == null || detail.getUser() == null
                || StringUtil.isNullOrEmpty(detail.getExtraInfo()))
            throw new MsgOrderDetailException(2, "消息不完整");
 
        if (detail.getType() == MsgTypeOrderTypeEnum.businessRunning) {
            MsgOrderDetail msgOrderDetail = msgOrderDetailMapper.getByUniqueKey(detail.getUniquekey());
            if (msgOrderDetail != null) {
                return;
            }
        }
 
 
        // 锁住订单号
        Jedis jedis = redisManager.getJedis();
        try {
            String key = "rs-order-" + detail.getOrderId();
            if (jedis.setnx(key, "1") > 0) {
                jedis.expire(key, 60);
                MsgOrderDetail old = msgOrderDetailMapper.selectByUidAndOrderId(detail.getUser().getId(),
                        detail.getOrderId());
                if (old == null) {
                    detail.setCreateTime(new Date());
                    detail.setUpdateTime(new Date());
                    detail.setRead(false);
                    msgOrderDetailMapper.insertSelective(detail);
                    //加入消息索引
                    msgOverViewsService.save(MsgOverViewsFactory.create(detail));
                    // 消息内容
                    msgExtraService.addMsgExtra(detail.getId(), detail.getExtraInfo(), MsgExtra.MSG_TYPE_ORDER);
                } else {
                    MsgOrderDetail update = new MsgOrderDetail();
                    update.setId(old.getId());
                    update.setUpdateTime(new Date());
                    update.setState(detail.getState());
                    update.setPayMoney(detail.getPayMoney());
                    update.setHongBaoMoney(detail.getHongBaoMoney());
                    update.setRead(false);
                    update.setBeiZhu(detail.getBeiZhu());
                    msgOrderDetailMapper.updateByPrimaryKeySelective(update);
                    //更新消息索引时间
                    msgOverViewsService.updateTime(MsgOverViewsFactory.create(update).getId(), update.getUpdateTime());
                    msgExtraService.addMsgExtra(update.getId(), detail.getExtraInfo(), MsgExtra.MSG_TYPE_ORDER);
                }
                if (needNotify)
                    userMsgReadStateService.addOrderMsgUnReadCount(detail.getUser().getId(), 1);
                jedis.del(key);
            }
 
        } finally {
            jedis.close();
        }
 
    }
 
    @Override
    public List<MsgOrderDetail> listMsgOrderDetail(Long uid, int page) {
        return msgOrderDetailMapper.listByUid(uid, (page - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE);
    }
 
    @Override
    public List<MsgOrderDetail> listDetail(List<Long> ids) {
        return msgOrderDetailMapper.listByPrimaryKeys(ids);
    }
 
    @Override
    public long countMsgOrderDetail(Long uid) {
        return msgOrderDetailMapper.countByUid(uid);
    }
 
    @Override
    public void readMsgByUid(Long uid) {
        msgOrderDetailMapper.setMsgReadByUid(uid);
    }
 
    @Override
    public void updateMsgOrderDetail(MsgOrderDetail detail, boolean needNotify) throws MsgOrderDetailException {
        if (detail == null)
            throw new MsgOrderDetailException(1, "消息为空");
        if (detail.getOrderId() == null || detail.getState() == null || detail.getUser() == null)
            throw new MsgOrderDetailException(2, "消息不完整");
 
        MsgOrderDetail msg = msgOrderDetailMapper.selectByUidAndOrderId(detail.getUser().getId(), detail.getOrderId());
        if (msg == null)
            return;
 
        if (msg.getState() == CommonOrder.STATE_WQ)
            return;
 
        MsgOrderDetail update = new MsgOrderDetail();
        update.setId(msg.getId());
        update.setHongBaoMoney(detail.getHongBaoMoney());
        update.setPayMoney(detail.getPayMoney());
        if (detail.getState().intValue() != msg.getState()) {
            update.setState(detail.getState());
            // update.setUpdateTime(new Date());
            if (needNotify)
                userMsgReadStateService.addOrderMsgUnReadCount(detail.getUser().getId(), 1);
        }
        msgOrderDetailMapper.updateByPrimaryKeySelective(update);
    }
 
    @Override
    public List<MsgOrderDetail> listMsgOrderByOrderId(String orderId) {
        return msgOrderDetailMapper.listByOrderId(orderId);
    }
 
    @Override
    public MsgOrderDetail selectByPrimaryKey(Long id) {
        return msgOrderDetailMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public void deleteByPrimaryKey(Long id) {
        msgOrderDetailMapper.deleteByPrimaryKey(id);
        msgExtraService.deleteByPidAndType(id, MsgExtra.MSG_TYPE_ORDER);
    }
 
}