package nccloud.web.ic.outbound.sapplybill.action;

import nc.pub.templet.converter.util.helper.ExceptionUtils;
import nc.pubitf.pu.m422x.ic.m4455.IQuery422XFor4455;
import nc.ui.querytemplate.querytree.IQueryScheme;
import nc.vo.ic.pub.util.StringUtil;
import nc.vo.ic.pub.util.ValueCheckUtil;
import nc.vo.mmpac.pickm.entity.AggPickmVO;
import nc.vo.pub.BusinessException;
import nc.vo.scmpub.res.billtype.ICBillType;
import nc.vo.scmpub.util.BillBodySortUtils;
import nccloud.dto.scmpub.page.entity.SCMQueryTreeFormatVO;
import nccloud.framework.core.json.IJson;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.ClientInfo;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.container.SessionContext;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.ui.pattern.billgrid.BillGrid;
import nccloud.framework.web.ui.pattern.billgrid.BillGridOperator;
import nccloud.nc.vo.pub.pf.PullBillInfoVO;
import nccloud.pubitf.ic.pub.service.IICPubQueryService;
import nccloud.pubitf.platform.query.INCCloudQueryService;
import nccloud.pubitf.riart.businesstype.IPFBusinessTypeService;

import java.util.List;

/**
 * 查询备料计划的数据-出库申请单拉单使用
 * 2005适配2312
 *
 * @author mzr
 * @date 2024/12/21
 */
public class QueryPickmFor4455Action implements ICommonAction {
    public Object doAction(IRequest request) {
        String read = request.read();
        IJson json = JsonFactory.create();
        SCMQueryTreeFormatVO info = (SCMQueryTreeFormatVO) json.fromJson(read, SCMQueryTreeFormatVO.class);

        INCCloudQueryService queryutil = (INCCloudQueryService) ServiceLocator.find(INCCloudQueryService.class);
        IQueryScheme scheme = queryutil.convertCondition(info.getQueryInfo());

        /*Object transtype = info.getQueryInfo().getUserdefObj().get("transtype");
        PullBillInfoVO[] refbillinfos = queryRefPara(transtype);
        appendRefWhr(scheme, refbillinfos);*/

        IICPubQueryService<?, ?> schemeservice = (IICPubQueryService) ServiceLocator.find(IICPubQueryService.class);

        scheme = schemeservice.appendOrgPermission(scheme);

        AggPickmVO[] vos = null;

        IQuery422XFor4455 queryService = (IQuery422XFor4455) ServiceLocator.find(IQuery422XFor4455.class);
        try {
            vos = queryService.queryPickmAppsFor4455(scheme);
        } catch (BusinessException e) {
            ExceptionUtils.wrapException(e);
        }
        if (null == vos) {
            return null;
        }
        BillBodySortUtils.sort(vos);
        // VO与UI数据格式转换 卡片列表：BillGridOperator
        BillGridOperator operator = new BillGridOperator(info.getTempletid(), info.getPageCode());
        BillGrid[] grids = operator.toBillGrids(vos);
        /*if (grids != null) {
            StoreReqScaleUtil util = new StoreReqScaleUtil();
            for (BillGrid billGrid : grids) {
                util.processGrid(billGrid, false);
            }
        }*/
        return grids;
    }

    private void appendRefWhr(IQueryScheme scheme, PullBillInfoVO[] refbillinfos) {
        if (null == refbillinfos || refbillinfos.length == 0) {
            return;
        }

        for (PullBillInfoVO info : refbillinfos) {
            if ("422X".equals(info.getSrc_billtype())) {
                List<String> busitypes = info.getBusitypes();
                if (!ValueCheckUtil.isNullORZeroLength(busitypes)) {
                    scheme.put("busitypearray", busitypes.toArray(new String[0]));
                    List<String> tranList = info.getTranstypes();
                    if (!ValueCheckUtil.isNullORZeroLength(tranList)
                            && !StringUtil.isSEmptyOrNull((String) tranList.get(0))) {
                        scheme.put("transtypearray", tranList.toArray(new String[0]));
                    }
                }
            }
        }
    }

    private PullBillInfoVO[] queryRefPara(Object transtype) {
        ClientInfo clientinfo = SessionContext.getInstance().getClientInfo();
        String groupId = clientinfo.getPk_group();
        String userId = clientinfo.getUserid();

        IPFBusinessTypeService pfservice = (IPFBusinessTypeService) ServiceLocator.find(IPFBusinessTypeService.class);
        PullBillInfoVO[] refbillinfos = null;
        try {
            refbillinfos = pfservice.getPullBillInfo(ICBillType.SapplyBill.getCode(),
                    (null == transtype) ? null : (String) transtype, groupId, userId);
        } catch (BusinessException e) {
            ExceptionUtils.wrapException(e);
        }
        return refbillinfos;
    }
}