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.SYS_PSAPPMENUITEM;
import cn.ibizlab.core.uaa.service.ISYS_PSAPPMENUITEMService;
import cn.ibizlab.core.uaa.filter.SYS_PSAPPMENUITEMSearchContext;




@Slf4j
@Api(tags = {"SYS_PSAPPMENUITEM" })
@RestController("api-sys_psappmenuitem")
@RequestMapping("")
public class SYS_PSAPPMENUITEMResource {

    @Autowired
    private ISYS_PSAPPMENUITEMService sys_psappmenuitemService;

    @Autowired
    @Lazy
    private SYS_PSAPPMENUITEMMapping sys_psappmenuitemMapping;




    @ApiOperation(value = "Save", tags = {"SYS_PSAPPMENUITEM" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_psappmenuitems/save")
    public ResponseEntity<Boolean> save(@RequestBody SYS_PSAPPMENUITEMDTO sys_psappmenuitemdto) {
        return ResponseEntity.status(HttpStatus.OK).body(sys_psappmenuitemService.save(sys_psappmenuitemMapping.toDomain(sys_psappmenuitemdto)));
    }

    @ApiOperation(value = "SaveBatch", tags = {"SYS_PSAPPMENUITEM" },  notes = "SaveBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_psappmenuitems/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SYS_PSAPPMENUITEMDTO> sys_psappmenuitemdtos) {
        sys_psappmenuitemService.saveBatch(sys_psappmenuitemMapping.toDomain(sys_psappmenuitemdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @PreAuthorize("hasPermission('','Create',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Create", tags = {"SYS_PSAPPMENUITEM" },  notes = "Create")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_psappmenuitems")
    @Transactional
    public ResponseEntity<SYS_PSAPPMENUITEMDTO> create(@RequestBody SYS_PSAPPMENUITEMDTO sys_psappmenuitemdto) {
        SYS_PSAPPMENUITEM domain = sys_psappmenuitemMapping.toDomain(sys_psappmenuitemdto);
		sys_psappmenuitemService.create(domain);
        SYS_PSAPPMENUITEMDTO dto = sys_psappmenuitemMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission('','Create',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "createBatch", tags = {"SYS_PSAPPMENUITEM" },  notes = "createBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_psappmenuitems/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SYS_PSAPPMENUITEMDTO> sys_psappmenuitemdtos) {
        sys_psappmenuitemService.createBatch(sys_psappmenuitemMapping.toDomain(sys_psappmenuitemdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @PreAuthorize("hasPermission(#sys_psappmenuitem_id,'Update',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Update", tags = {"SYS_PSAPPMENUITEM" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/sys_psappmenuitems/{sys_psappmenuitem_id}")
    @Transactional
    public ResponseEntity<SYS_PSAPPMENUITEMDTO> update(@PathVariable("sys_psappmenuitem_id") String sys_psappmenuitem_id, @RequestBody SYS_PSAPPMENUITEMDTO sys_psappmenuitemdto) {
		SYS_PSAPPMENUITEM domain = sys_psappmenuitemMapping.toDomain(sys_psappmenuitemdto);
        domain.setAppmenuitemid(sys_psappmenuitem_id);
		sys_psappmenuitemService.update(domain);
		SYS_PSAPPMENUITEMDTO dto = sys_psappmenuitemMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(#sys_psappmenuitem_id,'Update',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "UpdateBatch", tags = {"SYS_PSAPPMENUITEM" },  notes = "UpdateBatch")
	@RequestMapping(method = RequestMethod.PUT, value = "/sys_psappmenuitems/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SYS_PSAPPMENUITEMDTO> sys_psappmenuitemdtos) {
        sys_psappmenuitemService.updateBatch(sys_psappmenuitemMapping.toDomain(sys_psappmenuitemdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @ApiOperation(value = "GetDraft", tags = {"SYS_PSAPPMENUITEM" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/sys_psappmenuitems/getdraft")
    public ResponseEntity<SYS_PSAPPMENUITEMDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(sys_psappmenuitemMapping.toDto(sys_psappmenuitemService.getDraft(new SYS_PSAPPMENUITEM())));
    }




    @ApiOperation(value = "CheckKey", tags = {"SYS_PSAPPMENUITEM" },  notes = "CheckKey")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_psappmenuitems/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody SYS_PSAPPMENUITEMDTO sys_psappmenuitemdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(sys_psappmenuitemService.checkKey(sys_psappmenuitemMapping.toDomain(sys_psappmenuitemdto)));
    }




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

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




    @PreAuthorize("hasPermission(#sys_psappmenuitem_id,'Get',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Get", tags = {"SYS_PSAPPMENUITEM" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/sys_psappmenuitems/{sys_psappmenuitem_id}")
    public ResponseEntity<SYS_PSAPPMENUITEMDTO> get(@PathVariable("sys_psappmenuitem_id") String sys_psappmenuitem_id) {
        SYS_PSAPPMENUITEM domain = sys_psappmenuitemService.get(sys_psappmenuitem_id);
        SYS_PSAPPMENUITEMDTO dto = sys_psappmenuitemMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission('Get',{#context,'Default',this.getEntity(),'Sql'})")
	@ApiOperation(value = "fetchDEFAULT", tags = {"SYS_PSAPPMENUITEM" } ,notes = "fetchDEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/sys_psappmenuitems/fetchdefault")
	public ResponseEntity<List<SYS_PSAPPMENUITEMDTO>> fetchDefault(SYS_PSAPPMENUITEMSearchContext context) {
        Page<SYS_PSAPPMENUITEM> domains = sys_psappmenuitemService.searchDefault(context) ;
        List<SYS_PSAPPMENUITEMDTO> list = sys_psappmenuitemMapping.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('Get',{#context,'Default',this.getEntity(),'Sql'})")
	@ApiOperation(value = "searchDEFAULT", tags = {"SYS_PSAPPMENUITEM" } ,notes = "searchDEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/sys_psappmenuitems/searchdefault")
	public ResponseEntity<Page<SYS_PSAPPMENUITEMDTO>> searchDefault(@RequestBody SYS_PSAPPMENUITEMSearchContext context) {
        Page<SYS_PSAPPMENUITEM> domains = sys_psappmenuitemService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(sys_psappmenuitemMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}


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

}
