package org.springblade.flow.base.controller;

import java.util.List;

import javax.validation.Valid;

import org.springblade.flow.base.ActionResult;
import org.springblade.flow.base.entity.ModuleDataAuthorizeSchemeEntity;
import org.springblade.flow.base.model.moduledataauthorizescheme.DataAuthorizeSchemeCrForm;
import org.springblade.flow.base.model.moduledataauthorizescheme.DataAuthorizeSchemeInfoVO;
import org.springblade.flow.base.model.moduledataauthorizescheme.DataAuthorizeSchemeListVO;
import org.springblade.flow.base.model.moduledataauthorizescheme.DataAuthorizeSchemeUpForm;
import org.springblade.flow.base.service.ModuleDataAuthorizeSchemeService;
import org.springblade.flow.base.vo.ListVO;
import org.springblade.flow.database.exception.DataException;
import org.springblade.flow.util.JsonUtil;
import org.springblade.flow.util.JsonUtilEx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 数据权限方案
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2019年9月27日 上午9:18
 */
@Api(tags = "数据权限方案", value = "ModuleDataAuthorizeScheme")
@RestController
@RequestMapping("/paas/system/ModuleDataAuthorizeScheme")
public class ModuleDataAuthorizeSchemeController {

	@Autowired
	private ModuleDataAuthorizeSchemeService schemeService;

	/**
	 * 列表
	 *
	 * @param moduleId 功能主键
	 * @return
	 */
	@ApiOperation("方案列表")
	@GetMapping("/{moduleId}/List")
	public ActionResult<ListVO<DataAuthorizeSchemeListVO>> list(@PathVariable("moduleId") String moduleId) {
		List<ModuleDataAuthorizeSchemeEntity> data = schemeService.getList(moduleId);
		List<DataAuthorizeSchemeListVO> list = JsonUtil.getJsonToList(data, DataAuthorizeSchemeListVO.class);
		ListVO<DataAuthorizeSchemeListVO> vo = new ListVO<>();
		vo.setList(list);
		return ActionResult.success(vo);
	}

	/**
	 * 信息
	 *
	 * @param id 主键值
	 * @return
	 */
	@ApiOperation("获取方案信息")
	@GetMapping("/{id}")
	public ActionResult<DataAuthorizeSchemeInfoVO> info(@PathVariable("id") String id) throws DataException {
		ModuleDataAuthorizeSchemeEntity entity = schemeService.getInfo(id);
		DataAuthorizeSchemeInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, DataAuthorizeSchemeInfoVO.class);
		return ActionResult.success(vo);
	}

	/**
	 * 新建
	 *
	 * @param dataAuthorizeSchemeCrForm 实体对象
	 * @return
	 */
	@ApiOperation("新建方案")
	@PostMapping
	public ActionResult create(@RequestBody @Valid DataAuthorizeSchemeCrForm dataAuthorizeSchemeCrForm) {
		ModuleDataAuthorizeSchemeEntity entity = JsonUtil.getJsonToBean(dataAuthorizeSchemeCrForm,
				ModuleDataAuthorizeSchemeEntity.class);
		schemeService.create(entity);
		return ActionResult.success("新建成功");
	}

	/**
	 * 更新
	 *
	 * @param id 主键值
	 * @return
	 */
	@ApiOperation("更新方案")
	@PutMapping("/{id}")
	public ActionResult update(@PathVariable("id") String id,
			@RequestBody @Valid DataAuthorizeSchemeUpForm dataAuthorizeSchemeUpForm) {
		ModuleDataAuthorizeSchemeEntity entity = JsonUtil.getJsonToBean(dataAuthorizeSchemeUpForm,
				ModuleDataAuthorizeSchemeEntity.class);
		boolean flag = schemeService.update(id, entity);
		if (flag == false) {
			return ActionResult.success("更新失败，数据不存在");
		}
		return ActionResult.success("更新成功");
	}

	/**
	 * 删除
	 *
	 * @param id 主键值
	 * @return
	 */
	@ApiOperation("删除方案")
	@DeleteMapping("/{id}")
	public ActionResult delete(@PathVariable("id") String id) {
		ModuleDataAuthorizeSchemeEntity entity = schemeService.getInfo(id);
		if (entity != null) {
			schemeService.delete(entity);
			return ActionResult.success("删除成功");
		}
		return ActionResult.fail("删除失败，数据不存在");
	}

}
