package com.sa.ibiz.demo.demo.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sa.ibiz.demo.demo.domain.User;
import com.sa.ibiz.demo.demo.service.UserService;
import com.sa.ibiz.demo.demo.service.dto.UserSearchFilter;
import com.sa.ibiz.demo.ibizutil.errors.BadRequestAlertException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.math.BigInteger;
import java.util.List;

@RestController
@RequestMapping("/demo/user")
public class UserResource
{
	@Autowired
	private UserService service;


	@PostMapping(value = "/update")
	public ResponseEntity<User> update(@Validated @RequestBody User data){
		boolean bRt=service.update(data);
		if(!bRt)
			throw new BadRequestAlertException("用户[update]操作发生错误","User",data.getUserid().toString());
		return ResponseEntity.ok().body(data);
	}
	@GetMapping(value = "/{id}")
	public ResponseEntity<User> get(@Validated @PathVariable String id){
		User data=new User();
		data.setUserid(id);;
		data=service.get(data);
		if(data==null)
			throw new BadRequestAlertException("用户[get]操作发生错误","User",id.toString());
		return ResponseEntity.ok().body(data);
	}
	@PostMapping(value = "/checkKey")
	public ResponseEntity<User> checkKey(@Validated @RequestBody User data){
		boolean bRt=service.checkKey(data);
		if(!bRt)
			throw new BadRequestAlertException("用户[checkKey]操作发生错误","User",data.getUserid().toString());
		return ResponseEntity.ok().body(data);
	}
	@PostMapping(value = "/getDraft")
	public ResponseEntity<User> getDraft(@Validated @RequestBody User data){
		boolean bRt=service.getDraft(data);
		if(!bRt)
			throw new BadRequestAlertException("用户[getDraft]操作发生错误","User","");
		return ResponseEntity.ok().body(data);
	}
	@PostMapping(value = "/remove")
	public ResponseEntity<User> remove(@Validated @RequestBody User data){
		boolean bRt=service.remove(data);
		if(!bRt)
			throw new BadRequestAlertException("用户[remove]操作发生错误","User",data.getUserid().toString());
		return ResponseEntity.ok().body(data);
	}
	@PostMapping(value = "/create")
	public ResponseEntity<User> create(@Validated @RequestBody User data){
		boolean bRt=service.create(data);
		if(!bRt)
			throw new BadRequestAlertException("用户[create]操作发生错误","User",data.getUserid().toString());
		return ResponseEntity.ok().body(data);
	}
    @PostMapping(value="/searchdefault")
	public ResponseEntity<Page<User>> searchDefaultPage(@Validated @RequestBody UserSearchFilter filter){
		Page<User> searchResult = service.searchDefault(filter);
		return ResponseEntity.ok().body(searchResult);
	}

	@PostMapping(value="/listdefault")
	public ResponseEntity<List<User>> listDefault(@Validated @RequestBody UserSearchFilter filter){
		List<User> searchResult = service.listDefault(filter);
		return ResponseEntity.ok().body(searchResult);
	}
}
