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.SysPermission;
import cn.ibizlab.core.uaa.service.ISysPermissionService;
import cn.ibizlab.core.uaa.filter.SysPermissionSearchContext;

@Slf4j
@Api(tags = {"SysPermission" })
@RestController("api-syspermission")
@RequestMapping("")
public class SysPermissionResource {

    @Autowired
    private ISysPermissionService syspermissionService;

    @Autowired
    @Lazy
    public SysPermissionMapping syspermissionMapping;

    public SysPermissionDTO permissionDTO=new SysPermissionDTO();

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysPermission-Save-all')")
    @ApiOperation(value = "Save", tags = {"SysPermission" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/syspermissions/save")
    public ResponseEntity<Boolean> save(@RequestBody SysPermissionDTO syspermissiondto) {
        return ResponseEntity.status(HttpStatus.OK).body(syspermissionService.save(syspermissionMapping.toDomain(syspermissiondto)));
    }

    @PreAuthorize("hasPermission('Save',{'Sql',this.humanMapping,#humandtos})")
    @ApiOperation(value = "SaveBatch", tags = {"SysPermission" },  notes = "SaveBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/syspermissions/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SysPermissionDTO> syspermissiondtos) {
        syspermissionService.saveBatch(syspermissionMapping.toDomain(syspermissiondtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysPermission-GetDraft-all')")
    @ApiOperation(value = "GetDraft", tags = {"SysPermission" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/syspermissions/getdraft")
    public ResponseEntity<SysPermissionDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(syspermissionMapping.toDto(syspermissionService.getDraft(new SysPermission())));
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysPermission-Remove-all')")
    @ApiOperation(value = "Remove", tags = {"SysPermission" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/syspermissions/{syspermission_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("syspermission_id") String syspermission_id) {
         return ResponseEntity.status(HttpStatus.OK).body(syspermissionService.remove(syspermission_id));
    }

    @PreAuthorize("hasPermission('Remove',{'Sql',this.humanMapping,this.permissionDTO,#ids})")
    @ApiOperation(value = "RemoveBatch", tags = {"SysPermission" },  notes = "RemoveBatch")
	@RequestMapping(method = RequestMethod.DELETE, value = "/syspermissions/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        syspermissionService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysPermission-Update-all')")
    @ApiOperation(value = "Update", tags = {"SysPermission" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/syspermissions/{syspermission_id}")
    @Transactional
    public ResponseEntity<SysPermissionDTO> update(@PathVariable("syspermission_id") String syspermission_id, @RequestBody SysPermissionDTO syspermissiondto) {
		SysPermission domain = syspermissionMapping.toDomain(syspermissiondto);
        domain.setPermissionid(syspermission_id);
		syspermissionService.update(domain);
		SysPermissionDTO dto = syspermissionMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission('Update',{'Sql',this.humanMapping,#humandtos})")
    @ApiOperation(value = "UpdateBatch", tags = {"SysPermission" },  notes = "UpdateBatch")
	@RequestMapping(method = RequestMethod.PUT, value = "/syspermissions/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SysPermissionDTO> syspermissiondtos) {
        syspermissionService.updateBatch(syspermissionMapping.toDomain(syspermissiondtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysPermission-Create-all')")
    @ApiOperation(value = "Create", tags = {"SysPermission" },  notes = "Create")
	@RequestMapping(method = RequestMethod.POST, value = "/syspermissions")
    @Transactional
    public ResponseEntity<SysPermissionDTO> create(@RequestBody SysPermissionDTO syspermissiondto) {
        SysPermission domain = syspermissionMapping.toDomain(syspermissiondto);
		syspermissionService.create(domain);
        SysPermissionDTO dto = syspermissionMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission('Create',{'Sql',this.humanMapping,#humandtos})")
    @ApiOperation(value = "createBatch", tags = {"SysPermission" },  notes = "createBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/syspermissions/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SysPermissionDTO> syspermissiondtos) {
        syspermissionService.createBatch(syspermissionMapping.toDomain(syspermissiondtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysPermission-CheckKey-all')")
    @ApiOperation(value = "CheckKey", tags = {"SysPermission" },  notes = "CheckKey")
	@RequestMapping(method = RequestMethod.POST, value = "/syspermissions/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody SysPermissionDTO syspermissiondto) {
        return  ResponseEntity.status(HttpStatus.OK).body(syspermissionService.checkKey(syspermissionMapping.toDomain(syspermissiondto)));
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysPermission-Get-all')")
    @ApiOperation(value = "Get", tags = {"SysPermission" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/syspermissions/{syspermission_id}")
    public ResponseEntity<SysPermissionDTO> get(@PathVariable("syspermission_id") String syspermission_id) {
        SysPermission domain = syspermissionService.get(syspermission_id);
        SysPermissionDTO dto = syspermissionMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysPermission-Default-all')")
	@ApiOperation(value = "fetchDEFAULT", tags = {"SysPermission" } ,notes = "fetchDEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/syspermissions/fetchdefault")
	public ResponseEntity<List<SysPermissionDTO>> fetchDefault(SysPermissionSearchContext context) {
        Page<SysPermission> domains = syspermissionService.searchDefault(context) ;
        List<SysPermissionDTO> list = syspermissionMapping.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("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysPermission-Default-all')")
	@ApiOperation(value = "searchDEFAULT", tags = {"SysPermission" } ,notes = "searchDEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/syspermissions/searchdefault")
	public ResponseEntity<Page<SysPermissionDTO>> searchDefault(@RequestBody SysPermissionSearchContext context) {
        Page<SysPermission> domains = syspermissionService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(syspermissionMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}
