
package nccloud.web.mmpac.pmo.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nc.itf.mmpac.pmo.pac0002.IPMOQueryService;
import nc.util.mmf.framework.base.MMArrayUtil;
import nc.util.mmf.framework.base.MMCollectionUtil;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.mmpac.pmo.pac0002.entity.PMOAggVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOItemVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nccloud.framework.core.util.GridCompareUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.ui.pattern.billcard.BillCard;
import nccloud.framework.web.ui.pattern.billcard.BillCardOperator;
import nccloud.mmpac.pmo.pub.entity.CardInfo;
import nccloud.mmpac.pmo.pub.entity.PmoConst;
import nccloud.mmpac.pmo.pub.entity.SingleInfo;
import nccloud.web.mmpac.pmo.constant.NCCPMOConstLang;
import nccloud.web.mmpac.pmo.util.PmoScaleUtil;
import nccloud.web.mmpac.pmo.util.PmoUtil;
import nccloud.web.mmpub.pub.resexp.PfResumeExceptionNccUtils;

public abstract class PmoCardOperAction extends PMOCommonAction {
    public PmoCardOperAction() {
    }

    public Object doAction(IRequest request) {
        BillCard card = null;
        BillCard originBillcard = null;
        BillCard returnCard = null;
        PfResumeExceptionNccUtils.before(request);
        BillCardOperator operator = new BillCardOperator(PmoConst.PAGECODE);
        PMOAggVO[] vos = null;
        PMOAggVO[] returnvos = null;

        try {
            CardInfo cardInfo = (CardInfo) JsonFactory.create().fromJson(request.read(), CardInfo.class);
            String cpmhoid = cardInfo.getId();
            PMOAggVO vo = null;

            try {
                vo = ((IPMOQueryService) ServiceLocator.find(IPMOQueryService.class)).queryByPk(cpmhoid);
            } catch (BusinessException e) {
                ExceptionUtils.wrappException(e);
            }

            if (MMValueCheck.isEmpty(vo)) {
                ExceptionUtils.wrappBusinessException(NCCPMOConstLang.getHint4Delete());
            }

            vo.getParentVO().setFatpcheck(cardInfo.getFatpcheck());
            List<SingleInfo> bodys = cardInfo.getBodyInfo();
            if (MMCollectionUtil.isNotEmpty(bodys)) {
                Map<String, SingleInfo> cacheMap = new HashMap();
                List<PMOItemVO> itemList = new ArrayList();

                for (SingleInfo body : bodys) {
                    cacheMap.put(body.getId(), body);
                }

                PMOItemVO[] items = vo.getChildrenVO();
                if (MMArrayUtil.isNotEmpty(items)) {
                    for (PMOItemVO item : items) {
                        if (cacheMap.containsKey(item.getPrimaryKey())) {
                            item.setTs(((SingleInfo) cacheMap.get(item.getPrimaryKey())).getTs());
                            item.setAttributeValue("rowid", ((SingleInfo) cacheMap.get(item.getPrimaryKey())).getRowid());
                            itemList.add(item);
                        }
                    }

                    vo.setChildrenVO((CircularlyAccessibleValueObject[]) itemList.toArray(new PMOItemVO[itemList.size()]));
                }
            }

            vo.getParentVO().setTs(cardInfo.getTs());
            (new PmoUtil()).processhslScale(vo);
            originBillcard = operator.toCard(vo);
            operator.setOriginBillcard(originBillcard);
            vo.getParent().setStatus(1);
            vos = new PMOAggVO[]{vo};
            this.checkPermission(vos);
            (new PmoUtil()).clearPseudocolumn(vos);
            returnvos = this.process(vos);
            vos = (PMOAggVO[]) this.combine(vos, returnvos);
            card = operator.toCard(vos[0]);
            this.afterProcess(card);
            returnCard = GridCompareUtils.compareBillCardGrid(operator.getOriginBillcard(), card);
            return returnCard;
        } catch (Exception e) {
            return PfResumeExceptionNccUtils.handleResumeException(e);
        }
    }

    protected abstract PMOAggVO[] process(PMOAggVO[] var1) throws BusinessException;

    public void afterProcess(BillCard card) {
        PmoScaleUtil util = new PmoScaleUtil();
        util.processBillCard(card);
    }
}
