package com.bohuikeji.delivery.backstage.thread;

import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.frame.module.admin.component.RedisUtil;
import com.bohuikeji.frame.module.admin.entity.vo.AddressVo;
import com.bohuikeji.frame.module.admin.exception.WebVerifiedException;
import com.bohuikeji.frame.module.admin.utils.MapUtils;
import java.text.MessageFormat;

/**
 * 地址解析线程
 */
public class AddressGeoThread implements Runnable{

    private TOrder order;
    private RedisUtil redisUtil;
    private String address;
    private String redisKey;
    private int index;


    public AddressGeoThread(TOrder order,RedisUtil redisUtil,String address,String redisKey,int index){
        this.order=order;
        this.address=address;
        this.redisUtil=redisUtil;
        this.index=index;
        this.redisKey=redisKey;
    }


    @Override
    public void run() {
        AddressVo receiptAddressVo;
        try {
            receiptAddressVo=MapUtils.getAddressGeo(address);
        }catch (WebVerifiedException e){
            //地址解析失败则添加异常信息到Redis
            redisUtil.pushList("batchError:"+order.getOrderBatchNum(),MessageFormat.format("【解析错误】收件地址解析错误，内容：{0}，不是合法的地址信息，位于第{1}行",address,index+1),60*60*8);
            return;
        }
        order.setReceiptAddress(receiptAddressVo);
        //计算距离
        order.setOrderDistance(Math.round(MapUtils.getDistanceKm(order.getMailingAddress(),order.getReceiptAddress())));
        order.setOrderDistanceDec(MapUtils.getDistanceKma(order.getMailingAddress(),order.getReceiptAddress()));
        //最长缓存8小时
        //最长缓存8小时
        redisUtil.pushList(redisKey,order,60*60*8);
    }
}
