zyy
2025-12-06 ab7e92d5154b5acf05699bd527c4d8b5fff10550
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package com.yami.trading.service.ats.impl;
 
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.trading.bean.ats.StockAts;
import com.yami.trading.bean.ats.dto.StockAtsDto;
import com.yami.trading.bean.contract.domain.ContractApplyOrder;
import com.yami.trading.bean.dz.ExchangeApplyOrderDz;
import com.yami.trading.bean.exchange.ExchangeApplyOrder;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.bean.model.MoneyLog;
import com.yami.trading.bean.model.User;
import com.yami.trading.bean.model.Wallet;
import com.yami.trading.bean.model.WalletExtend;
import com.yami.trading.common.constants.Constants;
import com.yami.trading.common.domain.Result;
import com.yami.trading.common.exception.YamiShopBindException;
import com.yami.trading.common.util.Arith;
import com.yami.trading.common.util.RandomUtil;
import com.yami.trading.dao.ats.StockAtsMapper;
import com.yami.trading.service.MoneyLogService;
import com.yami.trading.service.WalletService;
import com.yami.trading.service.ats.StockAtsService;
import com.yami.trading.service.exchange.ExchangeApplyOrderService;
import com.yami.trading.service.item.ItemService;
import com.yami.trading.service.user.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
 
@Service
@Slf4j
public class StockAtsServiceImpl extends ServiceImpl<StockAtsMapper, StockAts> implements StockAtsService {
 
    @Autowired
    StockAtsMapper stockAtsMapper;
    @Autowired
    UserService userService;
    @Autowired
    WalletService walletService;
    @Autowired
    MoneyLogService moneyLogService;
    @Autowired
    ExchangeApplyOrderService exchangeApplyOrderService;
    @Autowired
    ItemService itemService;
 
    @Override
    public Result getList(int pageNum, int pageSize, String partyId) {
        try {
            QueryWrapper<StockAts> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", partyId);
 
            Page<StockAts> page = new Page<>(pageNum, pageSize);
            page = stockAtsMapper.selectPage(page, queryWrapper);
            return Result.succeed(page);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return Result.failed("获取失败");
    }
 
    @Transactional
    @Override
    public Result buyAts(double price, String partyId) {
        try {
            if (price <= 0) {
                throw new YamiShopBindException("请输入");
            }
            User party = userService.getById(partyId);
            if (!party.isEnabled()) {
                throw new YamiShopBindException("用户已禁用");
            }
 
            BigDecimal amt = BigDecimal.valueOf(price);
 
            Wallet wallet = this.walletService.saveWalletByPartyId(partyId);
            if (wallet.getMoney().compareTo(amt) < 0) {
                return Result.failed("余额不足");
            }
 
 
            StockAts order = new StockAts();
            order.setUserId(partyId);
            order.setPrice(amt);
            order.setAddTime(new Date());
            order.setOrderNo(com.yami.trading.common.util.DateUtil.getToday("yyMMddHHmmss") + RandomUtil.getRandomNum(8));
            stockAtsMapper.insert(order);
 
            return Result.succeed("提交成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.failed("失败");
        }
    }
 
    @Override
    public Result<Page<StockAtsDto>> getListByAdmin(int pageNum, int pageSize, String keywords) {
        try {
            Page<StockAtsDto> page = new Page<>(pageNum, pageSize);
            stockAtsMapper.getListByAdmin(page, keywords);
            return Result.succeed(page);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return Result.failed("获取失败");
    }
 
    @Transactional
    @Override
    public Result atsCheck(String id, Integer checkType, String stockCode, double closePrice, double price) {
        try {
            StockAts order = stockAtsMapper.selectById(id);
            if (order == null) {
                throw new YamiShopBindException("订单不存在");
            }
            if (!order.getState().equals(ExchangeApplyOrderDz.STATE_SUBMITTED)) {
                throw new YamiShopBindException("订单已审核");
            }
            //1.通过  2.拒绝
            if (checkType == 2) {
                order.setState(ExchangeApplyOrderDz.STATE_FAILED);
                stockAtsMapper.updateById(order);
                return Result.succeed("操作成功");
            }
            if (price <= 0 || closePrice <= 0) {
                throw new YamiShopBindException("输入正确金额");
            }
 
 
            Wallet wallet = this.walletService.saveWalletByPartyId(order.getUserId());
            BigDecimal buyAmt = BigDecimal.valueOf(price);
            BigDecimal closeAmt = BigDecimal.valueOf(closePrice);
            BigDecimal orderFree = BigDecimal.ZERO;;
            if (buyAmt.doubleValue() < 300) {
                orderFree = BigDecimal.ONE;
            }
            BigDecimal orderAmt = buyAmt.add(orderFree);
            if (wallet.getMoney().compareTo(orderAmt) < 0) {
                throw new YamiShopBindException("订单失败,用户资金不足");
            }
 
            Item item = itemService.findBySymbol(stockCode);
            if (item == null) {
                throw new YamiShopBindException("股票代码不存在");
            }
            if (!item.getType().equalsIgnoreCase(Item.US_STOCKS)) {
                throw new YamiShopBindException("请输入美股代码");
            }
 
            BigDecimal amountBefore = wallet.getMoney();
 
            order.setStockCode(item.getSymbol());
            order.setStockName(item.getName());
            order.setStockSpell(item.getSymbolData());
            order.setPrice(buyAmt);
            order.setFee(orderFree.doubleValue());
            order.setState(ExchangeApplyOrderDz.STATE_POSITION);
            order.setBuyTime(new Date());
            order.setClosePrice(closeAmt);
            stockAtsMapper.updateById(order);
            walletService.update(wallet.getUserId(), Arith.sub(0, orderAmt.doubleValue()));
 
            //TODO 转持仓
            intoPosition(order);
 
            MoneyLog log = new MoneyLog();
            log.setCategory(Constants.MONEYLOG_CATEGORY_EXCHANGE);
            String name = Constants.MONEYLOG_MAP.get(Item.US_STOCKS);
            String type2 = Constants.MONEYLOG_MAP_TYPE.get(Item.US_STOCKS);
            log.setAmountBefore(amountBefore);
            log.setAmount(orderAmt.negate());
            log.setAmountAfter(amountBefore.subtract(orderAmt));
            log.setLog(name + type2 + "ATS买入成交" + ",订单号[" + order.getOrderNo() + "]");
            log.setUserId(order.getUserId());
            log.setWalletType(Constants.WALLET);
            log.setSymbol(order.getStockCode());
            log.setContentType(type2 + Constants.MONEYLOG_CONTENT_OPEN);
            log.setCreateTime(new Date());
            log.setUpdateTime(new Date());
            moneyLogService.save(log);
            return Result.succeed("审核成功,订单已转持仓");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.failed("操作失败:" + e.getMessage());
        }
    }
 
    public void intoPosition(StockAts order) {
        //到账币种数量
        BigDecimal buyNum = order.getPrice().divide(order.getClosePrice(), 5, RoundingMode.HALF_UP);
 
        ExchangeApplyOrder exOrder = new ExchangeApplyOrder();
        exOrder.setPartyId(order.getUserId());
        exOrder.setSymbol(order.getStockCode());
        exOrder.setOffset(ExchangeApplyOrder.OFFSET_OPEN);
        exOrder.setSymbolValue(buyNum.doubleValue());
        exOrder.setVolume(order.getPrice().doubleValue());
        exOrder.setPrice(order.getClosePrice().doubleValue());
        exOrder.setOrderPriceType(ContractApplyOrder.ORDER_PRICE_TYPE_OPPONENT);
        exOrder.setState(ExchangeApplyOrder.STATE_CREATED);
        exOrder.setClosePrice(order.getClosePrice().doubleValue());
        exOrder.setOrderNo(com.yami.trading.common.util.DateUtil.getToday("yyMMddHHmmss") + RandomUtil.getRandomNum(8));
        exOrder.setFee(order.getFee());
        exOrder.setCreateTime(new Date());
 
        double realValue = exOrder.getSymbolValue();
        //入账
        WalletExtend walletExtend = walletService.saveExtendByPara(exOrder.getPartyId(), exOrder.getSymbol());
        double amountBeforeExtend = walletExtend.getAmount();
        this.walletService.updateExtend(walletExtend.getPartyId(), walletExtend.getWallettype(), realValue);
 
        MoneyLog log = new MoneyLog();
        log.setCategory(Constants.MONEYLOG_CATEGORY_EXCHANGE);
        String name = Constants.MONEYLOG_MAP.get(Item.US_STOCKS);
        String type2 = Constants.MONEYLOG_MAP_TYPE.get(Item.US_STOCKS);
        log.setAmountBefore(new BigDecimal(amountBeforeExtend));
        log.setAmount(BigDecimal.valueOf(realValue));
        log.setAmountAfter(BigDecimal.valueOf(amountBeforeExtend + realValue));
        log.setLog(name + type2 + "ATS买入委托单成交,订单号[" + exOrder.getOrderNo() + "]");
        log.setUserId(exOrder.getPartyId());
        log.setSymbol(exOrder.getSymbol());
        log.setWalletType(exOrder.getSymbol());
        log.setContentType(type2 + Constants.MONEYLOG_CONTENT_OPEN);
        // 记录账变日志
        moneyLogService.save(log);
 
        exchangeApplyOrderService.save(exOrder);
    }
 
}