fq
zj
2025-06-13 08ea05f85c9dd77834e56a2b912b2c43ac8888cd
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
package project.contract.data.loadcache;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
 
import kernel.bo.RecordObjectMapper;
import kernel.util.Arith;
import project.contract.ContractOrder;
import project.contract.ContractRedisKeys;
import project.redis.RedisHandler;
import project.wallet.AssetService;
 
public class ContractLoadCacheService {
    
    private static final Logger logger = LoggerFactory.getLogger(ContractLoadCacheService.class);
 
    private JdbcTemplate jdbcTemplate;
    private RedisHandler redisHandler;
    private AssetService assetService;
 
    public void loadcache() {
        load();
        logger.info("完成ContractOrder数据加载redis");
    }
 
    public void load() {
         List<ContractOrder> list = jdbcTemplate.query("SELECT * FROM T_CONTRACT_ORDER", 
                    RecordObjectMapper.newInstance(ContractOrder.class));
         
        Map<String, Map<String, ContractOrder>> cacheMap = new ConcurrentHashMap<String, Map<String, ContractOrder>>();
        
        // 永续合约:总资产、总保证金、总未实现盈利
        Map<String, Map<String, Double>> contractAssetsMap = new ConcurrentHashMap<String, Map<String, Double>>();
 
        for (ContractOrder order : list) {
            
            if (ContractOrder.STATE_SUBMITTED.equals(order.getState())) {
                
                if (cacheMap.containsKey(order.getPartyId())) {
                    Map<String, ContractOrder> map = cacheMap.get(order.getPartyId().toString());
                    map.put(order.getOrder_no(), order);
                    cacheMap.put(order.getPartyId().toString(), map);
                } else {
                    Map<String, ContractOrder> map = new ConcurrentHashMap<String, ContractOrder>();
                    map.put(order.getOrder_no(), order);
                    cacheMap.put(order.getPartyId().toString(), map);
                }
                
                // 获取 单个订单 永续合约总资产、总保证金、总未实现盈利
                Map<String, Double> contractAssetsOrder = this.assetService.getMoneyContractByOrder(order);
                
                if (contractAssetsMap.containsKey(order.getPartyId())) {
                    Map<String, Double> contractAssetsOld = contractAssetsMap.get(order.getPartyId().toString());
                    if (null == contractAssetsOld) {
                        contractAssetsOld = new HashMap<String, Double>();
                        contractAssetsOld.put("money_contract", 0.000D);
                        contractAssetsOld.put("money_contract_deposit", 0.000D);
                        contractAssetsOld.put("money_contract_profit", 0.000D);
                    }
                    contractAssetsOld.put("money_contract", Arith.add(contractAssetsOld.get("money_contract"), contractAssetsOrder.get("money_contract")));
                    contractAssetsOld.put("money_contract_deposit", Arith.add(contractAssetsOld.get("money_contract_deposit"), contractAssetsOrder.get("money_contract_deposit")));
                    contractAssetsOld.put("money_contract_profit", Arith.add(contractAssetsOld.get("money_contract_profit"), contractAssetsOrder.get("money_contract_profit")));                
                    contractAssetsMap.put(order.getPartyId().toString(), contractAssetsOld);
                } else {
                    contractAssetsMap.put(order.getPartyId().toString(), contractAssetsOrder);
                }
            }
            
            this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ORDERNO + order.getOrder_no(),  order);
        }
        
        for (Entry<String, Map<String, ContractOrder>> entry : cacheMap.entrySet()) {
            this.redisHandler.setSync(ContractRedisKeys.CONTRACT_SUBMITTED_ORDER_PARTY_ID + entry.getKey(), entry.getValue());
        }
        
        for (Entry<String, Map<String, Double>> entry : contractAssetsMap.entrySet()) {
            this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_PARTY_ID + entry.getKey(), entry.getValue().get("money_contract"));
            this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_DEPOSIT_PARTY_ID + entry.getKey(), entry.getValue().get("money_contract_deposit"));        
            this.redisHandler.setSync(ContractRedisKeys.CONTRACT_ASSETS_PROFIT_PARTY_ID + entry.getKey(), entry.getValue().get("money_contract_profit"));
        }
    }
 
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
 
    public void setRedisHandler(RedisHandler redisHandler) {
        this.redisHandler = redisHandler;
    }
 
    public void setAssetService(AssetService assetService) {
        this.assetService = assetService;
    }
 
}