package com.engine.htsc.wdzx;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.doc.customize.util.GetRequestJsonUtils;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.jgxt.service.JgxtTreeNodeServer;
import com.engine.htsc.jgxt.service.imp.JgxtTreeNodeServerimp;
import com.engine.htsc.wdzx.service.DocmergeService;
import com.engine.htsc.wdzx.service.GetchangedetailService;
import com.engine.htsc.wdzx.service.WdzxModelService;
import com.engine.htsc.wdzx.service.WdzxTreeNodeServer;
import com.engine.htsc.wdzx.service.imp.DocmergeServiceImp;
import com.engine.htsc.wdzx.service.imp.GetchangedetailServiceImp;
import com.engine.htsc.wdzx.service.imp.WdzxModelServiceImp;
import com.engine.htsc.wdzx.service.imp.WdzxTreeNodeServerimp;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.GlobatTools.RequestJSsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
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;

/**
 * @author xf
 * @version 1.0.0
 * @ClassName WdzxAction.java
 * @createTime 2021年11月02日 01:25:00
 */
public class WdzxAction {

    private WdzxTreeNodeServer getTreeNodeService(){
        return ServiceUtil.getService(WdzxTreeNodeServerimp.class);
    }
    private GetchangedetailService getchangedetailService(){
        return ServiceUtil.getService(GetchangedetailServiceImp.class);
    }

    private DocmergeService docmergeService(){
        return ServiceUtil.getService(DocmergeServiceImp.class);
    }

    private WdzxModelService wdzxModelService(){
        return ServiceUtil.getService(WdzxModelServiceImp.class);
    }
    @POST
    @Path("/createtreenode")
    @Produces({MediaType.APPLICATION_JSON})
    public String createtreenode(@Context HttpServletRequest req, @Context HttpServletResponse rep){
        JAVATools.SetCmdwritelog("文档中心自由节点");
        User user = HrmUserVarify.getUser( req, rep );
        Map<String,Object> Paramsmap= ParamUtil.request2Map(req);
        Map<String,Object> map= getTreeNodeService().createtreenode(user,Paramsmap);
        if(Util.null2String(map.get("flag"))=="true"){
            return JAVATools.Apiretrun("200","true");
        }else{
            return JAVATools.Apiretrun("400","false");
        }

    }


    @POST
    @Path("/getchangedetail")
    @Produces({MediaType.APPLICATION_JSON})
    public String getchangedetail(@Context HttpServletRequest req, @Context HttpServletResponse rep){
        JAVATools.SetCmdwritelog("文档中心自由节点");
        User user = HrmUserVarify.getUser( req, rep );
        Map<String,Object> Paramsmap= ParamUtil.request2Map(req);
      JSONObject jsonObject= getchangedetailService().getchangdetailinfo(user,Paramsmap);
        if(jsonObject!=null ){
            return JAVATools.Apiretrun("200",jsonObject);
        }else{
             return JAVATools.Apiretrun("400","false");
        }
    }



    @POST
    @Path("/getmodeinfo")
    @Produces({MediaType.APPLICATION_JSON})
    public String getmodeinfo(@Context HttpServletRequest req, @Context HttpServletResponse rep){
        User user = HrmUserVarify.getUser( req, rep );
        Map<String,Object> Paramsmap= ParamUtil.request2Map(req);
        JSONObject value= wdzxModelService().getmodeinfo(Paramsmap);
        if(value==null || "".equals(value)){
            return JAVATools.Apiretrun("400","false");
        }else{
            return JAVATools.Apiretrun("200",value);
        }
    }



    @POST
    @Path("/getfileversion")
    @Produces({MediaType.APPLICATION_JSON})
    public String getfileversion(@Context HttpServletRequest req, @Context HttpServletResponse rep){
        JAVATools.SetCmdwritelog("文档中心自由节点");
        User user = HrmUserVarify.getUser( req, rep );
        Map<String,Object> Paramsmap= ParamUtil.request2Map(req);
        String value= getchangedetailService().getversion(Paramsmap);
        if(value==null || "".equals(value)){
            return JAVATools.Apiretrun("400","false");
        }else{
            return JAVATools.Apiretrun("200",value);
        }
    }

    @POST
    @Path("/getdocmerge")
    @Produces({MediaType.APPLICATION_JSON})
    public String getdocmerge(@Context HttpServletRequest req, @Context HttpServletResponse rep){
        JAVATools.SetCmdwritelog("文档中心合并");
        JSONArray jsonarray =new JSONArray();
        try {
            JSONArray json = RequestJSsonUtil.getRequestJsonArrray(req);
            JAVATools.SetCmdwritelog("文档中心"+json.toJSONString());
            User user = HrmUserVarify.getUser(req, rep);
           // Map<String, Object> Paramsmap = ParamUtil.request2Map(req);
            jsonarray = docmergeService().getdocmerge(json);
        }catch (Exception ex){

        }
        if (jsonarray == null || jsonarray.size() <= 0) {
            return JAVATools.Apiretrun("400", "false");
        } else {
            return JAVATools.Apiretrun("200", jsonarray);
        }
    }

    @POST
    @Path("/getdocinfo")
    @Produces({MediaType.APPLICATION_JSON})
    public String getdocinfo(@Context HttpServletRequest req, @Context HttpServletResponse rep){
       JAVATools.SetCmdwritelog("查询模板");
       JSONArray jsonArray=new JSONArray();
       try {
           User user = HrmUserVarify.getUser(req, rep);
           Map<String, Object> Paramsmap = ParamUtil.request2Map(req);
           JSONObject json = GetRequestJsonUtils.getRequestJsonObject(req);
           JAVATools.SetCmdwritelog("文档中心" + json.toJSONString());
           jsonArray = wdzxModelService().getjsonarray(json);
       }catch (Exception exo){
           JAVATools.SetCmdwritelog(exo.getMessage());
       }
        if(jsonArray==null ||jsonArray.size()<=0){
            return JAVATools.Apiretrun("400","false");
        }else{
            return JAVATools.Apiretrun("200",jsonArray);

        }
    }

    /**
     * 这是一个文档中心通过子节点ywcjid查询父节点名称的接口，被流程和建模调用。
     * @param req ywcj 子节点的ywcjid,通过子节点
     * @param rep ywcj 返回的父节点名称
     * @return
     */
    @POST
    @Path("/getYwcjByid")
    @Produces({MediaType.APPLICATION_JSON})
    public String getYwcjByid(@Context HttpServletRequest req, @Context HttpServletResponse rep){
        JAVATools.SetCmdwritelog("根据业务场景id查询");

//        User user = HrmUserVarify.getUser(req, rep);
//        JAVATools.writelog("USER=");
        Map<String, Object> Paramsmap = ParamUtil.request2Map(req);
        JAVATools.writelog("MAP="+Paramsmap);
        HashMap<String, Object> ywcjByid = getTreeNodeService().getYwcjByid(Paramsmap);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","success");
        map.put("data",ywcjByid);
        return JSONObject.toJSONString(map);

    }
}
