CallReportConsumer.java 2.72 KB
package cn.fw.valhalla.component;

import cn.fw.pstn.sdk.enums.DialTypeEnum;
import cn.fw.pstn.sdk.mq.CallReport;
import cn.fw.valhalla.common.utils.StringUtils;
import cn.fw.valhalla.domain.dto.CallReportDTO;
import cn.fw.valhalla.domain.enums.CallTypeEnum;
import cn.fw.valhalla.rpc.ehr.EhrRpcService;
import cn.fw.valhalla.rpc.ehr.dto.StaffInfoDTO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Objects;


/**
 * 监听关注公众号mq
 *
 * @author kurisu
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "call_report", consumerGroup = "${spring.application.name}" + "-call_report")
public class CallReportConsumer implements RocketMQListener<CallReport> {
    private final StringRedisTemplate redisTemplate;
    private final EhrRpcService ehrRpcService;

    @Value("${spring.cache.custom.global-prefix}:mq:call:report")
    @Getter
    private String callReportKey;

    @Autowired
    public CallReportConsumer(final StringRedisTemplate redisTemplate,
                              final EhrRpcService ehrRpcService) {
        this.redisTemplate = redisTemplate;
        this.ehrRpcService = ehrRpcService;
    }

    @Override
    public void onMessage(CallReport t) {
        log.info("监听通话记录mq: CallReport=[{}]", t);
        try {
            if (Objects.isNull(t)) {
                return;
            }
            CallReportDTO dto = new CallReportDTO();
            BeanUtils.copyProperties(t, dto);
            dto.setDialType(CallTypeEnum.CALL);
            if (DialTypeEnum.P_CALL.equals(t.getCallType())) {
                dto.setDialType(CallTypeEnum.P_CALL);
            }
            String staffMobile = t.getStaffMobile();
            if (StringUtils.isEmpty(staffMobile)) {
                return;
            }
            StaffInfoDTO info = ehrRpcService.queryStaffInfoByMobile(staffMobile);
            if (Objects.nonNull(info)) {
                dto.setStaffId(info.getId());
                dto.setStaffName(info.getName());
                dto.setGroupId(info.getGroupId());
            }
            redisTemplate.opsForList().rightPush(getCallReportKey(), JSONObject.toJSONString(dto));
        } catch (Exception ex) {
            log.error("消费通话记录mq失败,原因:{}", JSON.toJSONString(ex));
        }
    }
}