package com.bootdo.system.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.system.domain.SearchDO;
import com.bootdo.system.service.SearchService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2021-06-08 15:25:01
 */
 
@Controller
@RequestMapping("/system/search")
public class SearchController {
	@Autowired
	private SearchService searchService;
	
	@GetMapping()
	@RequiresPermissions("system:search:search")
	String Search(){
	    return "system/search/search";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:search:search")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<SearchDO> searchList = searchService.list(query);
		int total = searchService.count(query);
		PageUtils pageUtils = new PageUtils(searchList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:search:add")
	String add(){
	    return "system/search/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:search:edit")
	String edit(@PathVariable("id") Long id,Model model){
		SearchDO search = searchService.get(id);
		model.addAttribute("search", search);
	    return "system/search/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:search:add")
	public R save( SearchDO search){
		if(searchService.save(search)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:search:edit")
	public R update( SearchDO search){
		searchService.update(search);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:search:remove")
	public R remove( Long id){
		if(searchService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:search:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		searchService.batchRemove(ids);
		return R.ok();
	}
	
}
