/**
 * 文件：ProductVersionController.java 版本：1.0.0 日期： Copyright &reg; All right
 * reserved.
 */
package com.mortals.xhx.module.materia.web;

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.PublishStatusEnum;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.module.materia.model.MateriaBomVersionEntity;
import com.mortals.xhx.module.materia.service.MateriaBomVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * <p>
 * Title: 产品发布表
 * </p>
 * <p>
 * Description: ProductVersionController
 * </p>
 * <p>
 * Copyright: Copyright &reg;
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author
 * @version 1.0.0
 */
@RestController
@RequestMapping("materia/bom/version")
public class MateriaBomVersionController extends
		BaseCRUDJsonMappingController<MateriaBomVersionService, MateriaBomVersionForm, MateriaBomVersionEntity, Long> {

	@Autowired
	private ParamService paramService;

	public MateriaBomVersionController() {
		super.setFormClass(MateriaBomVersionForm.class);
		super.setModuleDesc("产品发布表");
	}

	@Override
	protected void addAfter(HttpServletRequest request, HttpServletResponse response, MateriaBomVersionForm form,
			Map<String, Object> model) throws AppException {
		super.addAfter(request, response, form, model);
		form.getEntity().setPublishUser(this.getCurUser().getRealName());
//		form.getEntity().setPublishTime(new Date());
	}

	@Override
	protected void init(HttpServletRequest request, HttpServletResponse response, MateriaBomVersionForm form,
			Map<String, Object> model, Context context) {
		this.addDict(model, "companyCode", paramService.getParamByFirstOrganize(Constant.Param_VerCompanyCode));
		this.addDict(model, "rdCode", paramService.getParamByFirstOrganize(Constant.Param_VerRdCode));
		this.addDict(model, "stage", paramService.getParamByFirstOrganize(Constant.Param_VerStageCode));
		this.addDict(model, "publishStatus", PublishStatusEnum.getEnumMap());

		this.addDict(model, "useType", paramService.getParamByFirstOrganize(Constant.Param_BomUseType));
		this.addDict(model, "status", paramService.getParamByFirstOrganize(Constant.Param_BomStatus));


		super.init(request, response, form, model, context);
	}

	@PostMapping(value = "submitVersion")
	public String submitVersion(@RequestParam(value = "id", required = false) Long id) {
		JSONObject ret = new JSONObject();
		ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
		try {
			JSONObject data = new JSONObject();
			Boolean aBoolean = this.service.submitProduct(id, getContext());
			if(!aBoolean){
			 throw new AppException("提交失败！");
			}
			ret.put(KEY_RESULT_DATA, data);
		} catch (Exception e) {
			log.error("版本提交异常",e);
			ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
			ret.put(KEY_RESULT_MSG, e.getMessage());
		}
		return ret.toJSONString();
	}

	@PostMapping(value = "audit")
	public String audit(@RequestParam String processInstanceId, @RequestParam String result, @RequestParam (required = false, defaultValue = "")  String remark)  {
		JSONObject ret = new JSONObject();
		log.info(String.format("receive submit version result:%s,processInstanceId：%s",result,processInstanceId ));
		try {
			JSONObject data = new JSONObject();
			this.service.auditProduct(processInstanceId, result, remark);

			ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
			ret.put(KEY_RESULT_DATA, data);
		} catch (Exception e) {
			ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
			ret.put(KEY_RESULT_MSG, e.getMessage());
		}
		return ret.toJSONString();
	}

	@PostMapping(value = "updateBom")
	public String updateBom(@RequestParam Long productVersionId, @RequestParam (required = true)  Long bomId)  {
		JSONObject ret = new JSONObject();
		try {
			JSONObject data = new JSONObject();
			this.service.updateBom(productVersionId, bomId);

			ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
			ret.put(KEY_RESULT_DATA, data);
		} catch (Exception e) {
			ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
			ret.put(KEY_RESULT_MSG, e.getMessage());
		}
		return ret.toJSONString();
	}

}