package com.sifang.modules.taskdispatch.util;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.BusinessException;
import com.sifang.modules.sys.util.SpringUtils;
import com.sifang.modules.taskdispatch.constant.ReviewGroupConst;
import com.sifang.modules.taskdispatch.dao.CrmReviewGroupDao;
import org.apache.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class ReviewGroupUtil {

    /**
     * 91设计审核 92档案审核 93安装验收 94并网验收 95勘测信息变更 96 完工信息变更 97设计变更 98客户信息变更 99物料变更 100并网信息变更)
     */
    public static String mapFormKey2GroupId(String formKey, String partnerId) {
//        if (Objects.equals("91", formKey)) {
//            return ReviewGroupConst.GROUP_ID_DESIGN;
//        } else if (Objects.equals("92", formKey)) {
//            return ReviewGroupConst.GROUP_ID_ARCHIVE;
//        } else if (Objects.equals("93", formKey)) {
//            return ReviewGroupConst.GROUP_ID_INSTALL;
//        } else if (Objects.equals("94", formKey)) {
//            return ReviewGroupConst.GROUP_ID_GRID_MERGE;
//        } else if (Objects.equals("95", formKey)) {
//            return ReviewGroupConst.GROUP_ID_DESIGN;
//        } else if (Objects.equals("96", formKey)) {
//            return ReviewGroupConst.GROUP_ID_INSTALL;
//        } else if (Objects.equals("97", formKey)) {
//            return ReviewGroupConst.GROUP_ID_DESIGN;
//        } else if (Objects.equals("98", formKey)) {
//            return ReviewGroupConst.GROUP_ID_ARCHIVE;
//        } else if (Objects.equals("99", formKey)) {
//            return ReviewGroupConst.GROUP_ID_INSTALL;
//        } else if (Objects.equals("100", formKey)) {
//            return ReviewGroupConst.GROUP_ID_GRID_MERGE;
//        }
        String type = formKey2GroupType(formKey);
        if (StringUtils.isBlank(type)) {
            throw new IllegalArgumentException("invalid form key: " + formKey);
        }
        CrmReviewGroupDao reviewGroupDao = SpringUtils.getBean(CrmReviewGroupDao.class);
        Record group = reviewGroupDao.getByPartnerIdAndType(partnerId, type);
        if (group == null) {
            throw new BusinessException("尚未维护审核组处理此类型待办事项！");
        }
        return group.getStr("id");
    }

    public static String formKey2GroupType(String formKey) {
        if (Objects.equals("91", formKey)) {
            return ReviewGroupConst.GROUP_TYPE_DESIGN;
        } else if (Objects.equals("92", formKey)) {
            return ReviewGroupConst.GROUP_TYPE_ARCHIVE;
        } else if (Objects.equals("93", formKey)) {
            return ReviewGroupConst.GROUP_TYPE_INSTALL;
        } else if (Objects.equals("94", formKey)) {
            return ReviewGroupConst.GROUP_TYPE_GRID_MERGE;
        } else if (Objects.equals("95", formKey)) {
            return ReviewGroupConst.GROUP_TYPE_DESIGN;
        } else if (Objects.equals("96", formKey)) {
            return ReviewGroupConst.GROUP_TYPE_INSTALL;
        } else if (Objects.equals("97", formKey)) {
            return ReviewGroupConst.GROUP_TYPE_DESIGN;
        } else if (Objects.equals("98", formKey)) {
            return ReviewGroupConst.GROUP_TYPE_ARCHIVE;
        } else if (Objects.equals("99", formKey)) {
            return ReviewGroupConst.GROUP_TYPE_INSTALL;
        } else if (Objects.equals("100", formKey)) {
            return ReviewGroupConst.GROUP_TYPE_GRID_MERGE;
        }
        throw new BusinessException("invalid form key: " + formKey);
    }

//    public static List<String> mapGroupId2FormKey(String groupId) {
//        if (Objects.equals(groupId, ReviewGroupConst.GROUP_ID_DESIGN)) {
//            return Arrays.asList("91", "95", "97");
//        } else if (Objects.equals(groupId, ReviewGroupConst.GROUP_ID_ARCHIVE)) {
//            return Arrays.asList("92", "98");
//        } else if (Objects.equals(groupId, ReviewGroupConst.GROUP_ID_INSTALL)) {
//            return Arrays.asList("93", "96", "99");
//        } else if (Objects.equals(groupId, ReviewGroupConst.GROUP_ID_GRID_MERGE)) {
//            return Arrays.asList("94","100");
//        }
//        throw new BusinessException("invalid group: " + groupId);
//    }

    public static List<String> mapGroupType2FormKey(String type) {
        if (Objects.equals(type, ReviewGroupConst.GROUP_TYPE_DESIGN)) {
            return Arrays.asList("91", "95", "97");
        } else if (Objects.equals(type, ReviewGroupConst.GROUP_TYPE_ARCHIVE)) {
            return Arrays.asList("92", "98");
        } else if (Objects.equals(type, ReviewGroupConst.GROUP_TYPE_INSTALL)) {
            return Arrays.asList("93", "96", "99");
        } else if (Objects.equals(type, ReviewGroupConst.GROUP_TYPE_GRID_MERGE)) {
            return Arrays.asList("94","100");
        }
        throw new BusinessException("invalid group: " + type);
    }

//    public static String mapGroupId2Name(String groupId) {
//        if (Objects.equals(groupId, ReviewGroupConst.GROUP_ID_DESIGN)) {
//            return "设计审核组";
//        } else if (Objects.equals(groupId, ReviewGroupConst.GROUP_ID_ARCHIVE)) {
//            return "档案审核组";
//        } else if (Objects.equals(groupId, ReviewGroupConst.GROUP_ID_INSTALL)) {
//            return "安装验收组";
//        } else if (Objects.equals(groupId, ReviewGroupConst.GROUP_ID_GRID_MERGE)) {
//            return "并网验收组";
//        }
//        throw new BusinessException("invalid group id:" + groupId);
//    }

    public static String mapGroupType2Name(String type) {
        if (Objects.equals(type, ReviewGroupConst.GROUP_TYPE_DESIGN)) {
            return "设计审核组";
        } else if (Objects.equals(type, ReviewGroupConst.GROUP_TYPE_ARCHIVE)) {
            return "档案审核组";
        } else if (Objects.equals(type, ReviewGroupConst.GROUP_TYPE_INSTALL)) {
            return "安装验收组";
        } else if (Objects.equals(type, ReviewGroupConst.GROUP_TYPE_GRID_MERGE)) {
            return "并网验收组";
        }
        throw new BusinessException("invalid group id:" + type);
    }

}
