CallReportDealTask.java 5.6 KB
package cn.fw.valhalla.controller.task;

import cn.fw.valhalla.common.utils.StringUtils;
import cn.fw.valhalla.domain.db.customer.AccidentPool;
import cn.fw.valhalla.domain.db.customer.Customer;
import cn.fw.valhalla.domain.db.customer.CustomerBaseInfo;
import cn.fw.valhalla.domain.dto.CallReportDTO;
import cn.fw.valhalla.rpc.ehr.EhrRpcService;
import cn.fw.valhalla.rpc.ehr.dto.StaffInfoDTO;
import cn.fw.valhalla.service.bus.follow.FollowBizService;
import cn.fw.valhalla.service.data.AccidentPoolService;
import cn.fw.valhalla.service.data.CustomerBaseInfoService;
import cn.fw.valhalla.service.data.CustomerService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.lang.Nullable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static cn.fw.common.businessvalidator.Validator.BV;

/**
 * @author : kurisu
 * @className : CallReportDealTask
 * @description : 通话记录处理任务
 * @date: 2021-01-19 16:02
 */
@Slf4j
@Component
@ConditionalOnProperty(prefix = "task", name = "switch", havingValue = "on")
public class CallReportDealTask {
    private final CustomerBaseInfoService baseInfoService;
    private final CustomerService customerService;
    private final AccidentPoolService accidentPoolService;
    private final FollowBizService followBizService;
    private final EhrRpcService ehrRpcService;
    private final StringRedisTemplate redisTemplate;

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

    @Autowired
    public CallReportDealTask(final CustomerBaseInfoService baseInfoService,
                              final CustomerService customerService,
                              final AccidentPoolService accidentPoolService,
                              final FollowBizService followBizService,
                              final EhrRpcService ehrRpcService,
                              final StringRedisTemplate redisTemplate) {
        this.baseInfoService = baseInfoService;
        this.customerService = customerService;
        this.accidentPoolService = accidentPoolService;
        this.followBizService = followBizService;
        this.ehrRpcService = ehrRpcService;
        this.redisTemplate = redisTemplate;
    }

    /**
     * 处理通话记录
     */
    @Scheduled(initialDelay = 1000 * 5, fixedRate = 1000 * 15)
    public void dealCallReport() {
        List<String> failList = new ArrayList<>();
        String callStr;
        while ((callStr = redisTemplate.opsForList().leftPop(getCallReportKey())) != null) {
            CallReportDTO dto = JSONObject.parseObject(callStr, CallReportDTO.class);
            if (Objects.isNull(dto)) {
                continue;
            }
            final String staffMobile = dto.getStaffMobile();
            final String peerMobile = dto.getPeerNo();
            final Long groupId = dto.getGroupId();
            final Long staffId = dto.getStaffId();
            try {
                boolean isValid = Objects.nonNull(staffId) && Objects.nonNull(groupId);
                if (!isValid) {
                    StaffInfoDTO info = ehrRpcService.queryStaffInfoByMobile(staffMobile);
                    BV.notNull(info, () -> String.format("[%s]员工信息获取失败", staffMobile));
                    assert info != null;
                    dto.setStaffId(info.getId());
                    dto.setGroupId(info.getGroupId());
                }
                followBizService.readCallReport(dto, true, queryAccidentCar(peerMobile, groupId));
                followBizService.readCallReport(dto, false, queryCustomerIds(peerMobile, groupId));
            } catch (Exception e) {
                if (StringUtils.isValid(callStr)) {
                    failList.add(callStr);
                }
                log.error("处理通话记录失败", e);
            }
        }
        if (!CollectionUtils.isEmpty(failList)) {
            redisTemplate.opsForList().rightPushAll(getCallReportKey(), failList);
        }
    }

    @Nullable
    private Long queryAccidentCar(String mobileNo, Long groupId) {
        AccidentPool accidentPool = accidentPoolService.getOne(Wrappers.<AccidentPool>lambdaQuery()
                .eq(AccidentPool::getReportMobile, mobileNo)
                .eq(AccidentPool::getGroupId, groupId)
                .orderByDesc(AccidentPool::getCreateTime)
                .last(" limit 1 ")
        );
        if (Objects.isNull(accidentPool)) {
            return null;
        }
        return accidentPool.getId();
    }

    @Nullable
    private Long[] queryCustomerIds(String mobileNo, Long groupId) {
        //fixme 新增档案是限制 同一手机号只能有一个baseinfo数据
        CustomerBaseInfo baseInfo = baseInfoService.queryByMobile(mobileNo, groupId);
        if (Objects.isNull(baseInfo)) {
            return null;
        }
        Long baseInfoId = baseInfo.getId();
        List<Customer> customerList = customerService.queryByBaseId(baseInfoId);
        if (CollectionUtils.isEmpty(customerList)) {
            return null;
        }
        return customerList.stream().map(Customer::getId).toArray(Long[]::new);
    }
}