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.SysOperatorPriv;
import cn.ibizlab.core.uaa.service.ISysOperatorPrivService;
import cn.ibizlab.core.uaa.filter.SysOperatorPrivSearchContext;

@Slf4j
@Api(tags = {"SysOperatorPriv" })
@RestController("api-sysoperatorpriv")
@RequestMapping("")
public class SysOperatorPrivResource {

    @Autowired
    private ISysOperatorPrivService sysoperatorprivService;

    @Autowired
    @Lazy
    public SysOperatorPrivMapping sysoperatorprivMapping;

    public SysOperatorPrivDTO permissionDTO=new SysOperatorPrivDTO();

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

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

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysOperatorPriv-CheckKey-all')")
    @ApiOperation(value = "CheckKey", tags = {"SysOperatorPriv" },  notes = "CheckKey")
	@RequestMapping(method = RequestMethod.POST, value = "/sysoperatorprivs/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody SysOperatorPrivDTO sysoperatorprivdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(sysoperatorprivService.checkKey(sysoperatorprivMapping.toDomain(sysoperatorprivdto)));
    }

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

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysOperatorPriv-Create-all')")
    @ApiOperation(value = "Create", tags = {"SysOperatorPriv" },  notes = "Create")
	@RequestMapping(method = RequestMethod.POST, value = "/sysoperatorprivs")
    @Transactional
    public ResponseEntity<SysOperatorPrivDTO> create(@RequestBody SysOperatorPrivDTO sysoperatorprivdto) {
        SysOperatorPriv domain = sysoperatorprivMapping.toDomain(sysoperatorprivdto);
		sysoperatorprivService.create(domain);
        SysOperatorPrivDTO dto = sysoperatorprivMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission('Create',{'Sql',this.humanMapping,#humandtos})")
    @ApiOperation(value = "createBatch", tags = {"SysOperatorPriv" },  notes = "createBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sysoperatorprivs/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SysOperatorPrivDTO> sysoperatorprivdtos) {
        sysoperatorprivService.createBatch(sysoperatorprivMapping.toDomain(sysoperatorprivdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysOperatorPriv-Get-all')")
    @ApiOperation(value = "Get", tags = {"SysOperatorPriv" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/sysoperatorprivs/{sysoperatorpriv_id}")
    public ResponseEntity<SysOperatorPrivDTO> get(@PathVariable("sysoperatorpriv_id") String sysoperatorpriv_id) {
        SysOperatorPriv domain = sysoperatorprivService.get(sysoperatorpriv_id);
        SysOperatorPrivDTO dto = sysoperatorprivMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysOperatorPriv-Update-all')")
    @ApiOperation(value = "Update", tags = {"SysOperatorPriv" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/sysoperatorprivs/{sysoperatorpriv_id}")
    @Transactional
    public ResponseEntity<SysOperatorPrivDTO> update(@PathVariable("sysoperatorpriv_id") String sysoperatorpriv_id, @RequestBody SysOperatorPrivDTO sysoperatorprivdto) {
		SysOperatorPriv domain = sysoperatorprivMapping.toDomain(sysoperatorprivdto);
        domain.setOperatorprivid(sysoperatorpriv_id);
		sysoperatorprivService.update(domain);
		SysOperatorPrivDTO dto = sysoperatorprivMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission('Update',{'Sql',this.humanMapping,#humandtos})")
    @ApiOperation(value = "UpdateBatch", tags = {"SysOperatorPriv" },  notes = "UpdateBatch")
	@RequestMapping(method = RequestMethod.PUT, value = "/sysoperatorprivs/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SysOperatorPrivDTO> sysoperatorprivdtos) {
        sysoperatorprivService.updateBatch(sysoperatorprivMapping.toDomain(sysoperatorprivdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysOperatorPriv-Save-all')")
    @ApiOperation(value = "Save", tags = {"SysOperatorPriv" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/sysoperatorprivs/save")
    public ResponseEntity<Boolean> save(@RequestBody SysOperatorPrivDTO sysoperatorprivdto) {
        return ResponseEntity.status(HttpStatus.OK).body(sysoperatorprivService.save(sysoperatorprivMapping.toDomain(sysoperatorprivdto)));
    }

    @PreAuthorize("hasPermission('Save',{'Sql',this.humanMapping,#humandtos})")
    @ApiOperation(value = "SaveBatch", tags = {"SysOperatorPriv" },  notes = "SaveBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sysoperatorprivs/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SysOperatorPrivDTO> sysoperatorprivdtos) {
        sysoperatorprivService.saveBatch(sysoperatorprivMapping.toDomain(sysoperatorprivdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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