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

package nc.bs.cm.meascostcontrast;

import java.util.Map;
import nc.bd.framework.base.CMArrayUtil;
import nc.bd.framework.db.CMSqlBuilder;
import nc.cmpub.business.util.CMUtil;
import nc.vo.bd.bdactivity.entity.BDActivityVO;
import nc.vo.bd.material.MaterialVersionVO;
import nc.vo.bd.material.marbasclass.MarBasClassVO;
import nc.vo.bd.material.marcostclass.MarCostClassVO;
import nc.vo.bd.material.measdoc.MeasdocVO;
import nc.vo.bd.material.prod.MaterialProdVO;
import nc.vo.cm.costobject.entity.CostObjectVO;
import nc.vo.cm.costrevert.CtRevertHeadVO;
import nc.vo.cm.costrevert.CtRevertItemVO;
import nc.vo.cm.inprocost.entity.InproCostHeadVO;
import nc.vo.cm.inprocost.entity.InproCostItemVO;
import nc.vo.cm.meascostcontrast.CMCommonConst1614;
import nc.vo.cm.meascostcontrast.MeasCostParamVO;
import nc.vo.cm.meascostcontrast.MeasCostQueryConditionVO;
import nc.vo.cm.meascostcontrast.MeasCostUtil;
import nc.vo.cm.pc.pc0410.ProdcostHeadVO;
import nc.vo.cm.pc.pc0410.ProdcostItemVO;
import nc.vo.cmpub.framework.report.CMConditionVO;
import nc.vo.ml.MultiLangUtil;
import nc.vo.org.AccbookVO;
import nc.vo.org.OrgVO;
import nc.vo.pmpub.project.ProjectHeadVO;
import nc.vo.profitcenter.ProfitCenterVO;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.resa.costcenter.CostCenterVO;
import nc.vo.resa.factor.FactorAsoaVO;
import nc.vo.resa.factor.FactorVO;

public class MeasCostQuerySQLBuilder {
    public MeasCostQuerySQLBuilder() {
    }

    public String getSqlByQueryVOs(MeasCostParamVO paramVO) {
        Map<String, CMConditionVO> conditionMap = paramVO.getConditionMap();
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.append(this.getSelectSQL(conditionMap));
        sql.append(" from ");
        sql.l();
        sql.append(this.getVcostobjSQL(paramVO));
        sql.r();
        sql.append("cm_costobj");
        sql.append(" inner join ");
        sql.l();
        sql.append(this.getFactorSQL(paramVO));
        sql.r();
        sql.append("cost_factor");
        sql.on("cm_costobj", "vcostobjcode", "cost_factor", "vcostobjcode");
        sql.and();
        sql.append("cm_costobj.cccode=cost_factor.cccode");
        String[] showDetailDiff = MeasCostUtil.getShowDetailDiff(conditionMap);
        if (CMArrayUtil.isEquals(CMCommonConst1614.SUBTYPE_ITEMS, showDetailDiff)) {
            sql.and();
            sql.append("cm_costobj.bsubtype");
            sql.append("=");
            sql.append("cost_factor.bsubtype");
        } else if (CMArrayUtil.isEquals(CMCommonConst1614.CENTERFROM_ITEMS, showDetailDiff)) {
            sql.and();
            sql.append("cm_costobj.bfromcostcenter");
            sql.append("=");
            sql.append("cost_factor.bfromcostcenter");
        }

        sql.append(" order by ");
        sql.append("cm_costobj");
        sql.append(".");
        sql.append("vcostobjcode");
        sql.append(",");
        sql.append("cost_factor");
        sql.append(".");
        sql.append("factorcode");
        return sql.toString();
    }

    private String getSelectSQL(Map<String, CMConditionVO> conditionMap) {
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.select();
        sql.append("cm_costobj.bookname");
        sql.append(",");
        sql.append("cm_costobj.profitcentername");
        sql.append(",");
        sql.append("cm_costobj.orgname");
        sql.append(",");
        sql.append("cm_costobj.cccode");
        sql.append(",");
        sql.append("cm_costobj.ccname");
        sql.append(",");
        sql.append("cm_costobj.vcostobjcode");
        sql.append(",");
        sql.append("cm_costobj.vcostobjname");
        sql.append(",");
        sql.append("cm_costobj.vcostobjname0");
        sql.append(",");
        sql.append("cm_costobj.nnum");
        sql.append(",");
        sql.append("cm_costobj.nsum");
        sql.append(",");
        sql.append("cm_costobj.measdocname");
        sql.append(",");
        sql.append("cm_costobj.bitnumber");
        sql.append(",");
        sql.append("cm_costobj.materialspec");
        sql.append(",");
        sql.append("cm_costobj.materialtype");
        sql.append(",");
        sql.append("cost_factor.factorcode");
        sql.append(",");
        sql.append("cost_factor.factorname");
        sql.append(",");
        sql.append("cost_factor.factorname0");
        sql.append(",");
        sql.append("cost_factor.factornsum");
        sql.append(",");
        sql.append("cost_factor.factorlev");
        sql.append(",");
        sql.append("cost_factor.pk_factorasoa");

        // todo
        sql.append(",");
        sql.append("nvl(cost_factor.project_code,'') projectcode");
        sql.append(",");
        sql.append("nvl(cost_factor.project_name,'') projectname");

        String[] showDetailDiff = MeasCostUtil.getShowDetailDiff(conditionMap);
        if (CMArrayUtil.isEquals(CMCommonConst1614.STDCOST_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("cm_costobj.nstdcost_nsum");
            sql.append(" nstdcostnsum ");
            sql.append(",");
            sql.append("cm_costobj.nnumdiff_nsum");
            sql.append(" nnumdiffnsum ");
            sql.append(",");
            sql.append("cm_costobj.npricediff_nsum");
            sql.append(" npricediffnsum ");
            sql.append(",");
            sql.append("cost_factor.nstdcost_factor");
            sql.append(" nstdcostfactor ");
            sql.append(",");
            sql.append("cost_factor.nnumdiff_factor");
            sql.append(" nnumdifffactor ");
            sql.append(",");
            sql.append("cost_factor.npricediff_factor");
            sql.append(" npricedifffactor ");
        } else if (CMArrayUtil.isEquals(CMCommonConst1614.SUBTYPE_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("cm_costobj.bsubtype");
        } else if (CMArrayUtil.isEquals(CMCommonConst1614.CENTERFROM_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("cm_costobj.bfromcostcenter");
        }

        return sql.toString();
    }

    private String getVcostobjSQL(MeasCostParamVO paramVO) {
        Map<String, CMConditionVO> conditionMap = paramVO.getConditionMap();
        CMSqlBuilder sql = new CMSqlBuilder();
        Integer ireportsource = MeasCostQueryConditionVO.REVERT_BEFORE_VALUE;
        if (conditionMap.containsKey("ireportsource") && ((CMConditionVO)conditionMap.get("ireportsource")).getSingleValue() != null) {
            ireportsource = Integer.valueOf(((CMConditionVO)conditionMap.get("ireportsource")).getSingleValue());
        }

        Integer ishowtype = MeasCostQueryConditionVO.PRODUCT_VALUE;
        if (conditionMap.containsKey("ishowtype") && ((CMConditionVO)conditionMap.get("ishowtype")).getSingleValue() != null) {
            ishowtype = Integer.valueOf(((CMConditionVO)conditionMap.get("ishowtype")).getSingleValue());
        }

        sql.append(this.getVcostobjSelectSQLForNNum(conditionMap));
        sql.l();
        if (MeasCostQueryConditionVO.COSTOBJECT_VALUE.equals(ishowtype) && MeasCostQueryConditionVO.REVERT_BEFORE_VALUE.equals(ireportsource)) {
            sql.append(this.getVcostobjSelectSQLForNNum(conditionMap));
            sql.l();
            sql.append(this.getVcostobjSelectSQL(conditionMap, MeasCostQueryConditionVO.COSTOBJECT_VALUE));
            sql.append(this.getVcostobjSelectSQLAddMeasdoc(conditionMap, MeasCostQueryConditionVO.COSTOBJECT_VALUE));
            sql.append(this.getVcostobjFromSQL(conditionMap, (Integer)null));
            sql.append(this.getVcostobjWhereSQL(paramVO));
            sql.append(this.getVcostobjGroupSQL(conditionMap, MeasCostQueryConditionVO.COSTOBJECT_VALUE));
            sql.append(this.getVcostobjGroupSQLAddMeasdoc(ishowtype, ireportsource));
            sql.r();
            sql.append(" tt ");
            sql.append(this.getVcostobjGroupSQLForNNum(conditionMap));
        } else if (MeasCostQueryConditionVO.PRODUCT_VALUE.equals(ishowtype)) {
            sql.append(this.getVcostobjSelectSQLForNNum(conditionMap));
            sql.l();
            sql.append(this.getVcostobjSelectSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_VALUE));
            sql.append(this.getVcostobjSelectSQLAddMeasdoc(conditionMap, MeasCostQueryConditionVO.PRODUCT_VALUE));
            sql.append(this.getVcostobjFromSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_VALUE));
            sql.append(this.getVcostobjWhereSQL(paramVO));
            sql.append(this.getVcostobjGroupSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_VALUE));
            sql.append(this.getVcostobjGroupSQLAddMeasdoc(ishowtype, ireportsource));
            sql.r();
            sql.append(" tt ");
            sql.append(this.getVcostobjGroupSQLForNNum(conditionMap));
        } else if (MeasCostQueryConditionVO.PRODUCT_COST_VALUE.equals(ishowtype)) {
            sql.append(this.getVcostobjSelectSQLForNNum(conditionMap));
            sql.l();
            sql.append(this.getVcostobjSelectSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
            sql.append(this.getVcostobjSelectSQLAddMeasdoc(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
            sql.append(this.getVcostobjFromSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
            sql.append(this.getVcostobjWhereSQL(paramVO));
            sql.append(this.getVcostobjGroupSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
            sql.r();
            sql.append(" tt ");
            sql.append(this.getVcostobjGroupSQLForNNum(conditionMap));
        } else if (MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE.equals(ishowtype)) {
            sql.l();
            sql.append(this.getVcostobjSelectSQLForNNum(conditionMap));
            sql.l();
            sql.append(this.getVcostobjSelectSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
            sql.append(this.getVcostobjSelectSQLAddMeasdoc(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
            sql.append(this.getVcostobjFromSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_VALUE));
            sql.innerjoin(MarBasClassVO.getDefaultTableName());
            sql.append(" ");
            sql.append(MarBasClassVO.getDefaultTableName());
            sql.on(MarBasClassVO.getDefaultTableName(), "pk_marbasclass", MaterialVersionVO.getDefaultTableName(), "pk_marbasclass");
            sql.append(this.getVcostobjWhereSQL(paramVO));
            sql.append(this.getVcostobjGroupSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
            sql.r();
            sql.append(" tt ");
            sql.append(this.getVcostobjGroupSQLForNNum(conditionMap));
            sql.r();
            sql.append(" union all ");
            sql.l();
            sql.append(this.getVcostobjSelectSQLForNNum(conditionMap));
            sql.l();
            sql.append(this.getVcostobjSelectSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
            sql.append(this.getVcostobjSelectSQLAddMeasdoc(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
            sql.append(this.getVcostobjFromSQLFeature(conditionMap));
            sql.append(this.getVcostobjWhereSQL(paramVO));
            sql.append(this.getVcostobjGroupSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
            sql.r();
            sql.append(" tt ");
            sql.append(this.getVcostobjGroupSQLForNNum(conditionMap));
            sql.r();
            sql.append(" union all ");
            sql.l();
            sql.append(this.getVcostobjSelectSQLForNNum(conditionMap));
            sql.l();
            sql.append(this.getVcostobjSelectSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
            sql.append(this.getVcostobjSelectSQLAddMeasdoc(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
            sql.append(this.getVcostobjFromSQLNoFeature(conditionMap));
            sql.append(this.getVcostobjWhereSQL(paramVO));
            sql.append(this.getVcostobjGroupSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
            sql.r();
            sql.append(" tt ");
            sql.append(this.getVcostobjGroupSQLForNNum(conditionMap));
            sql.r();
        }

        sql.r();
        sql.append(" tt ");
        sql.append(this.getVcostobjGroupSQLForNNum(conditionMap));
        return sql.toString();
    }

    private String getVcostobjFromSQLFeature(Map<String, CMConditionVO> conditionMap) {
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.append(this.getVcostobjFromSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
        sql.innerjoin(" bd_materialprod ");
        sql.append(" ");
        sql.append(MaterialProdVO.getDefaultTableName());
        sql.on(MaterialProdVO.getDefaultTableName(), "pk_marcostclass", MarCostClassVO.getDefaultTableName(), "pk_marcostclass");
        sql.and();
        sql.append(MaterialProdVO.getDefaultTableName() + ".pk_org");
        sql.append("=");
        sql.append("cm_cost_table.pk_org");
        sql.innerjoin(MaterialVersionVO.getDefaultTableName());
        sql.append(" ");
        sql.append(MaterialVersionVO.getDefaultTableName());
        sql.on(MaterialVersionVO.getDefaultTableName(), "pk_material", MaterialProdVO.getDefaultTableName(), "pk_material");
        sql.and();
        sql.append(MaterialProdVO.getDefaultTableName() + ".classfeature");
        sql.append("='");
        sql.append("Y");
        sql.append("'");
        sql.innerjoin(MarBasClassVO.getDefaultTableName());
        sql.append(" ");
        sql.append(MarBasClassVO.getDefaultTableName());
        sql.on(MarBasClassVO.getDefaultTableName(), "pk_marbasclass", MaterialVersionVO.getDefaultTableName(), "pk_marbasclass");
        return sql.toString();
    }

    private String getVcostobjFromSQLNoFeature(Map<String, CMConditionVO> conditionMap) {
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.append(this.getVcostobjFromSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
        sql.innerjoin(" bd_materialprod ");
        sql.append(" ");
        sql.append(MaterialProdVO.getDefaultTableName());
        sql.on(MaterialProdVO.getDefaultTableName(), "pk_marcostclass", MarCostClassVO.getDefaultTableName(), "pk_marcostclass");
        sql.and();
        sql.append(MaterialProdVO.getDefaultTableName() + ".pk_org");
        sql.append("=");
        sql.append("cm_cost_table.pk_org");
        sql.innerjoin(MaterialVersionVO.getDefaultTableName());
        sql.append(" ");
        sql.append(MaterialVersionVO.getDefaultTableName());
        sql.on(MaterialVersionVO.getDefaultTableName(), "pk_material", MaterialProdVO.getDefaultTableName(), "pk_material");
        sql.and();
        sql.append(MaterialVersionVO.getDefaultTableName() + ".pk_material");
        sql.in();
        sql.l();
        sql.select();
        sql.append(" min");
        sql.l();
        sql.append(MaterialProdVO.getDefaultTableName() + ".pk_material");
        sql.r();
        sql.from(MaterialProdVO.getDefaultTableName());
        sql.where();
        sql.append(MaterialProdVO.getDefaultTableName() + ".pk_marcostclass");
        sql.append("<> '~'");
        sql.and();
        sql.append(" not exists ");
        sql.l();
        sql.select();
        sql.append("bd_materialprod_temp1.pk_material");
        sql.from(" bd_materialprod ");
        sql.append(" ");
        sql.append("bd_materialprod_temp1");
        sql.innerjoin(" bd_materialprod ");
        sql.append(" ");
        sql.append("bd_materialprod_temp2");
        sql.on("bd_materialprod_temp1", "pk_marcostclass", "bd_materialprod_temp2", "pk_marcostclass");
        sql.and();
        sql.appendIDIsNotNull("bd_materialprod_temp1.pk_marcostclass");
        sql.and();
        sql.appendIDIsNotNull("bd_materialprod_temp2.pk_marcostclass");
        sql.and();
        sql.append("bd_materialprod_temp2.classfeature");
        sql.append("='");
        sql.append("Y");
        sql.append("'");
        sql.r();
        sql.groupBy(MaterialProdVO.getDefaultTableName(), new String[]{"pk_marcostclass"});
        sql.r();
        sql.innerjoin(MarBasClassVO.getDefaultTableName());
        sql.append(" ");
        sql.append(MarBasClassVO.getDefaultTableName());
        sql.on(MarBasClassVO.getDefaultTableName(), "pk_marbasclass", MaterialVersionVO.getDefaultTableName(), "pk_marbasclass");
        return sql.toString();
    }

    private String getFactorSQL(MeasCostParamVO paramVO) {
        Map<String, CMConditionVO> conditionMap = paramVO.getConditionMap();
        CMSqlBuilder sql = new CMSqlBuilder();
        String baloneshowactivity = "N";
        if (conditionMap.containsKey("baloneshowactivity") && ((CMConditionVO)conditionMap.get("baloneshowactivity")).getSingleValue() != null) {
            ((CMConditionVO)conditionMap.get("baloneshowactivity")).getSingleValue();
        }

        if ("Y".equals(baloneshowactivity)) {
            sql.l();
            sql.append(this.getFactorSubSQL(paramVO, MeasCostQueryConditionVO.ACT_VALUE));
            sql.r();
            sql.append(" union all ");
            sql.l();
            sql.append(this.getFactorSubSQL(paramVO, MeasCostQueryConditionVO.NOACT_VALUE));
            sql.r();
        } else {
            sql.append(this.getFactorSubSQL(paramVO, (Integer)null));
        }

        return sql.toString();
    }

    private String getFactorSubSQL(MeasCostParamVO paramVO, Integer iAct) {
        Map<String, CMConditionVO> conditionMap = paramVO.getConditionMap();
        Integer ishowtype = MeasCostQueryConditionVO.PRODUCT_VALUE;
        if (conditionMap.containsKey("ishowtype") && ((CMConditionVO)conditionMap.get("ishowtype")).getSingleValue() != null) {
            ishowtype = Integer.valueOf(((CMConditionVO)conditionMap.get("ishowtype")).getSingleValue());
        }

        CMSqlBuilder sql = new CMSqlBuilder();
        if (MeasCostQueryConditionVO.COSTOBJECT_VALUE.equals(ishowtype)) {
            if (MeasCostQueryConditionVO.ACT_VALUE.equals(iAct)) {
                sql.append(this.getFactorSelectSQLAct(conditionMap, MeasCostQueryConditionVO.COSTOBJECT_VALUE));
                sql.append(this.getFactorFromSQLAct(conditionMap, (Integer)null));
                sql.append(this.getFactorWhereSQL(paramVO));
                sql.append(this.getFactorGroupSQLAct(conditionMap, MeasCostQueryConditionVO.COSTOBJECT_VALUE));
            } else if (MeasCostQueryConditionVO.NOACT_VALUE.equals(iAct)) {
                sql.append(this.getFactorSelectSQL(conditionMap, MeasCostQueryConditionVO.COSTOBJECT_VALUE));
                sql.append(this.getFactorFromSQLNoAct(conditionMap, (Integer)null));
                sql.append(this.getFactorWhereSQL(paramVO));
                sql.append(this.getFactorGroupSQL(conditionMap, MeasCostQueryConditionVO.COSTOBJECT_VALUE));
            } else {
                sql.append(this.getFactorSelectSQL(conditionMap, MeasCostQueryConditionVO.COSTOBJECT_VALUE));
                sql.append(this.getFactorFromSQL(conditionMap, (Integer)null));
                sql.append(this.getFactorWhereSQL(paramVO));
                sql.append(this.getFactorGroupSQL(conditionMap, MeasCostQueryConditionVO.COSTOBJECT_VALUE));
            }
        } else if (MeasCostQueryConditionVO.PRODUCT_VALUE.equals(ishowtype)) {
            if (MeasCostQueryConditionVO.ACT_VALUE.equals(iAct)) {
                sql.append(this.getFactorSelectSQLAct(conditionMap, MeasCostQueryConditionVO.PRODUCT_VALUE));
                sql.append(this.getFactorFromSQLAct(conditionMap, MeasCostQueryConditionVO.PRODUCT_VALUE));
                sql.append(this.getFactorWhereSQL(paramVO));
                sql.append(this.getFactorGroupSQLAct(conditionMap, MeasCostQueryConditionVO.PRODUCT_VALUE));
            } else if (MeasCostQueryConditionVO.NOACT_VALUE.equals(iAct)) {
                sql.append(this.getFactorSelectSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_VALUE));
                sql.append(this.getFactorFromSQLNoAct(conditionMap, MeasCostQueryConditionVO.PRODUCT_VALUE));
                sql.append(this.getFactorWhereSQL(paramVO));
                sql.append(this.getFactorGroupSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_VALUE));
            } else {
                sql.append(this.getFactorSelectSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_VALUE));
                sql.append(this.getFactorFromSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_VALUE));
                sql.append(this.getFactorWhereSQL(paramVO));
                sql.append(this.getFactorGroupSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_VALUE));
            }
        } else if (MeasCostQueryConditionVO.PRODUCT_COST_VALUE.equals(ishowtype)) {
            if (MeasCostQueryConditionVO.ACT_VALUE.equals(iAct)) {
                sql.append(this.getFactorSelectSQLAct(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
                sql.append(this.getFactorFromSQLAct(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
                sql.append(this.getFactorWhereSQL(paramVO));
                sql.append(this.getFactorGroupSQLAct(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
            } else if (MeasCostQueryConditionVO.NOACT_VALUE.equals(iAct)) {
                sql.append(this.getFactorSelectSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
                sql.append(this.getFactorFromSQLNoAct(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
                sql.append(this.getFactorWhereSQL(paramVO));
                sql.append(this.getFactorGroupSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
            } else {
                sql.append(this.getFactorSelectSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
                sql.append(this.getFactorFromSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
                sql.append(this.getFactorWhereSQL(paramVO));
                sql.append(this.getFactorGroupSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_COST_VALUE));
            }
        } else if (MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE.equals(ishowtype)) {
            sql.l();
            if (MeasCostQueryConditionVO.ACT_VALUE.equals(iAct)) {
                sql.append(this.getFactorSelectSQLAct(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
                sql.append(this.getFactorFromSQLAct(conditionMap, MeasCostQueryConditionVO.PRODUCT_VALUE));
            } else if (MeasCostQueryConditionVO.NOACT_VALUE.equals(iAct)) {
                sql.append(this.getFactorSelectSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
                sql.append(this.getFactorFromSQLNoAct(conditionMap, MeasCostQueryConditionVO.PRODUCT_VALUE));
            } else {
                sql.append(this.getFactorSelectSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
                sql.append(this.getFactorFromSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_VALUE));
            }

            sql.innerjoin(MarBasClassVO.getDefaultTableName());
            sql.append(" ");
            sql.append(MarBasClassVO.getDefaultTableName());
            sql.on(MarBasClassVO.getDefaultTableName(), "pk_marbasclass", MaterialVersionVO.getDefaultTableName(), "pk_marbasclass");
            sql.append(this.getFactorWhereSQL(paramVO));
            if (MeasCostQueryConditionVO.ACT_VALUE.equals(iAct)) {
                sql.append(this.getFactorGroupSQLAct(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
            } else if (MeasCostQueryConditionVO.NOACT_VALUE.equals(iAct)) {
                sql.append(this.getFactorGroupSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
            } else {
                sql.append(this.getFactorGroupSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
            }

            sql.r();
            sql.append(" union all ");
            sql.l();
            if (MeasCostQueryConditionVO.ACT_VALUE.equals(iAct)) {
                sql.append(this.getFactorSelectSQLAct(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
                sql.append(this.getFactorFromSQLAct(conditionMap, MeasCostQueryConditionVO.FEATURE_VALUE));
                sql.append(this.getFactorWhereSQL(paramVO));
                sql.append(this.getFactorGroupSQLAct(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
            } else if (MeasCostQueryConditionVO.NOACT_VALUE.equals(iAct)) {
                sql.append(this.getFactorSelectSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
                sql.append(this.getFactorFromSQLNoAct(conditionMap, MeasCostQueryConditionVO.FEATURE_VALUE));
                sql.append(this.getFactorWhereSQL(paramVO));
                sql.append(this.getFactorGroupSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
            } else {
                sql.append(this.getFactorSelectSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
                sql.append(this.getFactorFromSQL(conditionMap, MeasCostQueryConditionVO.FEATURE_VALUE));
                sql.append(this.getFactorWhereSQL(paramVO));
                sql.append(this.getFactorGroupSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
            }

            sql.r();
            sql.append(" union all ");
            sql.l();
            if (MeasCostQueryConditionVO.ACT_VALUE.equals(iAct)) {
                sql.append(this.getFactorSelectSQLAct(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
                sql.append(this.getFactorFromSQLAct(conditionMap, MeasCostQueryConditionVO.NOFEATURE_VALUE));
                sql.append(this.getFactorWhereSQL(paramVO));
                sql.append(this.getFactorGroupSQLAct(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
            } else if (MeasCostQueryConditionVO.NOACT_VALUE.equals(iAct)) {
                sql.append(this.getFactorSelectSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
                sql.append(this.getFactorFromSQLNoAct(conditionMap, MeasCostQueryConditionVO.NOFEATURE_VALUE));
                sql.append(this.getFactorWhereSQL(paramVO));
                sql.append(this.getFactorGroupSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
            } else {
                sql.append(this.getFactorSelectSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
                sql.append(this.getFactorFromSQL(conditionMap, MeasCostQueryConditionVO.NOFEATURE_VALUE));
                sql.append(this.getFactorWhereSQL(paramVO));
                sql.append(this.getFactorGroupSQL(conditionMap, MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE));
            }

            sql.r();
        }

        return sql.toString();
    }

    private String getVcostobjSelectSQL(Map<String, CMConditionVO> conditionMap, Integer ishowtype) {
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.append(this.getShowTypeSelectSQL(conditionMap, ishowtype));
        sql.append(",");
        sql.append("max");
        sql.l();
        sql.append("cm_cost_table.nnum");
        sql.r();
        Integer ireportsource = Integer.valueOf(((CMConditionVO)conditionMap.get("ireportsource")).getSingleValue());
        sql.append(" ");
        sql.append("nnum");
        sql.append(",");
        if (MeasCostQueryConditionVO.REVERT_BEFORE_VALUE.equals(ireportsource)) {
            sql.sum("cm_cost_table_b", "nmoney", "nsum");
        } else if (MeasCostQueryConditionVO.REVERT_AFTER_VALUE.equals(ireportsource)) {
            sql.sum("cm_cost_table_b", "aftercost", "nsum");
        }

        String[] showDetailDiff = MeasCostUtil.getShowDetailDiff(conditionMap);
        if (CMArrayUtil.isEquals(CMCommonConst1614.STDCOST_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.sum("cm_cost_table_b", "nstdcost", "nstdcost_nsum");
            sql.append(",");
            sql.sum("cm_cost_table_b", "nnumdiff", "nnumdiff_nsum");
            sql.append(",");
            sql.sum("cm_cost_table_b", "npricediff", "npricediff_nsum");
        } else if (CMArrayUtil.isEquals(CMCommonConst1614.SUBTYPE_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("cm_cost_table_b.bsubtype");
        } else if (CMArrayUtil.isEquals(CMCommonConst1614.CENTERFROM_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("cm_cost_table_b.bfromcostcenter");
        }

        return sql.toString();
    }

    private void getBookAProfitcenterAOrgName(CMSqlBuilder sql, UFBoolean isGroupby) {
        if (isGroupby.booleanValue()) {
            sql.append(",");
        }

        String accbookName = CMUtil.getFiledNameByMultiLang("name");
        String var10001 = AccbookVO.getDefaultTableName();
        sql.append("isnull(" + var10001 + "." + accbookName + "," + AccbookVO.getDefaultTableName() + ".name) ");
        if (!isGroupby.booleanValue()) {
            sql.append("bookname");
        }

        sql.append(",");
        String profitcentername = CMUtil.getFiledNameByMultiLang("name");
        var10001 = ProfitCenterVO.getDefaultTableName();
        sql.append("isnull(" + var10001 + "." + profitcentername + "," + ProfitCenterVO.getDefaultTableName() + ".name) ");
        if (!isGroupby.booleanValue()) {
            sql.append("profitcentername");
        }

        sql.append(",");
        String orgName = CMUtil.getFiledNameByMultiLang("name");
        var10001 = OrgVO.getDefaultTableName();
        sql.append("isnull(" + var10001 + "." + orgName + "," + OrgVO.getDefaultTableName() + ".name) ");
        if (!isGroupby.booleanValue()) {
            sql.append("orgname");
        }

        sql.append(",");
    }

    private String getShowTypeSelectSQL(Map<String, CMConditionVO> conditionMap, Integer ishowtype) {
        Integer ireportsource = Integer.valueOf(((CMConditionVO)conditionMap.get("ireportsource")).getSingleValue());
        Integer ireportcontent = -1;
        String content = null;
        if (conditionMap.get("ireportcontent") != null) {
            content = ((CMConditionVO)conditionMap.get("ireportcontent")).getSingleValue();
        }

        if (content != null && MeasCostQueryConditionVO.REVERT_BEFORE_VALUE.equals(ireportsource)) {
            ireportcontent = Integer.valueOf(content);
        }

        CMSqlBuilder sql = new CMSqlBuilder();
        sql.select();
        if (!MeasCostQueryConditionVO.PRODCOST_VALUE.equals(ireportcontent) && !MeasCostQueryConditionVO.WASTER_VALUE.equals(ireportcontent)) {
            if (MeasCostQueryConditionVO.INPROCOST_VALUE.equals(ireportcontent)) {
                sql.append("cm_cost_table.cinprocostid");
            } else if (ireportcontent == -1) {
                sql.append("cm_cost_table.pk_ctrevert");
            }
        } else {
            sql.append("cm_cost_table.cprodcostid");
        }

        sql.append(" ");
        sql.append("PK");
        sql.append(",");
        this.getBookAProfitcenterAOrgName(sql, UFBoolean.FALSE);
        sql.append(CostCenterVO.getDefaultTableName() + ".cccode");
        String ccname = CMUtil.getFiledNameByMultiLang("ccname");
        String var10001 = CostCenterVO.getDefaultTableName();
        sql.append(",isnull(" + var10001 + "." + ccname + "," + CostCenterVO.getDefaultTableName() + ".ccname) ccname");
        sql.append(",");
        if (MeasCostQueryConditionVO.COSTOBJECT_VALUE.equals(ishowtype) && MeasCostQueryConditionVO.REVERT_BEFORE_VALUE.equals(ireportsource)) {
            sql.append(CostObjectVO.getDefaultTableName() + ".vcostobjcode");
            sql.append(" ");
            sql.append("vcostobjcode");
            sql.append(",");
            var10001 = CostObjectVO.getDefaultTableName();
            sql.append(var10001 + ".vcostobjname" + MultiLangUtil.getCurrentLangSeqSuffix());
            sql.append(" ");
            sql.append("vcostobjname");
            sql.append(",");
            sql.append(CostObjectVO.getDefaultTableName() + ".vcostobjname");
            sql.append(" ");
            sql.append("vcostobjname0");
        } else if (MeasCostQueryConditionVO.PRODUCT_VALUE.equals(ishowtype)) {
            sql.append(MaterialVersionVO.getDefaultTableName() + ".code");
            sql.append(" ");
            sql.append("vcostobjcode");
            sql.append(",");
            var10001 = MaterialVersionVO.getDefaultTableName();
            sql.append(var10001 + ".name" + MultiLangUtil.getCurrentLangSeqSuffix());
            sql.append(" ");
            sql.append("vcostobjname");
            sql.append(",");
            sql.append(MaterialVersionVO.getDefaultTableName() + ".name");
            sql.append(" ");
            sql.append("vcostobjname0");
        } else if (MeasCostQueryConditionVO.PRODUCT_COST_VALUE.equals(ishowtype)) {
            sql.append(MarCostClassVO.getDefaultTableName() + ".code");
            sql.append(" ");
            sql.append("vcostobjcode");
            sql.append(",");
            var10001 = MarCostClassVO.getDefaultTableName();
            sql.append(var10001 + ".name" + MultiLangUtil.getCurrentLangSeqSuffix());
            sql.append(" ");
            sql.append("vcostobjname");
            sql.append(",");
            sql.append(MarCostClassVO.getDefaultTableName() + ".name");
            sql.append(" ");
            sql.append("vcostobjname0");
        } else if (MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE.equals(ishowtype)) {
            sql.append(MarBasClassVO.getDefaultTableName() + ".code");
            sql.append(" ");
            sql.append("vcostobjcode");
            sql.append(",");
            var10001 = MarBasClassVO.getDefaultTableName();
            sql.append(var10001 + ".name" + MultiLangUtil.getCurrentLangSeqSuffix());
            sql.append(" ");
            sql.append("vcostobjname");
            sql.append(",");
            sql.append(MarBasClassVO.getDefaultTableName() + ".name");
            sql.append(" ");
            sql.append("vcostobjname0");
        }

        return sql.toString();
    }

    private String getVcostobjFromSQL(Map<String, CMConditionVO> conditionMap, Integer ishowtype) {
        CMSqlBuilder sql = new CMSqlBuilder();
        if (MeasCostQueryConditionVO.FEATURE_VALUE.equals(ishowtype)) {
            sql.append(this.getVcostobjFromSQLFeature(conditionMap));
            return sql.toString();
        } else if (MeasCostQueryConditionVO.NOFEATURE_VALUE.equals(ishowtype)) {
            sql.append(this.getVcostobjFromSQLNoFeature(conditionMap));
            return sql.toString();
        } else {
            sql.append(" from ");
            Integer ireportsource = Integer.valueOf(((CMConditionVO)conditionMap.get("ireportsource")).getSingleValue());
            Integer ishowtypeTemp = Integer.valueOf(((CMConditionVO)conditionMap.get("ishowtype")).getSingleValue());
            if (MeasCostQueryConditionVO.REVERT_BEFORE_VALUE.equals(ireportsource)) {
                Integer ireportcontent = Integer.valueOf(((CMConditionVO)conditionMap.get("ireportcontent")).getSingleValue());
                if (!MeasCostQueryConditionVO.PRODCOST_VALUE.equals(ireportcontent) && !MeasCostQueryConditionVO.WASTER_VALUE.equals(ireportcontent)) {
                    if (MeasCostQueryConditionVO.INPROCOST_VALUE.equals(ireportcontent)) {
                        sql.append(InproCostHeadVO.getDefaultTableName());
                        sql.append(" ");
                        sql.append("cm_cost_table");
                        sql.innerjoin(InproCostItemVO.getDefaultTableName());
                        sql.append(" ");
                        sql.append("cm_cost_table_b");
                        sql.on("cm_cost_table", "cinprocostid", "cm_cost_table_b", "cinprocostid");
                    }
                } else {
                    sql.append(ProdcostHeadVO.getDefaultTableName());
                    sql.append(" ");
                    sql.append("cm_cost_table");
                    sql.innerjoin(ProdcostItemVO.getDefaultTableName());
                    sql.append(" ");
                    sql.append("cm_cost_table_b");
                    sql.on("cm_cost_table", "cprodcostid", "cm_cost_table_b", "cprodcostid");
                }

                sql.innerjoin(CostObjectVO.getDefaultTableName());
                sql.append(" ");
                sql.append(CostObjectVO.getDefaultTableName());
                sql.on(CostObjectVO.getDefaultTableName(), "ccostobjectid", "cm_cost_table", "ccostobjectid");
                if (MeasCostQueryConditionVO.PRODUCT_VALUE.equals(ishowtype)) {
                    sql.innerjoin(MaterialVersionVO.getDefaultTableName());
                    sql.append(" ");
                    sql.append(MaterialVersionVO.getDefaultTableName());
                    sql.on(MaterialVersionVO.getDefaultTableName(), "pk_source", CostObjectVO.getDefaultTableName(), "cmaterialid");
                    sql.append(" ");
                    sql.append("left join ");
                    sql.append("bd_measdoc");
                    sql.append(" ");
                    sql.append("bd_measdoc");
                    sql.on("bd_measdoc", "pk_measdoc", MaterialVersionVO.getDefaultTableName(), "pk_measdoc");
                } else if (MeasCostQueryConditionVO.PRODUCT_COST_VALUE.equals(ishowtype)) {
                    sql.innerjoin(MarCostClassVO.getDefaultTableName());
                    sql.append(" ");
                    sql.append(MarCostClassVO.getDefaultTableName());
                    sql.on(MarCostClassVO.getDefaultTableName(), "pk_marcostclass", CostObjectVO.getDefaultTableName(), "cmarcostclassid");
                } else if (MeasCostQueryConditionVO.COSTOBJECT_VALUE.equals(ishowtype) || null == ishowtype && MeasCostQueryConditionVO.COSTOBJECT_VALUE.equals(ishowtypeTemp)) {
                    sql.append(" ");
                    sql.append("left join ");
                    sql.append("bd_measdoc");
                    sql.append(" ");
                    sql.append("bd_measdoc");
                    sql.on("bd_measdoc", "pk_measdoc", CostObjectVO.getDefaultTableName(), "pk_measdoc");
                }
            } else if (MeasCostQueryConditionVO.REVERT_AFTER_VALUE.equals(ireportsource)) {
                sql.append(CtRevertHeadVO.getDefaultTableName());
                sql.append(" ");
                sql.append("cm_cost_table");
                sql.innerjoin(CtRevertItemVO.getDefaultTableName());
                sql.append(" ");
                sql.append("cm_cost_table_b");
                sql.on("cm_cost_table", "pk_ctrevert", "cm_cost_table_b", "pk_ctrevert");
                if (MeasCostQueryConditionVO.PRODUCT_VALUE.equals(ishowtype)) {
                    sql.innerjoin(MaterialVersionVO.getDefaultTableName());
                    sql.append(" ");
                    sql.append(MaterialVersionVO.getDefaultTableName());
                    sql.on(MaterialVersionVO.getDefaultTableName(), "pk_source", "cm_cost_table", "cmaterialid");
                    sql.append(" ");
                    sql.append("left join ");
                    sql.append("bd_measdoc");
                    sql.append(" ");
                    sql.append("bd_measdoc");
                    sql.on("bd_measdoc", "pk_measdoc", MaterialVersionVO.getDefaultTableName(), "pk_measdoc");
                } else if (MeasCostQueryConditionVO.PRODUCT_COST_VALUE.equals(ishowtype)) {
                    sql.innerjoin(MarCostClassVO.getDefaultTableName());
                    sql.append(" ");
                    sql.append(MarCostClassVO.getDefaultTableName());
                    sql.on(MarCostClassVO.getDefaultTableName(), "pk_marcostclass", "cm_cost_table", "cmarcostclassid");
                }
            }

            String var10001 = AccbookVO.getDefaultTableName();
            sql.innerjoin(var10001 + " " + AccbookVO.getDefaultTableName());
            sql.on("cm_cost_table", "pk_accbook", AccbookVO.getDefaultTableName(), "pk_accbook");
            sql.append(" left join ");
            var10001 = ProfitCenterVO.getDefaultTableName();
            sql.append(var10001 + " " + ProfitCenterVO.getDefaultTableName());
            sql.on("cm_cost_table", "pk_profitcenter", ProfitCenterVO.getDefaultTableName(), "pk_profitcenter");
            sql.append(" left join ");
            var10001 = OrgVO.getDefaultTableName();
            sql.append(var10001 + " " + OrgVO.getDefaultTableName());
            sql.on("cm_cost_table", "pk_org", OrgVO.getDefaultTableName(), "pk_org");
            var10001 = CostCenterVO.getDefaultTableName();
            sql.innerjoin(var10001 + " " + CostCenterVO.getDefaultTableName());
            sql.on("cm_cost_table", "ccostcenterid", CostCenterVO.getDefaultTableName(), "pk_costcenter");
            sql.innerjoin(FactorAsoaVO.getDefaultTableName());
            sql.append(" ");
            sql.append(FactorAsoaVO.getDefaultTableName());
            sql.on("cm_cost_table_b", "celementid", FactorAsoaVO.getDefaultTableName(), "pk_factorasoa");

            // LEFT JOIN bd_project bd_project ON cm_costobject.cprojectid = bd_project.pk_project
            // 联查项目表 不是所有的物料都启用项目,所以用leftjoin
            // todo
            sql.append(" left join " + ProjectHeadVO.getDefaultTableName());
            sql.append(" ");
            sql.append(ProjectHeadVO.getDefaultTableName());
            sql.on(ProjectHeadVO.getDefaultTableName(), "pk_project", "cm_costobject", "cprojectid");

            return sql.toString();
        }
    }

    private String getVcostobjWhereSQL(MeasCostParamVO paramVO) {
        return paramVO.getWhereSql();
    }

    private String getVcostobjGroupSQL(Map<String, CMConditionVO> conditionMap, Integer ishowtype) {
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.append(this.getShowTypeGroupSQL(conditionMap, ishowtype));
        String[] showDetailDiff = MeasCostUtil.getShowDetailDiff(conditionMap);
        if (CMArrayUtil.isEquals(CMCommonConst1614.SUBTYPE_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("cm_cost_table_b.bsubtype");
        } else if (CMArrayUtil.isEquals(CMCommonConst1614.CENTERFROM_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("cm_cost_table_b.bfromcostcenter");
        }

        return sql.toString();
    }

    private String getShowTypeGroupSQL(Map<String, CMConditionVO> conditionMap, Integer ishowtype) {
        Integer ireportsource = Integer.valueOf(((CMConditionVO)conditionMap.get("ireportsource")).getSingleValue());
        Integer ireportcontent = -1;
        String content = null;
        if (conditionMap.get("ireportcontent") != null) {
            content = ((CMConditionVO)conditionMap.get("ireportcontent")).getSingleValue();
        }

        if (content != null && MeasCostQueryConditionVO.REVERT_BEFORE_VALUE.equals(ireportsource)) {
            ireportcontent = Integer.valueOf(content);
        }

        CMSqlBuilder sqlTemp = new CMSqlBuilder();
        if (!MeasCostQueryConditionVO.PRODCOST_VALUE.equals(ireportcontent) && !MeasCostQueryConditionVO.WASTER_VALUE.equals(ireportcontent)) {
            if (MeasCostQueryConditionVO.INPROCOST_VALUE.equals(ireportcontent)) {
                sqlTemp.append("cm_cost_table.cinprocostid");
            } else if (ireportcontent == -1) {
                sqlTemp.append("cm_cost_table.pk_ctrevert");
            }
        } else {
            sqlTemp.append("cm_cost_table.cprodcostid");
        }

        CMSqlBuilder sql = new CMSqlBuilder();
        if (MeasCostQueryConditionVO.COSTOBJECT_VALUE.equals(ishowtype) && MeasCostQueryConditionVO.REVERT_BEFORE_VALUE.equals(ireportsource)) {
            sql.groupBy(CostObjectVO.getDefaultTableName(), new String[]{"vcostobjcode", "vcostobjname", "vcostobjname" + MultiLangUtil.getCurrentLangSeqSuffix()});
        } else if (MeasCostQueryConditionVO.PRODUCT_VALUE.equals(ishowtype)) {
            sql.groupBy(MaterialVersionVO.getDefaultTableName(), new String[]{"code", "name", "name" + MultiLangUtil.getCurrentLangSeqSuffix()});
        } else if (MeasCostQueryConditionVO.PRODUCT_COST_VALUE.equals(ishowtype)) {
            sql.groupBy(MarCostClassVO.getDefaultTableName(), new String[]{"code", "name", "name" + MultiLangUtil.getCurrentLangSeqSuffix()});
        } else if (MeasCostQueryConditionVO.PRODUCT_BASIC_VALUE.equals(ishowtype)) {
            sql.groupBy(MarBasClassVO.getDefaultTableName(), new String[]{"code", "name", "name" + MultiLangUtil.getCurrentLangSeqSuffix()});
        }

        this.getBookAProfitcenterAOrgName(sql, UFBoolean.TRUE);
        String ccname = CMUtil.getFiledNameByMultiLang("ccname");
        sql.append(CostCenterVO.getDefaultTableName() + ".cccode");
        String var10001 = CostCenterVO.getDefaultTableName();
        sql.append(",isnull(" + var10001 + "." + ccname + "," + CostCenterVO.getDefaultTableName() + ".ccname)");
        sql.append(",");
        sql.append(sqlTemp);
        return sql.toString();
    }

    private String getFactorSelectSQL(Map<String, CMConditionVO> conditionMap, Integer ishowtype) {
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.append(this.getShowTypeSelectSQL(conditionMap, ishowtype));
        sql.append(",");
        String var10001 = FactorVO.getDefaultTableName();
        sql.append(var10001 + ".factorname" + MultiLangUtil.getCurrentLangSeqSuffix());
        sql.append(" ");
        sql.append("factorname");
        sql.append(",");
        sql.append(FactorVO.getDefaultTableName() + ".factorname");
        sql.append(" ");
        sql.append("factorname0");
        sql.append(",");
        sql.append(FactorVO.getDefaultTableName() + ".factorcode");
        sql.append(",");
        sql.append("max(");
        sql.append(FactorVO.getDefaultTableName());
        sql.append(".");
        sql.append("factorlev");
        sql.append(")");
        sql.append(" ");
        sql.append("factorlev");
        sql.append(",");
        sql.append("max(");
        sql.append(FactorAsoaVO.getDefaultTableName());
        sql.append(".");
        sql.append("pk_factorasoa");
        sql.append(")");
        sql.append(" ");
        sql.append("pk_factorasoa");
        sql.append(",");

        // todo 增加项目字段
        sql.append("max(");
        sql.append(ProjectHeadVO.getDefaultTableName());
        sql.append(".");
        sql.append("project_code");
        sql.append(")");
        sql.append(" ");
        sql.append("project_code");
        sql.append(",");
        sql.append("max(");
        sql.append(ProjectHeadVO.getDefaultTableName());
        sql.append(".");
        sql.append("project_name");
        sql.append(")");
        sql.append(" ");
        sql.append("project_name");
        sql.append(",");

        Integer ireportsource = Integer.valueOf(((CMConditionVO)conditionMap.get("ireportsource")).getSingleValue());
        if (MeasCostQueryConditionVO.REVERT_BEFORE_VALUE.equals(ireportsource)) {
            sql.sum("cm_cost_table_b", "nmoney", "factornsum");
        } else if (MeasCostQueryConditionVO.REVERT_AFTER_VALUE.equals(ireportsource)) {
            sql.sum("cm_cost_table_b", "aftercost", "factornsum");
        }

        String[] showDetailDiff = MeasCostUtil.getShowDetailDiff(conditionMap);
        if (CMArrayUtil.isEquals(CMCommonConst1614.STDCOST_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.sum("cm_cost_table_b", "nstdcost", "nstdcost_factor");
            sql.append(",");
            sql.sum("cm_cost_table_b", "nnumdiff", "nnumdiff_factor");
            sql.append(",");
            sql.sum("cm_cost_table_b", "npricediff", "npricediff_factor");
        } else if (CMArrayUtil.isEquals(CMCommonConst1614.SUBTYPE_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("cm_cost_table_b.bsubtype");
        } else if (CMArrayUtil.isEquals(CMCommonConst1614.CENTERFROM_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("cm_cost_table_b.bfromcostcenter");
        }

        return sql.toString();
    }

    private String getFactorSelectSQLAct(Map<String, CMConditionVO> conditionMap, Integer ishowtype) {
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.append(this.getShowTypeSelectSQL(conditionMap, ishowtype));
        sql.append(",");
        String var10001 = FactorAsoaVO.getDefaultTableName();
        sql.append(var10001 + "_activity.factorname" + MultiLangUtil.getCurrentLangSeqSuffix());
        sql.append(" ");
        sql.append("factorname");
        sql.append(",");
        sql.append(FactorAsoaVO.getDefaultTableName() + "_activity.factorname");
        sql.append(" ");
        sql.append("factorname0");
        sql.append(",");
        sql.append(FactorAsoaVO.getDefaultTableName() + "_activity.factorcode");
        sql.append(",");
        sql.append(" 0 ");
        sql.append(" ");
        sql.append("factorlev");
        sql.append(",");
        sql.append("max(");
        sql.append(FactorAsoaVO.getDefaultTableName() + "_activity");
        sql.append(".");
        sql.append("pk_factorasoa");
        sql.append(")");
        sql.append(" ");
        sql.append("pk_factorasoa");
        sql.append(",");
        Integer ireportsource = Integer.valueOf(((CMConditionVO)conditionMap.get("ireportsource")).getSingleValue());
        if (MeasCostQueryConditionVO.REVERT_BEFORE_VALUE.equals(ireportsource)) {
            sql.sum("cm_cost_table_b", "nmoney", "factornsum");
        } else if (MeasCostQueryConditionVO.REVERT_AFTER_VALUE.equals(ireportsource)) {
            sql.sum("cm_cost_table_b", "aftercost", "factornsum");
        }

        String[] showDetailDiff = MeasCostUtil.getShowDetailDiff(conditionMap);
        if (CMArrayUtil.isEquals(CMCommonConst1614.STDCOST_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.sum("cm_cost_table_b", "nstdcost", "nstdcost_factor");
            sql.append(",");
            sql.sum("cm_cost_table_b", "nnumdiff", "nnumdiff_factor");
            sql.append(",");
            sql.sum("cm_cost_table_b", "npricediff", "npricediff_factor");
        } else if (CMArrayUtil.isEquals(CMCommonConst1614.SUBTYPE_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("cm_cost_table_b.bsubtype");
        } else if (CMArrayUtil.isEquals(CMCommonConst1614.CENTERFROM_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("cm_cost_table_b.bfromcostcenter");
        }

        return sql.toString();
    }

    private String getFactorFromSQL(Map<String, CMConditionVO> conditionMap, Integer ishowtype) {
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.append(this.getVcostobjFromSQL(conditionMap, ishowtype));
        sql.append(MeasCostFactorSQLBuilder.getFactorCodeFromSQL());
        return sql.toString();
    }

    private String getFactorFromSQLAct(Map<String, CMConditionVO> conditionMap, Integer ishowtype) {
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.append(this.getVcostobjFromSQL(conditionMap, ishowtype));
        sql.innerjoin(BDActivityVO.getDefaultTableName());
        sql.append(" ");
        sql.append(BDActivityVO.getDefaultTableName());
        sql.on(BDActivityVO.getDefaultTableName(), "cactivityid", "cm_cost_table_b", "cactivityid");
        return sql.toString();
    }

    private String getFactorFromSQLNoAct(Map<String, CMConditionVO> conditionMap, Integer ishowtype) {
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.append(this.getVcostobjFromSQL(conditionMap, ishowtype));
        sql.and();
        sql.append("cm_cost_table_b.cactivityid");
        sql.append(" ='~' ");
        sql.append(MeasCostFactorSQLBuilder.getFactorCodeFromSQL());
        return sql.toString();
    }

    private String getFactorWhereSQL(MeasCostParamVO paramVO) {
        return paramVO.getWhereSql();
    }

    private String getFactorGroupSQL(Map<String, CMConditionVO> conditionMap, Integer ishowtype) {
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.append(this.getShowTypeGroupSQL(conditionMap, ishowtype));
        sql.append(",");
        sql.append(FactorVO.getDefaultTableName() + ".factorcode");
        sql.append(",");
        String var10001 = FactorVO.getDefaultTableName();
        sql.append(var10001 + ".factorname" + MultiLangUtil.getCurrentLangSeqSuffix());
        sql.append(",");
        sql.append(FactorVO.getDefaultTableName() + ".factorname");
        String[] showDetailDiff = MeasCostUtil.getShowDetailDiff(conditionMap);
        if (CMArrayUtil.isEquals(CMCommonConst1614.SUBTYPE_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("cm_cost_table_b.bsubtype");
        } else if (CMArrayUtil.isEquals(CMCommonConst1614.CENTERFROM_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("cm_cost_table_b.bfromcostcenter");
        }

        return sql.toString();
    }

    private String getFactorGroupSQLAct(Map<String, CMConditionVO> conditionMap, Integer ishowtype) {
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.append(this.getShowTypeGroupSQL(conditionMap, ishowtype));
        sql.append(",");
        sql.append(FactorAsoaVO.getDefaultTableName() + "_activity.factorcode");
        sql.append(",");
        sql.append(FactorAsoaVO.getDefaultTableName() + "_activity.factorname");
        sql.append(",");
        String var10001 = FactorAsoaVO.getDefaultTableName();
        sql.append(var10001 + "_activity.factorname" + MultiLangUtil.getCurrentLangSeqSuffix());
        String[] showDetailDiff = MeasCostUtil.getShowDetailDiff(conditionMap);
        if (CMArrayUtil.isEquals(CMCommonConst1614.SUBTYPE_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("cm_cost_table_b.bsubtype");
        } else if (CMArrayUtil.isEquals(CMCommonConst1614.CENTERFROM_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("cm_cost_table_b.bfromcostcenter");
        }

        return sql.toString();
    }

    private String getVcostobjSelectSQLForNNum(Map<String, CMConditionVO> conditionMap) {
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.append(" select ");
        sql.select("tt", new String[]{"vcostobjcode", "vcostobjname"});
        sql.append(",");
        sql.append("vcostobjname0");
        sql.append(" ");
        sql.append("vcostobjname0");
        sql.append(",tt.bookname");
        sql.append(",tt.profitcentername");
        sql.append(",tt.orgname");
        sql.append(",tt.cccode");
        sql.append(",tt.ccname");
        sql.append(",sum(tt.");
        sql.append("nnum");
        sql.append(") ");
        sql.append("nnum");
        sql.append(",sum(tt.");
        sql.append("nsum");
        sql.append(") ");
        sql.append("nsum");
        sql.append(",tt.");
        sql.append("measdocname");
        sql.append(",tt.");
        sql.append("bitnumber");
        sql.append(",tt.");
        sql.append("materialspec");
        sql.append(",tt.");
        sql.append("materialtype");
        String[] showDetailDiff = MeasCostUtil.getShowDetailDiff(conditionMap);
        if (CMArrayUtil.isEquals(CMCommonConst1614.STDCOST_ITEMS, showDetailDiff)) {
            sql.append(",sum(tt.");
            sql.append("nstdcost_nsum");
            sql.append(") ");
            sql.append("nstdcost_nsum");
            sql.append(",sum(tt.");
            sql.append("nnumdiff_nsum");
            sql.append(") ");
            sql.append("nnumdiff_nsum");
            sql.append(",sum(tt.");
            sql.append("npricediff_nsum");
            sql.append(") ");
            sql.append("npricediff_nsum");
        } else if (CMArrayUtil.isEquals(CMCommonConst1614.SUBTYPE_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("tt.bsubtype");
        } else if (CMArrayUtil.isEquals(CMCommonConst1614.CENTERFROM_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("tt.bfromcostcenter");
        }

        sql.append(" from ");
        return sql.toString();
    }

    private String getVcostobjGroupSQLForNNum(Map<String, CMConditionVO> conditionMap) {
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.groupBy("tt", new String[]{"vcostobjcode", "vcostobjname", "vcostobjname0", "cccode", "ccname", "measdocname", "bitnumber", "materialspec", "materialtype", "bookname", "profitcentername", "orgname"});
        String[] showDetailDiff = MeasCostUtil.getShowDetailDiff(conditionMap);
        if (CMArrayUtil.isEquals(CMCommonConst1614.SUBTYPE_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("tt.bsubtype");
        } else if (CMArrayUtil.isEquals(CMCommonConst1614.CENTERFROM_ITEMS, showDetailDiff)) {
            sql.append(",");
            sql.append("tt.bfromcostcenter");
        }

        return sql.toString();
    }

    public String getSQLMeasdocForNumDigits(MeasCostParamVO paramVO) {
        Map<String, CMConditionVO> conditionMap = paramVO.getConditionMap();
        Integer ireportsource = Integer.valueOf(((CMConditionVO)conditionMap.get("ireportsource")).getSingleValue());
        Integer ireportcontent = -1;
        String content = null;
        if (conditionMap.get("ireportcontent") != null) {
            content = ((CMConditionVO)conditionMap.get("ireportcontent")).getSingleValue();
        }

        if (content != null) {
            ireportcontent = Integer.valueOf(content);
        }

        if (MeasCostQueryConditionVO.REVERT_BEFORE_VALUE.equals(ireportsource)) {
            if (MeasCostQueryConditionVO.PRODCOST_VALUE.equals(ireportcontent) || MeasCostQueryConditionVO.WASTER_VALUE.equals(ireportcontent)) {
                return this.getSQLMeasdocByProdcost(paramVO);
            }

            if (MeasCostQueryConditionVO.INPROCOST_VALUE.equals(ireportcontent)) {
                return this.getSQLMeasdocByInProdcost(paramVO);
            }
        }

        return this.getSQLMeasdocByCtrevProdcost(paramVO);
    }

    private String getSQLMeasdocByProdcost(MeasCostParamVO paramVO) {
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.append(" select CM_COST_TABLE.cmeasdocid from ");
        sql.append(ProdcostHeadVO.getDefaultTableName());
        sql.append(" ");
        sql.append("cm_cost_table");
        sql.innerjoin(ProdcostItemVO.getDefaultTableName());
        sql.append(" ");
        sql.append("cm_cost_table_b");
        sql.on("cm_cost_table", "cprodcostid", "cm_cost_table_b", "cprodcostid");
        sql.innerjoin(FactorAsoaVO.getDefaultTableName());
        sql.on("cm_cost_table_b", "celementid", FactorAsoaVO.getDefaultTableName(), "pk_factorasoa");
        sql.append(" ");
        sql.innerjoin(CostObjectVO.getDefaultTableName());
        sql.on("cm_cost_table", "ccostobjectid", CostObjectVO.getDefaultTableName(), "ccostobjectid");
        sql.append(" ");
        sql.append(paramVO.getWhereSql());
        return sql.toString();
    }

    private String getSQLMeasdocByInProdcost(MeasCostParamVO paramVO) {
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.append(" select CM_COST_TABLE.cmeasdocid from ");
        sql.append(InproCostHeadVO.getDefaultTableName());
        sql.append(" ");
        sql.append("cm_cost_table");
        sql.innerjoin(InproCostItemVO.getDefaultTableName());
        sql.append(" ");
        sql.append("cm_cost_table_b");
        sql.on("cm_cost_table", "cinprocostid", "cm_cost_table_b", "cinprocostid");
        sql.innerjoin(FactorAsoaVO.getDefaultTableName());
        sql.on("cm_cost_table_b", "celementid", FactorAsoaVO.getDefaultTableName(), "pk_factorasoa");
        sql.append(" ");
        sql.innerjoin(CostObjectVO.getDefaultTableName());
        sql.on("cm_cost_table", "ccostobjectid", CostObjectVO.getDefaultTableName(), "ccostobjectid");
        sql.append(" ");
        sql.append(paramVO.getWhereSql());
        return sql.toString();
    }

    private String getSQLMeasdocByCtrevProdcost(MeasCostParamVO paramVO) {
        CMSqlBuilder sql = new CMSqlBuilder();
        sql.append(" select CM_COST_TABLE.cmeasureid from ");
        sql.append(CtRevertHeadVO.getDefaultTableName());
        sql.append(" ");
        sql.append("cm_cost_table");
        sql.innerjoin(CtRevertItemVO.getDefaultTableName());
        sql.append(" ");
        sql.append("cm_cost_table_b");
        sql.on("cm_cost_table", "pk_ctrevert", "cm_cost_table_b", "pk_ctrevert");
        sql.innerjoin(FactorAsoaVO.getDefaultTableName());
        sql.on("cm_cost_table_b", "celementid", FactorAsoaVO.getDefaultTableName(), "pk_factorasoa");
        sql.append(" ");
        sql.append(paramVO.getWhereSql());
        return sql.toString();
    }

    private String getVcostobjSelectSQLAddMeasdoc(Map<String, CMConditionVO> conditionMap, Integer ishowtype) {
        Integer ireportsource = Integer.valueOf(((CMConditionVO)conditionMap.get("ireportsource")).getSingleValue());
        CMSqlBuilder sql = new CMSqlBuilder();
        String measdocName = CMUtil.getFiledNameByMultiLang("name");
        if (MeasCostQueryConditionVO.REVERT_BEFORE_VALUE.equals(ireportsource)) {
            if (MeasCostQueryConditionVO.PRODUCT_VALUE.equals(ishowtype)) {
                sql.append(",");
                sql.append(MaterialVersionVO.getDefaultTableName() + ".materialspec");
                sql.append(" ");
                sql.append("materialspec");
                sql.append(",");
                sql.append(MaterialVersionVO.getDefaultTableName() + ".materialtype");
                sql.append(" ");
                sql.append("materialtype");
                String var10001 = MeasdocVO.getDefaultTableName();
                sql.append(",isnull(" + var10001 + "." + measdocName + "," + MeasdocVO.getDefaultTableName() + ".name) measdocname");
                sql.append("," + MeasdocVO.getDefaultTableName() + ".bitnumber  bitnumber");
            } else if (MeasCostQueryConditionVO.COSTOBJECT_VALUE.equals(ishowtype)) {
                sql.append(",");
                sql.append("cm_costobject.materialspec");
                sql.append(" ");
                sql.append("materialspec");
                sql.append(",");
                sql.append("cm_costobject.materialtype");
                sql.append(" ");
                sql.append("materialtype");
                String var6 = MeasdocVO.getDefaultTableName();
                sql.append(",isnull(" + var6 + "." + measdocName + "," + MeasdocVO.getDefaultTableName() + ".name) measdocname");
                sql.append("," + MeasdocVO.getDefaultTableName() + ".bitnumber  bitnumber");
            } else {
                sql.append(",'' ");
                sql.append("materialspec");
                sql.append(",'' ");
                sql.append("materialtype");
                sql.append(",'' ");
                sql.append("measdocname");
                sql.append(",'' ");
                sql.append("bitnumber");
            }
        } else if (MeasCostQueryConditionVO.REVERT_AFTER_VALUE.equals(ireportsource)) {
            if (MeasCostQueryConditionVO.PRODUCT_VALUE.equals(ishowtype)) {
                sql.append(",");
                sql.append(MaterialVersionVO.getDefaultTableName() + ".materialspec");
                sql.append(" ");
                sql.append("materialspec");
                sql.append(",");
                sql.append(MaterialVersionVO.getDefaultTableName() + ".materialtype");
                sql.append(" ");
                sql.append("materialtype");
                String var7 = MeasdocVO.getDefaultTableName();
                sql.append(",isnull(" + var7 + "." + measdocName + "," + MeasdocVO.getDefaultTableName() + ".name) measdocname");
                sql.append("," + MeasdocVO.getDefaultTableName() + ".bitnumber  bitnumber");
            } else {
                sql.append(",'' ");
                sql.append("materialspec");
                sql.append(",'' ");
                sql.append("materialtype");
                sql.append(",'' ");
                sql.append("measdocname");
                sql.append(",'' ");
                sql.append("bitnumber");
            }
        }

        return sql.toString();
    }

    private String getVcostobjGroupSQLAddMeasdoc(Integer ishowtype, Integer ireportsource) {
        CMSqlBuilder sql = new CMSqlBuilder();
        String measdocName = CMUtil.getFiledNameByMultiLang("name");
        if (MeasCostQueryConditionVO.COSTOBJECT_VALUE.equals(ishowtype) && MeasCostQueryConditionVO.REVERT_BEFORE_VALUE.equals(ireportsource)) {
            sql.append(",");
            sql.append("cm_costobject.materialspec");
            sql.append(",");
            sql.append("cm_costobject.materialtype");
            String var5 = MeasdocVO.getDefaultTableName();
            sql.append(",isnull(" + var5 + "." + measdocName + "," + MeasdocVO.getDefaultTableName() + ".name) ");
            sql.append("," + MeasdocVO.getDefaultTableName() + ".bitnumber");
        } else if (MeasCostQueryConditionVO.PRODUCT_VALUE.equals(ishowtype)) {
            sql.append(",");
            sql.append(MaterialVersionVO.getDefaultTableName() + ".materialspec");
            sql.append(",");
            sql.append(MaterialVersionVO.getDefaultTableName() + ".materialtype");
            String var10001 = MeasdocVO.getDefaultTableName();
            sql.append(",isnull(" + var10001 + "." + measdocName + "," + MeasdocVO.getDefaultTableName() + ".name) ");
            sql.append("," + MeasdocVO.getDefaultTableName() + ".bitnumber");
        }

        return sql.toString();
    }
}
