package com.ag.account.feeUtil;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ag.entity.fee.Tariff;
import com.ag.entity.fee.TariffRitem;
import com.ag.entity.fee.face.ICondition;
import com.ag.entity.fee.face.IRuleCondition;
import com.ag.entity.fee.face.SourceProperty;
import com.ag.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.CollectionUtils;

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

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

    /**
     * 费率有效期检查，有效返回true，无效返回false
     * @return
     */
    public static boolean checkValidity(Tariff tariff, String bizTime){
        if (tariff.getEffectiveDate() == null || tariff.getExpiredDate() == null){
            return true;
        }
        //失效日期+1天
        DateTime endDate = DateUtil.offsetDay(tariff.getExpiredDate(), 1);
        DateTime bizDate = DateUtil.parseDate(bizTime);
        return DateUtil.isIn(bizDate, tariff.getEffectiveDate(), endDate);
    }

    /*public static void main(String[] args) {
        String bizTime = "2020-12-01 00:00:00";
        TariffVO vo = new TariffVO();
        vo.setEffectiveDate(TimeUtil.getTimestamp("2020-11-01"));
        vo.setExpiredDate(TimeUtil.getTimestamp("2020-11-30"));
        System.out.println(RuleUtil.checkValidity(vo, bizTime));
    }*/

    public static boolean compareRule(SourceProperty reqData, List<TariffRitem> conditionLst, Map<String, Integer> eqMap) {
        if (CollectionUtils.isEmpty(conditionLst)) {
            return false;
        }
        JSONObject property = reqData.getProperty();
        //如果有扩展需要，传递条件对象type，否则可传递type的比较属性
        for (IRuleCondition condition : conditionLst) {
            //过滤金额条件
            if (ICondition.RATE.equals(condition.getKeyName())){
                continue;
            }
            //*匹配所有，不校验
            if (ObjectUtil.equal(condition.getCompareVal(), "*")){
                continue;
            }
            //非查找项，不需要进行值比较
            if(eqMap.containsKey(condition.getKeyName()) && eqMap.get(condition.getKeyName()) == 1){
                continue;
            }
            //数据源不包含费率key, 此费率条件不满足
            if (!property.containsKey(condition.getKeyName())){
                return false;
            }
            String propertyVal = property.getString(condition.getKeyName());
            if (!compare(condition, propertyVal)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 通用比较方法
     * @param condition
     * @param typeVal
     * @return
     */
    private static boolean compare(IRuleCondition condition, String typeVal) {
        if(StrUtil.isEmpty(typeVal)){
            return false;
        }
        if ("=".equals(condition.getOperChar())) {
            if (StrUtil.join(",",condition.getCompareVal(), ",").contains(StrUtil.join(",",typeVal, ","))) {
                return true;
            }
        }
        if ("!=".equals(condition.getOperChar())) {
            if (!StrUtil.join(",",condition.getCompareVal(), ",").contains(StrUtil.join(",",typeVal, ","))) {
                return true;
            }
        }
        if ("CONTAINS".equals(condition.getOperChar())){
            return compareContains(condition, typeVal);
        }
        if ("NO CONTAINS".equals(condition.getOperChar())){
            return !compareContains(condition, typeVal);
        }
        if ("PREFIX".equals(condition.getOperChar())){
            return comparePrefix(condition, typeVal);
        }
        if ("NO PREFIX".equals(condition.getOperChar())){
            return !comparePrefix(condition, typeVal);
        }
        return false;
    }

    private static boolean comparePrefix(IRuleCondition condition, String typeVal) {
        String condtionVal = condition.getCompareVal() + ",";
        String[] arrs = condtionVal.split(",");
        for (String val : arrs){
            if (StrUtil.isEmpty(val)){
                continue;
            }
            if (typeVal.indexOf(val) == 0){
                return true;
            }
        }
        return false;
    }



    private static boolean compareContains(IRuleCondition condition, String typeVal){
        String condtionVal = condition.getCompareVal() + ",";
        String[] arrs = condtionVal.split(",");
        for (String val : arrs){
            if (StrUtil.isEmpty(val)){
                continue;
            }
            if (typeVal.contains(val)){
                return true;
            }
        }
        return false;
    }






}
