InsurerRpcService.java 5.53 KB
package cn.fw.valhalla.rpc.angel;

import cn.fw.angel.sdk.api.InsuranceOrderApiService;
import cn.fw.angel.sdk.api.InsurerApiService;
import cn.fw.angel.sdk.result.InsuranceOrderDTO;
import cn.fw.data.base.domain.common.Message;
import cn.fw.valhalla.common.utils.StringUtils;
import cn.fw.valhalla.rpc.angel.dto.InsuranceDTO;
import cn.fw.valhalla.rpc.angel.dto.InsurerDTO;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
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.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * create at 2020-07-22
 *
 * @author kurisu
 */
@Slf4j
@Service
public class InsurerRpcService {

    private final InsuranceOrderApiService insuranceEstimateApiService;
    private final InsurerApiService insurerApiService;
    /**
     * Redis工具
     */
    private final StringRedisTemplate redisTemplate;

    @Value("${spring.cache.custom.global-prefix}:rpc:angel")
    @Getter
    private String keyPrefix;

    @Autowired
    InsurerRpcService(final InsuranceOrderApiService insuranceEstimateApiService,
                      final InsurerApiService insurerApiService,
                      final StringRedisTemplate redisTemplate) {
        this.insuranceEstimateApiService = insuranceEstimateApiService;
        this.insurerApiService = insurerApiService;
        this.redisTemplate = redisTemplate;
    }


    public InsurerDTO getById(Long insurerId) {
        try {
            Message<cn.fw.angel.sdk.result.InsurerDTO> msg = insurerApiService.getById(insurerId);
            log.info("查询保险公司结果:{}", msg);
            if (!msg.isSuccess() || Objects.isNull(msg.getData())) {
                return null;
            }

            InsurerDTO dto = new InsurerDTO();
            BeanUtils.copyProperties(msg.getData(), dto);
            return dto;
        } catch (Exception e) {
            log.error("查询保险公司失败", e);
            return null;
        }
    }


    /**
     * 根据档案id查询正在生效的保险信息
     *
     * @param customerId
     * @return
     */
    public InsuranceDTO queryInsByCustId(Long customerId) {
        final String key = generateKey(customerId);
        try {
            String cache = getFromCache(key);
            if (StringUtils.isValid(cache)) {
                return JSONObject.parseObject(cache, InsuranceDTO.class);
            }
            Message<List<InsuranceOrderDTO>> msg = insuranceEstimateApiService.getCusLatest(customerId);

            log.info("查询正在生效的保险信息:{}", msg.getResult());
            List<InsuranceOrderDTO> data = msg.getData();
            if (!msg.isSuccess() || CollectionUtils.isEmpty(data)) {
                return null;
            }
            Date tciEndTime = null;
            Date vciEndTime = null;
            Date tciStartTime = null;
            Date vciStartTime = null;
            for (InsuranceOrderDTO insuranceOrderDTO : data) {
                if (1 == insuranceOrderDTO.getInsuranceInfo().getInsuranceType()) {
                    tciEndTime = insuranceOrderDTO.getInsuranceInfo().getEndTime();
                    tciStartTime = insuranceOrderDTO.getInsuranceInfo().getStartTime();
                } else if (2 == insuranceOrderDTO.getInsuranceInfo().getInsuranceType()) {
                    vciEndTime = insuranceOrderDTO.getInsuranceInfo().getEndTime();
                    vciStartTime = insuranceOrderDTO.getInsuranceInfo().getStartTime();
                }
            }
            final Date now = new Date();
            boolean before1 = !Objects.isNull(tciEndTime) && now.before(tciEndTime);
            boolean before2 = !Objects.isNull(vciEndTime) && now.before(vciEndTime);

            if (before1 || before2) {
                InsuranceDTO dto = new InsuranceDTO();
                BeanUtils.copyProperties(data, dto);
                dto.setEffectDate(vciStartTime);
                dto.setExpiryDate(vciEndTime);
                dto.setTciEffectDate(tciStartTime);
                dto.setTciExpiryDate(tciEndTime);
                setToCache(key, JSONObject.toJSONString(dto), 30);
                return dto;
            }
            return null;
        } catch (Exception e) {
            log.error("查询正在生效的保险信息失败:", e);
            return null;
        }
    }

    private String getFromCache(final String key) {
        String json = null;
        try {
            BoundValueOperations<String, String> ops = redisTemplate.boundValueOps(key);
            json = ops.get();
        } catch (Exception e) {
            log.error("从缓存获取保有客保险信息失败", e);
        }
        return json;
    }

    private void setToCache(final String key, final String value, long timeout) {
        try {
            redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.error("缓存保有客保险信息失败", e);
        }
    }


    private String generateKey(final Long customerId) {
        Assert.notNull(customerId, "customerId cannot be null");

        return String.format("%s:%s", getKeyPrefix(), customerId);
    }
}