1
zyy
yesterday 4fefff17528a878d345ff3311c297a66a671b8d6
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
package com.yami.trading.init.cache;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.trading.bean.model.C2cPaymentMethodConfig;
import com.yami.trading.bean.model.C2cTranslate;
import com.yami.trading.common.constants.PayMethodTypeEnum;
import com.yami.trading.common.constants.PayTemplateParamEnum;
import com.yami.trading.common.constants.RedisKeys;
import com.yami.trading.common.util.Json;
import com.yami.trading.service.c2c.C2cPaymentMethodConfigService;
import com.yami.trading.service.c2c.C2cTranslateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * C2cTranslate 表加字段后,迁移旧记录,根据规则为新加字段填充值;
 * 所有盘口迁移完后,本服务可以关闭掉。
 *
 * @author caster
 * @since 2023/11/18
 **/
@Service
@Slf4j
public class LoadTranslateCache {
    @Autowired
    C2cPaymentMethodConfigService c2cPaymentMethodConfigService;
 
    @Autowired
    C2cTranslateService c2cTranslateService;
 
    @Autowired
    private RedisTemplate redisTemplate;
 
    public void loadData() {
        try {
            transferOldData();
        } catch (Exception e) {
            log.error("------> LoadTranslateCache.loadData 执行 transferOldData 方法报错: ", e);
        }
 
        QueryWrapper<C2cTranslate> allTranslateRecordWrapper = new QueryWrapper<C2cTranslate>();
        List<C2cTranslate> allTranslateRecordList = c2cTranslateService.list(allTranslateRecordWrapper);
 
        for (C2cTranslate oneTranslateEntity : allTranslateRecordList) {
            redisTemplate.opsForValue().set(RedisKeys.C2C_TRANSLATE_CONTENT_LANGUAGE + oneTranslateEntity.getLangKey() +
                    ":" + oneTranslateEntity.getLanguage(), Json.toJsonString(oneTranslateEntity));
        }
    }
 
    /**
     * 翻译功能改造,t_c2c_translate 表添加新字段:lang_key 后,对旧数据做迁移
     */
    private void transferOldData() {
        // 1. 迁移支付模板自定义参数多语言
        QueryWrapper<C2cPaymentMethodConfig> allPayConfigWrapper = new QueryWrapper<C2cPaymentMethodConfig>();
        List<C2cPaymentMethodConfig> allPayConfigList = c2cPaymentMethodConfigService.list(allPayConfigWrapper);
 
        String payParamLangKeyPrefix = "pay.param.";
        for (C2cPaymentMethodConfig oneConfigEntity : allPayConfigList) {
            List<C2cTranslate> c2cPaymentMethods = c2cTranslateService.list(Wrappers.<C2cTranslate>query().lambda().eq(C2cTranslate::getPaymentMethodId, oneConfigEntity.getUuid()));
            for (C2cTranslate oneTranslate : c2cPaymentMethods) {
                if (StrUtil.isNotBlank(oneTranslate.getLangKey())) {
                    // 已做过迁移的,跳过,必要情况下可以强制再迁移一次
                    continue;
                }
 
                if (oneTranslate.getContent().equalsIgnoreCase(oneConfigEntity.getMethodName())) {
                    oneTranslate.setLangKey(payParamLangKeyPrefix + PayTemplateParamEnum.METHOD_NAME.getCode() + "." + oneConfigEntity.getUuid());
                } else if (oneTranslate.getContent().equalsIgnoreCase(oneConfigEntity.getParamName1())) {
                    oneTranslate.setLangKey(payParamLangKeyPrefix + PayTemplateParamEnum.PARAM_1.getCode() + "." + oneConfigEntity.getUuid());
                } else if (oneTranslate.getContent().equalsIgnoreCase(oneConfigEntity.getParamName2())) {
                    oneTranslate.setLangKey(payParamLangKeyPrefix + PayTemplateParamEnum.PARAM_2.getCode() + "." + oneConfigEntity.getUuid());
                } else if (oneTranslate.getContent().equalsIgnoreCase(oneConfigEntity.getParamName3())) {
                    oneTranslate.setLangKey(payParamLangKeyPrefix + PayTemplateParamEnum.PARAM_3.getCode() + "." + oneConfigEntity.getUuid());
                } else if (oneTranslate.getContent().equalsIgnoreCase(oneConfigEntity.getParamName4())) {
                    oneTranslate.setLangKey(payParamLangKeyPrefix + PayTemplateParamEnum.PARAM_4.getCode() + "." + oneConfigEntity.getUuid());
                } else if (oneTranslate.getContent().equalsIgnoreCase(oneConfigEntity.getParamName5())) {
                    oneTranslate.setLangKey(payParamLangKeyPrefix + PayTemplateParamEnum.PARAM_5.getCode() + "." + oneConfigEntity.getUuid());
                } else if (oneTranslate.getContent().equalsIgnoreCase(oneConfigEntity.getParamName6())) {
                    oneTranslate.setLangKey(payParamLangKeyPrefix + PayTemplateParamEnum.PARAM_6.getCode() + "." + oneConfigEntity.getUuid());
                } else if (oneTranslate.getContent().equalsIgnoreCase(oneConfigEntity.getParamName7())) {
                    oneTranslate.setLangKey(payParamLangKeyPrefix + PayTemplateParamEnum.PARAM_7.getCode() + "." + oneConfigEntity.getUuid());
                } else if (oneTranslate.getContent().equalsIgnoreCase(oneConfigEntity.getParamName8())) {
                    oneTranslate.setLangKey(payParamLangKeyPrefix + PayTemplateParamEnum.PARAM_8.getCode() + "." + oneConfigEntity.getUuid());
                } else if (oneTranslate.getContent().equalsIgnoreCase(oneConfigEntity.getParamName9())) {
                    oneTranslate.setLangKey(payParamLangKeyPrefix + PayTemplateParamEnum.PARAM_9.getCode() + "." + oneConfigEntity.getUuid());
                } else if (oneTranslate.getContent().equalsIgnoreCase(oneConfigEntity.getParamName10())) {
                    oneTranslate.setLangKey(payParamLangKeyPrefix + PayTemplateParamEnum.PARAM_10.getCode() + "." + oneConfigEntity.getUuid());
                } else if (oneTranslate.getContent().equalsIgnoreCase(oneConfigEntity.getParamName11())) {
                    oneTranslate.setLangKey(payParamLangKeyPrefix + PayTemplateParamEnum.PARAM_11.getCode() + "." + oneConfigEntity.getUuid());
                } else if (oneTranslate.getContent().equalsIgnoreCase(oneConfigEntity.getParamName12())) {
                    oneTranslate.setLangKey(payParamLangKeyPrefix + PayTemplateParamEnum.PARAM_12.getCode() + "." + oneConfigEntity.getUuid());
                } else if (oneTranslate.getContent().equalsIgnoreCase(oneConfigEntity.getParamName13())) {
                    oneTranslate.setLangKey(payParamLangKeyPrefix + PayTemplateParamEnum.PARAM_13.getCode() + "." + oneConfigEntity.getUuid());
                } else if (oneTranslate.getContent().equalsIgnoreCase(oneConfigEntity.getParamName14())) {
                    oneTranslate.setLangKey(payParamLangKeyPrefix + PayTemplateParamEnum.PARAM_14.getCode() + "." + oneConfigEntity.getUuid());
                } else if (oneTranslate.getContent().equalsIgnoreCase(oneConfigEntity.getParamName15())) {
                    oneTranslate.setLangKey(payParamLangKeyPrefix + PayTemplateParamEnum.PARAM_15.getCode() + "." + oneConfigEntity.getUuid());
                } else {
                    log.error("-------> LoadTranslateCache.transferOldData translate 当前记录:{} 的 content 值不正确:{}", oneTranslate.getUuid(), oneTranslate.getContent());
                    continue;
                }
 
                c2cTranslateService.update(oneTranslate);
            }
        }
 
        // 2. 迁移支付方式多语言
        String payTypeLangKeyPrefix = "pay.type.";
        List<C2cTranslate> list1 = c2cTranslateService.list(Wrappers.<C2cTranslate>query().lambda().eq(C2cTranslate::getPaymentMethodId, "0"));
        List<C2cTranslate> list2 = c2cTranslateService.list(Wrappers.<C2cTranslate>query().lambda().isNull(C2cTranslate::getPaymentMethodId));
        List<C2cTranslate> list3 = c2cTranslateService.list(Wrappers.<C2cTranslate>query().lambda().eq(C2cTranslate::getPaymentMethodId, ""));
 
        List<C2cTranslate> payTypeTranslateList = new ArrayList<>();
        payTypeTranslateList.addAll(list1);
        payTypeTranslateList.addAll(list2);
        payTypeTranslateList.addAll(list3);
 
        for (C2cTranslate oneTranslate : payTypeTranslateList) {
            if (StrUtil.isNotBlank(oneTranslate.getLangKey())) {
                // 已做过迁移的,跳过,必要情况下可以强制再迁移一次
                continue;
            }
 
            PayMethodTypeEnum payType = PayMethodTypeEnum.nameOf(oneTranslate.getContent());
            if (payType == null) {
                log.error("-------> LoadTranslateCache.transferOldData translate 当前记录:{} 的 content 值不正确:{}", oneTranslate.getUuid(), oneTranslate.getContent());
                continue;
            }
 
            oneTranslate.setLangKey(payTypeLangKeyPrefix + payType.getCode());
            oneTranslate.setPaymentMethodId("0");
 
            c2cTranslateService.update(oneTranslate);
        }
    }
}