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.EMDRWG1;
import cn.ibizlab.eam.core.eam_core.service.IEMDRWG1Service;
import cn.ibizlab.eam.core.eam_core.filter.EMDRWG1SearchContext;




@Slf4j
@Api(tags = {"EMDRWG1" })
@RestController("WebApi-emdrwg1")
@RequestMapping("")
public class EMDRWG1Resource {

    @Autowired
    private IEMDRWG1Service emdrwg1Service;

    @Autowired
    @Lazy
    private EMDRWG1Mapping emdrwg1Mapping;




    @PreAuthorize("hasPermission(#emdrwg1_id,'Get',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Get", tags = {"EMDRWG1" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/emdrwg1s/{emdrwg1_id}")
    public ResponseEntity<EMDRWG1DTO> get(@PathVariable("emdrwg1_id") String emdrwg1_id) {
        EMDRWG1 domain = emdrwg1Service.get(emdrwg1_id);
        EMDRWG1DTO dto = emdrwg1Mapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }




    @PreAuthorize("hasPermission(#emdrwg1_id,'Update',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Update", tags = {"EMDRWG1" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/emdrwg1s/{emdrwg1_id}")
    @Transactional
    public ResponseEntity<EMDRWG1DTO> update(@PathVariable("emdrwg1_id") String emdrwg1_id, @RequestBody EMDRWG1DTO emdrwg1dto) {
		EMDRWG1 domain = emdrwg1Mapping.toDomain(emdrwg1dto);
        domain.setEmdrwg1id(emdrwg1_id);
		emdrwg1Service.update(domain);
		EMDRWG1DTO dto = emdrwg1Mapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(#emdrwg1_id,'Update',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "UpdateBatch", tags = {"EMDRWG1" },  notes = "UpdateBatch")
	@RequestMapping(method = RequestMethod.PUT, value = "/emdrwg1s/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EMDRWG1DTO> emdrwg1dtos) {
        emdrwg1Service.updateBatch(emdrwg1Mapping.toDomain(emdrwg1dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




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

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




    @ApiOperation(value = "Save", tags = {"EMDRWG1" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/emdrwg1s/save")
    public ResponseEntity<Boolean> save(@RequestBody EMDRWG1DTO emdrwg1dto) {
        return ResponseEntity.status(HttpStatus.OK).body(emdrwg1Service.save(emdrwg1Mapping.toDomain(emdrwg1dto)));
    }

    @ApiOperation(value = "SaveBatch", tags = {"EMDRWG1" },  notes = "SaveBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/emdrwg1s/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EMDRWG1DTO> emdrwg1dtos) {
        emdrwg1Service.saveBatch(emdrwg1Mapping.toDomain(emdrwg1dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @PreAuthorize("hasPermission('','Create',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Create", tags = {"EMDRWG1" },  notes = "Create")
	@RequestMapping(method = RequestMethod.POST, value = "/emdrwg1s")
    @Transactional
    public ResponseEntity<EMDRWG1DTO> create(@RequestBody EMDRWG1DTO emdrwg1dto) {
        EMDRWG1 domain = emdrwg1Mapping.toDomain(emdrwg1dto);
		emdrwg1Service.create(domain);
        EMDRWG1DTO dto = emdrwg1Mapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }
    @PreAuthorize("hasPermission('','Create',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "createBatch", tags = {"EMDRWG1" },  notes = "createBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/emdrwg1s/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EMDRWG1DTO> emdrwg1dtos) {
        emdrwg1Service.createBatch(emdrwg1Mapping.toDomain(emdrwg1dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @ApiOperation(value = "GetDraft", tags = {"EMDRWG1" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/emdrwg1s/getdraft")
    public ResponseEntity<EMDRWG1DTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(emdrwg1Mapping.toDto(emdrwg1Service.getDraft(new EMDRWG1())));
    }




    @ApiOperation(value = "CheckKey", tags = {"EMDRWG1" },  notes = "CheckKey")
	@RequestMapping(method = RequestMethod.POST, value = "/emdrwg1s/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EMDRWG1DTO emdrwg1dto) {
        return  ResponseEntity.status(HttpStatus.OK).body(emdrwg1Service.checkKey(emdrwg1Mapping.toDomain(emdrwg1dto)));
    }

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


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

}
