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

package nccloud.web.mmpac.pickm.action;

import java.util.HashMap;
import java.util.Map;
import nc.itf.mmpac.pickm.IPickmQueryService;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.mmpac.pickm.entity.AggPickmVO;
import nc.vo.mmpac.pickm.entity.PickmHeadVO;
import nc.vo.mmpac.pickm.entity.PickmItemVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pubapp.pattern.model.entity.bill.AbstractBill;
import nccloud.dto.mmpac.pickm.pub.entity.PickmDeleteDTO;
import nccloud.dto.mmpac.pickm.pub.entity.PickmDeleteInfo;
import nccloud.dto.mmpub.script.entity.MMScriptResultDTO;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.core.json.IJson;
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.framework.web.ui.pattern.grid.Grid;
import nccloud.pubitf.mmpub.commit.service.IBatchRunScriptService;
import nccloud.pubitf.riart.pflow.CloudPFlowContext;
import nccloud.web.mmpac.pickm.util.PickmScaleUtil;
import nccloud.web.mmpub.pub.action.DataPermissionAction;
import nccloud.web.mmpub.pub.assign.CommitAssignUtils;
import nccloud.web.mmpub.pub.busi.MMATOMaterialCheckUtil;
import nccloud.web.mmpub.pub.resexp.PfResumeExceptionNccUtils;
import nccloud.web.mmpub.pub.utils.MMBatchOperatorResult;

public class PickmCommitAction extends DataPermissionAction {
    private String appcode = null;

    public PickmCommitAction() {
    }

    public Object doAction(IRequest request) {
        String read = request.read();
        IJson json = JsonFactory.create();

        try {
            CommitAssignUtils.before(request);
            PickmDeleteDTO infoDTO = (PickmDeleteDTO)json.fromJson(read, PickmDeleteDTO.class);
            PickmDeleteInfo[] info = infoDTO.getDeleteInfo();
            this.appcode = infoDTO.getAppcode();
            BillCardOperator operator = new BillCardOperator(infoDTO.getPageCode());
            AggPickmVO[] aggVOs = this.getAggPickmVOs(info);
            if (null == aggVOs || aggVOs.length == 0) {
                ExceptionUtils.wrapBusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("5008100_0", "05008100-0000"));
            }

            this.checkPermission(aggVOs);
            (new MMATOMaterialCheckUtil("5099002_0", "05099002-0450")).CheckMaterialIsATO(aggVOs, "cmaterialvid", "cffileid", (String)null, (String)null);
            BillCard orginCard = operator.toCard(aggVOs[0]);
            PickmScaleUtil stoUtil = new PickmScaleUtil();
            stoUtil.processBillCard(orginCard);
            operator.setOriginBillcard(orginCard);
            CloudPFlowContext context = new CloudPFlowContext();
            context.setBillType("55A3");
            context.setActionName("SAVE");
            context.setBillVos(aggVOs);
            MMScriptResultDTO obj = ((IBatchRunScriptService)ServiceLocator.find(IBatchRunScriptService.class)).runBacth(context, AggPickmVO.class);
            Object returndata = obj.getData();
            if (returndata != null && returndata instanceof Map) {
                return returndata;
            } else {
                boolean iscard = infoDTO.isCard();
                if (iscard) {
                    this.processResult(obj, infoDTO.getPageCode());
                    return obj;
                } else {
                    MMBatchOperatorResult resultInto = new MMBatchOperatorResult(obj, infoDTO.getPageCode());
                    Grid grid = resultInto.getSucessVOs();
                    if (MMValueCheck.isNotEmpty(grid)) {
                        this.afterProcess(grid);
                    }

                    return resultInto;
                }
            }
        } catch (Exception e) {
            return PfResumeExceptionNccUtils.handleResumeException(e);
        }
    }

    private void afterProcess(Grid grid) {
        PickmScaleUtil scaleUtil = new PickmScaleUtil();
        scaleUtil.processGrid(grid);
    }

    private AggPickmVO[] getAggPickmVOs(PickmDeleteInfo[] deleteinfo) throws BusinessException {
        String[] pks = new String[deleteinfo.length];
        Map<String, UFDateTime> map = new HashMap();
        Map<String, UFDateTime> bodymap = new HashMap();

        for(int i = 0; i < deleteinfo.length; ++i) {
            String pk = deleteinfo[i].getPks();
            pks[i] = pk;
            map.put(pk, deleteinfo[i].getTs());
            if (MMValueCheck.isNotEmpty(deleteinfo[i].getBodys())) {
                for(PickmDeleteInfo bodyInfo : deleteinfo[i].getBodys()) {
                    bodymap.put(bodyInfo.getPks(), bodyInfo.getTs());
                }
            }
        }

        IPickmQueryService query = (IPickmQueryService)ServiceLocator.find(IPickmQueryService.class);
        AggPickmVO[] aggVOs = query.queryBillsByPks(pks);
        if (aggVOs != null && aggVOs.length > 0) {
            for(int j = 0; j < aggVOs.length; ++j) {
                String key = aggVOs[j].getParentVO().getCpickmid();
                aggVOs[j].getParentVO().setAttributeValue("ts", map.get(key));
                if (MMValueCheck.isNotEmpty(aggVOs[j].getChildrenVO())) {
                    for(PickmItemVO body : (PickmItemVO[])aggVOs[j].getChildrenVO()) {
                        body.setStatus(1);
                        if (MMValueCheck.isNotEmpty(bodymap) && bodymap.containsKey(body.getCpickm_bid())) {
                            body.setTs((UFDateTime)bodymap.get(body.getCpickm_bid()));
                        }
                    }
                }
            }
        }

        return aggVOs;
    }

    public void processResult(MMScriptResultDTO dto, String pagecode) {
        AbstractBill[] sucessVOs = dto.getSucessVOs();
        if (sucessVOs != null && sucessVOs.length != 0) {
            dto.setData(this.processCardBill(sucessVOs, pagecode));
            dto.setSucessVOs((AbstractBill[])null);
        }
    }

    private Object processCardBill(AbstractBill[] bills, String pagecode) {
        PickmHeadVO headerVO = (PickmHeadVO)bills[0].getParent();
        String pk = headerVO.getCpickmid();
        IPickmQueryService query = (IPickmQueryService)ServiceLocator.find(IPickmQueryService.class);

        try {
            AggPickmVO[] rvo = query.queryBillsByPks(new String[]{pk});
            BillCardOperator operator = new BillCardOperator(pagecode);
            BillCard billCard = operator.toCard(rvo[0]);
            this.afterProcess(billCard);
            return billCard;
        } catch (Exception e) {
            ExceptionUtils.wrapException(e);
            return null;
        }
    }

    private void afterProcess(BillCard card) {
        PickmScaleUtil scalUtil = new PickmScaleUtil();
        scalUtil.processBillCard(card);
    }

    public String getPermissioncode() {
        return MMValueCheck.isNotEmpty(this.appcode) && this.appcode.startsWith("5009") ? "55C3" : "55A3";
    }

    public String getActioncode() {
        return MMValueCheck.isNotEmpty(this.appcode) && this.appcode.startsWith("5009") ? "DPickmAuditBP" : "PickmAuditBP";
    }

    public String getBillCodeField() {
        return "vbillcode";
    }
}
