package com.mortals.xhx.module.doc.template.web;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.module.doc.template.model.DocTemplateEntity;
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.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.template.service.DocTemplateService;

import java.util.Map;

/**
* DocTemplateController
*
* doc模板 控制器
*
* @author zxfei
* @date 2021-10-16
*/
@RestController
@RequestMapping("doc/template")
public class DocTemplateController extends BaseCRUDJsonBodyMappingController<DocTemplateService,DocTemplateEntity,Long> {
    @Autowired
    private ParamService paramService;

    public DocTemplateController(){
        super.setFormClass(DocTemplateForm.class);
        super.setModuleDesc( "doc模板");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        super.init( model, context);
    }

    /**
     * 解析样表数据并保存
     */
    @PostMapping(value = "parseXmltoJson")
    public String parseXmltoJson(DocTemplateEntity docTemplate) {
        JSONObject jsonObject = new JSONObject();
        try {
            this.getService().parseDocXml(docTemplate,getContext());
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
        } 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();
    }

    /**
     * 解析样表数据并保存
     */
    @PostMapping(value = "parseDocxtoJson")
    public String parseDocxtoJson(DocTemplateEntity docTemplate) {
        JSONObject jsonObject = new JSONObject();
        try {
            this.getService().parseDocxToJson(docTemplate,getContext());
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
        } 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();
    }
}