package com.seeyon.apps.src_ydctdatasync.utils;

import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumBean;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumItem;
import com.seeyon.ctp.util.JDBCAgent;

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

public class EnumMapUtils {
    public static String getEnumItemValue(String rootPCode,JDBCAgent agent,String groupValue,String targetValue) {
        String queryIdSql = "SELECT ce.ID FROM ctp_enum ce inner join ctp_enum cei on ce.`PARENT_ID` = cei.ID where CEI.`PROGRAM_COD`E = ? and ce.`ENUMNAME` = ?";
        Long enumId = null;
        try {
            agent.execute(queryIdSql, Arrays.asList(rootPCode,groupValue));
            Map map = agent.resultSetToMap();
            enumId = (Long)map.get("id");
        } catch (Exception e) {
            return null;
        }
        EnumManager enumManagerNew = (EnumManager) AppContext.getBean("enumManagerNew");
        CtpEnumBean ctpEnumBean = enumManagerNew.getEnum(enumId);
        if(ctpEnumBean == null) {
            return "";
        }
        List<CtpEnumItem> ctpEnumItems =  ctpEnumBean.getItems();
        if(ctpEnumBean.getItems() == null) {
            return null;
        }
        for (CtpEnumItem enumItem : ctpEnumItems) {
            if(enumItem.getShowvalue().equals(targetValue)) {
                return enumItem.getValue();
            }
        }
        return null;
    }
}
