package com.engine.htsc.jgxt.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;

import com.engine.htsc.jgxt.service.JgxtApiServer;
import com.engine.htsc.jgxt.service.JgxtTreeNodeServer;
import com.engine.htsc.jgxt.service.JgxtVfServer;

import com.engine.htsc.jgxt.service.imp.JgxtApiServerImp;
import com.engine.htsc.jgxt.service.imp.JgxtTreeNodeServerimp;
import com.engine.htsc.jgxt.service.imp.JgxtVfServerImp;
import com.engine.integration.util.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

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.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class JgxtVfAction {

    private JgxtVfServer getService() {
        return ServiceUtil.getService(JgxtVfServerImp.class);
    }

    private JgxtTreeNodeServer getTreeNodeService() {
        return ServiceUtil.getService(JgxtTreeNodeServerimp.class);
    }

    private JgxtApiServer getJgxtApiServer() {
        return ServiceUtil.getService(JgxtApiServerImp.class);
    }


    /**
     * @title getdeptvf
     * @remark 判断前端逻辑营业部部门的逻辑关系
     * @author xf
     * @Params：
     * @param: req
     * @param: rep
     * @updateTime 2021/9/14
     * @return: java.lang.String
     */
    @POST
    @Path("/getdeptvf")
    @Produces({MediaType.APPLICATION_JSON})
    public String getdeptvf(@Context HttpServletRequest req, @Context HttpServletResponse rep) {
        JSONObject jsonObject = getService().deptvf(ParamUtil.request2Map(req));
        return JAVATools.Apiretrun("200", jsonObject);
    }


    @POST
    @Path("/getdeptvfvalue")
    @Produces({MediaType.APPLICATION_JSON})
    public String getdeptvfvalue(@Context HttpServletRequest req, @Context HttpServletResponse rep) {
        JSONObject jsonObject = getService().deptvfValue(ParamUtil.request2Map(req));
        return JAVATools.Apiretrun("200", jsonObject);
    }

    @POST
    @Path("/gethrmuser")
    @Produces({MediaType.APPLICATION_JSON})
    public String gethrmuser(@Context HttpServletRequest req, @Context HttpServletResponse rep) {
        JSONObject value = getService().getuser(ParamUtil.request2Map(req));

        return JAVATools.Apiretrun("200", value);
    }

    /**
     * @title createtreenode
     * @remark 获取分支机构和总部的id
     * @author xf
     * @Params：
     * @param: req
     * @param: rep
     * @updateTime 2021/9/14
     * @return: java.lang.String
     */
    @POST
    @Path("/createtreenode")
    @Produces({MediaType.APPLICATION_JSON})
    public String createtreenode(@Context HttpServletRequest req, @Context HttpServletResponse rep) {
        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("/letterencry")
    @Produces({MediaType.APPLICATION_JSON})
    public String letterencry(@Context HttpServletRequest req, @Context HttpServletResponse rep) {
        User user = HrmUserVarify.getUser(req, rep);
        Map<String, Object> Paramsmap = ParamUtil.request2Map(req);
        JSONObject jsonObject = getJgxtApiServer().letterencry(Paramsmap);
        if (jsonObject == null || jsonObject.size() <= 0) {
            return JAVATools.Apiretrun("400", "解析有误，请重试！");
        } else {
            return JAVATools.Apiretrun("200", jsonObject);
        }
    }

    @POST
    @Path("/getfreenode")
    @Produces({MediaType.APPLICATION_JSON})
    public String getfreenode(@Context HttpServletRequest req, @Context HttpServletResponse rep) {
        JSONObject jsonObject = getService().getfreenode(ParamUtil.request2Map(req));
        return JAVATools.Apiretrun("200", jsonObject);
    }


    @POST
    @Path("/getleaderuserlist")
    @Produces({MediaType.APPLICATION_JSON})
    public String getleaderuserlist(@Context HttpServletRequest req, @Context HttpServletResponse rep) {
        String value = getService().getleaderuserlist(ParamUtil.request2Map(req));
        return JAVATools.Apiretrun("200", value);
    }

    @POST
    @Path("/feedbacktodetail")
    @Produces({MediaType.APPLICATION_JSON})
    public String feedbacktodetail(@Context HttpServletRequest req, @Context HttpServletResponse rep) {
        JAVATools.SetCmdwritelog("监管协同拆入反馈意见！！");
        User user = HrmUserVarify.getUser(req, rep);
        String value = getService().feedbacktodetail(ParamUtil.request2Map(req), user);
        if (value == "0") {
            return JAVATools.Apiretrun("200", "插入成功");
        } else {
            return JAVATools.Apiretrun("400", "插入失败");
        }
    }

    @GET
    @Path("/freenode")
    @Produces(MediaType.APPLICATION_JSON)
    public String freenode(@Context HttpServletRequest req, @Context HttpServletResponse rep) {
        String requestid = req.getParameter("requestid");
        return JAVATools.Apiretrun("200", getJgxtApiServer().freeNode(requestid));
    }


    @GET
    @Path("/getdeptnametoid")
    @Produces(MediaType.APPLICATION_JSON)
    public String getdeptnametoid(@Context HttpServletRequest req, @Context HttpServletResponse rep) {
        JSONObject jsonvalue = getService().getdeptnametoid(ParamUtil.request2Map(req));
        if (jsonvalue == null) {
            return JAVATools.Apiretrun("400", "");
        } else {
            return JAVATools.Apiretrun("200", jsonvalue);
        }
    }

    /**
     * @title getBusinessName
     * @remark 分公司获取营业部的名称
     * @author xf
     * @updateTime 2022/1/18
     * @return: java.lang.String
     */
    @POST
    @Path("/getBusinessName")
    @Produces({MediaType.APPLICATION_JSON})
    public String getBusinessName(@Context HttpServletRequest req, @Context HttpServletResponse rep) {
        String userdeptname = getService().getBusinessName(ParamUtil.request2Map(req));
        if (userdeptname == null) {
            return JAVATools.Apiretrun("400", "error");
        } else {
            return JAVATools.Apiretrun("200", userdeptname);
        }

    }

    @POST
    @Path("/updatelabeltype")
    @Produces({MediaType.APPLICATION_JSON})
    public String updatelabeltype(@Context HttpServletRequest req, @Context HttpServletResponse rep) {
        String value = getService().updatelabeltype(ParamUtil.request2Map(req));

        if (StringUtils.isBlank(value)) {
            return JAVATools.Apiretrun("400", "error");
        } else {
            return JAVATools.Apiretrun("200", "ok");
        }

    }
}
