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

import nc.bs.ic.pub.base.ICRule;
import nc.pubitf.uapbd.CurrencyRateUtilHelper;
import nc.vo.ic.general.define.ICBillFlag;
import nc.vo.ic.m47.entity.SubcontInBodyVO;
import nc.vo.ic.m47.entity.SubcontInHeadVO;
import nc.vo.ic.m47.entity.SubcontInVO;
import nc.vo.ic.org.OrgInfoQuery;
import nc.vo.ic.pub.define.ICPubMetaNameConst;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.AppContext;
import nc.vo.pubapp.pattern.pub.PubAppTool;

/**
 * @Classname Choose47DefaultFillRule
 * @Description TODO
 * @Version 1.0.0
 * @Date 2025/5/15 10:19
 * @Created by ame
 */
public class Choose47DefaultFillRule<E extends SubcontInVO> extends ICRule<E> {

    @Override
    public void process(E[] vos) {
        for (E billvo : vos) {
            //填充默认值
            setDefultInfo(billvo);
            //设置财务组织,币种
            setFanaceorgValue(billvo);
        }
    }

    private void setFanaceorgValue(SubcontInVO vo) {
        SubcontInHeadVO head = vo.getHead();
        String pk_org = head.getPk_org();
        if (pk_org != null) {
            String cfinanceoid =
                    new OrgInfoQuery().getFinanceOrgIDByCalBodyID(pk_org);
            String cfinancevid =
                    new OrgInfoQuery().getFinanceOrgVIDByCalBodyID(pk_org);
            String pk_orgv = head.getPk_org_v();
            String cwareid = head.getCwarehouseid();

            String ccurrencyid = CurrencyRateUtilHelper.getInstance()
                    .getLocalCurrtypeByOrgID(cfinanceoid);
            SubcontInBodyVO[] bodys = vo.getBodys();
            for (SubcontInBodyVO body : bodys) {

                // 有来源的单据行不设置默认值，只有自制入才设置默认值
                String csourcebillbid = body.getCsourcebillbid();
                if (!PubAppTool.isNull(csourcebillbid)) {
                    continue;
                }
                // 币种
                if (null == body.getAttributeValue(SubcontInBodyVO.CORIGCURRENCYID)) {
                    body.setAttributeValue(SubcontInBodyVO.CORIGCURRENCYID, ccurrencyid);// TODO
                    body.setAttributeValue(SubcontInBodyVO.NCHANGESTDRATE, UFDouble.ONE_DBL);
                }
                body.setAttributeValue(SubcontInBodyVO.CCURRENCYID, ccurrencyid);
            }

        }

    }

    private void setDefultInfo(SubcontInVO vo) {
        // 单据状态
        vo.getHead().setAttributeValue(ICPubMetaNameConst.FBILLFLAG,
                ICBillFlag.FREE.value());
        UFDate busdate = AppContext.getInstance().getBusiDate();
        String pk_group = (String) vo.getParentVO()
                .getAttributeValue(ICPubMetaNameConst.PK_GROUP);
        // 单据日期
        if (null == vo.getParentVO()
                .getAttributeValue(ICPubMetaNameConst.DBILLDATE)) {
            vo.getParentVO().setAttributeValue(ICPubMetaNameConst.DBILLDATE, busdate);
        }
        // 制单人
        if (null == vo.getParentVO()
                .getAttributeValue(ICPubMetaNameConst.OPERATOR)) {
            vo.getParentVO().setAttributeValue(ICPubMetaNameConst.OPERATOR,
                    AppContext.getInstance().getPkUser());
        }
        // 集团
        if (null == pk_group) {
            pk_group = AppContext.getInstance().getPkGroup();
            vo.getParentVO().setAttributeValue(ICPubMetaNameConst.PK_GROUP, pk_group);
        }
        for (SubcontInBodyVO body : vo.getBodys()) {
            // 入库日期
            UFDate dbizdate = body.getDbizdate();
            if (null == dbizdate) {
                body.setDbizdate(busdate);
            }
            body.setPk_org(vo.getParentVO().getPk_org());
            body.setPk_org_v(vo.getParentVO().getPk_org_v());
            body.setPk_group(pk_group);
        }
    }

}
