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_USER;
import cn.ibizlab.core.uaa.service.ISYS_USERService;
import cn.ibizlab.core.uaa.filter.SYS_USERSearchContext;




@Slf4j
@Api(tags = {"SYS_USER" })
@RestController("api-sys_user")
@RequestMapping("")
public class SYS_USERResource {

    @Autowired
    private ISYS_USERService sys_userService;

    @Autowired
    @Lazy
    private SYS_USERMapping sys_userMapping;




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

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




    @PreAuthorize("hasPermission(#sys_user_id,'Update',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Update", tags = {"SYS_USER" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/sys_users/{sys_user_id}")
    @Transactional
    public ResponseEntity<SYS_USERDTO> update(@PathVariable("sys_user_id") String sys_user_id, @RequestBody SYS_USERDTO sys_userdto) {
		SYS_USER domain = sys_userMapping.toDomain(sys_userdto);
        domain.setUserid(sys_user_id);
		sys_userService.update(domain);
		SYS_USERDTO dto = sys_userMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(#sys_user_id,'Update',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "UpdateBatch", tags = {"SYS_USER" },  notes = "UpdateBatch")
	@RequestMapping(method = RequestMethod.PUT, value = "/sys_users/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SYS_USERDTO> sys_userdtos) {
        sys_userService.updateBatch(sys_userMapping.toDomain(sys_userdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @ApiOperation(value = "CheckKey", tags = {"SYS_USER" },  notes = "CheckKey")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_users/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody SYS_USERDTO sys_userdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(sys_userService.checkKey(sys_userMapping.toDomain(sys_userdto)));
    }




    @PreAuthorize("hasPermission(#sys_user_id,'Get',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Get", tags = {"SYS_USER" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/sys_users/{sys_user_id}")
    public ResponseEntity<SYS_USERDTO> get(@PathVariable("sys_user_id") String sys_user_id) {
        SYS_USER domain = sys_userService.get(sys_user_id);
        SYS_USERDTO dto = sys_userMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }




    @ApiOperation(value = "Save", tags = {"SYS_USER" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_users/save")
    public ResponseEntity<Boolean> save(@RequestBody SYS_USERDTO sys_userdto) {
        return ResponseEntity.status(HttpStatus.OK).body(sys_userService.save(sys_userMapping.toDomain(sys_userdto)));
    }

    @ApiOperation(value = "SaveBatch", tags = {"SYS_USER" },  notes = "SaveBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_users/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SYS_USERDTO> sys_userdtos) {
        sys_userService.saveBatch(sys_userMapping.toDomain(sys_userdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }




    @ApiOperation(value = "GetDraft", tags = {"SYS_USER" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/sys_users/getdraft")
    public ResponseEntity<SYS_USERDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(sys_userMapping.toDto(sys_userService.getDraft(new SYS_USER())));
    }




    @PreAuthorize("hasPermission('','Create',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "Create", tags = {"SYS_USER" },  notes = "Create")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_users")
    @Transactional
    public ResponseEntity<SYS_USERDTO> create(@RequestBody SYS_USERDTO sys_userdto) {
        SYS_USER domain = sys_userMapping.toDomain(sys_userdto);
		sys_userService.create(domain);
        SYS_USERDTO dto = sys_userMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission('','Create',{this.getEntity(),'Sql'})")
    @ApiOperation(value = "createBatch", tags = {"SYS_USER" },  notes = "createBatch")
	@RequestMapping(method = RequestMethod.POST, value = "/sys_users/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SYS_USERDTO> sys_userdtos) {
        sys_userService.createBatch(sys_userMapping.toDomain(sys_userdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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


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

}
