package nccloud.api.impl.ic.m47.check;

import nc.vo.ic.m47.entity.SubcontInBodyVO;
import nc.vo.ic.pub.define.ICPubMetaNameConst;
import nc.vo.pub.IAttributeMeta;
import nc.vo.pub.ISuperVO;
import nc.vo.pub.ValidationException;
import nc.vo.pubapp.pattern.model.entity.bill.AbstractBill;
import nc.vo.pubapp.pattern.pub.MapList;
import nc.vo.scmpub.check.vovalidate.VONullValidate;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Classname CheckSubcontInSaveValidator
 * @Description TODO
 * @Version 1.0.0
 * @Date 2025/5/15 9:54
 * @Created by ame
 */
public class CheckSubcontInSaveValidator extends VONullValidate {
    @Override
    public String[] getHeadNotNullFields() {
        // TODO Auto-generated method stub
        //组织，仓库，出入库类型编码
        String[] headnames =
                {
                        ICPubMetaNameConst.PK_ORG, ICPubMetaNameConst.CWAREHOUSEID, ICPubMetaNameConst.VTRANTYPECODE
                };
        return headnames;
    }

    @Override
    public String[] getBodyNotNullFields() {
        String[] bodynames = {
                ICPubMetaNameConst.CMATERIALVID, ICPubMetaNameConst.NSHOULDASSISTNUM
//                , ICPubMetaNameConst.CASTUNITID
        };
        return bodynames;
    }

    @Override
    public MapList<String, String> getMultiBodyNotNullFields() {
        return null;
    }

    @Override
    public void otherCheck(AbstractBill billVO, List<ValidationException> exceptions) {
//        check(billVO, null, new String[]{
//                ICPubMetaNameConst.NNUM,
//                SubcontInBodyVO.NORIGTAXMNY
//        }, exceptions);
    }

    protected void check(AbstractBill bill, String[] headnullkeys,
                         String[] bodynullkeys, List<ValidationException> exceptions) {
        // 检查表头

        // 检查表体
        checkBody(bill, bodynullkeys, exceptions);
    }

    private void checkBody(AbstractBill bill, String[] bodynullkeys,
                           List<ValidationException> exceptions) {
        if (bodynullkeys != null) {
            int row = 1;
            ISuperVO[] supervos = bill.getChildren(SubcontInBodyVO.class);
            Set<String> messagelist = new HashSet<String>();
            for (ISuperVO vo : supervos) {
                Set<String> nullbodyset = new HashSet<String>();
                for (String bodynullkey : bodynullkeys) {
                    if (vo.getAttributeValue(bodynullkey) == null) {
                        IAttributeMeta attribute = vo.getMetaData().getAttribute(bodynullkey);
                        nullbodyset.add(attribute.getColumn().getLabel());
                    }
                }
                if (nullbodyset.size() > 0 && nullbodyset.size() == bodynullkeys.length) {
                    String message =
                            "表体第" + row + "行以下字段不能同时为空：" + getshow(nullbodyset, "、");/*-=notranslate=-*/
                    messagelist.add(message);
                }
                row++;
            }
            if (messagelist.size() > 0) {
                exceptions.add(new ValidationException(getshow(messagelist, "\r\n")));
            }
        }
    }

    private String getshow(Set<String> nullfields, String partition) {
        StringBuffer str = new StringBuffer();
        for (String field : nullfields) {
            str.append(field + partition);
        }
        str.deleteCharAt(str.length() - 1);
        return str.toString();
    }
}
