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.SysAppMenuItem;
import cn.ibizlab.core.uaa.service.ISysAppMenuItemService;
import cn.ibizlab.core.uaa.filter.SysAppMenuItemSearchContext;

@Slf4j
@Api(tags = {"SysAppMenuItem" })
@RestController("api-sysappmenuitem")
@RequestMapping("")
public class SysAppMenuItemResource {

    @Autowired
    private ISysAppMenuItemService sysappmenuitemService;

    @Autowired
    @Lazy
    public SysAppMenuItemMapping sysappmenuitemMapping;

    public SysAppMenuItemDTO permissionDTO=new SysAppMenuItemDTO();

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysAppMenuItem-Save-all')")
    @ApiOperation(value = "Save", tags = {"SysAppMenuItem" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/sysappmenuitems/save")
    public ResponseEntity<Boolean> save(@RequestBody SysAppMenuItemDTO sysappmenuitemdto) {
        return ResponseEntity.status(HttpStatus.OK).body(sysappmenuitemService.save(sysappmenuitemMapping.toDomain(sysappmenuitemdto)));
    }

    @PreAuthorize("hasPermission('Save',{'Sql',this.humanMapping,#humandtos})")
    @ApiOperation(value = "SaveBatch", tags = {"SysAppMenuItem" },  notes = "SaveBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sysappmenuitems/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SysAppMenuItemDTO> sysappmenuitemdtos) {
        sysappmenuitemService.saveBatch(sysappmenuitemMapping.toDomain(sysappmenuitemdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysAppMenuItem-Create-all')")
    @ApiOperation(value = "Create", tags = {"SysAppMenuItem" },  notes = "Create")
	@RequestMapping(method = RequestMethod.POST, value = "/sysappmenuitems")
    @Transactional
    public ResponseEntity<SysAppMenuItemDTO> create(@RequestBody SysAppMenuItemDTO sysappmenuitemdto) {
        SysAppMenuItem domain = sysappmenuitemMapping.toDomain(sysappmenuitemdto);
		sysappmenuitemService.create(domain);
        SysAppMenuItemDTO dto = sysappmenuitemMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission('Create',{'Sql',this.humanMapping,#humandtos})")
    @ApiOperation(value = "createBatch", tags = {"SysAppMenuItem" },  notes = "createBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sysappmenuitems/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SysAppMenuItemDTO> sysappmenuitemdtos) {
        sysappmenuitemService.createBatch(sysappmenuitemMapping.toDomain(sysappmenuitemdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysAppMenuItem-Update-all')")
    @ApiOperation(value = "Update", tags = {"SysAppMenuItem" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/sysappmenuitems/{sysappmenuitem_id}")
    @Transactional
    public ResponseEntity<SysAppMenuItemDTO> update(@PathVariable("sysappmenuitem_id") String sysappmenuitem_id, @RequestBody SysAppMenuItemDTO sysappmenuitemdto) {
		SysAppMenuItem domain = sysappmenuitemMapping.toDomain(sysappmenuitemdto);
        domain.setAppmenuitemid(sysappmenuitem_id);
		sysappmenuitemService.update(domain);
		SysAppMenuItemDTO dto = sysappmenuitemMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission('Update',{'Sql',this.humanMapping,#humandtos})")
    @ApiOperation(value = "UpdateBatch", tags = {"SysAppMenuItem" },  notes = "UpdateBatch")
	@RequestMapping(method = RequestMethod.PUT, value = "/sysappmenuitems/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SysAppMenuItemDTO> sysappmenuitemdtos) {
        sysappmenuitemService.updateBatch(sysappmenuitemMapping.toDomain(sysappmenuitemdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysAppMenuItem-CheckKey-all')")
    @ApiOperation(value = "CheckKey", tags = {"SysAppMenuItem" },  notes = "CheckKey")
	@RequestMapping(method = RequestMethod.POST, value = "/sysappmenuitems/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody SysAppMenuItemDTO sysappmenuitemdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(sysappmenuitemService.checkKey(sysappmenuitemMapping.toDomain(sysappmenuitemdto)));
    }

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

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

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','ibzuaa-SysAppMenuItem-Get-all')")
    @ApiOperation(value = "Get", tags = {"SysAppMenuItem" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/sysappmenuitems/{sysappmenuitem_id}")
    public ResponseEntity<SysAppMenuItemDTO> get(@PathVariable("sysappmenuitem_id") String sysappmenuitem_id) {
        SysAppMenuItem domain = sysappmenuitemService.get(sysappmenuitem_id);
        SysAppMenuItemDTO dto = sysappmenuitemMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

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