package org.springblade.flow.form.controller;

import java.util.List;

import javax.validation.Valid;

import org.springblade.flow.base.ActionResult;
import org.springblade.flow.database.exception.DataException;
import org.springblade.flow.engine.enums.FlowStatusEnum;
import org.springblade.flow.exception.WorkFlowException;
import org.springblade.flow.form.entity.MaterialEntryEntity;
import org.springblade.flow.form.entity.MaterialRequisitionEntity;
import org.springblade.flow.form.model.materialrequisition.MaterialEntryEntityInfoModel;
import org.springblade.flow.form.model.materialrequisition.MaterialRequisitionForm;
import org.springblade.flow.form.model.materialrequisition.MaterialRequisitionInfoVO;
import org.springblade.flow.form.service.MaterialRequisitionService;
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.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 = "MaterialRequisition")
@RestController
@RequestMapping("/paas/workflow/Form/MaterialRequisition")
public class MaterialRequisitionController {

	@Autowired
	private MaterialRequisitionService materialRequisitionService;

	/**
	 * 获取领料单信息
	 *
	 * @param id 主键值
	 * @return
	 */
	@ApiOperation("获取领料单信息")
	@GetMapping("/{id}")
	public ActionResult<MaterialRequisitionInfoVO> info(@PathVariable("id") String id) throws DataException {
		MaterialRequisitionEntity entity = materialRequisitionService.getInfo(id);
		List<MaterialEntryEntity> entityList = materialRequisitionService.getMaterialEntryList(id);
		MaterialRequisitionInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, MaterialRequisitionInfoVO.class);
		vo.setEntryList(JsonUtil.getJsonToList(entityList, MaterialEntryEntityInfoModel.class));
		return ActionResult.success(vo);
	}

	/**
	 * 新建领料单
	 *
	 * @param materialRequisitionForm 表单对象
	 * @return
	 * @throws WorkFlowException
	 */
	@ApiOperation("新建领料单")
	@PostMapping
	public ActionResult create(@RequestBody @Valid MaterialRequisitionForm materialRequisitionForm)
			throws WorkFlowException {
		MaterialRequisitionEntity material = JsonUtil.getJsonToBean(materialRequisitionForm,
				MaterialRequisitionEntity.class);
		List<MaterialEntryEntity> materialEntryList = JsonUtil.getJsonToList(materialRequisitionForm.getEntryList(),
				MaterialEntryEntity.class);
		if (FlowStatusEnum.save.getMessage().equals(materialRequisitionForm.getStatus())) {
			materialRequisitionService.save(material.getId(), material, materialEntryList);
			return ActionResult.success("保存成功");
		}
		materialRequisitionService.submit(material.getId(), material, materialEntryList);
		return ActionResult.success("提交成功，请耐心等待");
	}

	/**
	 * 修改领料单
	 *
	 * @param materialRequisitionForm 表单对象
	 * @param id                      主键
	 * @return
	 * @throws WorkFlowException
	 */
	@ApiOperation("修改领料单")
	@PutMapping("/{id}")
	public ActionResult update(@RequestBody @Valid MaterialRequisitionForm materialRequisitionForm,
			@PathVariable("id") String id) throws WorkFlowException {
		MaterialRequisitionEntity material = JsonUtil.getJsonToBean(materialRequisitionForm,
				MaterialRequisitionEntity.class);
		List<MaterialEntryEntity> materialEntryList = JsonUtil.getJsonToList(materialRequisitionForm.getEntryList(),
				MaterialEntryEntity.class);
		if (FlowStatusEnum.save.getMessage().equals(materialRequisitionForm.getStatus())) {
			materialRequisitionService.save(id, material, materialEntryList);
			return ActionResult.success("保存成功");
		}
		materialRequisitionService.submit(id, material, materialEntryList);
		return ActionResult.success("提交成功，请耐心等待");
	}
}
