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

import nc.itf.scmpub.reference.uap.bd.currency.CurrencyInfo;
import nc.vo.ic.general.define.ICBillVO;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.scmpub.res.billtype.ICBillType;
import nc.vo.scmpub.util.BillBodySortUtils;
import nccloud.commons.lang.StringUtils;
import nccloud.dto.ic.pub.entity.BillVOWithExtendInfo;
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.model.row.Cell;
import nccloud.framework.web.ui.pattern.billcard.BillCard;
import nccloud.pubitf.ic.pub.service.IICBillVOQueryService;
import nccloud.web.ic.pub.precision.ICBaseBillcardPrecisionHandler;
import nccloud.web.ic.pub.utils.DealDisplayIsNullUtil;
import nccloud.web.ic.ui.pattern.billcard.ICBillCardOperator;

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

/**
 * @Classname QueryMaterialOutCardInfoAction
 * @Description TODO
 * @Version 1.0.0
 * @Date 2025/6/25 16:48
 * @Created by ame
 */
public class QueryMaterialOutCardInfoAction implements ICommonAction {
    @Override
    public Object doAction(IRequest request) {
        String read = request.read();
        IJson json = JsonFactory.create();
        Map<String,Object> info = json.fromJson(read, Map.class);
        List<String> pks = (List<String>) info.get("pks");
        String pageid = (String) info.get("pageid");
        String pagecodechi = (String) info.get("pagecodechi");
        Map<String,Object> retmap = new HashMap();
        BillVOWithExtendInfo[] billVOWithExtendInfos = ((IICBillVOQueryService) ServiceLocator.find(IICBillVOQueryService.class)).queryBillDev(ICBillType.MaterialOut, pks.toArray(new String[0]));
        if(billVOWithExtendInfos!=null && billVOWithExtendInfos.length>0){
            List<BillCard> billCardList=new ArrayList<>();
            for(BillVOWithExtendInfo extendInfo : billVOWithExtendInfos){
                BillBodySortUtils.sort(new ICBillVO[]{extendInfo.getBillvo()});
                BillCard retcard = this.afterProcess(pagecodechi, pageid, extendInfo.getBillvo());
                billCardList.add(retcard);
//                retmap.put("isLocationManaged", extendInfo.isLocationManaged());
//                retmap.put("materialSerialMap", extendInfo.getMaterialSerialMap());
//                retmap.put("materialSnunitMap", extendInfo.getMaterialSnunitMap());
            }

            retmap.put("billcard", billCardList);
            return retmap;
        }else{
            throw new RuntimeException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4008027_0", "04008027-0180"));
        }
    }
    public BillCard afterProcess(String pagecodechi, String pageid, ICBillVO vo) {
        ICBillCardOperator operator = new ICBillCardOperator((String)null, pageid, pagecodechi);
        BillCard retcard = operator.toCard(vo);
        String ccurrencyid = (String)vo.getBody(0).getAttributeValue("ccurrencyid");
        if (!StringUtils.isEmpty(ccurrencyid)) {
            String name = CurrencyInfo.getName(ccurrencyid);
            retcard.getBody().getModel().getListRows().forEach((row) -> {
                Cell cell = new Cell();
                cell.setValue(ccurrencyid);
                cell.setDisplay(name);
                row.addCell("ccurrencyid", cell);
            });
        }

        ICBaseBillcardPrecisionHandler handler = this.getPrecisionHandler(retcard);
        handler.process();
        DealDisplayIsNullUtil.dealBillCardDisPlayIsNull(new String[]{"ccorrespondcode", "vexigencycode"}, retcard);
        return retcard;
    }
    protected ICBaseBillcardPrecisionHandler getPrecisionHandler(BillCard card) {
        return new ICBaseBillcardPrecisionHandler(card);
    }
}
