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.SysAuthLog;
import cn.ibizlab.core.uaa.service.ISysAuthLogService;
import cn.ibizlab.core.uaa.filter.SysAuthLogSearchContext;

@Slf4j
@Api(tags = {"SysAuthLog" })
@RestController("api-sysauthlog")
@RequestMapping("")
public class SysAuthLogResource {

    @Autowired
    private ISysAuthLogService sysauthlogService;

    @Autowired
    @Lazy
    public SysAuthLogMapping sysauthlogMapping;

    public SysAuthLogDTO permissionDTO=new SysAuthLogDTO();

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

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysAuthLog-Update-all')")
    @ApiOperation(value = "Update", tags = {"SysAuthLog" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/sysauthlogs/{sysauthlog_id}")
    @Transactional
    public ResponseEntity<SysAuthLogDTO> update(@PathVariable("sysauthlog_id") String sysauthlog_id, @RequestBody SysAuthLogDTO sysauthlogdto) {
		SysAuthLog domain = sysauthlogMapping.toDomain(sysauthlogdto);
        domain.setLogid(sysauthlog_id);
		sysauthlogService.update(domain);
		SysAuthLogDTO dto = sysauthlogMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission('Update',{'Sql',this.humanMapping,#humandtos})")
    @ApiOperation(value = "UpdateBatch", tags = {"SysAuthLog" },  notes = "UpdateBatch")
	@RequestMapping(method = RequestMethod.PUT, value = "/sysauthlogs/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SysAuthLogDTO> sysauthlogdtos) {
        sysauthlogService.updateBatch(sysauthlogMapping.toDomain(sysauthlogdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

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

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysAuthLog-Create-all')")
    @ApiOperation(value = "Create", tags = {"SysAuthLog" },  notes = "Create")
	@RequestMapping(method = RequestMethod.POST, value = "/sysauthlogs")
    @Transactional
    public ResponseEntity<SysAuthLogDTO> create(@RequestBody SysAuthLogDTO sysauthlogdto) {
        SysAuthLog domain = sysauthlogMapping.toDomain(sysauthlogdto);
		sysauthlogService.create(domain);
        SysAuthLogDTO dto = sysauthlogMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission('Create',{'Sql',this.humanMapping,#humandtos})")
    @ApiOperation(value = "createBatch", tags = {"SysAuthLog" },  notes = "createBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sysauthlogs/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SysAuthLogDTO> sysauthlogdtos) {
        sysauthlogService.createBatch(sysauthlogMapping.toDomain(sysauthlogdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysAuthLog-CheckKey-all')")
    @ApiOperation(value = "CheckKey", tags = {"SysAuthLog" },  notes = "CheckKey")
	@RequestMapping(method = RequestMethod.POST, value = "/sysauthlogs/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody SysAuthLogDTO sysauthlogdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(sysauthlogService.checkKey(sysauthlogMapping.toDomain(sysauthlogdto)));
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysAuthLog-Get-all')")
    @ApiOperation(value = "Get", tags = {"SysAuthLog" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/sysauthlogs/{sysauthlog_id}")
    public ResponseEntity<SysAuthLogDTO> get(@PathVariable("sysauthlog_id") String sysauthlog_id) {
        SysAuthLog domain = sysauthlogService.get(sysauthlog_id);
        SysAuthLogDTO dto = sysauthlogMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysAuthLog-Save-all')")
    @ApiOperation(value = "Save", tags = {"SysAuthLog" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/sysauthlogs/save")
    public ResponseEntity<Boolean> save(@RequestBody SysAuthLogDTO sysauthlogdto) {
        return ResponseEntity.status(HttpStatus.OK).body(sysauthlogService.save(sysauthlogMapping.toDomain(sysauthlogdto)));
    }

    @PreAuthorize("hasPermission('Save',{'Sql',this.humanMapping,#humandtos})")
    @ApiOperation(value = "SaveBatch", tags = {"SysAuthLog" },  notes = "SaveBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sysauthlogs/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SysAuthLogDTO> sysauthlogdtos) {
        sysauthlogService.saveBatch(sysauthlogMapping.toDomain(sysauthlogdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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