package cn.axzo.msg.center.common.model;


import cn.axzo.msg.center.common.enums.IdentityType;
import cn.axzo.msg.center.common.enums.OuType;
import cn.axzo.msg.center.common.exception.ServiceException;

import static cn.axzo.msg.center.common.enums.OuType.*;

/**
 * @author tanjie@axzo.cn
 * @date 2022/6/15 10:28
 */
public class ProfileIdRepair {
//    private static int NEW_ID_START = 5000000;
//
//    private static int ENT_USER_START_ID = 1200000;
//
//    private static int PLAT_USER_NEW_START = 1400000;
//
//    private static int WORKER_START = 1600000;
//
//    private static int WORKER_LEADER_START = 1800000;
//
//    private static int REGULATOR_START = 2000000;
//
//    /* 迁移范围 */
//    private static int TRANSFER_ID_START = 1000000;
//
//    private static int TEAM_START = 3000000;
//    private static int ENT_START = 3200000;
//    private static int AGENCY_START = 3400000;
//    private static int REGULATORY_START = 3600000;

    private static int NEW_ID_START = 0;

    private static int ENT_USER_START_ID = 0;

    private static int PLAT_USER_NEW_START = 0;

    private static int WORKER_START = 0;

    private static int WORKER_LEADER_START = 0;

    private static int REGULATOR_START = 0;

    /* 迁移范围 */
    private static int TRANSFER_ID_START = 0;

    private static int TEAM_START = 0;
    private static int ENT_START = 0;
    private static int AGENCY_START = 0;
    private static int REGULATORY_START = 0;

    public static Long identityOld2NewId(Long id, IdentityType identityType) {
        return id;
//        if (id > TRANSFER_ID_START) {
//            return id;
//        }
//        switch (identityType) {
//            case OPERATOR:
//                return id + PLAT_USER_NEW_START;
//            case WORKER:
//                return id + WORKER_START;
//            case PRACTITIONER:
//                return id + ENT_USER_START_ID;
//            case WORKER_LEADER:
//                return id + WORKER_LEADER_START;
//            case REGULATOR:
//                return id + REGULATOR_START;
//            default:
//                throw new ServiceException("bad identity type to convert id");
//        }
    }

    public static Long identityNew2OldId(Long id, IdentityType identityType) {
        return id;

//        if (id < TRANSFER_ID_START) {// 进来的就是旧id
//            return id;
//        }
//
//        if (id >= NEW_ID_START) {// 是全新的id，不用换
//            return id;
//        }
//
//        switch (identityType) {
//            case OPERATOR:
//                return id - PLAT_USER_NEW_START;
//            case WORKER:
//                return id - WORKER_START;
//            case PRACTITIONER:
//                return id - ENT_USER_START_ID;
//            case WORKER_LEADER:
//                return id - WORKER_LEADER_START;
//            case REGULATOR:
//                return id - REGULATOR_START;
//            default:
//                throw new ServiceException("bad identity type to convert id");
//        }
    }


    public static Long workerOld2NewId(Long id) {
        return identityOld2NewId(id, IdentityType.WORKER);
    }

    public static Long workerNewId2OldId(Long id) {
        return identityNew2OldId(id, IdentityType.WORKER);
    }

    public static Long teamLeaderOld2NewId(Long id) {
        return identityOld2NewId(id, IdentityType.WORKER);
    }

    public static Long teamLeaderNew2OldId(Long id) {
        return identityNew2OldId(id, IdentityType.WORKER_LEADER);
    }

    public static Long entUserNew2OldId(Long id) {
        return identityNew2OldId(id, IdentityType.PRACTITIONER);
    }

    public static Long entUserOld2NewId(Long id) {
        return identityOld2NewId(id, IdentityType.PRACTITIONER);
    }

    public static Long regulatorNew2OldId(Long id) {
        return identityNew2OldId(id, IdentityType.REGULATOR);
    }

    public static Long regulatorOld2NewId(Long id) {
        return identityOld2NewId(id, IdentityType.REGULATOR);
    }

    public static Long platUserOld2NewId(Long id) {
        return identityOld2NewId(id, IdentityType.OPERATOR);
    }

    public static Long platUserNew2OldId(Long id) {
        return identityNew2OldId(id, IdentityType.OPERATOR);
    }

    public static Long workerLeaderNewId2Old(Long id) {
        return identityNew2OldId(id, IdentityType.WORKER_LEADER);
    }

    public static Long workerLeaderOldId2New(Long id) {
        return identityOld2NewId(id, IdentityType.WORKER_LEADER);
    }

    public static Long ouOld2NewId(Long id, OuType ouType) {
        if (id > TRANSFER_ID_START) {
            return id;
        }
        switch (ouType) {
            case WORKER_GROUP:
                return id + TEAM_START;
            case CONTRACTOR_UNIT:
                return id + ENT_START;
            case BUILDING_UNIT:
                return id + ENT_START;
            case LAB_SUBCONTRACTOR_UNIT:
                return id + AGENCY_START;
            case PROFESSIONAL_SUBCONTRACTOR_UNIT:
                return id + AGENCY_START;
            case SUPERVISING_UNIT:
                return id + REGULATORY_START;
            default:
                throw new ServiceException("bad identity type to convert id");
        }
    }

    public static Long ouNew2OldId(Long id, OuType ouType) {
        if (id < TRANSFER_ID_START) {// 进来的就是旧id
            return id;
        }

        if (id >= NEW_ID_START) {// 是全新的id，不用换
            return id;
        }

        switch (ouType) {
            case WORKER_GROUP:
                return id - TEAM_START;
            case CONTRACTOR_UNIT:
                return id - ENT_START;
            case BUILDING_UNIT:
                return id - ENT_START;
            case LAB_SUBCONTRACTOR_UNIT:
                return id - AGENCY_START;
            case PROFESSIONAL_SUBCONTRACTOR_UNIT:
                return id - AGENCY_START;
            case SUPERVISING_UNIT:
                return id - REGULATORY_START;
            default:
                throw new ServiceException("bad identity type to convert id");
        }
    }

	public static Long identityNew2OldId(Long identityId, Integer identityType) {
		return identityNew2OldId(identityId, IdentityType.getIdentityType(identityType));
	}
}
