package nccloud.web.hryf.pub.utils;

import nc.ui.pub.bill.IBillItem;
import nc.vo.hi.psndoc.PsnJobVO;
import nc.vo.hi.psndoc.PsnOrgVO;
import nc.vo.uif2.LoginContext;
import nccloud.commons.lang.ArrayUtils;
import nccloud.framework.core.env.Locator;
import nccloud.framework.core.json.IJson;
import nccloud.framework.web.convert.templet.PageMetaCreator;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.ui.config.Area;
import nccloud.framework.web.ui.config.ITempletResource;
import nccloud.framework.web.ui.config.Item;
import nccloud.framework.web.ui.config.PageTemplet;
import nccloud.framework.web.ui.config.TempletQueryPara;
import nccloud.framework.web.ui.meta.PageMeta;
import nccloud.itf.hrhi.pub.IPsndocConst;
import nccloud.web.uapbd.commons.web.ParamUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

/**
 * @Auther: xueshh
 * @Date: 2021/3/23 18:53
 */
public class PsndocTempUtils {

    public static String getModelStr(ParamUtils param, String fieldName) {
        if (fieldName == null) {
            fieldName = "model";
        }
        IJson json = JsonFactory.create();
        HashMap m = new HashMap();
        m.put("model", param.get(fieldName));
        return json.toJson(m);
    }

    public static PageTemplet getPageTemp(LoginContext context, String pageCode) {
        return getPageTemp(context, null, pageCode);
    }

    public static PageTemplet getPageTemp(LoginContext context, String appCode, String pageCode) {
        TempletQueryPara tqpara = new TempletQueryPara();
        tqpara.setAppcode(appCode);
        tqpara.setPagecode(pageCode);
        tqpara.setPk_group(context.getPk_group());
        tqpara.setUserid(context.getPk_loginUser());
        PageTemplet pt = Locator.find(ITempletResource.class).query(tqpara);
        return pt;
    }

    public static PageMeta getPageMeta(final PageTemplet pt) {
        PageMetaCreator pagecreator = new PageMetaCreator();
        PageMeta meta = pagecreator.create(pt);
        return meta;
    }

    public static List<String> getAreaCodeList(PageTemplet pt) {
        Area[] areas = pt.getAllAreas();
        List<Area> areaList = Arrays.asList(areas);
        // 区域排序
        Collections.sort(areaList, new Comparator<Area>() {
            @Override
            public int compare(Area o1, Area o2) {
                return o1.getOrder() - o2.getOrder();
            }
        });
        ArrayList<String> sort = new ArrayList<>();
        sort.add("bd_psndoc");
        sort.add("hi_psnorg_h");
        sort.add("hi_psnjob_h");
        for (Area area : areaList) {
            if (ArrayUtils.contains(IPsndocConst.excludeCode, area.getCode())) {
                continue;
            }

            // 去掉没有显示项的子集
            Item[] items = area.getItems();
            boolean show = false;
            for (Item item : items) {
                if (item.isVisible()) {
                    show = true;
                    break;
                }
            }
            if (!show) {
                continue;
            }

            sort.add(area.getCode());
        }
        return sort;
    }

    /**
     * 获取表头或表头某个区域
     *
     * @param pageCode
     * @param areaCode
     * @param pos
     * @return
     */
    public static Area getAreaByCodePos(LoginContext context, String pageCode, String areaCode, int pos) {
        TempletQueryPara tqpara = new TempletQueryPara();
        tqpara.setPagecode(pageCode);
        tqpara.setPk_group(context.getPk_group());
        tqpara.setUserid(context.getPk_loginUser());
        PageTemplet pt = Locator.find(ITempletResource.class).query(tqpara);
        if (pos == IBillItem.HEAD && (PsnJobVO.getDefaultTableName().equals(areaCode)) || PsnOrgVO.getDefaultTableName().equals(areaCode)) {
            // 任职和组织关系表头模版编码有区别
            areaCode = areaCode + "_h";
        }
        Area area = pt.getArea(areaCode);
        return area;
    }

    /**
     * 获取区域的某个item
     *
     * @return
     */
    public static Item getItemByCode(Area area, String code) {
        return area.getItemByCode(code);
    }
}
