admin
2019-07-30 573c491b4a1ba60e12a5678a01c1546c0077c1ee
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
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.MsgMoneyDetailMapper;
import com.yeshi.fanli.entity.bus.msg.MsgMoneyDetail;
import com.yeshi.fanli.entity.bus.msg.MsgMoneyDetail.MsgTypeMoneyTypeEnum;
import com.yeshi.fanli.exception.msg.MsgMoneyDetailException;
import com.yeshi.fanli.service.inter.msg.MsgMoneyDetailService;
import com.yeshi.fanli.service.inter.msg.UserMsgReadStateService;
import com.yeshi.fanli.util.Constant;
 
@Service
public class MsgMoneyDetailServiceImpl implements MsgMoneyDetailService {
 
    @Resource
    private MsgMoneyDetailMapper msgMoneyDetailMapper;
 
    @Resource
    private UserMsgReadStateService userMsgReadStateService;
 
    @Override
    public void addMsgMoneyDetail(MsgMoneyDetail detail) throws MsgMoneyDetailException {
        if (detail == null)
            throw new MsgMoneyDetailException(1, "消息为空");
        if (detail.getMsgType() == MsgTypeMoneyTypeEnum.extract) {
            if (detail.getExtract() == null || detail.getUser() == null)
                throw new MsgMoneyDetailException(2, "消息信息不全");
            MsgMoneyDetail msg = msgMoneyDetailMapper.selectBySourceIdAndMsgType(detail.getExtract().getId(),
                    MsgTypeMoneyTypeEnum.extract);
            if (msg == null) {
                detail.setCreateTime(new Date());
                detail.setUpdateTime(new Date());
                detail.setRead(false);
                msgMoneyDetailMapper.insertSelective(detail);
            } else {
                MsgMoneyDetail update = new MsgMoneyDetail();
                update.setId(msg.getId());
                update.setUpdateTime(new Date());
                update.setRead(false);
                update.setStateDesc(detail.getStateDesc());
                msgMoneyDetailMapper.updateByPrimaryKeySelective(update);
            }
 
        } else if (detail.getMsgType() == MsgTypeMoneyTypeEnum.extractValid) {
            if (detail.getAlipayAccountValid() == null || detail.getUser() == null)
                throw new MsgMoneyDetailException(2, "消息信息不全");
            MsgMoneyDetail msg = msgMoneyDetailMapper.selectBySourceIdAndMsgType(detail.getAlipayAccountValid().getId(),
                    MsgTypeMoneyTypeEnum.extractValid);
            if (msg == null) {
                detail.setCreateTime(new Date());
                detail.setUpdateTime(new Date());
                detail.setRead(false);
                msgMoneyDetailMapper.insertSelective(detail);
            } else {
                MsgMoneyDetail update = new MsgMoneyDetail();
                update.setId(msg.getId());
                update.setUpdateTime(new Date());
                update.setRead(false);
                update.setStateDesc(detail.getStateDesc());
                msgMoneyDetailMapper.updateByPrimaryKeySelective(update);
            }
 
        } else if (detail.getMsgType() == MsgTypeMoneyTypeEnum.fanli) {
            if (detail.getMoney() == null || detail.getGoodsCount() == null || detail.getOrderId() == null
                    || detail.getBalance() == null)
                throw new MsgMoneyDetailException(2, "消息信息不全");
 
            detail.setCreateTime(new Date());
            detail.setUpdateTime(new Date());
            detail.setRead(false);
            msgMoneyDetailMapper.insertSelective(detail);
        } else if (detail.getMsgType() == MsgTypeMoneyTypeEnum.invite
                || detail.getMsgType() == MsgTypeMoneyTypeEnum.share) {
            if (detail.getMoney() == null || detail.getGoodsCount() == null || detail.getOrderCount() == null
                    || detail.getBalance() == null)
                throw new MsgMoneyDetailException(2, "消息信息不全");
            detail.setCreateTime(new Date());
            detail.setUpdateTime(new Date());
            detail.setRead(false);
            msgMoneyDetailMapper.insertSelective(detail);
        } else if (detail.getMsgType() == MsgTypeMoneyTypeEnum.fanliWeiQuan
                || detail.getMsgType() == MsgTypeMoneyTypeEnum.inviteWeiQuan
                || detail.getMsgType() == MsgTypeMoneyTypeEnum.shareWeiQuan) {
            if (detail.getMoney() == null || detail.getOrderId() == null || detail.getBalance() == null)
                throw new MsgMoneyDetailException(2, "消息信息不全");
            detail.setCreateTime(new Date());
            detail.setUpdateTime(new Date());
            detail.setRead(false);
            msgMoneyDetailMapper.insertSelective(detail);
        } else if (detail.getMsgType() == MsgTypeMoneyTypeEnum.orderReward) {
            if (detail.getMoney() == null || detail.getOrderId() == null || detail.getBalance() == null)
                throw new MsgMoneyDetailException(2, "消息信息不全");
            detail.setCreateTime(new Date());
            detail.setUpdateTime(new Date());
            detail.setRead(false);
            msgMoneyDetailMapper.insertSelective(detail);
        } else if (detail.getMsgType() == MsgTypeMoneyTypeEnum.systemEqualize) {
            if (detail.getMoney() == null || detail.getOrderId() == null || detail.getBalance() == null)
                throw new MsgMoneyDetailException(2, "消息信息不全");
            detail.setCreateTime(new Date());
            detail.setUpdateTime(new Date());
            detail.setRead(false);
            msgMoneyDetailMapper.insertSelective(detail);
        }
 
        userMsgReadStateService.addMoneyMsgUnReadCount(detail.getUser().getId(), 1);
    }
 
    @Override
    public List<MsgMoneyDetail> listMsgMoneyDetail(Long uid, int page) {
        return msgMoneyDetailMapper.listByUid(uid, (page - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE);
    }
 
    @Override
    public long countMsgMoneyDetail(Long uid) {
        return msgMoneyDetailMapper.countByUid(uid);
    }
 
    @Override
    public void readMsgByUid(Long uid) {
        msgMoneyDetailMapper.setMsgReadByUid(uid);
    }
 
}