package nccloud.web.mmpac.pickm.query;

import nc.bs.trade.business.HYPubBO;
import nc.itf.mmpac.pickm.IPickmQueryService;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.mmpac.pickm.entity.AggPickmVO;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDouble;
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.model.GridModel;
import nccloud.framework.web.ui.model.row.Cell;
import nccloud.framework.web.ui.model.row.Row;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 备料计划-借料入库弹窗-列表查询
 *
 * @author mzr
 * @date 2025/7/8
 */
public class PickmByIdsQueryAction implements ICommonAction {
    public Object doAction(IRequest request) {
        String read = request.read();
        IJson json = JsonFactory.create();
        PickmQueryInfoDTO query = json.fromJson(read, PickmQueryInfoDTO.class);
        try {
            String pageid ="50080102_card";
            String[] pks = query.getPks();
            IPickmQueryService service = ServiceLocator.find(IPickmQueryService.class);
            AggPickmVO[] vos = service.queryBillsByPks(pks);
            if (null == vos || vos.length == 0) {
                ExceptionUtils.wrapBusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("5008100_0", "05008100-0005"));
            }
            List<Row> list=new ArrayList<>();
            List<Grid> bodyList = new ArrayList<>();
            for (AggPickmVO vo : vos) {
                if(vo.getParentVO().getFbillstatus()!=-1){
                    BillCardOperator operator = new BillCardOperator(pageid);
                    BillCard billCard = operator.toCard(vo);
                    if(billCard.getBody()==null){
                        continue;
                    }
                    List<Row> rows = List.of(billCard.getBody().getModel().getRows());
                    for (Row row:rows){
                        UFDouble nplanoutastnum = (UFDouble) row.getValues().get("nplanoutastnum").getValue();
                        UFDouble naccoutastnum = (UFDouble) row.getValues().get("naccoutastnum").getValue()!=null? (UFDouble)row.getValues().get("naccoutastnum").getValue():UFDouble.ZERO_DBL;
                        UFDouble nshouldastnum = (UFDouble) row.getValues().get("nshouldastnum").getValue()!=null? (UFDouble)row.getValues().get("nshouldastnum").getValue():UFDouble.ZERO_DBL;
                        UFDouble npscastnum = (UFDouble) row.getValues().get("npscastnum").getValue()!=null? (UFDouble)row.getValues().get("npscastnum").getValue():UFDouble.ZERO_DBL;
                        UFDouble borrowedQty =nplanoutastnum.sub(naccoutastnum).sub(nshouldastnum).sub(npscastnum);
                        // 新增备料数量字段，取值为：计划出库数量-累计出库数量-累计发货数量-累计委外数量
                        if (borrowedQty.doubleValue() <= 0) {
                           continue;
                        }
                        Cell borrowedQtyCell = new Cell();
                        borrowedQtyCell.setValue(borrowedQty);
                        row.getValues().put("borrowedQty", borrowedQtyCell);
                        // 产品编码	取备料计划表头产品编码
                        Cell cmaterialvid = new Cell();
                        cmaterialvid.setValue(vo.getParentVO().getCmaterialvid());
                        row.getValues().put("cmaterialvid", borrowedQtyCell);
                        // 产品名称	取备料计划表头产品名称
                        Cell cmaterialvidName = new Cell();
                        HYPubBO hybo = new HYPubBO();
                        Object cmaterialvidName1 = hybo.findColValue("bd_material", "name", "pk_material = '"+vo.getParentVO().getCmaterialvid()+"' ");
                        cmaterialvidName.setValue(cmaterialvidName1);
                        row.getValues().put("cmaterialvidName", cmaterialvidName);
                        // 生产序号	取备料计划表头物料档案对应表头自定义项26
                        Cell cmaterialvidDef26 = new Cell();
                        Object cmaterialvidDef261 = hybo.findColValue("bd_material", "def26", "pk_material = '"+vo.getParentVO().getCmaterialvid()+"' ");
                        cmaterialvidDef26.setValue(cmaterialvidDef261);
                        row.getValues().put("cmaterialvidDef26", cmaterialvidDef26);
                        list.add(row);

                        // 备料计划单号	取备料计划表头
                        Cell vbillcode = new Cell();
                        vbillcode.setValue(vo.getParentVO().getVbillcode());
                        row.getValues().put("vbillcode", vbillcode);
                        list.add(row);

                        // 来源计划单号	取备料计划表头
                        Cell vsourcebillcode = new Cell();
                        vsourcebillcode.setValue(vo.getParentVO().getVsourcebillcode());
                        row.getValues().put("vsourcebillcode", vsourcebillcode);
                        list.add(row);
                    }

//                    bodyList.add(billCard.getBody());
                }
            }
            Grid grid = new Grid();
            GridModel model = new GridModel();
            model.add(list);
            grid.setModel(model);
            Map<String, Object> returnMap = new HashMap<>();
            Map<String, Object> list1 = new HashMap<>();
            list1.put("rows",list);
            returnMap.put("data", list1);
            returnMap.put("success", true);
            return returnMap;
        } catch (Exception ex) {
            ExceptionUtils.wrapBusinessException(ex.getMessage());
            Map<String, Object> returnMap = new HashMap<>();
            returnMap.put("data", null);
            returnMap.put("success", false);
            return returnMap;
        }
    }
}
