package com.engine.htsc.htNoticetRelease.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.htsc.htNoticetRelease.utils.HTNoticeReleaseUtil;
import com.engine.htsc.trip.util.AjaxResult;
import weaver.conn.RecordSet;
import weaver.general.StringUtil;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cus.HTUtil;
import weaver.interfaces.htsc.gwlc.WrokflowToOAUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

public class HTNoticeReleaseAction {

    /**
     * 总部公告流程生成流程编号
     *
     * @return 字符串
     */
    @POST
    @Path("/createRequestMark")
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<String> createHTNoticeReleaseRequestMark() {
        try {
            HTNoticeReleaseUtil htno = new HTNoticeReleaseUtil();
            String HTNoticeReleaseRequestMark = htno.createHTNoticeReleaseRequestMark();
            return AjaxResult.ok(HTNoticeReleaseRequestMark);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 外部系统通过xp触发总部公告流程时，更改正文 docdetail表和docimagefile表字段 保证排版发布节点能够套红定稿成功
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/updatezw")
    @Produces(MediaType.TEXT_PLAIN)
    public String updateDocument(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            String workflowid = Util.null2String(request.getParameter("workflowid"));
            String requestid = Util.null2String(request.getParameter("requestid"));
            JAVATools.SetCmdwritelog("updatezw====workflowid: ", workflowid);
            JAVATools.SetCmdwritelog("updatezw====requestid: ", requestid);

            if(!"".equals(workflowid) && !"".equals(requestid)){
                String tableName = "";
                String zw = "";
                boolean flag1;
                boolean flag2;

                RecordSet rs = new RecordSet();
                String formsql = "select abs(formid) as formid from workflow_base where id = " + workflowid;
                rs.execute(formsql);
                if (rs.next()) {
                    tableName = "formtable_main_" + Util.null2String(rs.getString("formid"));
                }
                //查询建模表正文id
                String sqlMain = "select zw from " + tableName + " where requestid = " + requestid;
                rs.execute(sqlMain);
                JAVATools.SetCmdwritelog("查询" + tableName + "语句:" + sqlMain);
                JAVATools.SetCmdwritelog("查询结果" + rs.toString());
                if (rs.next()) {
                    zw = Util.null2String(rs.getString("zw"));
                }

                //更新docdetail表
                String sql1 = "update docdetail set doctype = 2 where doctype = 1 and id = " + zw;
                flag1 = rs.execute(sql1);
                if (flag1) {
                    JAVATools.SetCmdwritelog("requsetid = " + requestid + "更新docdetail表语句执行成功: " + sql1);
                } else {
                    JAVATools.SetCmdwritelog("requsetid = " + requestid + "更新docdetail表语句执行失败: " + sql1);
                }
                //更新docimagefile表
                String sql2 = "update docimagefile set isextfile = '' where isextfile = 1 and docid = " + zw;
                flag2 = rs.execute(sql2);
                if (flag2) {
                    JAVATools.SetCmdwritelog("requsetid = " + requestid + "更新docimagefile表语句执行成功: " + sql2);
                } else {
                    JAVATools.SetCmdwritelog("requsetid = " + requestid + "更新docimagefile表语句执行失败: " + sql2);
                }
                if(flag1 && flag2){
                    apidatas.put("success",true);
                    apidatas.put("msg","updatezw====更新成功");
                    apidatas.put("requestid",requestid);
                }
            }else{
                apidatas.put("success",false);
                apidatas.put("msg","requestid或者workflowid存在空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success", false);
            apidatas.put("msg", "系统内部错误");
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 更新泛微总部公告附件
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/filefw")
    @Produces(MediaType.TEXT_PLAIN)
    public String fileFw(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            String requestid = Util.null2String(request.getParameter("requestid"));
            JAVATools.SetCmdwritelog("updatezw====requestid: ", requestid);

            if(!"".equals(requestid)){
                WrokflowToOAUtil wrokflowToOAUtil = new WrokflowToOAUtil();
                String tableName = "";
                String fj = "";
                RecordSet rs = new RecordSet();
                tableName = JAVATools.getMaintable(requestid);
                String sqlMain = "select fj from " + tableName + " where requestid = " + requestid ;
                rs.execute(sqlMain);
                if (rs.next()) {
                     fj = Util.null2String(rs.getString("fj"));
                }
                JAVATools.SetCmdwritelog("查询" + tableName + "语句:" + sqlMain);
                JAVATools.SetCmdwritelog("查询结果" + rs.toString());
                if(StringUtil.isEmpty(fj)){
                    apidatas.put("success",false);
                    apidatas.put("msg","未查到附件");
                    return JSONObject.toJSONString(apidatas);
                }

                String sql1 = "select *  from imagefile t1 left join docimagefile t2 on t1.IMAGEFILEID=t2.IMAGEFILEID where docid in ("+fj+" )";
                if(!rs.execute(sql1)){
                    apidatas.put("success",false);
                    apidatas.put("msg","未查到imagefileid");
                    return JSONObject.toJSONString(apidatas);
                }
                while (rs.next()) {
                    String imagefileid = Util.null2String(rs.getString("imagefileid"));
                    String imagefilename = Util.null2String(rs.getString("imagefilename"));
                    String filerealpath = Util.null2String(rs.getString("filerealpath"));
                    String iszip = Util.null2String(rs.getString("iszip"));
                    wrokflowToOAUtil.uploadFile2SG(imagefileid,imagefilename,filerealpath,iszip);
                }
                apidatas.put("success",true);
                apidatas.put("msg","updateFileFW====更新成功");
                apidatas.put("requestid",requestid);
            }else{
                apidatas.put("success",false);
                apidatas.put("msg","requestid存在空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success", false);
            apidatas.put("msg", "系统内部错误");
        }
        return JSONObject.toJSONString(apidatas);
    }

    @POST
    @Path("/approveBulletin")
    @Produces(MediaType.TEXT_PLAIN)
    public String approveBulletin(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String content = "";
        HTUtil htUtil = new HTUtil();
        PropBean propBean = new PropBean();
        String ApproveBulletin = propBean.getPropName("getInfoToIbull"); //测试环境ip地址
        try {
            Map<String,Object> param = ParamUtil.request2Map(request);
            String requestid = Util.null2String(param.get("requestid"));
            JAVATools.SetCmdwritelog("approveBulletin====requestid: ", requestid);
            JSONObject datas = new JSONObject();
            String tableName = "";
            Long id;
            String oaId = "";
            Short status = 4;
            String ggbh = "";
            RecordSet rs = new RecordSet();
            String formsql = "select abs(a.formid ) as formid  from workflow_base a,workflow_requestbase b where a.id = b.workflowid and b.requestid = " + requestid;
            rs.execute(formsql);
            if(rs.next()){
                tableName = "formtable_main_" + Util.null2String(rs.getString("formid"));
            }

            String mainsql = "select ggbh,lcbh from " + tableName + " where requestid = " + requestid;
            rs.execute(mainsql);
            if(rs.next()){
                ggbh = Util.null2String(rs.getString("ggbh"));
                oaId = Util.null2String(rs.getString("lcbh"));
            }
            if(!"".equals(ggbh)){
                id = Long.parseLong(ggbh);
                datas.put("id",id);
                datas.put("oaId",oaId);
                datas.put("status",status);
                JAVATools.SetCmdwritelog("参数详情"+requestid+"==="+datas.toString());

                String encode = "utf-8";
                Map<String, String> headers = new HashMap<>();
                JAVATools.SetCmdwritelog("url:"+ApproveBulletin);
                content = htUtil.httpPostRaw(ApproveBulletin, datas.toString(), headers, encode);
                JAVATools.SetCmdwritelog("content结果集："+ content);
            }else{
                content = "该流程非外部平台触发，无需回调接口";
            }

        }catch (Exception e) {
            e.printStackTrace();
            content = "approveBulletin接口内部错误";
            JAVATools.SetCmdwritelog("approveBulletin接口内部错误",e.getMessage());
        }
        return content;
    }
}
