admin
2019-03-13 33b4ed2bbf28ec16b66e552680f56a691a4e908d
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
package com.yeshi.fanli.service.impl.hongbao;
 
import java.io.Serializable;
import java.math.BigDecimal;
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.ThreeSaleGiftMapper;
import com.yeshi.fanli.dao.user.ThreeSaleGiftDao;
import com.yeshi.fanli.dto.HongBao;
import com.yeshi.fanli.entity.bus.user.OrderItem;
import com.yeshi.fanli.entity.bus.user.ThreeSaleGift;
import com.yeshi.fanli.service.inter.hongbao.ThreeSaleGiftService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
 
@Service
public class ThreeSaleGiftServiceImpl implements ThreeSaleGiftService {
 
    @Resource
    private ThreeSaleGiftDao dao;
 
    @Resource
    private ThreeSaleGiftMapper threeSaleGiftMapper;
 
    @Override
    public void save(ThreeSaleGift threeSaleGift) {
        dao.save(threeSaleGift);
    }
 
    @Override
    public void update(OrderItem orderItem) {
 
        List<ThreeSaleGift> list = dao.list("from ThreeSaleGift tsg where tsg.orderItem.id = ?",
                new Serializable[] { orderItem.getId() });
        Integer state = orderItem.getState();
        BigDecimal fanMoney = orderItem.getFanMoney();
        list.parallelStream().forEach(tsg -> {
            tsg.setState(state);
            tsg.setMoney(MoneyBigDecimalUtil.mul(tsg.getRate(), fanMoney));
            dao.update(tsg);
        });
 
    }
 
    @Override
    public List<ThreeSaleGift> findThreeSaleGiftList(long orderItemId) {
        return threeSaleGiftMapper.selectByOrderItem(orderItemId);
    }
 
    @Override
    public void update(ThreeSaleGift threeSaleGift) {
        dao.update(threeSaleGift);
    }
 
    @Override
    public ThreeSaleGift conver(HongBao oldHongBao, OrderItem parent) {
 
        int threeSaleGiftState = getThreeSaleGiftState(oldHongBao.getState());
 
        String hql = " from ThreeSaleGift tsg where tsg.orderItem.id = ? and tsg.money=? ";
        if (threeSaleGiftState == 0) {
            hql += " and (tsg.state = 1 or tsg.state=2) ";
        } else {
            hql += " and tsg.state=" + threeSaleGiftState;
        }
        List<ThreeSaleGift> list = dao.list(hql, 0, 1, new Serializable[] { parent.getId(), oldHongBao.getMoney() });
        if (list.size() > 0) {
            return list.get(0);
        }
 
        return null;
    }
 
    private int getThreeSaleGiftState(int hbState) {
        int state = 0;
        if (hbState == Constant.HB_GET) {
            state = 2;
        } else if (hbState == Constant.HB_GOT) {
            state = 3;
        } else if (hbState == Constant.HB_DISABLE) {
            state = -1;
        }
        return state;
    }
 
    @Override
    @Transactional
    public void updateByOrderItem(OrderItem orderItem) {
        List<ThreeSaleGift> list = threeSaleGiftMapper.selectByOrderItem(orderItem.getId());
        Integer state = orderItem.getState();
        BigDecimal fanMoney = orderItem.getFanMoney();
        list.parallelStream().forEach(tsg -> {
            ThreeSaleGift updateTsg = new ThreeSaleGift();
            updateTsg.setId(tsg.getId());
            updateTsg.setState(state);
            updateTsg.setMoney(MoneyBigDecimalUtil.mul(tsg.getRate(), fanMoney));
            threeSaleGiftMapper.updateByPrimaryKeySelective(updateTsg);
        });
    }
 
}