package com.prolog.rdc.tms.app.util;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.rdc.tms.app.model.entity.TmsShipbillDetailModel;
import com.prolog.rdc.tms.app.model.entity.TmsShipbillHeaderModel;
import com.prolog.rdc.tms.model.dto.VehicleCheckDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.ShipbillMatchLineDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.StrategyBusinessMatchingDTO;
import com.prolog.rdc.tms.model.vo.basicConfigure.ShipbillMatchLineVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.StrategyBusinessItemVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.StrategyBusinessMatchingVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleCheckVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class CommonUtil {

    @Autowired
    private MaindataFeign maindataTmsResource;

    /**
     * 计算装载率
     *
     * @param id
     * @param weight
     * @param volume
     * @return
     */
    public Map<String, String> calculateLoadingRate(Long id, BigDecimal weight, BigDecimal volume) {

        VehicleCheckVO vehicleCheckVO = new VehicleCheckVO();
        vehicleCheckVO.setVehicleId(id);

        RestMessage<VehicleCheckDTO> data = maindataTmsResource.getVehicle(vehicleCheckVO);
        if (!data.isSuccess()) {
            throw new RuntimeException("获取车辆失败");
        }
        VehicleCheckDTO vehicleCheckDTO = data.getData();

        StringBuffer weightStr = new StringBuffer();
        StringBuffer volumeStr = new StringBuffer();

        if (ToolKits.isNotEmpty(weight)) {
            BigDecimal divide = ToolKits.isEmpty(vehicleCheckDTO.getWeight()) ? BigDecimal.ZERO : weight.divide(vehicleCheckDTO.getWeight(), 2, BigDecimal.ROUND_HALF_UP);
            BigDecimal multiply = divide.multiply(new BigDecimal("100"));
            weightStr.append(multiply.intValue());
            weightStr.append("%");
        }
        if (ToolKits.isNotEmpty(volume)) {
            BigDecimal divide = ToolKits.isEmpty(vehicleCheckDTO.getVolume()) ? BigDecimal.ZERO : volume.divide(vehicleCheckDTO.getVolume(), 2, BigDecimal.ROUND_HALF_UP);
            BigDecimal multiply = divide.multiply(new BigDecimal("100"));
            volumeStr.append(multiply.intValue());
            volumeStr.append("%");
        }

        Map<String, String> map = Maps.newHashMap();
        map.put("weight", weightStr.toString());
        map.put("volume", volumeStr.toString());

        return map;
    }
}
