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.ComFieldsEntity;
import org.springblade.flow.base.model.comfields.ComFieldsCrForm;
import org.springblade.flow.base.model.comfields.ComFieldsInfoVO;
import org.springblade.flow.base.model.comfields.ComFieldsListVO;
import org.springblade.flow.base.model.comfields.ComFieldsUpForm;
import org.springblade.flow.base.service.ComFieldsService;
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;

/**
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2021/3/12 15:31
 */
@Api(tags = "常用字段", value = "CommonFields")
@RestController
@RequestMapping("/paas/system/CommonFields")
public class ComFieldsController {

	@Autowired
	private ComFieldsService comFieldsService;

	@GetMapping
	public ActionResult<ListVO<ComFieldsListVO>> list() {
		List<ComFieldsEntity> data = comFieldsService.getList();
		List<ComFieldsListVO> list = JsonUtil.getJsonToList(data, ComFieldsListVO.class);
		ListVO<ComFieldsListVO> vo = new ListVO<>();
		vo.setList(list);
		return ActionResult.success(vo);
	}

	@GetMapping("/{id}")
	public ActionResult<ComFieldsInfoVO> info(@PathVariable("id") String id) throws DataException {
		ComFieldsEntity entity = comFieldsService.getInfo(id);
		ComFieldsInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, ComFieldsInfoVO.class);
		return ActionResult.success(vo);
	}

	@PostMapping
	public ActionResult create(@RequestBody @Valid ComFieldsCrForm comFieldsCrForm) {
		ComFieldsEntity entity = JsonUtil.getJsonToBean(comFieldsCrForm, ComFieldsEntity.class);
		if (comFieldsService.isExistByFullName(entity.getField(), entity.getId())) {
			return ActionResult.fail("名称不能重复");
		}
		comFieldsService.create(entity);
		return ActionResult.success("新建成功");
	}

	@PutMapping("/{id}")
	public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ComFieldsUpForm comFieldsUpForm) {
		ComFieldsEntity entity = JsonUtil.getJsonToBean(comFieldsUpForm, ComFieldsEntity.class);
		if (comFieldsService.isExistByFullName(entity.getField(), id)) {
			return ActionResult.fail("名称不能重复");
		}
		boolean flag = comFieldsService.update(id, entity);
		if (flag == false) {
			return ActionResult.fail("更新失败，数据不存在");
		}
		return ActionResult.success("更新成功");
	}

	@DeleteMapping("/{id}")
	public ActionResult delete(@PathVariable("id") String id) {
		ComFieldsEntity entity = comFieldsService.getInfo(id);
		if (entity != null) {
			comFieldsService.delete(entity);
			return ActionResult.success("删除成功");
		}
		return ActionResult.fail("删除失败,数据不存在");
	}
}
