package com.ag.account.feeUtil;

import cn.hutool.core.util.ObjectUtil;
import com.ag.entity.fee.ItemProperties;
import com.ag.entity.fee.TariffRitem;
import com.ag.entity.fee.face.IRuleCondition;
import com.ag.entity.fee.face.SourceProperty;
import com.ag.entity.fee.vo.TariffVO;
import com.ag.util.StrUtil;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/26 0026.
 */
public class TariffUtil {

    /**
     * 如果存在多个满足箱属性条件的费率，匹配最符合规则的一个（1、条件数量，2、按显示顺序取优先级）
     */
    public static void compareNum(SourceProperty reqData, List<TariffVO> fits, Map<String, Integer> eqMap, List<ItemProperties> orderList) {
        for(TariffVO tariff : fits){
            //有费率有效期，直接选中退出
            if (tariff.getExpiredDate() != null && tariff.getEffectiveDate() != null){
                replaceTariff(tariff, reqData, eqMap, orderList);
                break;
            }
            //首次进入
            if (reqData.getCriteriaNum() == null){
                replaceTariff(tariff, reqData, eqMap, orderList);
                continue;
            }
            //当前费率的条件数量 > 存储条件数量，则用此费率替换存储费率
            if(accountCriteriaNum(tariff, eqMap) > reqData.getCriteriaNum()){
                replaceTariff(tariff, reqData, eqMap, orderList);
                continue;
            }
            //当前费率的条件数量 < 存储条件数量，忽略不计
            if(accountCriteriaNum(tariff, eqMap) < reqData.getCriteriaNum()){
                continue;
            }
            /*
             * 当前费率的条件数量 = 存储条件数量，进入
             * 按显示顺序重新编号，有key赋1，无key赋0，最后拼接，取拼接串最大值
             */
            String tariffSort = joinOrder(tariff, orderList);
            if (tariffSort.compareTo(reqData.getSort()) == 1){
                replaceTariff(tariff, reqData, eqMap, orderList);
            }

        }
    }

    private static void replaceTariff(TariffVO tariff, SourceProperty reqData, Map<String, Integer> eqMap, List<ItemProperties> orderList) {
        /*ruleType.setTariffId(tariff.getId());
        ruleType.setPrice(tariff.getRate());*/

        reqData.setCriteriaNum(accountCriteriaNum(tariff, eqMap));
        reqData.setSort(joinOrder(tariff, orderList));
        //替换费率
        reqData.setTariffVO(tariff);
    }

    /*private static void comparePrice(TariffVO tariff, SourceProperty ruleType, Map<String, Integer> eqMap) {
        if (ruleType.getTariffVO().getRate() > tariff.getRate()){
            return;
        }
        replaceTariff(tariff, ruleType, eqMap, orderMap);
    }*/

    public static void clear(SourceProperty ruleType){
        ruleType.setCriteriaNum(null);
        ruleType.setSort(null);
        //替换费率
        ruleType.setTariffVO(null);
        ruleType.setPayDetail(null);
        ruleType.setReceiveDetail(null);
    }

    //条件数量只统计查找列
    private static int accountCriteriaNum(TariffVO tariff, Map<String, Integer> eqMap){
        List<TariffRitem> rules = tariff.getRitemList();
        int num = 0;
        for (IRuleCondition rule : rules){
            if(!ObjectUtil.equal(rule.getCompareVal(),"*") && ObjectUtil.equal(eqMap.get(rule.getKeyName()), 0)){
                num ++;
            }
        }
        return num;
    }

    private static String joinOrder(TariffVO tariff, List<ItemProperties> orderList){
        List<TariffRitem> rules = tariff.getRitemList();
        String order = "";
        for (ItemProperties prop : orderList){
            boolean found = false;
            for (TariffRitem ritem : rules){
                if (ObjectUtil.equal(prop.getPropertyCode(), ritem.getKeyName())){
                    //该要素存在于费率条件中
                    order = StrUtil.join(order, "1");
                    found = true;
                    break;
                }
            }
            //该要素不存在于费率条件中
            if (!found){
                order = StrUtil.join(order, "0");
            }
        }
        return order;

    }

}
