package com.mortals.xhx.module.doc.baseinfo.web;

import com.alibaba.fastjson.JSONObject;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.module.doc.baseinfo.model.DocBaseinfoEntity;
import com.mortals.xhx.module.doc.template.model.vo.DocTemplateVo;
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 com.mortals.framework.model.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.module.doc.baseinfo.model.vo.DocBaseinfoVo;
import com.mortals.xhx.module.doc.baseinfo.service.DocBaseinfoService;

import java.util.Map;
import java.util.List;

/**
 * DocBaseinfoController
 * <p>
 * doc基础信息 控制器
 *
 * @author zxfei
 * @date 2021-10-16
 */
@RestController
@RequestMapping("doc/baseinfo")
public class DocBaseinfoController extends BaseCRUDJsonMappingController<DocBaseinfoService, DocBaseinfoForm, DocBaseinfoEntity, Long> {

    @Autowired
    private ParamService paramService;

    public DocBaseinfoController() {
        super.setFormClass(DocBaseinfoForm.class);
        super.setModuleDesc("doc基础信息");
    }

    @Override
    protected void init(HttpServletRequest request, HttpServletResponse response, DocBaseinfoForm form, Map<String, Object> model, Context context) {
        super.init(request, response, form, model, context);
    }


    /**
     * 合成表单与模板
     */
    @PostMapping(value = "mergeFormToDocByTemplate")
    public String mergeFormToDocByTemplate(@RequestParam(value = "form") String form, @RequestParam(value = "templateId") Long templateId) {
        JSONObject jsonObject = new JSONObject();
        try {
            String preView=this.getService().mergeFormToDocByTemplate(form, templateId,getContext());
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            jsonObject.put("preview", preView);
        } catch (Exception e) {
            log.error("合成表单异常", e);
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            jsonObject.put(KEY_RESULT_MSG, super.convertException(e));
        }
        return jsonObject.toJSONString();
    }

}