package cn.cerc.mis.core;

import java.util.Map;
import java.util.Objects;

import cn.cerc.db.core.DataRow;
import cn.cerc.db.core.DataSet;
import cn.cerc.db.core.IHandle;
import cn.cerc.db.core.ISession;
import cn.cerc.mis.client.ServiceExecuteException;
import cn.cerc.mis.client.ServiceSign;

public class ServiceQuery implements IHandle {
    private ServiceSign service;
    private DataSet dataIn;
    private DataSet dataOut;
    private ISession session;

    public static ServiceQuery open(IHandle handle, ServiceSign service, DataSet dataIn) {
        return new ServiceQuery(handle, service).call(dataIn);
    }

    public static ServiceQuery open(IHandle handle, ServiceSign service, DataRow headIn) {
        DataSet dataIn = new DataSet();
        dataIn.head().copyValues(headIn);
        return new ServiceQuery(handle, service).call(dataIn);
    }

    public static ServiceQuery open(IHandle handle, ServiceSign service, Map<String, Object> headIn) {
        DataSet dataIn = new DataSet();
        headIn.forEach((key, value) -> dataIn.head().setValue(key, value));
        return new ServiceQuery(handle, service).call(dataIn);
    }

    public ServiceQuery(IHandle handle) {
        this.setSession(handle.getSession());
    }

    public ServiceQuery(IHandle handle, ServiceSign service) {
        this(handle);
        this.service = service;
    }

    public ServiceQuery call(DataSet dataIn) {
        this.dataIn = dataIn;
        this.dataOut = this.service.call(this, dataIn);
        return this;
    }

    public ServiceQuery setService(ServiceSign service) {
        this.service = service;
        return this;
    }

    public boolean isOk() {
        Objects.requireNonNull(dataOut);
        return dataOut.state() > 0;
    }

    public boolean isOkElseThrow() throws ServiceExecuteException {
        if (!isOk())
            throw new ServiceExecuteException(dataOut.message());
        return true;
    }

    public boolean isFail() {
        Objects.requireNonNull(dataOut);
        return dataOut.state() <= 0;
    }

    public boolean isFailElseThrow() throws ServiceExecuteException {
        if (!isFail())
            throw new ServiceExecuteException(dataOut.message());
        return true;
    }

    public DataSet getDataOutElseThrow() throws ServiceExecuteException {
        Objects.requireNonNull(dataOut);
        if (dataOut.state() <= 0)
            throw new ServiceExecuteException(dataOut.message());
        return dataOut;
    }

    public DataRow getHeadOutElseThrow() throws ServiceExecuteException {
        Objects.requireNonNull(dataOut);
        if (dataOut.state() <= 0)
            throw new ServiceExecuteException(dataOut.message());
        return dataOut.head();
    }

    public final DataSet dataIn() {
        if (this.dataIn == null)
            this.dataIn = new DataSet();
        return dataIn;
    }

    public final DataSet dataOut() {
        if (this.dataOut == null)
            this.dataOut = new DataSet();
        return dataOut;
    }

    @Override
    public ISession getSession() {
        return session;
    }

    @Override
    public void setSession(ISession session) {
        this.session = session;
    }

    public final String serviceId() {
        return service.id();
    }

}

@@ -2062,195 +2062,8 @@
  %7D%0A%0A
-    public boolean isFailElseThrow() throws ServiceExecuteException %7B%0A        if (!isFail())%0A            throw new ServiceExecuteException(dataOut.message());%0A        return true;%0A    %7D%0A%0A
     

Update ServiceQuery.java