package com.brilliance.isc.common.taskdistributor.comparator;

import com.brilliance.isc.bo.Que;
import com.brilliance.isc.bo.TaskDistConfig;
import com.brilliance.isc.common.taskdistributor.consts.TaskDistConstants;
import com.brilliance.isc.common.util.StringUtils;

import java.util.Comparator;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

public class GeneralTaskComparator implements Comparator<Que> {

    private static final Date MAX_DATE = new Date(Long.MAX_VALUE);
    /**
     * 处理顺序：隔日优先，置顶处理 > 业务类型(拒付最高) > 加急标志 > 客户VIP标志 > 等待时间
     * @param q1
     * @param q2
     * @return
     */
    @Override
    public int compare(Que q1, Que q2) {
       String priorityAllocationFrm = ((TaskDistConfig)TaskDistConstants.sortflg.get().get(q1.getBchkeyinr())).getPriorityAllocationFrm();
        //隔日优先处理
        Date ct1 = Optional.ofNullable(q1.getCreateTime()).orElse(MAX_DATE);
        Date ct2 = Optional.ofNullable(q2.getCreateTime()).orElse(MAX_DATE);
        if(isAfterDay(ct1) && isAfterDay(ct2)){
            return ct1.compareTo(ct2);
        }else{
            if(isAfterDay(ct1)){
                return  -1;
            }else if(isAfterDay(ct2)){
                return  1;
            }
        }
        // 最迟分配时间排序(置顶)
        if(Objects.equals(q1.getPinflg(), TaskDistConstants.FLAG_ON) && Objects.equals(q2.getPinflg(), TaskDistConstants.FLAG_ON))
        {
            Date dd1 = Optional.ofNullable(q1.getDistDdl()).orElse(MAX_DATE);
            Date dd2 = Optional.ofNullable(q2.getDistDdl()).orElse(MAX_DATE);
            if (!dd1.equals(dd2)) {
                return dd1.compareTo(dd2);
            }
        }else{
            if(Objects.equals(q1.getPinflg(), TaskDistConstants.FLAG_ON)){
                return -1 ;
            }else if(Objects.equals(q2.getPinflg(), TaskDistConstants.FLAG_ON)){
                return 1;
            }
        }

        //业务类型排序:将配置好的类型业务排序靠前
        if(!StringUtils.isEmpty(priorityAllocationFrm)){
            String frm1 = q1.getFrm();
            String frm2 = q2.getFrm();
            if(!Objects.equals(frm1,frm2)){
                if(priorityAllocationFrm.contains(frm1) && !priorityAllocationFrm.contains(frm2) ){
                    return -1;
                }else if (priorityAllocationFrm.contains(frm2) && !priorityAllocationFrm.contains(frm1))
                    return 1;
            }
        }

        // 加急标志
        String urgFlg1 = Optional.ofNullable(q1.getUrgflg()).orElse("");
        String urgFlg2 = Optional.ofNullable(q2.getUrgflg()).orElse("");
        if(!urgFlg1.equals(urgFlg2)){
            return urgFlg2.compareTo(urgFlg1);
        }
        //客户VIP标志
        String vipFlg1 = Optional.ofNullable(q1.getVipflg()).orElse("");
        String vipFlg2 = Optional.ofNullable(q2.getVipflg()).orElse("");
        if(!vipFlg1.equals(vipFlg2)){
            return vipFlg2.compareTo(vipFlg1);
        }


        return ct1.compareTo(ct2);
    }

    public boolean isAfterDay(Date date){
        if(new Date().getDate() - date.getDate() > 0){
            return true;
        }
        return false;
    }
}
