package com.yem.wm.bc.change.op;

import com.yem.wm.bc.change.business.ChangeModelHelper;
import com.yem.wm.bc.change.common.consts.BizChangeStatusEnum;
import kd.bd.sbd.enums.StatusEnum;
import kd.bd.sbd.utils.StringUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.ILocaleString;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.filter.FilterBuilder;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.entity.validate.ErrorLevel;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;

/**
 * @author 夜子
 * @ClassName SrcBillBizChangeOpValidator
 * @description: 源单变更操作校验
 * @date 2023年08月03日
 * @version: 1.0
 */
public class SrcBillBizChangeOpValidator extends AbstractValidator {
    public void validate() {
        block3 :
        if (this.getOperateKey().equals("bizchange")) {
            if (this.dataEntities == null || this.dataEntities.length == 0)
                return;
            DynamicObject changeModel = ChangeModelHelper.getChangeModel4SrcBill((String) this.getEntityKey());
            if (changeModel == null) {
                this.addMessage(this.dataEntities[0],
                        ResManager.loadKDString((String) "当前单据没有启用的变更模型，请联系开发服务人员维护变更模型数据。",
                                (String) "SrcBillBizChangeOpValidator_2", (String) "yem-yc-changemodel",
                                (Object[]) new Object[0]),
                        ErrorLevel.Error);
                return;
            }
            FilterBuilder filterBuilder = ChangeModelHelper.convertStringToQFilter(
                    (String) changeModel.getString("areaconditionjson_tag"), (String) this.getEntityKey());
            for (ExtendedDataEntity data : this.dataEntities) {
                DynamicObject bill = data.getDataEntity();
                Long pk = (Long) bill.getPkValue();
                DynamicObject rBill = BusinessDataServiceHelper.loadSingle((Object) pk,
                        (String) this.getEntityKey(), (String) "billstatus,changestatus");
                if (!StatusEnum.AUDIT.getValue().equals(rBill.getString("billstatus"))) {
                    this.addMessage(data,
                            ResManager.loadKDString((String) "请选择已审核数据进行变更操作。",
                                    (String) "SrcBillBizChangeOpValidator_0", (String) "yem-yc-changemodel",
                                    (Object[]) new Object[0]),
                            ErrorLevel.Error);
                    return;
                }
                if (bill.getString("changestatus") != null
                        && BizChangeStatusEnum.CHANGING.getValue().equals(rBill.getString("changestatus"))) {
                    this.addMessage(data,
                            ResManager.loadKDString((String) "不能选择正在变更的数据进行变更操作。",
                                    (String) "SrcBillBizChangeOpValidator_1", (String) "yem-yc-changemodel",
                                    (Object[]) new Object[0]),
                            ErrorLevel.Error);
                    return;
                }
                if (filterBuilder == null)
                    continue;
                QFilter[] qFilter = new QFilter[]{filterBuilder.getQFilter(), new QFilter("id", "=", (Object) pk)};
                if (QueryServiceHelper.exists((String) bill.getDataEntityType().getName(), (QFilter[]) qFilter))
                    continue;
                String errorInfo = ResManager.loadKDString((String) "不符合变更条件，请检查变更模型的【数据范围】设置。",
                        (String) "SrcBillBizChangeOpValidator_3", (String) "yem-yc-changemodel",
                        (Object[]) new Object[0]);
                ILocaleString areaContionDesc = changeModel.getLocaleString("mul_areaconditiondesc");
                if (areaContionDesc != null && StringUtils.isNotEmpty((String) areaContionDesc.toString())) {
                    errorInfo = areaContionDesc.toString();
                }
                this.addMessage(data, errorInfo, ErrorLevel.Error);
            }
        }
    }
}