{{#neq apiEntity.entity.storage "NONE"}}
package {{packageName}}.{{lowerCase apiEntity.api.codeName}}.rest;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;
import java.util.HashMap;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import {{packageName}}.{{lowerCase apiEntity.api.codeName}}.dto.*;
import {{packageName}}.{{lowerCase apiEntity.api.codeName}}.mapping.*;
import {{packageName}}.core.{{apiEntity.entity.module}}.domain.{{apiEntity.entity.codeName}};
import {{packageName}}.core.{{apiEntity.entity.module}}.service.{{apiEntity.entity.codeName}}Service;
import {{packageName}}.core.{{apiEntity.entity.module}}.filter.{{apiEntity.entity.codeName}}SearchContext;
import cn.ibizlab.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"{{apiEntity.apiName}}" })
@RestController("{{lowerCase apiEntity.api.codeName}}-{{lowerCase apiEntity.codeName}}")
@RequestMapping("{{apiEntity.api.basePath}}")
public class {{apiEntity.codeName}}Resource {

    @Autowired
    public {{apiEntity.entity.codeName}}Service {{camelCase apiEntity.entity.codeName}}Service;

{{#each apiEntity.dtos}}
    {{#eq type "DEFAULT"}}
    @Autowired
    @Lazy
    public {{codeName}}Mapping {{camelCase codeName}}Mapping;

    {{/eq}}
{{/each}}

{{#each apiEntity.methods}}
{{#neq name "Select"}}
    @ApiOperation(value = "{{apiName}}", tags = {"{{tags}}" },  notes = "{{notes}}")
    {{#if permission}}
    @{{permission.ano}}("{{permission.cond}}")
    {{/if}}
    {{#eq methodType "DEACTION"}}
        {{#eq name "Update"}}
            {{#if apiEntity.entity.lastModifyField}}
    @VersionCheck(entity = "{{lowerCase apiEntity.entity.codeName}}" , versionfield = "{{camelCase apiEntity.entity.lastModifyField.codeName}}")
            {{/if}}
        {{/eq}}
    {{/eq}}
    @RequestMapping(method = RequestMethod.{{requestMethod}}, value = "{{requestPath2}}")
    public ResponseEntity<{{outParam}}> {{camelCase name}}{{#each pathVariables}}{{#if @first}}By{{else}}And{{/if}}{{pascalCase name}}{{/each}}
            ({{#each pathVariables}}{{#unless @first}}, {{/unless}}@PathVariable("{{camelCase name}}") {{type.java}} {{camelCase name}}{{/each}}{{#if body}}{{#if pathVariables}}, {{/if}}{{#neq requestMethod 'GET'}}@Validated @RequestBody {{/neq}}{{body}}{{/if}}) {
    {{!行为}}
    {{#eq methodType "DEACTION"}}
        {{#if needDto2Domain}}
        {{inParam2}} {{inParamName2}} = {{camelCase inputDTO.name}}Mapping.toDomain({{inParamName}});
            {{#if listIn}}
                {{#if pathVariables}}
        {{inParamName2}}.forEach(item -> {
                {{#each pathVariables}}{{#if @last}}
            item.set{{pascalCase name}}({{camelCase name}});
                {{/if}}{{/each}}
        });
                {{/if}}
            {{else}}
                {{#each pathVariables}}{{#if @last}}
        {{inParamName2}}.set{{pascalCase name}}({{camelCase name}});
                {{/if}}{{/each}}
            {{/if}}
        {{/if}}
        {{#if booleanReturn}}
        {{camelCase apiEntity.entity.codeName}}Service.{{camelCase name}}({{inParamName2}});
        {{outParam2}} rt = {{inParamName2}};
        {{else}}
        {{outParam2}} rt = {{camelCase apiEntity.entity.codeName}}Service.{{camelCase name}}({{inParamName2}});
        {{/if}}
        {{#if needDomain2Dto}}
        return ResponseEntity.status(HttpStatus.OK).body({{camelCase returnDTO.name}}Mapping.toDto(rt));
        {{else}}
        return ResponseEntity.status(HttpStatus.OK).body(rt);
        {{/if}}
    {{/eq}}
    {{!数据集}}
    {{#eq methodType "FETCH"}}
        {{#each pathVariables}}
            {{#if @last}}
        {{inParamName}}.set{{pascalCase name}}EQ({{camelCase name}});
            {{/if}}
        {{/each}}
        Page<{{apiEntity.entity.codeName}}> domains = {{camelCase apiEntity.entity.codeName}}Service.search{{pascalCase pSDEDataSet.codeName}}({{inParamName}}) ;
        {{outParam}} list = {{camelCase returnDTO.name}}Mapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf({{inParamName}}.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf({{inParamName}}.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    {{/eq}}
    }

{{/neq}}
{{/each}}

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','{{projectName}}-{{apiEntity.entity.codeName}}-Create-all')")
    @ApiOperation(value = "批量新建{{apiEntity.entity.logicName}}", tags = {"{{apiEntity.entity.logicName}}" },  notes = "批量新建{{apiEntity.entity.logicName}}")
	@RequestMapping(method = RequestMethod.POST, value = "{{pluralize apiEntity.codeName}}/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<{{apiEntity.codeName}}DTO> dtos) {
        {{camelCase apiEntity.entity.codeName}}Service.createBatch({{camelCase apiEntity.defaultDto.codeName}}Mapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','{{projectName}}-{{apiEntity.entity.codeName}}-Update-all')")
    @ApiOperation(value = "批量更新{{apiEntity.entity.logicName}}", tags = {"{{apiEntity.entity.logicName}}" },  notes = "批量更新{{apiEntity.entity.logicName}}")
	@RequestMapping(method = RequestMethod.PUT, value = "{{pluralize apiEntity.codeName}}/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<{{apiEntity.codeName}}DTO> dtos) {
        {{camelCase apiEntity.entity.codeName}}Service.updateBatch({{camelCase apiEntity.defaultDto.codeName}}Mapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','{{projectName}}-{{apiEntity.entity.codeName}}-Save-all')")
    @ApiOperation(value = "批量保存{{apiEntity.entity.logicName}}", tags = {"{{apiEntity.entity.logicName}}" },  notes = "批量保存{{apiEntity.entity.logicName}}")
	@RequestMapping(method = RequestMethod.POST, value = "{{pluralize apiEntity.codeName}}/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<{{apiEntity.codeName}}DTO> dtos) {
        {{camelCase apiEntity.entity.codeName}}Service.saveBatch({{camelCase apiEntity.defaultDto.codeName}}Mapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','{{projectName}}-{{apiEntity.entity.codeName}}-Save-all')")
    @ApiOperation(value = "批量导入{{apiEntity.entity.logicName}}", tags = {"{{apiEntity.entity.logicName}}" },  notes = "批量导入{{apiEntity.entity.logicName}}")
	@RequestMapping(method = RequestMethod.POST, value = "{{pluralize apiEntity.codeName}}/import")
    public ResponseEntity<Map> importData(@RequestParam(value = "config") String config ,@RequestBody List<{{apiEntity.codeName}}DTO> dtos) {
        Map rs=new HashMap();
        if(ObjectUtils.isEmpty(dtos)){
            rs.put("rst", 1);
            rs.put("msg", "未传入内容");
            return ResponseEntity.status(HttpStatus.NO_CONTENT).body(rs);
        }
        else{
            List<{{apiEntity.entity.codeName}}> list={{camelCase apiEntity.defaultDto.codeName}}Mapping.toDomain(dtos);
            List<{{apiEntity.entity.codeName}}> rtList=new ArrayList();
            list.forEach(item->{
                if({{camelCase apiEntity.entity.codeName}}Service.save(item))
                    rtList.add(item);
            });
            rs.put("rst", 0);
            rs.put("data",rtList);
        }
        return  ResponseEntity.status(HttpStatus.OK).body(rs);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','{{projectName}}-{{apiEntity.entity.codeName}}-Remove-all')")
    @ApiOperation(value = "批量删除{{apiEntity.entity.logicName}}", tags = {"{{apiEntity.entity.logicName}}" },  notes = "批量删除{{apiEntity.entity.logicName}}")
	@RequestMapping(method = RequestMethod.DELETE, value = {"{{pluralize apiEntity.codeName}}","/{{pluralize apiEntity.codeName}}/batch"})
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<{{apiEntity.entity.keyField.type.java}}> ids) {
        {{camelCase apiEntity.entity.codeName}}Service.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }
}
{{/neq}}