package nccloud.web.mmpac.pickm.query;

import nc.itf.mmpac.pickm.IPickmQueryService;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.mmpac.pickm.consts.PickmLangConsts;
import nc.vo.mmpac.pickm.entity.PickmItemVO;
import nccloud.dto.mmpac.pickm.pub.entity.PickmQueryInfoDTO;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.core.json.IJson;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.ui.pattern.grid.Grid;
import nccloud.framework.web.ui.pattern.grid.GridOperator;
import nccloud.web.mmpac.pickm.util.PickmScaleUtil;
import nccloud.web.mmpub.pub.action.NCCTempletQueryAction;

/**
 * 备料计划-借料入库弹窗查询
 *
 * @author mzr
 * @date 2025/7/8
 */
public class PickmItemsQuery implements ICommonAction {
    public Object doAction(IRequest request) {
        String read = request.read();
        IJson json = JsonFactory.create();
        PickmQueryInfoDTO queryInfo = json.fromJson(read, PickmQueryInfoDTO.class);
        try {
            String[] cpickmbids = queryInfo.getCpickmbids();
            if (null != cpickmbids && cpickmbids.length != 0) {
                IPickmQueryService service = ServiceLocator.find(IPickmQueryService.class);
                PickmItemVO[] itemVOS = service.queryItemsByIds(cpickmbids);
                if (MMValueCheck.isEmpty(itemVOS)) {
                    ExceptionUtils.wrapBusinessException(PickmLangConsts.getHIT_BODYNOTNULL());
                }

                GridOperator operator = new GridOperator(queryInfo.getPageid());
                if (MMValueCheck.isNotEmpty(queryInfo.getAppcode())) {
                    String templetid = NCCTempletQueryAction.getTempletIdByAppCode(queryInfo.getAppcode(), queryInfo.getPageid());
                    operator = new GridOperator(templetid, queryInfo.getPageid());
                }

                Grid grid = operator.toGrid(itemVOS);
                // Translator translator = new Translator();
                // translator.translate(grid);
                // this.afterProcess(grid);
                return grid;
            } else {
                return null;
            }
        } catch (Exception ex) {
            ExceptionUtils.wrapBusinessException(ex.getMessage());
            return null;
        }
    }

    public void afterProcess(Grid grid) {
        PickmScaleUtil scaleUtil = new PickmScaleUtil();
        scaleUtil.processHeadBodyGrid(grid);
    }
}
