package com.blueland.sys.power.web;


import javax.servlet.http.HttpServletRequest;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blueland.annotation.Log;
import com.blueland.model.TPfAuth;
import com.blueland.result.R;
import com.blueland.service.IAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.blueland.bo.AuthEntity;

import io.swagger.annotations.Api;

import static com.blueland.constant.CommonConstant.*;


@Api(value = "auth")
@RestController
@RequestMapping(value = "/auth")
public class AuthRest {
	@Autowired
	private IAuthService authService;
	
	
	/** 
	 * 搜索对象 
	 **/
	@RequestMapping(value = "",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Page<AuthEntity>> list(AuthEntity authEntity) {
		Page<AuthEntity> page = this.authService.findPage(authEntity);
		return R.ok(page);
	}

	
	/** 
	 * 查看对象
	 **/
	@RequestMapping(value = "/{authId}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<TPfAuth> view(@PathVariable String authId)  {
		TPfAuth authEntity  = authService.getById(authId);
		return R.ok(authEntity);
	}


	/** 
	 * 查看对象（支持多条件查询）
	 **/
	@RequestMapping(value = "get",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<TPfAuth> get(AuthEntity model)  {
		TPfAuth authEntity  = authService.get(model);
		return R.ok(authEntity);
	}
	
	
	/** 
	 * 新增对象
	 **/
	@Log(busiType = "新增权限", opType = SAVE, type = "web")
	@RequestMapping(value = "",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<AuthEntity> save(@RequestBody AuthEntity model,HttpServletRequest request)  {
		TPfAuth auth = BeanUtil.copyProperties(model, TPfAuth.class);
		authService.save(auth);
		return R.ok(model);
	}
	
	
	/**
	 * 更新对象
	 * @return 
	 **/
	@Log(busiType = "更新权限", opType = UPDATE, type = "web")
	@RequestMapping(value = "",method = RequestMethod.PUT,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<AuthEntity> update(@RequestBody AuthEntity model,HttpServletRequest request)  {
		TPfAuth auth = BeanUtil.copyProperties(model, TPfAuth.class);
		authService.updateById(auth);
		return R.ok(model);
	}
	
	/**
	 * 删除对象
	 * @return 
	 **/
	@Log(busiType = "删除权限", opType = DELETE, type = "web")
	@RequestMapping(value = "/{authId}",method = RequestMethod.DELETE,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Boolean> delete(@PathVariable String authId,HttpServletRequest request) {
		authService.removeById(authId);
		return R.ok();
	}
	
	/**
	 *  批量删除对象
	 * @return 
	 **/
	@Log(busiType = "删除权限", opType = DELETE, type = "web")
	@RequestMapping(value = "deleteBatch",method = RequestMethod.DELETE,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Boolean> deleteBatch(String[] ids,HttpServletRequest request) {
		for (int i = 0; i < ids.length; i++) {
			authService.removeById(ids[i]);
		}
		return R.ok();
	}
	
}



