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

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.module.doc.baseinfo.model.DocBaseinfoEntity;
import com.mortals.xhx.module.doc.form.model.DocFormEntity;
import com.mortals.xhx.module.doc.form.model.vo.DocFormVo;
import com.mortals.xhx.module.doc.form.service.DocFormService;
import com.mortals.xhx.module.doc.template.model.vo.DocTemplateVo;
import com.mortals.xhx.module.platform.model.PlatformQuery;
import com.mortals.xhx.module.platform.service.PlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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;
import java.util.stream.Collectors;

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

    @Autowired
    private PlatformService platformService;
    @Autowired
    private DocFormService docFormService;

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

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "platformSn", platformService.find(new PlatformQuery()).stream().collect(Collectors.toMap(x -> x.getPlatformSn(), y -> y.getName())));
        super.init(model, context);
    }


    /**
     * 合成表单与模板
     */
    @PostMapping(value = "mergeFormToDocByTemplate")
    public String mergeFormToDocByTemplate(@RequestParam(value = "form") String form, @RequestParam(value = "templateId") Long templateId) {
        JSONObject jsonObject = new JSONObject();
        try {
            String paths = this.getService().mergeFormToDocByTemplate(form, templateId, getContext());
            String[] vals = paths.split(";");
            String redirectUrl = this.getService().submitOtherSystem(form, templateId, vals[0], vals[1], getContext());
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            jsonObject.put("mergeDoc", vals[0]);
            jsonObject.put("preview", vals[1]);
            jsonObject.put("redirectUrl", redirectUrl);

        } 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 = "mergeFormToDocx")
    @UnAuth
    public String mergeFormToDocx(@RequestBody DocFormEntity docFormVo) {
        JSONObject jsonObject = new JSONObject();
        try {
            String paths = this.getService().mergeFormToDoc(docFormVo, getContext());
            String[] vals = paths.split(";");
            // String redirectUrl = this.getService().submitOtherSystem(form, templateId, vals[0], vals[1], getContext());
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            jsonObject.put("mergeDoc", vals[0]);
            jsonObject.put("preview", vals[1]);
            // jsonObject.put("redirectUrl", redirectUrl);
            docFormService.save(docFormVo,getContext());

        } 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();
    }

}