package com.blueland.sys.module.web;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blueland.annotation.Log;
import com.blueland.model.TPfModule;
import com.blueland.result.R;
import com.blueland.service.IModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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 com.blueland.bo.ModuleEntity;
import com.blueland.service.impl.ModuleServiceImpl;

import lombok.extern.slf4j.Slf4j;

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


@Slf4j
@Controller
@RequestMapping("/module")
public class ModuleRest {
	@Autowired
	private IModuleService moduleServiceImpl;
	
	
	/** 
	 * 搜索对象 
	 **/
	@RequestMapping(value = "",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Page<ModuleEntity>> list(ModuleEntity tpfModuleEntity) {
		Page<ModuleEntity> page = this.moduleServiceImpl.findPage(tpfModuleEntity);
		return R.ok(page);
	}

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


	/** 
	 * 查看对象（支持多条件查询）
	 **/
	@RequestMapping(value = "get",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<TPfModule> get(ModuleEntity model)  {
		TPfModule tpfModuleEntity  = moduleServiceImpl.get(model);
		return R.ok(tpfModuleEntity);
	}
	
	
	/** 
	 * 新增对象
	 **/
	@Log(busiType = "新增模块", opType = SAVE, type = "web")
	@RequestMapping(value = "",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<ModuleEntity> save(@RequestBody ModuleEntity model)  {
		TPfModule module = BeanUtil.copyProperties(model, TPfModule.class);
		moduleServiceImpl.save(module);
		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<ModuleEntity> update(@RequestBody ModuleEntity model)  {
		TPfModule module = BeanUtil.copyProperties(model, TPfModule.class);
		moduleServiceImpl.updateById(module);
		return R.ok(model);
	}
	
	/**
	 * 删除对象
	 * @return 
	 **/
	@Log(busiType = "删除模块", opType = DELETE, type = "web")
	@RequestMapping(value = "/{moduleId}",method = RequestMethod.DELETE,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Boolean> delete(@PathVariable String moduleId) {
		moduleServiceImpl.removeById(moduleId);
		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) {
		if (ObjectUtil.isEmpty(ids)) {
			throw new RuntimeException("id不能为空");
		}
		for (int i = 0; i < ids.length; i++) {
			moduleServiceImpl.removeById(ids[i]);
		}
		return R.ok();
	}
	
}



