package cn.ibizlab.api.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.transaction.annotation.Transactional;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.uaa.domain.SYS_ROLE_PERMISSION;
import cn.ibizlab.core.uaa.service.ISYS_ROLE_PERMISSIONService;
import cn.ibizlab.core.uaa.filter.SYS_ROLE_PERMISSIONSearchContext;




@Slf4j
@Api(tags = {"SYS_ROLE_PERMISSION" })
@RestController("api-sys_role_permission")
@RequestMapping("")
public class SYS_ROLE_PERMISSIONResource {

    @Autowired
    private ISYS_ROLE_PERMISSIONService sys_role_permissionService;

    @Autowired
    @Lazy
    private SYS_ROLE_PERMISSIONMapping sys_role_permissionMapping;




    @PreAuthorize("hasPermission(#sys_role_permission_id,'Get',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Get", tags = {"SYS_ROLE_PERMISSION" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/sys_role_permissions/{sys_role_permission_id}")
    public ResponseEntity<SYS_ROLE_PERMISSIONDTO> get(@PathVariable("sys_role_permission_id") String sys_role_permission_id) {
        SYS_ROLE_PERMISSION domain = sys_role_permissionService.get(sys_role_permission_id);
        SYS_ROLE_PERMISSIONDTO dto = sys_role_permissionMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }




    @PreAuthorize("hasPermission('Remove',{#sys_role_permission_id,{this.getEntity(),'Sql'}})")
    @ApiOperation(value = "Remove", tags = {"SYS_ROLE_PERMISSION" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/sys_role_permissions/{sys_role_permission_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("sys_role_permission_id") String sys_role_permission_id) {
         return ResponseEntity.status(HttpStatus.OK).body(sys_role_permissionService.remove(sys_role_permission_id));
    }

    @ApiOperation(value = "RemoveBatch", tags = {"SYS_ROLE_PERMISSION" },  notes = "RemoveBatch")
	@RequestMapping(method = RequestMethod.DELETE, value = "/sys_role_permissions/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        sys_role_permissionService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @ApiOperation(value = "GetDraft", tags = {"SYS_ROLE_PERMISSION" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/sys_role_permissions/getdraft")
    public ResponseEntity<SYS_ROLE_PERMISSIONDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(sys_role_permissionMapping.toDto(sys_role_permissionService.getDraft(new SYS_ROLE_PERMISSION())));
    }




    @PreAuthorize("hasPermission('','Create',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Create", tags = {"SYS_ROLE_PERMISSION" },  notes = "Create")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_role_permissions")
    @Transactional
    public ResponseEntity<SYS_ROLE_PERMISSIONDTO> create(@RequestBody SYS_ROLE_PERMISSIONDTO sys_role_permissiondto) {
        SYS_ROLE_PERMISSION domain = sys_role_permissionMapping.toDomain(sys_role_permissiondto);
		sys_role_permissionService.create(domain);
        SYS_ROLE_PERMISSIONDTO dto = sys_role_permissionMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission('','Create',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "createBatch", tags = {"SYS_ROLE_PERMISSION" },  notes = "createBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_role_permissions/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SYS_ROLE_PERMISSIONDTO> sys_role_permissiondtos) {
        sys_role_permissionService.createBatch(sys_role_permissionMapping.toDomain(sys_role_permissiondtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @ApiOperation(value = "CheckKey", tags = {"SYS_ROLE_PERMISSION" },  notes = "CheckKey")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_role_permissions/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody SYS_ROLE_PERMISSIONDTO sys_role_permissiondto) {
        return  ResponseEntity.status(HttpStatus.OK).body(sys_role_permissionService.checkKey(sys_role_permissionMapping.toDomain(sys_role_permissiondto)));
    }




    @ApiOperation(value = "Save", tags = {"SYS_ROLE_PERMISSION" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_role_permissions/save")
    public ResponseEntity<Boolean> save(@RequestBody SYS_ROLE_PERMISSIONDTO sys_role_permissiondto) {
        return ResponseEntity.status(HttpStatus.OK).body(sys_role_permissionService.save(sys_role_permissionMapping.toDomain(sys_role_permissiondto)));
    }

    @ApiOperation(value = "SaveBatch", tags = {"SYS_ROLE_PERMISSION" },  notes = "SaveBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_role_permissions/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SYS_ROLE_PERMISSIONDTO> sys_role_permissiondtos) {
        sys_role_permissionService.saveBatch(sys_role_permissionMapping.toDomain(sys_role_permissiondtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @PreAuthorize("hasPermission(#sys_role_permission_id,'Update',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Update", tags = {"SYS_ROLE_PERMISSION" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/sys_role_permissions/{sys_role_permission_id}")
    @Transactional
    public ResponseEntity<SYS_ROLE_PERMISSIONDTO> update(@PathVariable("sys_role_permission_id") String sys_role_permission_id, @RequestBody SYS_ROLE_PERMISSIONDTO sys_role_permissiondto) {
		SYS_ROLE_PERMISSION domain = sys_role_permissionMapping.toDomain(sys_role_permissiondto);
        domain.setRolepermissionid(sys_role_permission_id);
		sys_role_permissionService.update(domain);
		SYS_ROLE_PERMISSIONDTO dto = sys_role_permissionMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(#sys_role_permission_id,'Update',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "UpdateBatch", tags = {"SYS_ROLE_PERMISSION" },  notes = "UpdateBatch")
	@RequestMapping(method = RequestMethod.PUT, value = "/sys_role_permissions/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SYS_ROLE_PERMISSIONDTO> sys_role_permissiondtos) {
        sys_role_permissionService.updateBatch(sys_role_permissionMapping.toDomain(sys_role_permissiondtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission('Get',{#context,'Default',this.getEntity(),'Sql'})")
	@ApiOperation(value = "fetchDEFAULT", tags = {"SYS_ROLE_PERMISSION" } ,notes = "fetchDEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/sys_role_permissions/fetchdefault")
	public ResponseEntity<List<SYS_ROLE_PERMISSIONDTO>> fetchDefault(SYS_ROLE_PERMISSIONSearchContext context) {
        Page<SYS_ROLE_PERMISSION> domains = sys_role_permissionService.searchDefault(context) ;
        List<SYS_ROLE_PERMISSIONDTO> list = sys_role_permissionMapping.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);
	}

    @PreAuthorize("hasPermission('Get',{#context,'Default',this.getEntity(),'Sql'})")
	@ApiOperation(value = "searchDEFAULT", tags = {"SYS_ROLE_PERMISSION" } ,notes = "searchDEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/sys_role_permissions/searchdefault")
	public ResponseEntity<Page<SYS_ROLE_PERMISSIONDTO>> searchDefault(@RequestBody SYS_ROLE_PERMISSIONSearchContext context) {
        Page<SYS_ROLE_PERMISSION> domains = sys_role_permissionService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(sys_role_permissionMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}



    @ApiOperation(value = "GetBySYS_PERMISSION", tags = {"SYS_ROLE_PERMISSION" },  notes = "GetBySYS_PERMISSION")
	@RequestMapping(method = RequestMethod.GET, value = "/sys_permissions/{sys_permission_id}/sys_role_permissions/{sys_role_permission_id}")
    public ResponseEntity<SYS_ROLE_PERMISSIONDTO> getBySYS_PERMISSION(@PathVariable("sys_permission_id") String sys_permission_id, @PathVariable("sys_role_permission_id") String sys_role_permission_id) {
        SYS_ROLE_PERMISSION domain = sys_role_permissionService.get(sys_role_permission_id);
        SYS_ROLE_PERMISSIONDTO dto = sys_role_permissionMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "RemoveBySYS_PERMISSION", tags = {"SYS_ROLE_PERMISSION" },  notes = "RemoveBySYS_PERMISSION")
	@RequestMapping(method = RequestMethod.DELETE, value = "/sys_permissions/{sys_permission_id}/sys_role_permissions/{sys_role_permission_id}")
    @Transactional
    public ResponseEntity<Boolean> removeBySYS_PERMISSION(@PathVariable("sys_permission_id") String sys_permission_id, @PathVariable("sys_role_permission_id") String sys_role_permission_id) {
		return ResponseEntity.status(HttpStatus.OK).body(sys_role_permissionService.remove(sys_role_permission_id));
    }

    @ApiOperation(value = "RemoveBatchBySYS_PERMISSION", tags = {"SYS_ROLE_PERMISSION" },  notes = "RemoveBatchBySYS_PERMISSION")
	@RequestMapping(method = RequestMethod.DELETE, value = "/sys_permissions/{sys_permission_id}/sys_role_permissions/batch")
    public ResponseEntity<Boolean> removeBatchBySYS_PERMISSION(@RequestBody List<String> ids) {
        sys_role_permissionService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @ApiOperation(value = "GetDraftBySYS_PERMISSION", tags = {"SYS_ROLE_PERMISSION" },  notes = "GetDraftBySYS_PERMISSION")
    @RequestMapping(method = RequestMethod.GET, value = "/sys_permissions/{sys_permission_id}/sys_role_permissions/getdraft")
    public ResponseEntity<SYS_ROLE_PERMISSIONDTO> getDraftBySYS_PERMISSION(@PathVariable("sys_permission_id") String sys_permission_id) {
        SYS_ROLE_PERMISSION domain = new SYS_ROLE_PERMISSION();
        domain.setPermissionid(sys_permission_id);
        return ResponseEntity.status(HttpStatus.OK).body(sys_role_permissionMapping.toDto(sys_role_permissionService.getDraft(domain)));
    }

    @ApiOperation(value = "CreateBySYS_PERMISSION", tags = {"SYS_ROLE_PERMISSION" },  notes = "CreateBySYS_PERMISSION")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_permissions/{sys_permission_id}/sys_role_permissions")
    @Transactional
    public ResponseEntity<SYS_ROLE_PERMISSIONDTO> createBySYS_PERMISSION(@PathVariable("sys_permission_id") String sys_permission_id, @RequestBody SYS_ROLE_PERMISSIONDTO sys_role_permissiondto) {
        SYS_ROLE_PERMISSION domain = sys_role_permissionMapping.toDomain(sys_role_permissiondto);
        domain.setPermissionid(sys_permission_id);
		sys_role_permissionService.create(domain);
        SYS_ROLE_PERMISSIONDTO dto = sys_role_permissionMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "createBatchBySYS_PERMISSION", tags = {"SYS_ROLE_PERMISSION" },  notes = "createBatchBySYS_PERMISSION")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_permissions/{sys_permission_id}/sys_role_permissions/batch")
    public ResponseEntity<Boolean> createBatchBySYS_PERMISSION(@PathVariable("sys_permission_id") String sys_permission_id, @RequestBody List<SYS_ROLE_PERMISSIONDTO> sys_role_permissiondtos) {
        List<SYS_ROLE_PERMISSION> domainlist=sys_role_permissionMapping.toDomain(sys_role_permissiondtos);
        for(SYS_ROLE_PERMISSION domain:domainlist){
            domain.setPermissionid(sys_permission_id);
        }
        sys_role_permissionService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @ApiOperation(value = "CheckKeyBySYS_PERMISSION", tags = {"SYS_ROLE_PERMISSION" },  notes = "CheckKeyBySYS_PERMISSION")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_permissions/{sys_permission_id}/sys_role_permissions/checkkey")
    public ResponseEntity<Boolean> checkKeyBySYS_PERMISSION(@PathVariable("sys_permission_id") String sys_permission_id, @RequestBody SYS_ROLE_PERMISSIONDTO sys_role_permissiondto) {
        return  ResponseEntity.status(HttpStatus.OK).body(sys_role_permissionService.checkKey(sys_role_permissionMapping.toDomain(sys_role_permissiondto)));
    }

    @ApiOperation(value = "SaveBySYS_PERMISSION", tags = {"SYS_ROLE_PERMISSION" },  notes = "SaveBySYS_PERMISSION")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_permissions/{sys_permission_id}/sys_role_permissions/save")
    public ResponseEntity<Boolean> saveBySYS_PERMISSION(@PathVariable("sys_permission_id") String sys_permission_id, @RequestBody SYS_ROLE_PERMISSIONDTO sys_role_permissiondto) {
        SYS_ROLE_PERMISSION domain = sys_role_permissionMapping.toDomain(sys_role_permissiondto);
        domain.setPermissionid(sys_permission_id);
        return ResponseEntity.status(HttpStatus.OK).body(sys_role_permissionService.save(domain));
    }

    @ApiOperation(value = "SaveBatchBySYS_PERMISSION", tags = {"SYS_ROLE_PERMISSION" },  notes = "SaveBatchBySYS_PERMISSION")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_permissions/{sys_permission_id}/sys_role_permissions/savebatch")
    public ResponseEntity<Boolean> saveBatchBySYS_PERMISSION(@PathVariable("sys_permission_id") String sys_permission_id, @RequestBody List<SYS_ROLE_PERMISSIONDTO> sys_role_permissiondtos) {
        List<SYS_ROLE_PERMISSION> domainlist=sys_role_permissionMapping.toDomain(sys_role_permissiondtos);
        for(SYS_ROLE_PERMISSION domain:domainlist){
             domain.setPermissionid(sys_permission_id);
        }
        sys_role_permissionService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @ApiOperation(value = "UpdateBySYS_PERMISSION", tags = {"SYS_ROLE_PERMISSION" },  notes = "UpdateBySYS_PERMISSION")
	@RequestMapping(method = RequestMethod.PUT, value = "/sys_permissions/{sys_permission_id}/sys_role_permissions/{sys_role_permission_id}")
    @Transactional
    public ResponseEntity<SYS_ROLE_PERMISSIONDTO> updateBySYS_PERMISSION(@PathVariable("sys_permission_id") String sys_permission_id, @PathVariable("sys_role_permission_id") String sys_role_permission_id, @RequestBody SYS_ROLE_PERMISSIONDTO sys_role_permissiondto) {
        SYS_ROLE_PERMISSION domain = sys_role_permissionMapping.toDomain(sys_role_permissiondto);
        domain.setPermissionid(sys_permission_id);
        domain.setRolepermissionid(sys_role_permission_id);
		sys_role_permissionService.update(domain);
        SYS_ROLE_PERMISSIONDTO dto = sys_role_permissionMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "UpdateBatchBySYS_PERMISSION", tags = {"SYS_ROLE_PERMISSION" },  notes = "UpdateBatchBySYS_PERMISSION")
	@RequestMapping(method = RequestMethod.PUT, value = "/sys_permissions/{sys_permission_id}/sys_role_permissions/batch")
    public ResponseEntity<Boolean> updateBatchBySYS_PERMISSION(@PathVariable("sys_permission_id") String sys_permission_id, @RequestBody List<SYS_ROLE_PERMISSIONDTO> sys_role_permissiondtos) {
        List<SYS_ROLE_PERMISSION> domainlist=sys_role_permissionMapping.toDomain(sys_role_permissiondtos);
        for(SYS_ROLE_PERMISSION domain:domainlist){
            domain.setPermissionid(sys_permission_id);
        }
        sys_role_permissionService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

	@ApiOperation(value = "fetchDEFAULTBySYS_PERMISSION", tags = {"SYS_ROLE_PERMISSION" } ,notes = "fetchDEFAULTBySYS_PERMISSION")
    @RequestMapping(method= RequestMethod.GET , value="/sys_permissions/{sys_permission_id}/sys_role_permissions/fetchdefault")
	public ResponseEntity<List<SYS_ROLE_PERMISSIONDTO>> fetchSYS_ROLE_PERMISSIONDefaultBySYS_PERMISSION(@PathVariable("sys_permission_id") String sys_permission_id,SYS_ROLE_PERMISSIONSearchContext context) {
        context.setN_sys_permissionid_eq(sys_permission_id);
        Page<SYS_ROLE_PERMISSION> domains = sys_role_permissionService.searchDefault(context) ;
        List<SYS_ROLE_PERMISSIONDTO> list = sys_role_permissionMapping.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);
	}

	@ApiOperation(value = "searchDEFAULTBySYS_PERMISSION", tags = {"SYS_ROLE_PERMISSION" } ,notes = "searchDEFAULTBySYS_PERMISSION")
    @RequestMapping(method= RequestMethod.POST , value="/sys_permissions/{sys_permission_id}/sys_role_permissions/searchdefault")
	public ResponseEntity<Page<SYS_ROLE_PERMISSIONDTO>> searchSYS_ROLE_PERMISSIONDefaultBySYS_PERMISSION(@PathVariable("sys_permission_id") String sys_permission_id, @RequestBody SYS_ROLE_PERMISSIONSearchContext context) {
        context.setN_sys_permissionid_eq(sys_permission_id);
        Page<SYS_ROLE_PERMISSION> domains = sys_role_permissionService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(sys_role_permissionMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}



    @ApiOperation(value = "GetBySYS_ROLE", tags = {"SYS_ROLE_PERMISSION" },  notes = "GetBySYS_ROLE")
	@RequestMapping(method = RequestMethod.GET, value = "/sys_roles/{sys_role_id}/sys_role_permissions/{sys_role_permission_id}")
    public ResponseEntity<SYS_ROLE_PERMISSIONDTO> getBySYS_ROLE(@PathVariable("sys_role_id") String sys_role_id, @PathVariable("sys_role_permission_id") String sys_role_permission_id) {
        SYS_ROLE_PERMISSION domain = sys_role_permissionService.get(sys_role_permission_id);
        SYS_ROLE_PERMISSIONDTO dto = sys_role_permissionMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "RemoveBySYS_ROLE", tags = {"SYS_ROLE_PERMISSION" },  notes = "RemoveBySYS_ROLE")
	@RequestMapping(method = RequestMethod.DELETE, value = "/sys_roles/{sys_role_id}/sys_role_permissions/{sys_role_permission_id}")
    @Transactional
    public ResponseEntity<Boolean> removeBySYS_ROLE(@PathVariable("sys_role_id") String sys_role_id, @PathVariable("sys_role_permission_id") String sys_role_permission_id) {
		return ResponseEntity.status(HttpStatus.OK).body(sys_role_permissionService.remove(sys_role_permission_id));
    }

    @ApiOperation(value = "RemoveBatchBySYS_ROLE", tags = {"SYS_ROLE_PERMISSION" },  notes = "RemoveBatchBySYS_ROLE")
	@RequestMapping(method = RequestMethod.DELETE, value = "/sys_roles/{sys_role_id}/sys_role_permissions/batch")
    public ResponseEntity<Boolean> removeBatchBySYS_ROLE(@RequestBody List<String> ids) {
        sys_role_permissionService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @ApiOperation(value = "GetDraftBySYS_ROLE", tags = {"SYS_ROLE_PERMISSION" },  notes = "GetDraftBySYS_ROLE")
    @RequestMapping(method = RequestMethod.GET, value = "/sys_roles/{sys_role_id}/sys_role_permissions/getdraft")
    public ResponseEntity<SYS_ROLE_PERMISSIONDTO> getDraftBySYS_ROLE(@PathVariable("sys_role_id") String sys_role_id) {
        SYS_ROLE_PERMISSION domain = new SYS_ROLE_PERMISSION();
        domain.setRoleid(sys_role_id);
        return ResponseEntity.status(HttpStatus.OK).body(sys_role_permissionMapping.toDto(sys_role_permissionService.getDraft(domain)));
    }

    @ApiOperation(value = "CreateBySYS_ROLE", tags = {"SYS_ROLE_PERMISSION" },  notes = "CreateBySYS_ROLE")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_roles/{sys_role_id}/sys_role_permissions")
    @Transactional
    public ResponseEntity<SYS_ROLE_PERMISSIONDTO> createBySYS_ROLE(@PathVariable("sys_role_id") String sys_role_id, @RequestBody SYS_ROLE_PERMISSIONDTO sys_role_permissiondto) {
        SYS_ROLE_PERMISSION domain = sys_role_permissionMapping.toDomain(sys_role_permissiondto);
        domain.setRoleid(sys_role_id);
		sys_role_permissionService.create(domain);
        SYS_ROLE_PERMISSIONDTO dto = sys_role_permissionMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "createBatchBySYS_ROLE", tags = {"SYS_ROLE_PERMISSION" },  notes = "createBatchBySYS_ROLE")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_roles/{sys_role_id}/sys_role_permissions/batch")
    public ResponseEntity<Boolean> createBatchBySYS_ROLE(@PathVariable("sys_role_id") String sys_role_id, @RequestBody List<SYS_ROLE_PERMISSIONDTO> sys_role_permissiondtos) {
        List<SYS_ROLE_PERMISSION> domainlist=sys_role_permissionMapping.toDomain(sys_role_permissiondtos);
        for(SYS_ROLE_PERMISSION domain:domainlist){
            domain.setRoleid(sys_role_id);
        }
        sys_role_permissionService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @ApiOperation(value = "CheckKeyBySYS_ROLE", tags = {"SYS_ROLE_PERMISSION" },  notes = "CheckKeyBySYS_ROLE")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_roles/{sys_role_id}/sys_role_permissions/checkkey")
    public ResponseEntity<Boolean> checkKeyBySYS_ROLE(@PathVariable("sys_role_id") String sys_role_id, @RequestBody SYS_ROLE_PERMISSIONDTO sys_role_permissiondto) {
        return  ResponseEntity.status(HttpStatus.OK).body(sys_role_permissionService.checkKey(sys_role_permissionMapping.toDomain(sys_role_permissiondto)));
    }

    @ApiOperation(value = "SaveBySYS_ROLE", tags = {"SYS_ROLE_PERMISSION" },  notes = "SaveBySYS_ROLE")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_roles/{sys_role_id}/sys_role_permissions/save")
    public ResponseEntity<Boolean> saveBySYS_ROLE(@PathVariable("sys_role_id") String sys_role_id, @RequestBody SYS_ROLE_PERMISSIONDTO sys_role_permissiondto) {
        SYS_ROLE_PERMISSION domain = sys_role_permissionMapping.toDomain(sys_role_permissiondto);
        domain.setRoleid(sys_role_id);
        return ResponseEntity.status(HttpStatus.OK).body(sys_role_permissionService.save(domain));
    }

    @ApiOperation(value = "SaveBatchBySYS_ROLE", tags = {"SYS_ROLE_PERMISSION" },  notes = "SaveBatchBySYS_ROLE")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_roles/{sys_role_id}/sys_role_permissions/savebatch")
    public ResponseEntity<Boolean> saveBatchBySYS_ROLE(@PathVariable("sys_role_id") String sys_role_id, @RequestBody List<SYS_ROLE_PERMISSIONDTO> sys_role_permissiondtos) {
        List<SYS_ROLE_PERMISSION> domainlist=sys_role_permissionMapping.toDomain(sys_role_permissiondtos);
        for(SYS_ROLE_PERMISSION domain:domainlist){
             domain.setRoleid(sys_role_id);
        }
        sys_role_permissionService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @ApiOperation(value = "UpdateBySYS_ROLE", tags = {"SYS_ROLE_PERMISSION" },  notes = "UpdateBySYS_ROLE")
	@RequestMapping(method = RequestMethod.PUT, value = "/sys_roles/{sys_role_id}/sys_role_permissions/{sys_role_permission_id}")
    @Transactional
    public ResponseEntity<SYS_ROLE_PERMISSIONDTO> updateBySYS_ROLE(@PathVariable("sys_role_id") String sys_role_id, @PathVariable("sys_role_permission_id") String sys_role_permission_id, @RequestBody SYS_ROLE_PERMISSIONDTO sys_role_permissiondto) {
        SYS_ROLE_PERMISSION domain = sys_role_permissionMapping.toDomain(sys_role_permissiondto);
        domain.setRoleid(sys_role_id);
        domain.setRolepermissionid(sys_role_permission_id);
		sys_role_permissionService.update(domain);
        SYS_ROLE_PERMISSIONDTO dto = sys_role_permissionMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "UpdateBatchBySYS_ROLE", tags = {"SYS_ROLE_PERMISSION" },  notes = "UpdateBatchBySYS_ROLE")
	@RequestMapping(method = RequestMethod.PUT, value = "/sys_roles/{sys_role_id}/sys_role_permissions/batch")
    public ResponseEntity<Boolean> updateBatchBySYS_ROLE(@PathVariable("sys_role_id") String sys_role_id, @RequestBody List<SYS_ROLE_PERMISSIONDTO> sys_role_permissiondtos) {
        List<SYS_ROLE_PERMISSION> domainlist=sys_role_permissionMapping.toDomain(sys_role_permissiondtos);
        for(SYS_ROLE_PERMISSION domain:domainlist){
            domain.setRoleid(sys_role_id);
        }
        sys_role_permissionService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

	@ApiOperation(value = "fetchDEFAULTBySYS_ROLE", tags = {"SYS_ROLE_PERMISSION" } ,notes = "fetchDEFAULTBySYS_ROLE")
    @RequestMapping(method= RequestMethod.GET , value="/sys_roles/{sys_role_id}/sys_role_permissions/fetchdefault")
	public ResponseEntity<List<SYS_ROLE_PERMISSIONDTO>> fetchSYS_ROLE_PERMISSIONDefaultBySYS_ROLE(@PathVariable("sys_role_id") String sys_role_id,SYS_ROLE_PERMISSIONSearchContext context) {
        context.setN_sys_roleid_eq(sys_role_id);
        Page<SYS_ROLE_PERMISSION> domains = sys_role_permissionService.searchDefault(context) ;
        List<SYS_ROLE_PERMISSIONDTO> list = sys_role_permissionMapping.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);
	}

	@ApiOperation(value = "searchDEFAULTBySYS_ROLE", tags = {"SYS_ROLE_PERMISSION" } ,notes = "searchDEFAULTBySYS_ROLE")
    @RequestMapping(method= RequestMethod.POST , value="/sys_roles/{sys_role_id}/sys_role_permissions/searchdefault")
	public ResponseEntity<Page<SYS_ROLE_PERMISSIONDTO>> searchSYS_ROLE_PERMISSIONDefaultBySYS_ROLE(@PathVariable("sys_role_id") String sys_role_id, @RequestBody SYS_ROLE_PERMISSIONSearchContext context) {
        context.setN_sys_roleid_eq(sys_role_id);
        Page<SYS_ROLE_PERMISSION> domains = sys_role_permissionService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(sys_role_permissionMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}


    /**
     * 用户权限校验
     * @return
     */
	public SYS_ROLE_PERMISSION getEntity(){
        return new SYS_ROLE_PERMISSION();
    }

}
