package cn.ibizlab.eam.webapi.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.eam.webapi.dto.*;
import cn.ibizlab.eam.webapi.mapping.*;
import cn.ibizlab.eam.core.eam_core.domain.EMWOORI;
import cn.ibizlab.eam.core.eam_core.service.IEMWOORIService;
import cn.ibizlab.eam.core.eam_core.filter.EMWOORISearchContext;




@Slf4j
@Api(tags = {"EMWOORI" })
@RestController("WebApi-emwoori")
@RequestMapping("")
public class EMWOORIResource {

    @Autowired
    private IEMWOORIService emwooriService;

    @Autowired
    @Lazy
    private EMWOORIMapping emwooriMapping;




    @ApiOperation(value = "GetDraft", tags = {"EMWOORI" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/emwooris/getdraft")
    public ResponseEntity<EMWOORIDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(emwooriMapping.toDto(emwooriService.getDraft(new EMWOORI())));
    }




    @PreAuthorize("hasPermission('','Create',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Create", tags = {"EMWOORI" },  notes = "Create")
	@RequestMapping(method = RequestMethod.POST, value = "/emwooris")
    @Transactional
    public ResponseEntity<EMWOORIDTO> create(@RequestBody EMWOORIDTO emwooridto) {
        EMWOORI domain = emwooriMapping.toDomain(emwooridto);
		emwooriService.create(domain);
        EMWOORIDTO dto = emwooriMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }
    @PreAuthorize("hasPermission('','Create',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "createBatch", tags = {"EMWOORI" },  notes = "createBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/emwooris/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EMWOORIDTO> emwooridtos) {
        emwooriService.createBatch(emwooriMapping.toDomain(emwooridtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @PreAuthorize("hasPermission(#emwoori_id,'Get',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Get", tags = {"EMWOORI" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/emwooris/{emwoori_id}")
    public ResponseEntity<EMWOORIDTO> get(@PathVariable("emwoori_id") String emwoori_id) {
        EMWOORI domain = emwooriService.get(emwoori_id);
        EMWOORIDTO dto = emwooriMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }




    @ApiOperation(value = "CheckKey", tags = {"EMWOORI" },  notes = "CheckKey")
	@RequestMapping(method = RequestMethod.POST, value = "/emwooris/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EMWOORIDTO emwooridto) {
        return  ResponseEntity.status(HttpStatus.OK).body(emwooriService.checkKey(emwooriMapping.toDomain(emwooridto)));
    }




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

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




    @ApiOperation(value = "Save", tags = {"EMWOORI" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/emwooris/save")
    public ResponseEntity<Boolean> save(@RequestBody EMWOORIDTO emwooridto) {
        return ResponseEntity.status(HttpStatus.OK).body(emwooriService.save(emwooriMapping.toDomain(emwooridto)));
    }

    @ApiOperation(value = "SaveBatch", tags = {"EMWOORI" },  notes = "SaveBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/emwooris/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EMWOORIDTO> emwooridtos) {
        emwooriService.saveBatch(emwooriMapping.toDomain(emwooridtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @PreAuthorize("hasPermission(#emwoori_id,'Update',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Update", tags = {"EMWOORI" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/emwooris/{emwoori_id}")
    @Transactional
    public ResponseEntity<EMWOORIDTO> update(@PathVariable("emwoori_id") String emwoori_id, @RequestBody EMWOORIDTO emwooridto) {
		EMWOORI domain = emwooriMapping.toDomain(emwooridto);
        domain.setEmwooriid(emwoori_id);
		emwooriService.update(domain);
		EMWOORIDTO dto = emwooriMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(#emwoori_id,'Update',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "UpdateBatch", tags = {"EMWOORI" },  notes = "UpdateBatch")
	@RequestMapping(method = RequestMethod.PUT, value = "/emwooris/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EMWOORIDTO> emwooridtos) {
        emwooriService.updateBatch(emwooriMapping.toDomain(emwooridtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','eam_practice02-EMWOORI-IndexDER-all')")
	@ApiOperation(value = "fetchIndexDER", tags = {"EMWOORI" } ,notes = "fetchIndexDER")
    @RequestMapping(method= RequestMethod.GET , value="/emwooris/fetchindexder")
	public ResponseEntity<List<EMWOORIDTO>> fetchIndexDER(EMWOORISearchContext context) {
        Page<EMWOORI> domains = emwooriService.searchIndexDER(context) ;
        List<EMWOORIDTO> list = emwooriMapping.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','eam_practice02-EMWOORI-IndexDER-all')")
	@ApiOperation(value = "searchIndexDER", tags = {"EMWOORI" } ,notes = "searchIndexDER")
    @RequestMapping(method= RequestMethod.POST , value="/emwooris/searchindexder")
	public ResponseEntity<Page<EMWOORIDTO>> searchIndexDER(@RequestBody EMWOORISearchContext context) {
        Page<EMWOORI> domains = emwooriService.searchIndexDER(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(emwooriMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}


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

}
