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

package nc.bs.cm.meascostcontrast.process;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nc.bd.framework.base.CMNumberUtil2;
import nc.bs.cm.meascostcontrast.strategy.IMeasCostStrategy;
import nc.cmpub.framework.util.CMValidateUtil;
import nc.vo.cm.meascostcontrast.MeasCostItemVO;
import nc.vo.cm.meascostcontrast.MeasCostQueryConditionVO;
import nc.vo.cm.meascostcontrast.MeasCostQueryResultVO;
import nc.vo.pub.lang.UFDouble;
import org.apache.commons.lang3.StringUtils;

public class MeasCostSubtype implements IMeasCostStrategy {
    public MeasCostSubtype() {
    }

    public void saveItem(List<MeasCostQueryResultVO> factorList, MeasCostItemVO itemVO) {
        if (!CMValidateUtil.isEmpty(factorList)) {
            MeasCostQueryResultVO resultVO = (MeasCostQueryResultVO)factorList.get(0);
            UFDouble nnumTemp = UFDouble.ZERO_DBL;
            UFDouble nsumTemp = UFDouble.ZERO_DBL;
            UFDouble nmeascostN = UFDouble.ZERO_DBL;
            UFDouble nmeascostY = UFDouble.ZERO_DBL;
            UFDouble nsumN = UFDouble.ZERO_DBL;
            UFDouble nsumY = UFDouble.ZERO_DBL;
            if (StringUtils.equals(String.valueOf(MeasCostQueryConditionVO.NO_COSTCENTER_VALUE), ((MeasCostQueryResultVO)factorList.get(0)).getIsSumCccode())) {
                Map<String, UFDouble> cccodeMap = new HashMap();

                for(MeasCostQueryResultVO measCostQueryResultVO : factorList) {
                    if (!cccodeMap.containsKey(measCostQueryResultVO.getCccode())) {
                        nnumTemp = nnumTemp.add(measCostQueryResultVO.getNnum());
                        nsumTemp = nsumTemp.add(measCostQueryResultVO.getNsum());
                        cccodeMap.put(measCostQueryResultVO.getCccode(), UFDouble.ZERO_DBL);
                    }

                    measCostQueryResultVO.setCcname("");
                    measCostQueryResultVO.setCccode("");
                }

                resultVO.setNnum(nnumTemp);
                resultVO.setNsum(nsumTemp);
                nsumN = resultVO.getNsum();
                nmeascostN = CMNumberUtil2.div(nsumN, new UFDouble[]{resultVO.getNnum()});
            } else {
                for(MeasCostQueryResultVO resultVO1 : factorList) {
                    String bsubtype = resultVO1.getBsubtype();
                    if ("N".equals(bsubtype)) {
                        nsumN = resultVO1.getNsum();
                        nmeascostN = CMNumberUtil2.div(nsumN, new UFDouble[]{resultVO.getNnum()});
                    } else if ("Y".equals(bsubtype)) {
                        nsumY = resultVO1.getNsum();
                        nmeascostY = CMNumberUtil2.div(nsumY, new UFDouble[]{resultVO.getNnum()});
                    }
                }
            }

            itemVO.setBookname(resultVO.getBookname());
            itemVO.setProfitcentername(resultVO.getProfitcentername());
            itemVO.setOrgname(resultVO.getOrgname());
            itemVO.setCccode(resultVO.getCccode());
            itemVO.setCcname(resultVO.getCcname());
            itemVO.setVcostobjcode(resultVO.getVcostobjcode());
            itemVO.setVcostobjname(resultVO.getVcostobjname());
            itemVO.setMaterialspec(resultVO.getMaterialspec());
            itemVO.setMaterialtype(resultVO.getMaterialtype());
            itemVO.setMeasdocname(resultVO.getMeasdocname());
            itemVO.setBitnumber(resultVO.getBitnumber());
            itemVO.setNnum(resultVO.getNnum());
            itemVO.setProjectcode(resultVO.getProjectcode());
            itemVO.setProjectname(resultVO.getProjectname());
            itemVO.setAttrbuteByRule("nmeascost", "ncost", CMNumberUtil2.add(new UFDouble[]{nmeascostN, nmeascostY}));
            itemVO.setAttrbuteByRule("nmeascost", "thistype", nmeascostN);
            itemVO.setAttrbuteByRule("nmeascost", "subtype", nmeascostY);
            itemVO.setAttrbuteByRule("nsum", "ncost", CMNumberUtil2.add(new UFDouble[]{nsumN, nsumY}));
            itemVO.setAttrbuteByRule("nsum", "thistype", nsumN);
            itemVO.setAttrbuteByRule("nsum", "subtype", nsumY);
        }
    }

    public void saveFactor(List<MeasCostQueryResultVO> factorList, MeasCostItemVO itemVO) {
        if (!CMValidateUtil.isEmpty(factorList)) {
            String factorcode = ((MeasCostQueryResultVO)factorList.get(0)).getFactorcode();
            UFDouble factornsumN = UFDouble.ZERO_DBL;
            UFDouble factornsumY = UFDouble.ZERO_DBL;

            for(MeasCostQueryResultVO resultVO : factorList) {
                String bsubtype = resultVO.getBsubtype();
                if ("N".equals(bsubtype)) {
                    factornsumN = CMNumberUtil2.add(new UFDouble[]{factornsumN, resultVO.getFactorNsum()});
                } else if ("Y".equals(bsubtype)) {
                    factornsumY = CMNumberUtil2.add(new UFDouble[]{factornsumY, resultVO.getFactorNsum()});
                }
            }

            itemVO.setAttrbuteByRule(factorcode, "ncost", CMNumberUtil2.add(new UFDouble[]{factornsumN, factornsumY}));
            itemVO.setAttrbuteByRule(factorcode, "thistype", factornsumN);
            itemVO.setAttrbuteByRule(factorcode, "subtype", factornsumY);
        }
    }
}
