package com.xisc.pm.app.service.comparator;

import com.xisc.pm.api.dto.PmSlabPoolInResDTO;
import com.xisc.pm.domain.entity.PmPnsort;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Date;

/**
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024/9/12 14:54
 */
@Data
public class PmSlabPoolInDTOComparator implements Comparator<PmSlabPoolInResDTO> {

    private PmPnsort pmPnsort;

    @Override
    public int compare(PmSlabPoolInResDTO o1, PmSlabPoolInResDTO o2) {

        for (int i = 1; i <= 8; i++) {
            int result = 0;
            if (i == 1) {
                // 升序
                if (pmPnsort.getSortFlag().equals("0")) {
                    result = this.getResult(o2, o1, pmPnsort.getPriority1());
                } else {
                    // 降序
                    result = this.getResult(o1, o2, pmPnsort.getPriority1());
                }
                if (result != 0) {
                    return result;
                }
            } else if (i == 2) {
                // 升序
                if (pmPnsort.getSortFlag().equals("0")) {
                    result = this.getResult(o2, o1, pmPnsort.getPriority2());
                } else {
                    result = this.getResult(o1, o2, pmPnsort.getPriority2());
                }
                if (result != 0) {
                    return result;
                }
            } else if (i == 3) {
                // 升序
                if (pmPnsort.getSortFlag().equals("0")) {
                    result = this.getResult(o2, o1, pmPnsort.getPriority3());
                } else {
                    result = this.getResult(o1, o2, pmPnsort.getPriority3());
                }
                if (result != 0) {
                    return result;
                }
            } else if (i == 4) {
                // 升序
                if (pmPnsort.getSortFlag().equals("0")) {
                    result = this.getResult(o2, o1, pmPnsort.getPriority4());
                } else {
                    result = this.getResult(o1, o2, pmPnsort.getPriority4());
                }
                if (result != 0) {
                    return result;
                }
            } else if (i == 5) {
                // 升序
                if (pmPnsort.getSortFlag().equals("0")) {
                    result = this.getResult(o2, o1, pmPnsort.getPriority5());
                } else {
                    result = this.getResult(o1, o2, pmPnsort.getPriority5());
                }
                if (result != 0) {
                    return result;
                }
            } else if (i == 6) {
                // 升序
                if (pmPnsort.getSortFlag().equals("0")) {
                    result = this.getResult(o2, o1, pmPnsort.getPriority6());
                } else {
                    result = this.getResult(o1, o2, pmPnsort.getPriority6());
                }
                if (result != 0) {
                    return result;
                }
            } else if (i == 7) {
                // 升序
                if (pmPnsort.getSortFlag().equals("0")) {
                    result = this.getResult(o2, o1, pmPnsort.getPriority7());
                } else {
                    result = this.getResult(o1, o2, pmPnsort.getPriority7());
                }
                if (result != 0) {
                    return result;
                }
            } else if (i == 8) {
                // 升序
                if (pmPnsort.getSortFlag().equals("0")) {
                    result = this.getResult(o2, o1, pmPnsort.getPriority8());
                } else {
                    result = this.getResult(o1, o2, pmPnsort.getPriority8());
                }
                if (result != 0) {
                    return result;
                }
            }
        }
        return 0;
    }


    private int getResult(PmSlabPoolInResDTO o1, PmSlabPoolInResDTO o2, String priority) {
        int result = 0;
        if ("1".equals(priority)) {
            // 订货宽度
            result = this.compareNullable(o1.getOrderQWidth(), o2.getOrderQWidth());
        }
        if ("2".equals(priority)) {
            // 订货厚度
            result = this.compareNullable(o1.getOrderQThick(), o2.getOrderQThick());
        }
        if ("3".equals(priority)) {
            // 交货期
            result = this.compareNullable(o1.getDelivyDate(), o2.getDelivyDate());
        }
        if ("4".equals(priority)) {
            // 收货单位
            result = this.compareNullable(o1.getConsignCustCname(), o2.getConsignCustCname());
        }
        return result;
    }

    private int compareNullable(BigDecimal a, BigDecimal b) {
        if (a == null && b == null) {
            return 0;
        } else if (a == null) {
            return -1;
        } else if (b == null) {
            return 1;
        } else {
            return a.compareTo(b);
        }
    }

    private int compareNullable(Date a, Date b) {
        if (a == null && b == null) {
            return 0;
        } else if (a == null) {
            return -1;
        } else if (b == null) {
            return 1;
        } else {
            return a.compareTo(b);
        }
    }

    private int compareNullable(String a, String b) {
        if (a == null && b == null) {
            return 0;
        } else if (a == null) {
            return -1;
        } else if (b == null) {
            return 1;
        } else {
            return a.compareTo(b);
        }
    }
}

