zj
2024-06-03 3603ecb207f7e712c635f19531e05fac4d19e53f
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
package project.data.internal;
 
import kernel.exception.BusinessException;
import kernel.util.Arith;
import project.data.AdjustmentValue;
import project.data.AdjustmentValueCache;
import project.data.AdjustmentValueService;
import project.data.DataService;
import project.data.model.Realtime;
import project.item.ItemService;
import project.item.model.Item;
 
public class AdjustmentValueServiceImpl implements AdjustmentValueService {
    
    private DataService dataService;
    
    private ItemService itemService;
 
    public void adjust(String symbol, double value, double second) {
        if (value == 0.0D) {
            return;
        }
 
        Realtime realtime = dataService.realtime(symbol).get(0);
        double new_price = realtime.getClose();
        double plus = Math.abs(value);
        if (Arith.div(plus, new_price) > 0.1D) {
            throw new BusinessException("调整偏差过大,超过10%");
        }
 
        if (second <= 0) {
            /**
             * 即时生效
             */
 
            Double currentValue = AdjustmentValueCache.getCurrentValue().get(symbol);
 
            if (currentValue == null) {
                AdjustmentValueCache.getCurrentValue().put(symbol, value);
            } else {
                AdjustmentValueCache.getCurrentValue().put(symbol, Arith.add(currentValue, value));
            }
            /*
             * 持久化缓存
             */
            Item item = this.itemService.cacheBySymbol(symbol, false);
            if (item.getAdjustment_value() != AdjustmentValueCache.getCurrentValue().get(symbol)) {
                item.setAdjustment_value(AdjustmentValueCache.getCurrentValue().get(symbol));
                itemService.update(item);
            }
 
        } else {
            AdjustmentValue adjustmentValue = new AdjustmentValue();
            adjustmentValue.setSymbol(symbol);
            adjustmentValue.setValue(value);
            adjustmentValue.setSecond(second);
            AdjustmentValueCache.getDelayValue().put(symbol, adjustmentValue);
        }
    }
 
    @Override
    public Double getCurrentValue(String symbol) {
        return AdjustmentValueCache.getCurrentValue().get(symbol);
    }
 
    @Override
    public AdjustmentValue getDelayValue(String symbol) {
        return AdjustmentValueCache.getDelayValue().get(symbol);
    }
 
    public void setDataService(DataService dataService) {
        this.dataService = dataService;
    }
 
    public void setItemService(ItemService itemService) {
        this.itemService = itemService;
    }
}