package com.engine.htsc.ReimbursementFlow;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.esb.service.impl.CommonServiceImpl;
import com.engine.htsc.ReimbursementFlow.Dao.GettravelapplicationDao;
import com.engine.htsc.ReimbursementFlow.Dao.UfZbbxandclsplDaoImpl;
import com.engine.htsc.ReimbursementFlow.Esb.Dao.UfzbbxesblocalDao;
import com.engine.htsc.ReimbursementFlow.Service.GettravelsubsidyService;
import com.engine.htsc.ReimbursementFlow.Service.GettravlapplctionService;
import com.engine.htsc.ReimbursementFlow.Service.Impl.GettravelsubsidyServiceImpl;
import com.engine.htsc.ReimbursementFlow.Service.Impl.GettravlapplctionServiceImpl;
import com.engine.htsc.ReimbursementFlow.Service.UfCommonService;
import com.engine.htsc.ReimbursementFlow.Service.Impl.UfZbbxandclsplServiceImpl;
import com.engine.htsc.ReimbursementFlow.Service.UfZbbxandclsplService;


import com.engine.htsc.ReimbursementFlow.model.TravelLinkreq;


import com.engine.htsc.ReimbursementFlow.tool.Apireturn;
import org.omg.CORBA.OBJ_ADAPTER;
import weaver.conn.RecordSet;
import weaver.general.Util;
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.Consumes;
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.*;
import java.util.stream.Collectors;

public class Reimburseflow {

    public static String logtitle="差旅和费用报销审批链";

    UfZbbxandclsplService getService() {
        return ServiceUtil.getService(UfZbbxandclsplServiceImpl.class);
    }
    GettravelsubsidyService gettravelsubsidyService() {
        return ServiceUtil.getService(GettravelsubsidyServiceImpl.class);
    }
    GettravlapplctionService gettravlapplctionService(){
        return ServiceUtil.getService(GettravlapplctionServiceImpl.class);
    }


    @POST
    @Path("/GetUserJObGw")
    @Produces({MediaType.TEXT_PLAIN,MediaType.MULTIPART_FORM_DATA})

    public String GetUserJob(@Context HttpServletRequest request, @Context HttpServletResponse response){

        RecordSet rs=new RecordSet();
        JSONObject status=new JSONObject();
        String jobtitle="";
        String value="";
        String bxrysx="";
        String deptid="";
        try{
            String userid = Util.null2String(request.getParameter("userid"));
            if("".equals(userid)){
              return   JAVATools.Apiretrun("400","传参字段为空");
            }
            String sql="  select ( select jobtitlemark from hrmjobtitles where id=t.jobtitle) as jobtitlemark,departmentid  from hrmresource t where t.id=?";
          JAVATools.SetCmdwritelog(logtitle+"-获取用户的部门和岗位id",sql+"  params:"+userid);
            rs.executeQuery(sql,userid);
            if(rs.next()){
                jobtitle=Util.null2String(rs.getString("jobtitlemark"));
                deptid=Util.null2String(rs.getString("departmentid"));
            }
            if("".equals(jobtitle)&&"".equals(deptid)){
                JAVATools.Apiretrun("200","0");
            }
            //做角色判断
            if(!"".equals(jobtitle)) {
                value = GetGwtoUser(jobtitle, deptid);
                if (!"".equals(value)) {
                    return JAVATools.Apiretrun("200", value);
                }
            }

            //做部门判断
            if(!"".equals(deptid)){
                value=  GetBmtoUser(deptid);
                if(!"".equals(value)){
                    return JAVATools.Apiretrun("200",value);
                }
            }

            value="0";


        }catch (Exception e) {
            e.printStackTrace();

        }

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

    public String GetGwtoUser(String gwid,String deptid){
        String value="";
        String gwqcbm="";
        RecordSet rs=new RecordSet();
        String detailsql="  select t.gwqcbm,t.bxrysx from uf_zbclbxgwpp t where t.gw like '%"+gwid+"%'";
        JAVATools.SetCmdwritelog(logtitle+"-根据用户的岗位判断人员",detailsql);
        rs.execute(detailsql);
        if(rs.next()){
            value=Util.null2String(rs.getString("bxrysx"));
            gwqcbm=Util.null2String(rs.getString("gwqcbm"));
        }
        if("".equals(value)||"".equals(gwqcbm)){
            return value;
        }
      if(!JAVATools.DeptContain(deptid,gwqcbm)){
            return value;
      }
      return value;

    }

    public String GetBmtoUser(String deptid){
        RecordSet rs=new RecordSet();
        String value="";
        String bmsql="  select t.bxrysx from uf_zbclbxgwpp t where  ','||t.bm||',' like '%,"+deptid+",%'";
        JAVATools.SetCmdwritelog(logtitle+"-根据用户的部门判断人员",bmsql);
        rs.execute(bmsql);
        if(rs.next()){
            value=Util.null2String(rs.getString("bxrysx"));
        }
        return value;
    }


    /**
     * 流程审批链(非前端传参方式)
     * **/
    @POST
    @Path("/Gettreval")
    @Produces({MediaType.TEXT_PLAIN})
    public String Gettreval(@Context HttpServletRequest request, @Context HttpServletResponse response){
        String requestid = request.getParameter("requestid");
        String detailid = request.getParameter("detailid");
        if("".equals(requestid) && "".equals(detailid)){
            return  JAVATools.Apiretrun("400","参数为空");
        }
        UfZbbxandclsplService ufZbbxandclsplService=getService();
        JSONObject jsonObject=  ufZbbxandclsplService.setFormInnerValue(requestid,detailid);
        if(jsonObject!=null&&"".equals(jsonObject.toJSONString())){
          return  JAVATools.Apiretrun("400","查询数据为空");
        }

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


    /**
     * 流程审批链
     * **/

    @POST
    @Path("/GetjsontotrevalNew")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({MediaType.TEXT_PLAIN})
    public String GetjsontotrevalNew(TravelLinkreq travelLinkreq){
        JSONObject jsonObject = travelLinkreq.getValue();
        String unnumber = travelLinkreq.getUnnumber();
        String nodeunnumber = travelLinkreq.getNodeunnumber();
        JAVATools.SetCmdwritelog("总部差旅报销审批链:"+jsonObject.toJSONString());
        if(jsonObject==null||jsonObject.size()<=0||"".equals(jsonObject)){
            return  JAVATools.Apiretrun("400","参数为空");
        }
        UfZbbxandclsplService ufZbbxandclsplService=getService();
       // JSONObject jsonObject=value;

        if(jsonObject.size()<0){
            return  JAVATools.Apiretrun("400","参数为空");
        }
        List list=new ArrayList();
        for (int j=0;j<jsonObject.size();j++) {
            List detaillist=new ArrayList();
           // Map<String, Object> mapdetail = new HashMap<>();
           String detail = "detail" + j;
            String jsonarraystr = jsonObject.get(detail).toString();
            JSONArray jsonArray = (JSONArray) JSONObject.parse(jsonarraystr);
            if (jsonArray.size() > 0) {
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonobject = (JSONObject) jsonArray.get(i);
                    Map<String, Object> map = JSON.parseObject(jsonobject.toString(), HashMap.class);
                    detaillist.add(map);
                }
            }
            if (detaillist.size() > 0) {
                list.add(detaillist);
            }
        }

        JSONObject retjsonObject=  ufZbbxandclsplService.NewGetTravellist(list,unnumber,nodeunnumber);

        if(jsonObject!=null&&"".equals(retjsonObject.toJSONString())){
            return  JAVATools.Apiretrun("400","查询数据为空");
        }

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

    /**
     * 流程审批链
     * **/
    @POST
    @Path("/Getjsontotreval")
    @Produces({MediaType.TEXT_PLAIN})
    public String Getjsontotreval(@Context HttpServletRequest request, @Context HttpServletResponse response){
        String value = request.getParameter("value");
        String unnumber = request.getParameter("unnumber");
        String nodeunnumber = request.getParameter("nodeunnumber");

        //   jsonObject.get

        if("".equals(value)){
            return  JAVATools.Apiretrun("400","参数为空");
        }
        UfZbbxandclsplService ufZbbxandclsplService=getService();
        JSONObject jsonObject=JSONObject.parseObject(value);

        if(jsonObject.size()<0){
            return  JAVATools.Apiretrun("400","参数为空");
        }
        List list=new ArrayList();
        for (int j=0;j<jsonObject.size();j++) {
            List detaillist=new ArrayList();
            // Map<String, Object> mapdetail = new HashMap<>();
            String detail = "detail" + j;
            String jsonarraystr = jsonObject.get(detail).toString();
            JSONArray jsonArray = (JSONArray) JSONObject.parse(jsonarraystr);
            if (jsonArray.size() > 0) {
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonobject = (JSONObject) jsonArray.get(i);
                    Map<String, Object> map = JSON.parseObject(jsonobject.toString(), HashMap.class);
                    detaillist.add(map);
                }
            }
            if (detaillist.size() > 0) {
                list.add(detaillist);
            }
        }

        JSONObject retjsonObject=  ufZbbxandclsplService.NewGetTravellist(list,unnumber,nodeunnumber);

        if(jsonObject!=null&&"".equals(retjsonObject.toJSONString())){
            return  JAVATools.Apiretrun("400","查询数据为空");
        }

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



    /**
     * 查询费用会计节点
     * */
    @POST
    @Path("/Getfykj")
    @Produces({MediaType.TEXT_PLAIN})
    public String Getfykj(@Context HttpServletRequest request, @Context HttpServletResponse response){
        String unnumber = Util.null2String(request.getParameter("unnumber"));
        String fysjcdbm = Util.null2String(request.getParameter("fysjcdbm"));

        if("".equals(unnumber)){
            return  JAVATools.Apiretrun("400","参数为空");
        }
        UfZbbxandclsplDaoImpl ufZbbxandclsplDao=new UfZbbxandclsplDaoImpl();
       String fykjStr= ufZbbxandclsplDao.GetFykjtoHrmid(fysjcdbm,unnumber);
       if("".equals(fykjStr)){
           return JAVATools.Apiretrun("40","");
       }
        String value="";
       List list= Arrays.stream(fykjStr.split(",")).distinct().collect(Collectors.toList());
       if(list.size()>=2){
           value=new PropBean().getPropName("travelexpense_accountant");
       }else if(list.size()>=1){
           value=(String)list.stream().collect(Collectors.joining(""));
       }
          return JAVATools.Apiretrun("200",value);

    }

//  查询补贴接口
    /**
     * @return
     */
    @POST
    @Path("/Gettravelsubsidy")
    @Produces({MediaType.APPLICATION_JSON})
    public String Gettravelsubsidy(@Context HttpServletRequest request, @Context HttpServletResponse response){
     String usercode=Util.null2String(request.getParameter("usercode"));
     String startdate=Util.null2String(request.getParameter("startdate"));
     String enddate=Util.null2String(request.getParameter("enddate"));
     String type = Util.null2String(request.getParameter("type"));
//        if("".equals(usercode)){
//         Apireturn.returnmes(400,"fail",new net.sf.json.JSONArray());
//        }
        Map<String,String> map=new HashMap<>();
      //  Arrays.stream(usercode.split(",")).map(item->"'"+item+"'").collect(Collectors.joining(","))

        map.put("usercode", usercode);
        map.put("startdate",startdate);
        map.put("enddate",enddate);
        map.put("type",type);
        GettravelsubsidyService gettravelsubsidyService= gettravelsubsidyService();
        return gettravelsubsidyService.GettavelSubsiday(map).toString();
    }

    //判断流程出差关联
    @POST
    @Path("/Gettravlapplction")
    @Produces({MediaType.APPLICATION_JSON})
    public String Gettravlapplction(@Context HttpServletRequest request, @Context HttpServletResponse response){
        String travelcodeids=Util.null2String(request.getParameter("travelcodeids"));
        String unnumber=Util.null2String(request.getParameter("unnumber"));
        String requestid=Util.null2String(request.getParameter("requestid"));

        if("".equals(travelcodeids) || "".equals(unnumber)){
            return JAVATools.Apiretrun("400","");

            //(400,"数据为空",new net.sf.json.JSONArray());
        }
       GettravlapplctionService gettravlapplctionService=gettravlapplctionService();
        JSONArray jsonArray=gettravlapplctionService.traveldistctcclc(travelcodeids,unnumber,requestid);
        if(jsonArray==null||jsonArray.size()<=0){
            return JAVATools.Apiretrun("200","");
        }else{
              return JAVATools.Apiretrun("200",jsonArray);
        }
    }

}
