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.ModuleDataAuthorizeEntity;
import org.springblade.flow.base.model.moduledataauthorize.DataAuthorizeCrForm;
import org.springblade.flow.base.model.moduledataauthorize.DataAuthorizeInfoVO;
import org.springblade.flow.base.model.moduledataauthorize.DataAuthorizeListVO;
import org.springblade.flow.base.model.moduledataauthorize.DataAuthorizeUpForm;
import org.springblade.flow.base.service.ModuleDataAuthorizeService;
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 = "ModuleDataAuthorize")
@RestController
@RequestMapping("/paas/system/ModuleDataAuthorize")
public class ModuleDataAuthorizeController {

	@Autowired
	private ModuleDataAuthorizeService dataAuthorizeService;

	/**
	 * 获取数据权限配置信息列表
	 *
	 * @param moduleId 功能主键
	 * @return
	 */
	@ApiOperation("获取字段列表")
	@GetMapping("/{moduleId}/List")
	public ActionResult<ListVO<DataAuthorizeListVO>> list(@PathVariable("moduleId") String moduleId) {
		List<ModuleDataAuthorizeEntity> data = dataAuthorizeService.getList(moduleId);
		List<DataAuthorizeListVO> list = JsonUtil.getJsonToList(data, DataAuthorizeListVO.class);
		ListVO<DataAuthorizeListVO> vo = new ListVO<>();
		vo.setList(list);
		return ActionResult.success(vo);
	}

	/**
	 * 获取数据权限配置信息
	 *
	 * @param id 主键值
	 * @return
	 */
	@ApiOperation("获取数据权限配置信息")
	@GetMapping("/{id}")
	public ActionResult<DataAuthorizeInfoVO> info(@PathVariable("id") String id) throws DataException {
		ModuleDataAuthorizeEntity entity = dataAuthorizeService.getInfo(id);
		DataAuthorizeInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, DataAuthorizeInfoVO.class);
		return ActionResult.success(vo);
	}

	/**
	 * 新建数据权限配置
	 *
	 * @param dataAuthorizeCrForm 实体对象
	 * @return
	 */
	@ApiOperation("新建数据权限配置")
	@PostMapping
	public ActionResult create(@RequestBody @Valid DataAuthorizeCrForm dataAuthorizeCrForm) {
		ModuleDataAuthorizeEntity entity = JsonUtil.getJsonToBean(dataAuthorizeCrForm, ModuleDataAuthorizeEntity.class);
		dataAuthorizeService.create(entity);
		return ActionResult.success("新建成功");
	}

	/**
	 * 更新数据权限配置
	 *
	 * @param id 主键值
	 * @return
	 */
	@ApiOperation("更新数据权限配置")
	@PutMapping("/{id}")
	public ActionResult update(@PathVariable("id") String id,
			@RequestBody @Valid DataAuthorizeUpForm dataAuthorizeUpForm) {
		ModuleDataAuthorizeEntity entity = JsonUtil.getJsonToBean(dataAuthorizeUpForm, ModuleDataAuthorizeEntity.class);
		boolean flag = dataAuthorizeService.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) {
		ModuleDataAuthorizeEntity entity = dataAuthorizeService.getInfo(id);
		if (entity != null) {
			dataAuthorizeService.delete(entity);
			return ActionResult.success("删除成功");
		}
		return ActionResult.fail("删除失败，数据不存在");
	}

}
