ipo
zyy
2026-01-08 4affbdf8938d321c0926bc2b1832dfc81c317ffa
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
package com.yami.trading.admin.task.cms;
 
import java.lang.reflect.Field;
import java.util.List;
import java.util.regex.Pattern;
 
import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yami.trading.admin.facade.MachineTranslationService;
import com.yami.trading.bean.item.domain.ItemSummary;
import com.yami.trading.common.config.ThreadPool;
import com.yami.trading.service.item.ItemSummaryService;
 
import lombok.extern.slf4j.Slf4j;
 
@Slf4j
@Component
public class ItemSummaryTranslate {
 
    @Autowired
    private ItemSummaryService itemSummaryService;
    
    @Autowired
    private MachineTranslationService translationService;
    
    private ThreadPoolTaskExecutor taskExecutor=ThreadPool.getApplicationThreadPool();
    
    private static final Pattern CHINESE_PATTERN = Pattern.compile("[\u4e00-\u9fa5]");
 
    public ItemSummary translateChineseFields(ItemSummary item) throws IllegalAccessException {
        if (item == null) {
            throw new IllegalArgumentException("ItemSummary cannot be null");
        }
 
        ItemSummary enItemSummary = itemSummaryService.getBaseMapper().selectOne(new LambdaQueryWrapper<ItemSummary>().eq(ItemSummary::getSymbol, item.getSymbol()).eq(ItemSummary::getLang, "en"));
        if (enItemSummary == null) {
            enItemSummary = new ItemSummary();
        }
        BeanUtil.copyProperties(item, enItemSummary, "lang", "uuid");
        // 遍历类中的所有字段
        for (Field field : ItemSummary.class.getDeclaredFields()) {
            field.setAccessible(true);  // 使得private字段可以访问
            Object fieldValue = field.get(item);  // 获取字段的值
            if (fieldValue == null) {
                continue;
            }
            // 只处理字符串类型的字段
            if (fieldValue instanceof String) {
                String value = (String) fieldValue;
                // 如果字段不为空并且含有中文字符,则进行翻译
                if (CHINESE_PATTERN.matcher(value).find()) {
                    String translated = translationService.translate(value);  // 调用TranslateAPI进行翻译
                    field.set(enItemSummary, translated);  // 将翻译后的结果设置回字段
                }
            }
        }
        return enItemSummary;
 
    }
 
    @Scheduled(cron = "0 */5 * ? * *")
    public void translate() {
        QueryWrapper<ItemSummary> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("translate");
        queryWrapper.eq("lang", "zh-CN");
        List<ItemSummary> list = itemSummaryService.list(queryWrapper);
        for (ItemSummary itemSummary : list) {
            taskExecutor.execute(() -> {
                try {
                    ItemSummary itemSummaryEn = translateChineseFields(itemSummary);
                    itemSummaryEn.setSymbol(itemSummary.getSymbol());
//                    itemSummaryEn.setUuid(null);
                    itemSummaryEn.setLang("en");
                    itemSummary.setTranslate("1");
                    itemSummaryService.saveOrUpdate(itemSummaryEn);
                    itemSummaryService.updateById(itemSummary);
                } catch (Exception e) {
                    log.error("翻译简况失败: {}", itemSummary.getSymbol(), e);
                }
            });
        }
    }
}