## 去除国际化异步处理;
This commit is contained in:
parent
86f29b3278
commit
98cec09009
|
@ -3,21 +3,12 @@ package com.hzs.third.mq;
|
|||
import com.hzs.common.core.domain.R;
|
||||
import com.hzs.third.mq.dto.DelayedMessageDTO;
|
||||
|
||||
/**
|
||||
* @Description:
|
||||
* @Author: sui q
|
||||
* @Time: 2023/3/9 15:21
|
||||
* @Classname: IDelayedMessage
|
||||
* @PackageName: com.hzs.third.mq
|
||||
*/
|
||||
public interface IDelayedMessageApi {
|
||||
|
||||
/**
|
||||
* 发送延时队列消息
|
||||
*
|
||||
* @param delayedMessageDTO 内容
|
||||
* @return: void
|
||||
* @Author: sui q
|
||||
* @Date: 2023/3/9 15:18
|
||||
*/
|
||||
R<Boolean> sendDelayedMessage(DelayedMessageDTO delayedMessageDTO);
|
||||
}
|
||||
|
|
|
@ -10,11 +10,7 @@ import java.util.Date;
|
|||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Description: 用于延时队列,删除redis缓存
|
||||
* @Author: sui q
|
||||
* @Time: 2023/3/9 15:49
|
||||
* @Classname: DelayedMessageDTO
|
||||
* @PackageName: com.hzs.third.mq.dto
|
||||
* 用于延时队列,删除redis缓存
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
|
|
|
@ -29,9 +29,6 @@ import java.util.*;
|
|||
|
||||
/**
|
||||
* 国际化翻译控制器
|
||||
*
|
||||
* @author hzs
|
||||
* @since 2022-08-08
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
|
@ -148,11 +145,7 @@ public class BdTransactionController extends BaseController {
|
|||
}
|
||||
|
||||
/**
|
||||
* @description: 下载模板
|
||||
* @author: zhang jing
|
||||
* @date: 2023/4/6 11:48
|
||||
* @param: [response]
|
||||
* @return: void
|
||||
* 下载模板
|
||||
**/
|
||||
@PostMapping("/download")
|
||||
public void download(HttpServletResponse response) {
|
||||
|
@ -180,22 +173,6 @@ public class BdTransactionController extends BaseController {
|
|||
return AjaxResult.success(iBdTransactionService.importData(ordersList, updateSupport, SecurityUtils.getUserId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化语种(以中文为准,翻译为其它语种)
|
||||
*
|
||||
* @param language 语种
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/init-languages")
|
||||
public AjaxResult initLanguages(@RequestParam("language") Integer language) {
|
||||
ELanguages eLanguages = ELanguages.getLanguagesByValue(language);
|
||||
if (null == eLanguages) {
|
||||
return AjaxResult.error("语种不存在");
|
||||
}
|
||||
|
||||
return toAjax(iBdTransactionService.initToLanguage(eLanguages));
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新资源缓存
|
||||
*
|
||||
|
@ -265,20 +242,4 @@ public class BdTransactionController extends BaseController {
|
|||
return AjaxResult.error("翻译刷新处理失败");
|
||||
}
|
||||
|
||||
/**
|
||||
* 以指定语种为基础初始数据资源
|
||||
*
|
||||
* @param language 基础语种
|
||||
* @param pkIdList 翻译数据ID列表
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/init-other-language/{language}")
|
||||
public AjaxResult initOtherLanguage(@PathVariable("language") Integer language, @RequestParam List<Long> pkIdList) {
|
||||
ELanguages eLanguages = ELanguages.getLanguagesByValue(language);
|
||||
if (null == eLanguages) {
|
||||
return AjaxResult.error("语种不存在");
|
||||
}
|
||||
return toAjax(iBdTransactionService.initOtherLanguage(eLanguages, pkIdList));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -12,9 +12,6 @@ import java.util.Set;
|
|||
|
||||
/**
|
||||
* 统一翻译表 Mapper 接口
|
||||
*
|
||||
* @author hzs
|
||||
* @since 2022-08-08
|
||||
*/
|
||||
public interface BdTransactionMapper extends BaseMapper<BdTransaction> {
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package com.hzs.system.base.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.hzs.common.core.enums.ELanguages;
|
||||
import com.hzs.common.domain.system.base.BdTransaction;
|
||||
import com.hzs.common.domain.system.base.ext.TransactionParam;
|
||||
import com.hzs.common.domain.system.base.ext.TransactionResult;
|
||||
|
@ -10,9 +9,6 @@ import java.util.*;
|
|||
|
||||
/**
|
||||
* 统一翻译表 服务类
|
||||
*
|
||||
* @author hzs
|
||||
* @since 2022-08-08
|
||||
*/
|
||||
public interface IBdTransactionService extends IService<BdTransaction> {
|
||||
|
||||
|
@ -108,23 +104,6 @@ public interface IBdTransactionService extends IService<BdTransaction> {
|
|||
*/
|
||||
List<BdTransaction> queryListByColumn(String column, boolean isCache, boolean isUpload);
|
||||
|
||||
/**
|
||||
* 初始化指定语种
|
||||
*
|
||||
* @param eLanguages 指定语种
|
||||
* @return
|
||||
*/
|
||||
boolean initToLanguage(ELanguages eLanguages);
|
||||
|
||||
/**
|
||||
* 以指定语种为基础初始数据资源
|
||||
*
|
||||
* @param eLanguages 指定语种
|
||||
* @param pkIdList 翻译数据ID列表
|
||||
* @return
|
||||
*/
|
||||
boolean initOtherLanguage(ELanguages eLanguages, List<Long> pkIdList);
|
||||
|
||||
/**
|
||||
* 刷新资源
|
||||
*
|
||||
|
|
|
@ -7,9 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.hzs.common.core.constant.CacheConstants;
|
||||
import com.hzs.common.core.constant.LanguageConstants;
|
||||
import com.hzs.common.core.constant.RabbitMqConstants;
|
||||
import com.hzs.common.core.enums.ELanguages;
|
||||
import com.hzs.common.core.enums.EYesNo;
|
||||
import com.hzs.common.core.service.RedisService;
|
||||
import com.hzs.common.core.utils.DateUtils;
|
||||
import com.hzs.common.core.utils.OssUtil;
|
||||
|
@ -19,9 +17,7 @@ import com.hzs.common.domain.system.base.ext.TransactionParam;
|
|||
import com.hzs.common.domain.system.base.ext.TransactionResult;
|
||||
import com.hzs.system.base.mapper.BdTransactionMapper;
|
||||
import com.hzs.system.base.service.IBdTransactionService;
|
||||
import com.hzs.third.mq.dto.TransactionMqDTO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
@ -32,27 +28,14 @@ import java.util.*;
|
|||
|
||||
/**
|
||||
* 统一翻译表 服务实现类
|
||||
*
|
||||
* @author hzs
|
||||
* @since 2022-08-08
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class BdTransactionServiceImpl extends ServiceImpl<BdTransactionMapper, BdTransaction> implements IBdTransactionService {
|
||||
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
|
||||
/**
|
||||
* @description: 导入统一翻译信息
|
||||
* @author: zhang jing
|
||||
* @date: 2023/4/27 17:17
|
||||
* @param: [transactionList, updateSupport, userId]
|
||||
* @return: java.lang.String
|
||||
**/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean importData(List<BdTransaction> transactionList,
|
||||
|
@ -85,16 +68,6 @@ public class BdTransactionServiceImpl extends ServiceImpl<BdTransactionMapper, B
|
|||
public Integer insertTransactionExt(TransactionParam transactionParam) {
|
||||
// 入库并返回
|
||||
if (baseMapper.insertTransactionExt(transactionParam) > 0) {
|
||||
// MQ异步补全其它国际化翻译
|
||||
rabbitTemplate.convertAndSend(RabbitMqConstants.TRANSLATE_EXCHANGE, RabbitMqConstants.TRANSLATE_KEY,
|
||||
TransactionMqDTO.builder()
|
||||
.pkId(transactionParam.getPkId())
|
||||
.key(transactionParam.getKey() + transactionParam.getPkId())
|
||||
.column(transactionParam.getColumn())
|
||||
.columnValue(transactionParam.getColumnValue())
|
||||
.isCache(transactionParam.getIsCache())
|
||||
.build());
|
||||
|
||||
return transactionParam.getPkId();
|
||||
}
|
||||
return null;
|
||||
|
@ -104,17 +77,6 @@ public class BdTransactionServiceImpl extends ServiceImpl<BdTransactionMapper, B
|
|||
public Integer updateTransactionExt(TransactionParam transactionParam, Boolean updateOther) {
|
||||
// 入库并返回
|
||||
if (baseMapper.updateTransactionExt(transactionParam) > 0) {
|
||||
if (null != updateOther && updateOther) {
|
||||
// MQ异步补全其它国际化翻译
|
||||
rabbitTemplate.convertAndSend(RabbitMqConstants.TRANSLATE_EXCHANGE, RabbitMqConstants.TRANSLATE_KEY,
|
||||
TransactionMqDTO.builder()
|
||||
.pkId(transactionParam.getPkId())
|
||||
.key(transactionParam.getKey())
|
||||
.column(transactionParam.getColumn())
|
||||
.columnValue(transactionParam.getColumnValue())
|
||||
.isCache(transactionParam.getIsCache())
|
||||
.build());
|
||||
}
|
||||
return transactionParam.getPkId();
|
||||
}
|
||||
return null;
|
||||
|
@ -167,55 +129,6 @@ public class BdTransactionServiceImpl extends ServiceImpl<BdTransactionMapper, B
|
|||
return baseMapper.queryListByColumn(column, isCache, isUpload);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean initToLanguage(ELanguages eLanguages) {
|
||||
List<TransactionResult> transactionResultList = baseMapper.queryToColumnList(eLanguages.getColumn());
|
||||
if (CollectionUtil.isNotEmpty(transactionResultList)) {
|
||||
for (TransactionResult transactionResult : transactionResultList) {
|
||||
// MQ异步补全其它国际化翻译
|
||||
rabbitTemplate.convertAndSend(RabbitMqConstants.TRANSLATE_EXCHANGE, RabbitMqConstants.TRANSLATE_KEY,
|
||||
TransactionMqDTO.builder()
|
||||
.pkId(transactionResult.getPkId())
|
||||
.column(ELanguages.CN.getColumn())
|
||||
.columnValue(transactionResult.getFromContent())
|
||||
.isCache(EYesNo.YES.getIntValue())
|
||||
.toApiKey(eLanguages.getApiKey())
|
||||
.build());
|
||||
}
|
||||
} else {
|
||||
log.error("{} 没有可初始化内容", eLanguages.getLabel());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean initOtherLanguage(ELanguages eLanguages, List<Long> pkIdList) {
|
||||
List<TransactionResult> transactionResultList = baseMapper.queryFromColumnList(eLanguages.getColumn(), pkIdList);
|
||||
if (CollectionUtil.isNotEmpty(transactionResultList)) {
|
||||
for (TransactionResult transactionResult : transactionResultList) {
|
||||
// MQ异步补全其它国际化翻译
|
||||
rabbitTemplate.convertAndSend(RabbitMqConstants.TRANSLATE_EXCHANGE, RabbitMqConstants.TRANSLATE_KEY,
|
||||
TransactionMqDTO.builder()
|
||||
.pkId(transactionResult.getPkId())
|
||||
.column(eLanguages.getColumn())
|
||||
.columnValue(transactionResult.getFromContent())
|
||||
.isCache(EYesNo.YES.getIntValue())
|
||||
.build());
|
||||
}
|
||||
} else {
|
||||
log.error("{} 没有可初始化内容", eLanguages.getLabel());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新资源
|
||||
*
|
||||
* @param languages 刷新语种(ELanguages的value)
|
||||
* @param isCache 是否刷新缓存
|
||||
* @param isOss 是否刷新OSS
|
||||
*/
|
||||
@Override
|
||||
public void flushResources(Integer languages, boolean isCache, boolean isOss) {
|
||||
String column = null;
|
||||
|
|
|
@ -9,18 +9,6 @@ public class RabbitMqConstants {
|
|||
* 统一mq前缀
|
||||
*/
|
||||
public final static String PREFIX = "global.";
|
||||
/**
|
||||
* 翻译MQ exchange
|
||||
*/
|
||||
public static final String TRANSLATE_EXCHANGE = PREFIX + "translate.exchange";
|
||||
/**
|
||||
* 翻译MQ queue
|
||||
*/
|
||||
public static final String TRANSLATE_QUEUE = PREFIX + "translate.queue";
|
||||
/**
|
||||
* 翻译MQ key
|
||||
*/
|
||||
public static final String TRANSLATE_KEY = PREFIX + "translate.key";
|
||||
|
||||
/**
|
||||
* 奖金秒接 exchange
|
||||
|
|
|
@ -86,34 +86,4 @@ public enum ELanguages {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据语种column,获取语种
|
||||
*
|
||||
* @param column
|
||||
* @return
|
||||
*/
|
||||
public static ELanguages getLanguagesByColumn(String column) {
|
||||
for (ELanguages eLanguages : ELanguages.values()) {
|
||||
if (eLanguages.getColumn().equals(column)) {
|
||||
return eLanguages;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据语种apiKey,获取语种
|
||||
*
|
||||
* @param apiKey
|
||||
* @return
|
||||
*/
|
||||
public static ELanguages getLanguagesByApiKey(String apiKey) {
|
||||
for (ELanguages eLanguages : ELanguages.values()) {
|
||||
if (eLanguages.getApiKey().equals(apiKey)) {
|
||||
return eLanguages;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,12 +15,7 @@ import java.io.Serializable;
|
|||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 统一翻译表
|
||||
* </p>
|
||||
*
|
||||
* @author hzs
|
||||
* @since 2022-08-08
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
|
@ -227,7 +222,6 @@ public class BdTransaction implements Serializable {
|
|||
private Integer isCache;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 是否上传(0=是,1=否)
|
||||
*/
|
||||
|
|
|
@ -10,11 +10,7 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Description: 配置队列
|
||||
* @Author: sui q
|
||||
* @Time: 2023/3/9 15:26
|
||||
* @Classname: DelayRabbitConfig
|
||||
* @PackageName: com.hzs.third.mqcall.config
|
||||
* 配置队列
|
||||
*/
|
||||
@Configuration
|
||||
@Slf4j
|
||||
|
@ -45,12 +41,14 @@ public class DelayRabbitConfig {
|
|||
* 需要将一个队列绑定到交换机上,要求该消息与一个特定的路由键完全匹配。
|
||||
* 这是一个完整的匹配。如果一个队列绑定到该交换机上要求路由键 “dog”,则只有被标记为“dog”的消息才被转发,
|
||||
* 不会转发dog.puppy,也不会转发dog.guard,只会转发dog。
|
||||
*
|
||||
* @return DirectExchange
|
||||
*/
|
||||
@Bean
|
||||
public DirectExchange orderDelayExchange() {
|
||||
return new DirectExchange(RabbitMqConstants.DELAY_EXCHANGE);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Binding dlxBinding() {
|
||||
return BindingBuilder.bind(delayOrderQueue()).to(orderDelayExchange()).with(RabbitMqConstants.DELAY_ROUTING_KEY);
|
||||
|
@ -60,6 +58,7 @@ public class DelayRabbitConfig {
|
|||
public Queue effectQueue() {
|
||||
return new Queue(RabbitMqConstants.EFFECT_QUEUE_NAME, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将路由键和某模式进行匹配。此时队列需要绑定要一个模式上。
|
||||
* 符号“#”匹配一个或多个词,符号“*”匹配不多不少一个词。因此“audit.#”能够匹配到“audit.irs.corporate”,但是“audit.*” 只会匹配到“audit.irs”。
|
||||
|
|
|
@ -13,19 +13,14 @@ import org.springframework.messaging.Message;
|
|||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @description: 活动公共的监听方法, 用于处理活动
|
||||
* @author: sui q
|
||||
* @time: 2023/4/28 11:21
|
||||
* @classname: ActivityListener
|
||||
* @package_name: com.hzs.third.mqcall.listener
|
||||
* version 1.0.0
|
||||
* 活动公共的监听方法, 用于处理活动
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class ActivityListener {
|
||||
|
||||
@DubboReference
|
||||
IActivityServiceApi activityServiceApi;
|
||||
IActivityServiceApi iActivityServiceApi;
|
||||
|
||||
@RabbitListener(bindings = @QueueBinding(
|
||||
exchange = @Exchange(value = RabbitMqConstants.ACTIVITY_EXCHANGE, type = "topic"),
|
||||
|
@ -40,7 +35,7 @@ public class ActivityListener {
|
|||
Thread.sleep(1000);
|
||||
Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
|
||||
channel.basicAck(deliveryTag, false);
|
||||
activityServiceApi.handleActivity(saOrderExt);
|
||||
iActivityServiceApi.handleActivity(saOrderExt);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
e.printStackTrace();
|
||||
|
|
|
@ -1,120 +0,0 @@
|
|||
package com.hzs.third.mqcall.listener;
|
||||
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import com.hzs.common.core.constant.RabbitMqConstants;
|
||||
import com.hzs.common.core.domain.R;
|
||||
import com.hzs.common.core.enums.ELanguages;
|
||||
import com.hzs.common.core.utils.StringUtils;
|
||||
import com.hzs.system.base.ITransactionServiceApi;
|
||||
import com.hzs.system.base.dto.TransactionDTO;
|
||||
import com.hzs.third.mq.dto.TransactionMqDTO;
|
||||
import com.hzs.third.translate.dto.TranslateParam;
|
||||
import com.hzs.third.translate.service.ITranslateService;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.dubbo.config.annotation.DubboReference;
|
||||
import org.springframework.amqp.rabbit.annotation.*;
|
||||
import org.springframework.amqp.support.AmqpHeaders;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 国际化翻译监听
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class TransactionListener {
|
||||
|
||||
@Autowired
|
||||
private ITranslateService iTranslateService;
|
||||
|
||||
@DubboReference
|
||||
ITransactionServiceApi iTransactionServiceApi;
|
||||
|
||||
/**
|
||||
* 国际化全量翻译监听处理
|
||||
*
|
||||
* @param message
|
||||
* @param channel
|
||||
* @throws Exception
|
||||
*/
|
||||
@RabbitListener(bindings = @QueueBinding(
|
||||
exchange = @Exchange(value = RabbitMqConstants.TRANSLATE_EXCHANGE, type = "topic"),
|
||||
value = @Queue(value = RabbitMqConstants.TRANSLATE_QUEUE, durable = "true", autoDelete = "false"),
|
||||
key = RabbitMqConstants.TRANSLATE_KEY))
|
||||
@RabbitHandler
|
||||
public void onMessage(Message<TransactionMqDTO> message, Channel channel) throws Exception {
|
||||
// 手动应答
|
||||
Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
|
||||
channel.basicAck(deliveryTag, false);
|
||||
|
||||
// 根据传入的ID获取翻译内容
|
||||
TransactionMqDTO transactionMqDto = message.getPayload();
|
||||
|
||||
log.info("异步翻译入参信息:{}", transactionMqDto);
|
||||
|
||||
// 调用服务查询翻译内容
|
||||
ELanguages fromLanguage = ELanguages.getLanguagesByColumn(transactionMqDto.getColumn());
|
||||
if (null == fromLanguage) {
|
||||
log.error("异步翻译来源语言不存在");
|
||||
return;
|
||||
}
|
||||
|
||||
// 翻译内容
|
||||
String content = transactionMqDto.getColumnValue();
|
||||
// 翻译来源语言
|
||||
String from = fromLanguage.getApiKey();
|
||||
|
||||
// 翻译目标语言列表
|
||||
List<String> toList = new ArrayList<>();
|
||||
if (StringUtils.isNotEmpty(transactionMqDto.getToApiKey())) {
|
||||
toList.add(transactionMqDto.getToApiKey());
|
||||
} else {
|
||||
for (ELanguages value : ELanguages.values()) {
|
||||
if (!value.getApiKey().equals(from)) {
|
||||
toList.add(value.getApiKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (StringUtils.isNotEmpty(content)) {
|
||||
// 调用三方服务处理成功
|
||||
Map<String, String> dataMap = iTranslateService.translate(TranslateParam.builder()
|
||||
.from(from)
|
||||
.to(toList)
|
||||
.content(content)
|
||||
.build());
|
||||
|
||||
// 封装调用翻译服务入库内容
|
||||
TransactionDTO transactionDTO = new TransactionDTO();
|
||||
transactionDTO.setPkId(transactionMqDto.getPkId());
|
||||
ReflectUtil.setFieldValue(transactionDTO, fromLanguage.getColumn(), content);
|
||||
|
||||
// 赋值入库并处理是否加入缓存
|
||||
for (String apiKey : toList) {
|
||||
ELanguages languages = ELanguages.getLanguagesByApiKey(apiKey);
|
||||
if (null != languages) {
|
||||
String translateStr = dataMap.get(apiKey);
|
||||
|
||||
ReflectUtil.setFieldValue(transactionDTO, languages.getColumn(), translateStr);
|
||||
|
||||
log.info("{}", apiKey + ":" + translateStr);
|
||||
}
|
||||
}
|
||||
log.info("transactionDTO: {}", transactionDTO);
|
||||
|
||||
R<Boolean> updateR = iTransactionServiceApi.updateTransactionSync(transactionDTO);
|
||||
if (!updateR.isSuccess()) {
|
||||
log.error("异步翻译调用翻译服务入库失败: {}", updateR.getMsg());
|
||||
}
|
||||
} else {
|
||||
log.warn("异步翻译内容为空");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -7,13 +7,6 @@ import com.hzs.third.mqcall.service.IDelayedMessageService;
|
|||
import org.apache.dubbo.config.annotation.DubboService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
/**
|
||||
* @Description:
|
||||
* @Author: sui q
|
||||
* @Time: 2023/3/9 15:22
|
||||
* @Classname: DelayedMessageProvider
|
||||
* @PackageName: com.hzs.third.mqcall.provider
|
||||
*/
|
||||
@DubboService
|
||||
public class DelayedMessageProvider implements IDelayedMessageApi {
|
||||
|
||||
|
|
|
@ -3,20 +3,14 @@ package com.hzs.third.mqcall.service;
|
|||
import com.hzs.third.mq.dto.DelayedMessageDTO;
|
||||
|
||||
/**
|
||||
* @Description: 延时队列入口类
|
||||
* @Author: sui q
|
||||
* @Time: 2023/3/9 15:16
|
||||
* @Classname: IDelaydeMessageService
|
||||
* @PackageName: com.hzs.third.mqcall.service
|
||||
* 延时队列入口类
|
||||
*/
|
||||
public interface IDelayedMessageService {
|
||||
|
||||
/**
|
||||
* 发送延时队列消息
|
||||
* 发送延时队列消息
|
||||
*
|
||||
* @param delayedMessageDTO 内容
|
||||
* @return: void
|
||||
* @Author: sui q
|
||||
* @Date: 2023/3/9 15:18
|
||||
*/
|
||||
*/
|
||||
void sendDelayedMessage(DelayedMessageDTO delayedMessageDTO);
|
||||
}
|
||||
|
|
|
@ -9,11 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @Description: 延时队列实现类
|
||||
* @Author: sui q
|
||||
* @Time: 2023/3/9 15:20
|
||||
* @Classname: DelayedMessageImpl
|
||||
* @PackageName: com.hzs.third.mqcall.service.impl
|
||||
* 延时队列实现类
|
||||
*/
|
||||
@Service
|
||||
public class DelayedMessageServiceImpl implements IDelayedMessageService {
|
||||
|
@ -23,7 +19,7 @@ public class DelayedMessageServiceImpl implements IDelayedMessageService {
|
|||
|
||||
@Override
|
||||
public void sendDelayedMessage(DelayedMessageDTO delayedMessageDTO) {
|
||||
if(DateUtils.compareDateTimeAfter(delayedMessageDTO.getEffectDate(), DateUtils.currentDateTime())) {
|
||||
if (DateUtils.compareDateTimeAfter(delayedMessageDTO.getEffectDate(), DateUtils.currentDateTime())) {
|
||||
// 计算延时时间
|
||||
long second = DateUtils.betweenSecond(delayedMessageDTO.getEffectDate()) * 1000;
|
||||
rabbitTemplate.convertAndSend(RabbitMqConstants.DELAY_EXCHANGE, RabbitMqConstants.DELAY_ROUTING_KEY,
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
package com.hzs.third.translate.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Description:
|
||||
* Author: jiang chao
|
||||
* Time: 2022/9/8 17:51
|
||||
* Classname: TranslateConfig
|
||||
* PackageName: com.hzs.third.translate.config
|
||||
*/
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "baidu.translate")
|
||||
public class TranslateConfig {
|
||||
|
||||
/**
|
||||
* API appId
|
||||
*/
|
||||
private String appId;
|
||||
|
||||
/**
|
||||
* API secretKey
|
||||
*/
|
||||
private String secretKey;
|
||||
|
||||
/**
|
||||
* url
|
||||
*/
|
||||
private String url;
|
||||
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
package com.hzs.third.translate.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Description: 翻译服务入参
|
||||
* Author: jiang chao
|
||||
* Time: 2022/9/8 18:00
|
||||
* Classname: TranslateParam
|
||||
* PackageName: com.hzs.third.translate.dto
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
@Data
|
||||
public class TranslateParam implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 5415462905404464505L;
|
||||
|
||||
/**
|
||||
* 翻译来源语言ApiKey
|
||||
*/
|
||||
private String from;
|
||||
/**
|
||||
* 翻译目标语言ApiKey
|
||||
*/
|
||||
private List<String> to;
|
||||
/**
|
||||
* 翻译内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
package com.hzs.third.translate.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Description: 翻译返回内容
|
||||
* Author: jiang chao
|
||||
* Time: 2022/9/8 17:03
|
||||
* Classname: TranslateResult
|
||||
* PackageName: com.hzs.system.sys.controller.manage
|
||||
*/
|
||||
@Data
|
||||
public class TranslateResult {
|
||||
|
||||
/**
|
||||
* 翻译源语言
|
||||
*/
|
||||
private String from;
|
||||
|
||||
/**
|
||||
* 翻译目标语言
|
||||
*/
|
||||
private String to;
|
||||
|
||||
/**
|
||||
* 错误码
|
||||
*/
|
||||
private String error_code;
|
||||
|
||||
/**
|
||||
* 错误内容
|
||||
*/
|
||||
private String error_msg;
|
||||
|
||||
/**
|
||||
* 翻译结果
|
||||
*/
|
||||
private List<TranslateResultDetail> trans_result;
|
||||
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
package com.hzs.third.translate.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Description: 翻译返回内容
|
||||
* Author: jiang chao
|
||||
* Time: 2022/9/8 17:03
|
||||
* Classname: TranslateResult
|
||||
* PackageName: com.hzs.system.sys.controller.manage
|
||||
*/
|
||||
@Data
|
||||
public class TranslateResultDetail {
|
||||
|
||||
/**
|
||||
* 翻译源内容
|
||||
*/
|
||||
private String src;
|
||||
|
||||
/**
|
||||
* 翻译目标内容
|
||||
*/
|
||||
private String dst;
|
||||
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
package com.hzs.third.translate.service;
|
||||
|
||||
import com.hzs.third.translate.dto.TranslateParam;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Description:
|
||||
* @Author: jiang chao
|
||||
* @Time: 2023/3/7 18:42
|
||||
* @Classname: ITranslateService
|
||||
* @PackageName: com.hzs.third.translate.service
|
||||
*/
|
||||
public interface ITranslateService {
|
||||
|
||||
/**
|
||||
* 语言翻译
|
||||
*
|
||||
* @param param 翻译入参
|
||||
* @return
|
||||
*/
|
||||
Map<String, String> translate(TranslateParam param);
|
||||
|
||||
}
|
|
@ -1,86 +0,0 @@
|
|||
package com.hzs.third.translate.service.impl;
|
||||
|
||||
import cn.hutool.core.text.UnicodeUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.crypto.digest.MD5;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.hzs.common.core.constant.Constants;
|
||||
import com.hzs.common.core.exception.base.BaseException;
|
||||
import com.hzs.third.translate.config.TranslateConfig;
|
||||
import com.hzs.third.translate.dto.TranslateParam;
|
||||
import com.hzs.third.translate.dto.TranslateResult;
|
||||
import com.hzs.third.translate.service.ITranslateService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.dubbo.common.utils.CollectionUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @Description:
|
||||
* @Author: jiang chao
|
||||
* @Time: 2023/3/7 18:43
|
||||
* @Classname: TranslateServiceImpl
|
||||
* @PackageName: com.hzs.third.translate.service.impl
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class TranslateServiceImpl implements ITranslateService {
|
||||
|
||||
@Autowired
|
||||
private TranslateConfig translateConfig;
|
||||
|
||||
/**
|
||||
* 语言翻译
|
||||
*
|
||||
* @param param 翻译入参
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> translate(TranslateParam param) {
|
||||
try {
|
||||
// 请求参数加盐
|
||||
String salt = String.valueOf(System.currentTimeMillis());
|
||||
// MD5加密
|
||||
String sign = MD5.create().digestHex(translateConfig.getAppId() + param.getContent() + salt + translateConfig.getSecretKey()).toLowerCase();
|
||||
// 翻译内容进行URLEncode转码
|
||||
String content = URLEncoder.encode(param.getContent(), Constants.UTF8);
|
||||
|
||||
// 返回的翻译对应map
|
||||
Map<String, String> resultMap = new ConcurrentHashMap<>();
|
||||
|
||||
List<String> toList = param.getTo();
|
||||
if (CollectionUtils.isNotEmpty(toList)) {
|
||||
for (String str : toList) {
|
||||
String url = translateConfig.getUrl() + "?q=" + content + "&from=" + param.getFrom() + "&to=" + str + "&appid=" + translateConfig.getAppId() + "&salt=" + salt + "&sign=" + sign;
|
||||
log.info("异步翻译请求URL: {}", url);
|
||||
|
||||
String result = HttpUtil.get(url, CharsetUtil.CHARSET_UTF_8);
|
||||
log.info("异步翻译返回内容:{}", result);
|
||||
|
||||
// 翻译返回内容为 unicode,需要进行解码
|
||||
result = UnicodeUtil.toString(result);
|
||||
|
||||
TranslateResult translateResult = JSONUtil.toBean(result, TranslateResult.class);
|
||||
if (null != translateResult && null != translateResult.getFrom()) {
|
||||
// 返回成功并且有内容
|
||||
resultMap.put(str, translateResult.getTrans_result().get(0).getDst());
|
||||
}
|
||||
// // 非企业认证,QPS=1
|
||||
// Thread.sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
return resultMap;
|
||||
} catch (Exception e) {
|
||||
log.error("异步翻译异常!param: {}", param, e);
|
||||
throw new BaseException("异步翻译异常");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue