package {{packageName}}.client.{{lowerCase apiEntity.api.codeName}}.runtime.service.dataentity;

import net.ibizsys.central.r8.util.SpringContextHolder;
import net.ibizsys.central.util.ISearchContextDTO;
import {{packageName}}.client.{{lowerCase apiEntity.api.codeName}}.feign.dto.{{apiEntity.codeName}}DTO;
import {{packageName}}.client.{{lowerCase apiEntity.api.codeName}}.feign.{{apiEntity.codeName}}FeignClient;
import net.ibizsys.central.dataentity.IDataEntityRuntime;
import net.ibizsys.central.util.IEntityDTO;
import net.ibizsys.central.util.SearchContextDTO;
import net.ibizsys.model.dataentity.action.IPSDEAction;
import net.ibizsys.model.dataentity.ds.IPSDEDataSet;
import net.ibizsys.model.service.IPSSubSysServiceAPIDEMethod;
import net.ibizsys.runtime.util.DataTypeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * 实体[{{apiEntity.codeName}}] 对象
 *
 */
public class {{apiEntity.codeName}}SubSysServiceAPIDERuntime extends {{packageName}}.client.{{lowerCase apiEntity.api.codeName}}.runtime.service.{{apiEntity.api.codeName}}R8SubSysServiceAPIDERuntime{

    private {{apiEntity.codeName}}FeignClient getClient() {
        return SpringContextHolder.getBean({{apiEntity.codeName}}FeignClient.class);
    }

    @Override
    public IEntityDTO createClientEntity() {
        return new {{apiEntity.codeName}}DTO();
    }

    @Override
    public Object onInvokeMethod(IPSSubSysServiceAPIDEMethod iPSSubSysServiceAPIDEMethod, Map<String, Object> context, IDataEntityRuntime iDataEntityRuntime, IPSDEAction iPSDEAction, Object[] args) throws Throwable {
        Object key;
        if (args[0] instanceof IEntityDTO) {
            key = ((IEntityDTO) args[0]).get(iDataEntityRuntime.getKeyPSDEField().getName().toLowerCase());
        } else {
            key = args[0];
        }
        Object obj = null;
        String strActionName = iPSDEAction.getName().toLowerCase();
        switch (strActionName) {
{{#each apiEntity.pSDataEntity.allPSDEActions as |deAction|}}
            case "{{lowerCase deAction.name}}":
    {{#each apiEntity.methods as |method|}}
        {{#if (and (eq methodType "DEACTION") (eq method.pSDEServiceAPIMethod.pSDEAction.name deAction.name))}}
            {{#each method.parentApiEntities as |parent|}}
                {{#if (and (eq @index 0) @last)}}
                if (context != null && context.containsKey("{{parent.majorPSDEServiceAPI.name}}")) {
                    {{#eq return.type "DTO"}}obj = {{/eq}}{{#eq return.type "SIMPLE"}}obj = {{/eq}}{{#eq return.type "USER"}}obj = {{/eq}}getClient().{{camelCase method.name}}{{#each pathVariables as |pathVariable|}}{{#if @first}}By{{else}}And{{/if}}{{pascalCase pathVariable.name}}{{/each}}({{#each pathVariables as |pathVariable|}}{{#unless @first}}, {{/unless}}({{type.java}}) {{#unless (and @last method.pSDEServiceAPIMethod.isNeedResourceKey)}}context.get("{{parent.majorPSDEServiceAPI.name}}"){{/unless}}{{#if (and @last method.pSDEServiceAPIMethod.isNeedResourceKey)}}key{{/if}}{{/each}}{{#if body}}{{#if pathVariables}}, {{/if}}{{#eq input.type "DTO"}}({{body}}){{/eq}}{{#eq input.type "KEYFIELD"}}{{#eq pSDEAction.actionType "USERCUSTOM"}}({{body}}){{/eq}}{{/eq}}{{#eq input.type "DTOS"}}(List<{{body}}>){{/eq}}convert2ClientEntity(args[0]){{/if}});
                    break;
                }
                {{/if}}
            {{/each}}
        {{/if}}
    {{/each}}
    {{#each apiEntity.methods as |method|}}
        {{#if (and (eq methodType "DEACTION") (eq method.pSDEServiceAPIMethod.pSDEAction.name deAction.name))}}
            {{#unless method.parentApiEntities}}
                {{#eq return.type "DTO"}}obj = {{/eq}}{{#eq return.type "SIMPLE"}}obj = {{/eq}}{{#eq return.type "USER"}}obj = {{/eq}}getClient().{{camelCase method.name}}{{#each pathVariables as |pathVariables|}}{{#if @first}}By{{else}}And{{/if}}{{pascalCase pathVariables.name}}{{/each}}({{#each pathVariables as |pathVariable|}}{{#unless @first}}, {{/unless}}({{type.java}}) key{{/each}}{{#if body}}{{#if pathVariables}}, {{/if}}{{#eq input.type "DTO"}}({{body}}){{/eq}}{{#eq input.type "KEYFIELD"}}{{#eq pSDEAction.actionType "USERCUSTOM"}}({{body}}){{/eq}}{{/eq}}{{#eq input.type "DTOS"}}(List<{{body}}>){{/eq}}convert2ClientEntity(args[0]){{/if}});
                break;
            {{/unless}}
        {{/if}}
    {{/each}}
{{/each}}
{{#each apiEntity.methods}}
{{/each}}
        }
        IEntityDTO entityDTO = iDataEntityRuntime.createEntity();
        entityDTO.reload(obj, true, false);
        return entityDTO;
    }

    @Override
    public Object onInvokeMethod(IPSSubSysServiceAPIDEMethod iPSSubSysServiceAPIDEMethod, Map<String, Object> context, IDataEntityRuntime iDataEntityRuntime, IPSDEDataSet iPSDEDataSet, Object[] args) throws Throwable {
        Object obj = null;
        convertCondition((SearchContextDTO)args[0]);
        String strActionName = iPSDEDataSet.getName().toLowerCase();
        switch (strActionName) {
{{#each apiEntity.pSDataEntity.allPSDEDataSets as |dataSet|}}
            case "{{lowerCase dataSet.name}}":
    {{#each apiEntity.methods as |method|}}
        {{#if (and (eq methodType "FETCH") (eq method.pSDEServiceAPIMethod.pSDEDataSet.name dataSet.name))}}
            {{#each method.parentApiEntities as |parent|}}
                {{#if (and (eq @index 0) @last)}}
                if (context != null && context.containsKey("{{parent.majorPSDEServiceAPI.name}}")) {
                    obj = getClient().{{camelCase method.name}}{{#each method.pathVariables as |pathVariaable|}}{{#if @first}}By{{else}}And{{/if}}{{pascalCase pathVariaable.name}}{{/each}}({{#each method.pathVariables}}{{#unless @first}}, {{/unless}}({{type.java}}) context.get("{{parent.majorPSDEServiceAPI.name}}"){{/each}}{{#if body}}{{#if method.pathVariables}}, {{/if}}(SearchContextDTO)args[0]{{/if}});
                    break;
                }
                {{/if}}
            {{/each}}
        {{/if}}
    {{/each}}
    {{#each apiEntity.methods as |method|}}
        {{#if (and (eq methodType "FETCH") (eq method.pSDEServiceAPIMethod.pSDEDataSet.name dataSet.name))}}
            {{#unless method.parentApiEntities}}
                obj = getClient().{{camelCase method.name}}{{#each method.pathVariables}}{{#if @first}}By{{else}}And{{/if}}{{pascalCase method.name}}{{/each}}({{#each method.pathVariables}}{{#unless @first}}, {{/unless}}({{type.java}})key{{/each}}{{#if body}}{{#if method.pathVariables}}, {{/if}}(SearchContextDTO)args[0]{{/if}});
                break;
            {{/unless}}
        {{/if}}
    {{/each}}
{{/each}}
{{#each apiEntity.methods}}
{{/each}}
        }

        List recodes = new ArrayList();
        SearchContextDTO dto = (SearchContextDTO) args[0];
        if (obj != null && obj instanceof Page) {
            Page ret = (Page) obj;
            for (Object item : ret.getContent()) {
                recodes.add(convert2Entity(item));
            }
            return new PageImpl(recodes, dto.getPageable(), ret.getTotalElements());
        }
        return new PageImpl(recodes, dto.getPageable(), 0);
    }
}