zj
2025-02-25 dd315d5732e14fcf3df71e0cf213cc442bd8607b
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package project.futures.internal;
 
import java.io.Serializable;
import java.util.Date;
import java.util.List;
 
import org.springframework.jdbc.core.JdbcTemplate;
 
import kernel.bo.RecordObjectMapper;
import kernel.web.ApplicationUtil;
import project.Constants;
import project.futures.FuturesRedisKeys;
import project.futures.ProfitAndLossConfig;
import project.futures.ProfitAndLossConfigService;
import project.log.Log;
import project.log.LogService;
import project.party.PartyService;
import project.party.model.Party;
import project.redis.RedisHandler;
 
public class ProfitAndLossConfigServiceImpl implements ProfitAndLossConfigService {
 
    private JdbcTemplate jdbcTemplate;
    private RedisHandler redisHandler;
    private LogService logService;
    private PartyService partyService;
 
    public void save(ProfitAndLossConfig entity, String Operater_username) {
        ProfitAndLossConfig config = this.findByPartyId(entity.getPartyId());
        // 如果存在则更新
        if (config != null) {
            config.setRemark(entity.getRemark());
            config.setType(entity.getType());
            
            String updateSql = "UPDATE T_PROFIT_LOSS_CONFIG SET TYPE=?, REMARK=? WHERE UUID=?";
            jdbcTemplate.update(updateSql, config.getType(), config.getRemark(), config.getId());
            
            redisHandler.setSync(FuturesRedisKeys.FUTURES_PROFIT_LOSS_PARTY_ID + config.getPartyId().toString(), config);
        } else {
            entity.setId(ApplicationUtil.getCurrentTimeUUID());
            String insertSql = "INSERT INTO T_PROFIT_LOSS_CONFIG(UUID,PARTY_ID,TYPE,REMARK) VALUES (?,?,?,?)";
            jdbcTemplate.update(insertSql,entity.getId(),entity.getPartyId(),entity.getType(),entity.getRemark());
            
            redisHandler.setSync(FuturesRedisKeys.FUTURES_PROFIT_LOSS_PARTY_ID + entity.getPartyId().toString(), entity);
        }
        
        String type = "";
        
        if("profit".equals(entity.getType())) {
            type = "盈利";
        }
        if("loss".equals(entity.getType())) {
            type = "亏损";
        }
        if("buy_profit".equals(entity.getType())) {
            type = "买多盈利";
        }
        if("sell_profit".equals(entity.getType())) {
            type = "买空盈利";
        }
        if("buy_profit_sell_loss".equals(entity.getType())) {
            type = "买多盈利并且买空亏损";
        }
        if("sell_profit_buy_loss".equals(entity.getType())) {
            type = "买空盈利并且买多亏损";
        }
        
        Party party = this.partyService.cachePartyBy(entity.getPartyId(), true);
        Log log = new Log();
        log.setCategory(Constants.LOG_CATEGORY_OPERATION);
        log.setOperator(Operater_username);
        log.setUsername(party.getUsername());
        log.setPartyId(entity.getPartyId());
        log.setCreateTime(new Date());
        log.setLog("管理员手动添加场控交割状态。操作类型[" + type + "]。");
        this.logService.saveSync(log);
 
 
    }
 
    public void update(ProfitAndLossConfig entity,String Operater_username) {
        
        String updateSql = "UPDATE T_PROFIT_LOSS_CONFIG SET TYPE=?, REMARK=? WHERE UUID=?";
        jdbcTemplate.update(updateSql, entity.getType(), entity.getRemark(), entity.getId());
        
        redisHandler.setSync(FuturesRedisKeys.FUTURES_PROFIT_LOSS_PARTY_ID + entity.getPartyId().toString(), entity);
        
        String type = "";
        
        if("profit".equals(entity.getType())) {
            type = "盈利";
        }
        if("loss".equals(entity.getType())) {
            type = "亏损";
        }
        if("buy_profit".equals(entity.getType())) {
            type = "买多盈利";
        }
        if("sell_profit".equals(entity.getType())) {
            type = "买空盈利";
        }
        if("buy_profit_sell_loss".equals(entity.getType())) {
            type = "买多盈利并且买空亏损";
        }
        if("sell_profit_buy_loss".equals(entity.getType())) {
            type = "买空盈利并且买多亏损";
        }
        Party party = this.partyService.cachePartyBy(entity.getPartyId(), true);
        Log log = new Log();
        log.setCategory(Constants.LOG_CATEGORY_OPERATION);
        log.setOperator(Operater_username);
        log.setUsername(party.getUsername());
        log.setPartyId(entity.getPartyId());
        log.setCreateTime(new Date());
        log.setLog("管理员手动修改场控交割状态。修改后操作类型为[" + type + "]。");
        this.logService.saveSync(log);
    }
 
    public void delete(String id,String Operater_username) {
        ProfitAndLossConfig entity = findById(id);
        if (null != entity) {
            jdbcTemplate.update("DELETE FROM T_PROFIT_LOSS_CONFIG WHERE UUID=?", entity.getId());
            redisHandler.remove(FuturesRedisKeys.FUTURES_PROFIT_LOSS_PARTY_ID + entity.getPartyId().toString());
            
            Party party = this.partyService.cachePartyBy(entity.getPartyId(), true);
            Log log = new Log();
            log.setCategory(Constants.LOG_CATEGORY_OPERATION);
            log.setOperator(Operater_username);
            log.setUsername(party.getUsername());
            log.setPartyId(entity.getPartyId());
            log.setCreateTime(new Date());
            log.setLog("管理员手动删除场控交割状态");
            this.logService.saveSync(log);
        }
    }
 
    public ProfitAndLossConfig findById(String id) {
        List<ProfitAndLossConfig> list = jdbcTemplate.query("SELECT * FROM T_PROFIT_LOSS_CONFIG WHERE UUID=?", RecordObjectMapper.newInstance(ProfitAndLossConfig.class), id);
        if (null != list && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
 
    public ProfitAndLossConfig findByPartyId(Serializable partyId) {
        List<ProfitAndLossConfig> list = jdbcTemplate.query("SELECT * FROM T_PROFIT_LOSS_CONFIG WHERE PARTY_ID=?", 
                RecordObjectMapper.newInstance(ProfitAndLossConfig.class), partyId);
        if (list.size() > 0)
            return list.get(0);
        return null;
    }
 
    public ProfitAndLossConfig cacheByPartyId(Serializable partyId) {
        return (ProfitAndLossConfig) redisHandler
                .get(FuturesRedisKeys.FUTURES_PROFIT_LOSS_PARTY_ID + partyId.toString());
    }
 
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
 
    public void setRedisHandler(RedisHandler redisHandler) {
        this.redisHandler = redisHandler;
    }
 
    public void setLogService(LogService logService) {
        this.logService = logService;
    }
 
    public void setPartyService(PartyService partyService) {
        this.partyService = partyService;
    }
 
}