package cn.ibizlab.uaaapi.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.uaaapi.dto.*;
import cn.ibizlab.uaaapi.mapping.*;
import cn.ibizlab.core.uaa.domain.SYS_ROLE;
import cn.ibizlab.core.uaa.service.ISYS_ROLEService;
import cn.ibizlab.core.uaa.filter.SYS_ROLESearchContext;




@Slf4j
@Api(tags = {"SYS_ROLE" })
@RestController("uaaapi-sys_role")
@RequestMapping("")
public class SYS_ROLEResource {

    @Autowired
    private ISYS_ROLEService sys_roleService;

    @Autowired
    @Lazy
    private SYS_ROLEMapping sys_roleMapping;




    @ApiOperation(value = "Save", tags = {"SYS_ROLE" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_roles/save")
    public ResponseEntity<Boolean> save(@RequestBody SYS_ROLEDTO sys_roledto) {
        return ResponseEntity.status(HttpStatus.OK).body(sys_roleService.save(sys_roleMapping.toDomain(sys_roledto)));
    }

    @ApiOperation(value = "SaveBatch", tags = {"SaveBatch" },  notes = "SaveBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_roles/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SYS_ROLEDTO> sys_roledtos) {
        sys_roleService.saveBatch(sys_roleMapping.toDomain(sys_roledtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @PreAuthorize("hasPermission(#sys_role_id,'UPDATE',this.getEntity())")
    @ApiOperation(value = "Update", tags = {"SYS_ROLE" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/sys_roles/{sys_role_id}")
    @Transactional
    public ResponseEntity<SYS_ROLEDTO> update(@PathVariable("sys_role_id") String sys_role_id, @RequestBody SYS_ROLEDTO sys_roledto) {
		SYS_ROLE domain = sys_roleMapping.toDomain(sys_roledto);
        domain.setRoleid(sys_role_id);
		sys_roleService.update(domain);
		SYS_ROLEDTO dto = sys_roleMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(#sys_role_id,'UPDATE',this.getEntity())")
    @ApiOperation(value = "UpdateBatch", tags = {"UpdateBatch" },  notes = "UpdateBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_roles/updatebatch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SYS_ROLEDTO> sys_roledtos) {
        sys_roleService.updateBatch(sys_roleMapping.toDomain(sys_roledtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @PreAuthorize("hasPermission('','CREATE',this.getEntity())")
    @ApiOperation(value = "Create", tags = {"SYS_ROLE" },  notes = "Create")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_roles")
    @Transactional
    public ResponseEntity<SYS_ROLEDTO> create(@RequestBody SYS_ROLEDTO sys_roledto) {
        SYS_ROLE domain = sys_roleMapping.toDomain(sys_roledto);
		sys_roleService.create(domain);
        SYS_ROLEDTO dto = sys_roleMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission('','CREATE',this.getEntity())")
    @ApiOperation(value = "createBatch", tags = {"createBatch" },  notes = "createBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_roles/createbatch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SYS_ROLEDTO> sys_roledtos) {
        sys_roleService.createBatch(sys_roleMapping.toDomain(sys_roledtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @PreAuthorize("hasPermission(#sys_role_id,'READ',this.getEntity())")
    @ApiOperation(value = "Get", tags = {"SYS_ROLE" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/sys_roles/{sys_role_id}")
    public ResponseEntity<SYS_ROLEDTO> get(@PathVariable("sys_role_id") String sys_role_id) {
        SYS_ROLE domain = sys_roleService.get(sys_role_id);
        SYS_ROLEDTO dto = sys_roleMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }




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

    @ApiOperation(value = "RemoveBatch", tags = {"RemoveBatch" },  notes = "RemoveBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_roles/removebatch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        sys_roleService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @PreAuthorize("hasPermission('','CREATE',this.getEntity())")
    @ApiOperation(value = "GetDraft", tags = {"SYS_ROLE" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/sys_roles/getdraft")
    public ResponseEntity<SYS_ROLEDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(sys_roleMapping.toDto(sys_roleService.getDraft(new SYS_ROLE())));
    }




    @ApiOperation(value = "CheckKey", tags = {"SYS_ROLE" },  notes = "CheckKey")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_roles/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody SYS_ROLEDTO sys_roledto) {
        return  ResponseEntity.status(HttpStatus.OK).body(sys_roleService.checkKey(sys_roleMapping.toDomain(sys_roledto)));
    }

    @PreAuthorize("hasPermission('READ',{#context,'Default',this.getEntity()})")
	@ApiOperation(value = "fetchDEFAULT", tags = {"SYS_ROLE" } ,notes = "fetchDEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/sys_roles/fetchdefault")
	public ResponseEntity<List<SYS_ROLEDTO>> fetchDefault(SYS_ROLESearchContext context) {
        Page<SYS_ROLE> domains = sys_roleService.searchDefault(context) ;
        List<SYS_ROLEDTO> list = sys_roleMapping.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('READ',{#context,'Default',this.getEntity()})")
	@ApiOperation(value = "searchDEFAULT", tags = {"SYS_ROLE" } ,notes = "searchDEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/sys_roles/searchdefault")
	public ResponseEntity<Page<SYS_ROLEDTO>> searchDefault(SYS_ROLESearchContext context) {
        Page<SYS_ROLE> domains = sys_roleService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(sys_roleMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}


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

}
