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_PERMISSION;
import cn.ibizlab.core.uaa.service.ISYS_PERMISSIONService;
import cn.ibizlab.core.uaa.filter.SYS_PERMISSIONSearchContext;




@Slf4j
@Api(tags = {"SYS_PERMISSION" })
@RestController("api-sys_permission")
@RequestMapping("")
public class SYS_PERMISSIONResource {

    @Autowired
    private ISYS_PERMISSIONService sys_permissionService;

    @Autowired
    @Lazy
    private SYS_PERMISSIONMapping sys_permissionMapping;




    @ApiOperation(value = "Save", tags = {"SYS_PERMISSION" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_permissions/save")
    public ResponseEntity<Boolean> save(@RequestBody SYS_PERMISSIONDTO sys_permissiondto) {
        return ResponseEntity.status(HttpStatus.OK).body(sys_permissionService.save(sys_permissionMapping.toDomain(sys_permissiondto)));
    }

    @ApiOperation(value = "SaveBatch", tags = {"SYS_PERMISSION" },  notes = "SaveBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_permissions/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SYS_PERMISSIONDTO> sys_permissiondtos) {
        sys_permissionService.saveBatch(sys_permissionMapping.toDomain(sys_permissiondtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @ApiOperation(value = "GetDraft", tags = {"SYS_PERMISSION" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/sys_permissions/getdraft")
    public ResponseEntity<SYS_PERMISSIONDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(sys_permissionMapping.toDto(sys_permissionService.getDraft(new SYS_PERMISSION())));
    }




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

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




    @PreAuthorize("hasPermission(#sys_permission_id,'Update',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Update", tags = {"SYS_PERMISSION" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/sys_permissions/{sys_permission_id}")
    @Transactional
    public ResponseEntity<SYS_PERMISSIONDTO> update(@PathVariable("sys_permission_id") String sys_permission_id, @RequestBody SYS_PERMISSIONDTO sys_permissiondto) {
		SYS_PERMISSION domain = sys_permissionMapping.toDomain(sys_permissiondto);
        domain.setPermissionid(sys_permission_id);
		sys_permissionService.update(domain);
		SYS_PERMISSIONDTO dto = sys_permissionMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(#sys_permission_id,'Update',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "UpdateBatch", tags = {"SYS_PERMISSION" },  notes = "UpdateBatch")
	@RequestMapping(method = RequestMethod.PUT, value = "/sys_permissions/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SYS_PERMISSIONDTO> sys_permissiondtos) {
        sys_permissionService.updateBatch(sys_permissionMapping.toDomain(sys_permissiondtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @PreAuthorize("hasPermission('','Create',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Create", tags = {"SYS_PERMISSION" },  notes = "Create")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_permissions")
    @Transactional
    public ResponseEntity<SYS_PERMISSIONDTO> create(@RequestBody SYS_PERMISSIONDTO sys_permissiondto) {
        SYS_PERMISSION domain = sys_permissionMapping.toDomain(sys_permissiondto);
		sys_permissionService.create(domain);
        SYS_PERMISSIONDTO dto = sys_permissionMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission('','Create',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "createBatch", tags = {"SYS_PERMISSION" },  notes = "createBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_permissions/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SYS_PERMISSIONDTO> sys_permissiondtos) {
        sys_permissionService.createBatch(sys_permissionMapping.toDomain(sys_permissiondtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @ApiOperation(value = "CheckKey", tags = {"SYS_PERMISSION" },  notes = "CheckKey")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_permissions/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody SYS_PERMISSIONDTO sys_permissiondto) {
        return  ResponseEntity.status(HttpStatus.OK).body(sys_permissionService.checkKey(sys_permissionMapping.toDomain(sys_permissiondto)));
    }




    @PreAuthorize("hasPermission(#sys_permission_id,'Get',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Get", tags = {"SYS_PERMISSION" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/sys_permissions/{sys_permission_id}")
    public ResponseEntity<SYS_PERMISSIONDTO> get(@PathVariable("sys_permission_id") String sys_permission_id) {
        SYS_PERMISSION domain = sys_permissionService.get(sys_permission_id);
        SYS_PERMISSIONDTO dto = sys_permissionMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission('Get',{#context,'Default',this.getEntity(),'Sql'})")
	@ApiOperation(value = "fetchDEFAULT", tags = {"SYS_PERMISSION" } ,notes = "fetchDEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/sys_permissions/fetchdefault")
	public ResponseEntity<List<SYS_PERMISSIONDTO>> fetchDefault(SYS_PERMISSIONSearchContext context) {
        Page<SYS_PERMISSION> domains = sys_permissionService.searchDefault(context) ;
        List<SYS_PERMISSIONDTO> list = sys_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_PERMISSION" } ,notes = "searchDEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/sys_permissions/searchdefault")
	public ResponseEntity<Page<SYS_PERMISSIONDTO>> searchDefault(@RequestBody SYS_PERMISSIONSearchContext context) {
        Page<SYS_PERMISSION> domains = sys_permissionService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(sys_permissionMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}


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

}
