package com.blueland.sys.power.web;


import javax.servlet.http.HttpServletRequest;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blueland.annotation.Log;
import com.blueland.result.R;
import com.blueland.service.IAppService;
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.AppEntity;
import io.swagger.annotations.Api;

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


@Api(value = "app")
@RestController
@RequestMapping(value = "/api/app")
public class AppRest {
	@Autowired
	private IAppService appService;
	
	
	/** 
	 * 搜索对象 
	 **/
	@RequestMapping(value = "",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Page<AppEntity>> list(AppEntity appEntity) {
		Page<AppEntity> page = this.appService.findPage(appEntity);
		return R.ok(page);
	}

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


	/** 
	 * 查看对象（支持多条件查询）
	 **/
	@RequestMapping(value = "get",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<AppEntity> get(AppEntity model)  {
		AppEntity appEntity  = appService.get(model);
		return R.ok(appEntity);
	}
	
	
	/** 
	 * 新增对象
	 **/
	@Log(busiType = "新增APP", opType = SAVE, type = "web")
	@RequestMapping(value = "",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<AppEntity> save(@RequestBody AppEntity model,HttpServletRequest request)  {
		appService.save(model);
		return R.ok(model);
	}
	
	
	/**
	 * 更新对象
	 * @return 
	 **/
	@Log(busiType = "修改APP", opType = UPDATE, type = "web")
	@RequestMapping(value = "",method = RequestMethod.PUT,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<AppEntity> update(@RequestBody AppEntity model,HttpServletRequest request)  {
		appService.updateById(model);
		return R.ok(model);
	}
	
	/**
	 * 删除对象
	 * @return 
	 **/
	@Log(busiType = "删除APP", opType = DELETE, type = "web")
	@RequestMapping(value = "/{appId}",method = RequestMethod.DELETE,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Boolean> delete(@PathVariable String appId,HttpServletRequest request) {
		appService.removeById(appId);
		return R.ok();
	}
	
	/**
	 *  批量删除对象
	 * @return 
	 **/
	@Log(busiType = "批量删除APP", opType = DELETE, type = "web")
	@RequestMapping(value = "deleteBatch",method = RequestMethod.DELETE,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<AppEntity> deleteBatch(String[] ids,HttpServletRequest request) {
		if (ObjectUtil.isEmpty(ids)) {
			throw new RuntimeException("id不能为空");
		}
		for (int i = 0; i < ids.length; i++) {
			appService.removeById(ids[i]);
		}
		return R.ok();
	}
	
}



