{{#eq apiEntity.code ""}}
{{#if apiEntity.major}}
    {{#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 javax.servlet.ServletRequest;
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.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.I{{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.entity.logicName}}" })
@RestController("{{lowerCase apiEntity.api.codeName}}-{{lowerCase apiEntity.codeName}}")
@RequestMapping("")
public class {{apiEntity.codeName}}Resource {

    @Autowired
    public I{{apiEntity.entity.codeName}}Service service;

    @Autowired
    @Lazy
    public {{apiEntity.codeName}}Mapping mapping;

    @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}}")
    public ResponseEntity<{{apiEntity.codeName}}DTO> create(@Validated @RequestBody {{apiEntity.codeName}}DTO dto) {
        {{apiEntity.entity.codeName}} domain = mapping.toDomain(dto);
		service.create(domain);
		return ResponseEntity.status(HttpStatus.OK).body(mapping.toDto(domain));
    }

    @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) {
        service.createBatch(mapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','{{projectName}}-{{apiEntity.entity.codeName}}-Get-all')")
    @ApiOperation(value = "获取{{apiEntity.entity.logicName}}", tags = {"{{apiEntity.entity.logicName}}" },  notes = "获取{{apiEntity.entity.logicName}}")
	@RequestMapping(method = RequestMethod.GET, value = "/{{pluralize apiEntity.codeName}}/{id}")
    public ResponseEntity<{{apiEntity.codeName}}DTO> get(@PathVariable("id") {{apiEntity.entity.keyField.type.java}} id) {
        {{apiEntity.entity.codeName}} domain = service.get(id);
        {{apiEntity.codeName}}DTO dto = mapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @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}}/{id}")
    public ResponseEntity<Boolean> remove(@PathVariable("id") {{apiEntity.entity.keyField.type.java}} id) {
         return ResponseEntity.status(HttpStatus.OK).body(service.remove(id));
    }

    @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}}/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<{{apiEntity.entity.keyField.type.java}}> ids) {
        service.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    {{#if apiEntity.entity.lastModifyField}}
    @VersionCheck(entity = "{{lowerCase apiEntity.entity.codeName}}" , versionfield = "{{camelCase apiEntity.entity.lastModifyField.codeName}}")    
    {{/if}}
    @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}}/{id}")
    public ResponseEntity<{{apiEntity.codeName}}DTO> update(@PathVariable("id") {{apiEntity.entity.keyField.type.java}} id, @RequestBody {{apiEntity.codeName}}DTO dto) {
		{{apiEntity.entity.codeName}} domain  = mapping.toDomain(dto);
        domain.set{{pascalCase apiEntity.entity.keyField.codeName}}(id);
		service.update(domain);
        return ResponseEntity.status(HttpStatus.OK).body(mapping.toDto(domain));
    }

    @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) {
        service.updateBatch(mapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @ApiOperation(value = "检查{{apiEntity.entity.logicName}}", tags = {"{{apiEntity.entity.logicName}}" },  notes = "检查{{apiEntity.entity.logicName}}")
	@RequestMapping(method = RequestMethod.POST, value = "/{{pluralize apiEntity.codeName}}/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody {{apiEntity.codeName}}DTO dto) {
        return  ResponseEntity.status(HttpStatus.OK).body(service.checkKey(mapping.toDomain(dto)));
    }

    @ApiOperation(value = "获取{{apiEntity.entity.logicName}}草稿", tags = {"{{apiEntity.entity.logicName}}" },  notes = "获取{{apiEntity.entity.logicName}}草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/{{pluralize apiEntity.codeName}}/getdraft")
    public ResponseEntity<{{apiEntity.codeName}}DTO> getDraft({{apiEntity.codeName}}DTO dto) {
        {{apiEntity.entity.codeName}} domain = mapping.toDomain(dto);
        return ResponseEntity.status(HttpStatus.OK).body(mapping.toDto(service.getDraft(domain)));
    }

    @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}}/save")
    public ResponseEntity<{{apiEntity.codeName}}DTO> save(@RequestBody {{apiEntity.codeName}}DTO dto) {
        {{apiEntity.entity.codeName}} domain = mapping.toDomain(dto);
        service.save(domain);
        return ResponseEntity.status(HttpStatus.OK).body(mapping.toDto(domain));
    }

    @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) {
        service.saveBatch(mapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    {{#each apiEntity.entity.dataSets as |dataSet|}}
        
    @ApiOperation(value = "获取{{dataSet.name}}", tags = {"{{apiEntity.entity.logicName}}" } ,notes = "获取{{dataSet.name}}")
    @RequestMapping(method= RequestMethod.GET , value="/{{pluralize apiEntity.codeName}}/fetch{{lowerCase dataSet.codeName}}")
        {{#if dataSet.enableGroup}}
    public ResponseEntity<List<Map>> fetch{{dataSet.codeName}}({{apiEntity.entity.codeName}}SearchContext context) {
        Page<Map> page = service.search{{dataSet.codeName}}(context) ;
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
                .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
                .header("x-total", String.valueOf(page.getTotalElements()))
                .body(page.getContent());
	}
        {{else}}
    public ResponseEntity<List<{{apiEntity.codeName}}DTO>> fetch{{dataSet.codeName}}({{apiEntity.entity.codeName}}SearchContext context) {
        Page<{{apiEntity.entity.codeName}}> domains = service.search{{dataSet.codeName}}(context) ;
        List<{{apiEntity.codeName}}DTO> list = mapping.toDto(domains.getContent());
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
                .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
                .header("x-total", String.valueOf(domains.getTotalElements()))
                .body(list);
	}
        {{/if}}
	

	@ApiOperation(value = "查询{{dataSet.name}}", tags = {"{{apiEntity.entity.logicName}}" } ,notes = "查询{{dataSet.name}}")
    @RequestMapping(method= RequestMethod.POST , value="/{{pluralize apiEntity.codeName}}/search{{lowerCase dataSet.codeName}}")
        {{#if dataSet.enableGroup}}
	public ResponseEntity<Page<Map>> search{{dataSet.codeName}}(@RequestBody {{apiEntity.entity.codeName}}SearchContext context) {
        Page<Map> page = service.search{{dataSet.codeName}}(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(page.getContent(), context.getPageable(), page.getTotalElements()));
	}    
        {{else}}
    public ResponseEntity<Page<{{apiEntity.codeName}}DTO>> search{{dataSet.codeName}}(@RequestBody {{apiEntity.entity.codeName}}SearchContext context) {
        Page<{{apiEntity.entity.codeName}}> domains = service.search{{dataSet.codeName}}(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(mapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}   
        {{/if}}
    {{/each}}
	



}
    {{/neq}}
{{/if}}
{{/eq}}