//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nc.bs.pu.m422x.query;

import nc.bs.scmpub.util.SCMDataAccessUtils;
import nc.impl.pubapp.pattern.data.vo.VOQuery;
import nc.ui.querytemplate.querytree.IQueryScheme;
import nc.vo.mmpac.pickm.entity.AggPickmVO;
import nc.vo.mmpac.pickm.entity.PickmHeadVO;
import nc.vo.mmpac.pickm.entity.PickmItemVO;
import nc.vo.pubapp.pattern.model.tool.BillComposite;
import nc.vo.pubapp.query2.sql.process.QuerySchemeProcessor;

import java.util.HashSet;
import java.util.Set;

public abstract class Abstract422XRefQueryBP1 {
    protected String headtb;
    protected String itemtb;
    protected QuerySchemeProcessor psor;

    public Abstract422XRefQueryBP1(IQueryScheme queryScheme) {
        this.psor = new QuerySchemeProcessor(queryScheme);
        this.headtb = this.psor.getMainTableAlias();
        // this.itemtb = this.psor.getTableAliasOfAttribute("pk_storereq_b.pk_storereq_b");
    }

    public String getHeadtb() {
        return this.headtb;
    }

    public String getItemtb() {
        return this.itemtb;
    }

    public QuerySchemeProcessor getPsor() {
        return this.psor;
    }

    public abstract StringBuilder makeGetPKSql();

    public AggPickmVO[] queryPickmReqVOs() {
        SCMDataAccessUtils utils = new SCMDataAccessUtils(10000);
        // this.psor.appendCurrentGroup();
        StringBuilder sql = this.makeGetPKSql();
        String[][] pks = utils.query(sql.toString()).toTwoDimensionStringArray();
        if (null != pks && pks.length != 0) {
            Set<String> headPks = new HashSet();
            String[] itemPks = new String[pks.length];

            for (int i = 0; i < pks.length; ++i) {
                headPks.add(pks[i][0]);
                itemPks[i] = pks[i][1];
            }

            PickmHeadVO[] headers = (PickmHeadVO[]) (new VOQuery(PickmHeadVO.class)).query((String[]) headPks.toArray(new String[headPks.size()]));
            PickmItemVO[] items = (PickmItemVO[]) (new VOQuery(PickmItemVO.class)).query(itemPks);
            BillComposite<AggPickmVO> bc = new BillComposite(AggPickmVO.class);
            AggPickmVO tempVO = new AggPickmVO();
            bc.append(tempVO.getMetaData().getParent(), headers);
            bc.append(tempVO.getMetaData().getVOMeta(PickmItemVO.class), items);
            AggPickmVO[] queryResult = (AggPickmVO[]) bc.composite();
            // return this.processQueryResult(queryResult);
            return queryResult;
        } else {
            return null;
        }
    }

    public void setHeadtb(String headtb) {
        this.headtb = headtb;
    }

    public void setItemtb(String itemtb) {
        this.itemtb = itemtb;
    }

    public void setPsor(QuerySchemeProcessor psor) {
        this.psor = psor;
    }

    protected AggPickmVO[] processQueryResult(AggPickmVO[] queryResult) {
        return queryResult;
    }
}
