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_PSDEOPPRIV;
import cn.ibizlab.core.uaa.service.ISYS_PSDEOPPRIVService;
import cn.ibizlab.core.uaa.filter.SYS_PSDEOPPRIVSearchContext;




@Slf4j
@Api(tags = {"SYS_PSDEOPPRIV" })
@RestController("uaaapi-sys_psdeoppriv")
@RequestMapping("")
public class SYS_PSDEOPPRIVResource {

    @Autowired
    private ISYS_PSDEOPPRIVService sys_psdeopprivService;

    @Autowired
    @Lazy
    private SYS_PSDEOPPRIVMapping sys_psdeopprivMapping;




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

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




    @ApiOperation(value = "CheckKey", tags = {"SYS_PSDEOPPRIV" },  notes = "CheckKey")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_psdeopprivs/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody SYS_PSDEOPPRIVDTO sys_psdeopprivdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(sys_psdeopprivService.checkKey(sys_psdeopprivMapping.toDomain(sys_psdeopprivdto)));
    }




    @PreAuthorize("hasPermission('','CREATE',this.getEntity())")
    @ApiOperation(value = "GetDraft", tags = {"SYS_PSDEOPPRIV" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/sys_psdeopprivs/getdraft")
    public ResponseEntity<SYS_PSDEOPPRIVDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(sys_psdeopprivMapping.toDto(sys_psdeopprivService.getDraft(new SYS_PSDEOPPRIV())));
    }




    @PreAuthorize("hasPermission('','CREATE',this.getEntity())")
    @ApiOperation(value = "Create", tags = {"SYS_PSDEOPPRIV" },  notes = "Create")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_psdeopprivs")
    @Transactional
    public ResponseEntity<SYS_PSDEOPPRIVDTO> create(@RequestBody SYS_PSDEOPPRIVDTO sys_psdeopprivdto) {
        SYS_PSDEOPPRIV domain = sys_psdeopprivMapping.toDomain(sys_psdeopprivdto);
		sys_psdeopprivService.create(domain);
        SYS_PSDEOPPRIVDTO dto = sys_psdeopprivMapping.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_psdeopprivs/createbatch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SYS_PSDEOPPRIVDTO> sys_psdeopprivdtos) {
        sys_psdeopprivService.createBatch(sys_psdeopprivMapping.toDomain(sys_psdeopprivdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @PreAuthorize("hasPermission(#sys_psdeoppriv_id,'READ',this.getEntity())")
    @ApiOperation(value = "Get", tags = {"SYS_PSDEOPPRIV" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/sys_psdeopprivs/{sys_psdeoppriv_id}")
    public ResponseEntity<SYS_PSDEOPPRIVDTO> get(@PathVariable("sys_psdeoppriv_id") String sys_psdeoppriv_id) {
        SYS_PSDEOPPRIV domain = sys_psdeopprivService.get(sys_psdeoppriv_id);
        SYS_PSDEOPPRIVDTO dto = sys_psdeopprivMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }




    @PreAuthorize("hasPermission(#sys_psdeoppriv_id,'UPDATE',this.getEntity())")
    @ApiOperation(value = "Update", tags = {"SYS_PSDEOPPRIV" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/sys_psdeopprivs/{sys_psdeoppriv_id}")
    @Transactional
    public ResponseEntity<SYS_PSDEOPPRIVDTO> update(@PathVariable("sys_psdeoppriv_id") String sys_psdeoppriv_id, @RequestBody SYS_PSDEOPPRIVDTO sys_psdeopprivdto) {
		SYS_PSDEOPPRIV domain = sys_psdeopprivMapping.toDomain(sys_psdeopprivdto);
        domain.setPsdeopprivid(sys_psdeoppriv_id);
		sys_psdeopprivService.update(domain);
		SYS_PSDEOPPRIVDTO dto = sys_psdeopprivMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(#sys_psdeoppriv_id,'UPDATE',this.getEntity())")
    @ApiOperation(value = "UpdateBatch", tags = {"UpdateBatch" },  notes = "UpdateBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_psdeopprivs/updatebatch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SYS_PSDEOPPRIVDTO> sys_psdeopprivdtos) {
        sys_psdeopprivService.updateBatch(sys_psdeopprivMapping.toDomain(sys_psdeopprivdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @ApiOperation(value = "Save", tags = {"SYS_PSDEOPPRIV" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_psdeopprivs/save")
    public ResponseEntity<Boolean> save(@RequestBody SYS_PSDEOPPRIVDTO sys_psdeopprivdto) {
        return ResponseEntity.status(HttpStatus.OK).body(sys_psdeopprivService.save(sys_psdeopprivMapping.toDomain(sys_psdeopprivdto)));
    }

    @ApiOperation(value = "SaveBatch", tags = {"SaveBatch" },  notes = "SaveBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_psdeopprivs/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SYS_PSDEOPPRIVDTO> sys_psdeopprivdtos) {
        sys_psdeopprivService.saveBatch(sys_psdeopprivMapping.toDomain(sys_psdeopprivdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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


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

}
