yujian
2020-03-24 1d841af6d10c30b5eba4089a983c64fee662bbc9
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
package com.yeshi.fanli.service.impl.elme;
 
import java.math.BigDecimal;
import java.util.Date;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.UserExtraTaoBaoInfo;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.elme.ElmeHongBaoOrderMap;
import com.yeshi.fanli.entity.elme.ElmeOrder;
import com.yeshi.fanli.exception.elme.ElmeHongBaoOrderMapException;
import com.yeshi.fanli.exception.elme.ElmeOrderException;
import com.yeshi.fanli.service.inter.elme.ElmeHongBaoOrderMapService;
import com.yeshi.fanli.service.inter.elme.ElmeOrderProcessService;
import com.yeshi.fanli.service.inter.elme.ElmeOrderService;
import com.yeshi.fanli.service.inter.order.HongBaoV2Service;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.inter.order.msg.UserOrderMsgNotificationService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.tb.UserExtraTaoBaoInfoService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.elme.ElmeOrderUtil;
 
@Service
public class ElmeOrderProcessServiceImpl implements ElmeOrderProcessService {
 
    @Resource
    private ElmeOrderService elmeOrderService;
 
    @Resource
    private ElmeHongBaoOrderMapService elmeHongBaoOrderMapService;
 
    @Resource
    private UserInfoService userInfoService;
 
    @Resource
    private HongBaoManageService hongBaoManageService;
 
    @Resource
    private HongBaoV2Service hongBaoV2Service;
 
    @Resource
    private UserOrderMsgNotificationService userOrderMsgNotificationService;
 
    @Resource
    private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService;
 
    @Transactional(rollbackFor=Exception.class)
    @Override
    public void processOrder(ElmeOrder elmeOrder) throws ElmeOrderException {
        try {
            elmeOrderService.addOrder(elmeOrder);
        } catch (ElmeOrderException e) {
            return;
        }
 
        if (elmeOrder.getId() == null)
            return;
        // 查询订单是否存在
        ElmeHongBaoOrderMap map = elmeHongBaoOrderMapService.selectByOrderId(elmeOrder.getId());
        if (map == null)// 订单不存在
        {
            Long uid = null;
            if (StringUtil.isNullOrEmpty(elmeOrder.getChannelId())) {//新版本
                UserExtraTaoBaoInfo extra = userExtraTaoBaoInfoService.getByRelationId(elmeOrder.getRid());
                if (extra != null)
                    uid = extra.getUser().getId();
            } else {//老版本
                uid = Long.parseLong(elmeOrder.getRid());
            }
 
            if (uid == null)
                return;
 
            // 查询映射用户
            UserInfo user = userInfoService.selectByPKey(uid);
            if (user == null)
                return;
            // 制造hongbao
            HongBaoV2 hongBao = createHongBao(elmeOrder, user);
            hongBao.setUpdateTime(new Date());
            hongBaoV2Service.insertSelective(hongBao);
            // 添加红包映射
            ElmeHongBaoOrderMap newMap = new ElmeHongBaoOrderMap();
            newMap.setCreateTime(new Date());
            newMap.setElmeOrder(elmeOrder);
            newMap.setHongBao(hongBao);
            try {
                elmeHongBaoOrderMapService.addHongBaoOrderMap(newMap);
            } catch (ElmeHongBaoOrderMapException e) {
                throw new ElmeOrderException(e.getCode(), e.getMsg());
            }
            // 发送消息
            userOrderMsgNotificationService.orderElmeStatistic(user.getId(), elmeOrder.getOrderId(),
                    Constant.SOURCE_TYPE_ELME, elmeOrder.getPayMoney(), hongBao.getMoney(), hongBao.getState());
        } else {// 订单存在
            HongBaoV2 oldHongBao = hongBaoV2Service.selectByPrimaryKey(map.getHongBao().getId());
            if (oldHongBao == null)
                return;
            // 失效与到账状态的红包不需要修改
            if (oldHongBao.getState() == HongBaoV2.STATE_SHIXIAO || oldHongBao.getState() == HongBaoV2.STATE_YILINGQU)
                return;
 
            HongBaoV2 hongBao = createHongBao(elmeOrder, null);
            if (hongBao.getState() != oldHongBao.getState()) {// 是否需要修改红包状态
                HongBaoV2 update = new HongBaoV2(oldHongBao.getId());
                update.setState(hongBao.getState());
                update.setPreGetTime(hongBao.getPreGetTime());
                update.setUpdateTime(new Date());
                hongBaoV2Service.updateByPrimaryKeySelective(update);
                // 发送消息
                userOrderMsgNotificationService.orderElmeStateChanged(oldHongBao.getUserInfo().getId(),
                        elmeOrder.getOrderId(), Constant.SOURCE_TYPE_ELME, elmeOrder.getPayMoney(), hongBao.getMoney(),
                        hongBao.getState());
            }
        }
 
    }
 
    private HongBaoV2 createHongBao(ElmeOrder elmeOrder, UserInfo userInfo) {
        BigDecimal fanliRate = hongBaoManageService.getFanLiRate(elmeOrder.getOrderDate().getTime());
        HongBaoV2 hongBao = new HongBaoV2();
        hongBao.setCreateTime(new Date());
        hongBao.setGetTime(null);
        hongBao.setMoney(MoneyBigDecimalUtil.div(
                MoneyBigDecimalUtil.mul(ElmeOrderUtil.getCommission(elmeOrder.getPayMoney()), fanliRate),
                new BigDecimal(100)));
        if (elmeOrder.getIsSettle() == true)
            hongBao.setPreGetTime(new Date(elmeOrder.getOrderDate().getTime() + 1000 * 60 * 60 * 24 * 15L));
        if (elmeOrder.getPayMoney().compareTo(new BigDecimal(0)) <= 0||(elmeOrder.getState()!=null&&elmeOrder.getState()==0))
            hongBao.setState(HongBaoV2.STATE_SHIXIAO);
        else {
            if (elmeOrder.getIsSettle() == true) {
                hongBao.setState(HongBaoV2.STATE_KELINGQU);
            } else {
                hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
            }
        }
 
        hongBao.setType(HongBaoV2.TYPE_ELME);
        if (userInfo != null)
            hongBao.setUrank(userInfo.getRank());
        hongBao.setUserInfo(userInfo);
        hongBao.setVersion(2);
        return hongBao;
    }
 
}