package com.engine.htsc.fzjgtravel.Web;

import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;

import com.engine.govern.util.ParamsUtil;
import com.engine.htsc.fzjgtravel.Service.Impl.SubsidyServiceImpl;
import com.engine.htsc.fzjgtravel.Service.SubsidyService;
import org.json.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.DBTools;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.workflow.action.Action;

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

public class SubsidyAction {

    private SubsidyService getService(){
        return ServiceUtil.getService(SubsidyServiceImpl.class);
    }

    @POST
    @Path("/Getsubsidy")
    @Produces({MediaType.APPLICATION_JSON})
    public String GetSubsidy(@Context  HttpServletRequest req, @Context HttpServletRequest rep){
      return  getService().getSubsidyService(ParamUtil.request2Map(req));
    }

    @POST
    @Path("/travelflowreturn")
    @Produces({MediaType.APPLICATION_JSON})
    public String  TravelFlowReturnAction(@Context HttpServletRequest request, @Context HttpServletRequest rep){

        String requestid = request.getParameter("requestid");
        RecordSet rs = new RecordSet();
//        String requestid = Util.null2String(requestInfo.getRequestid());
        String tableunnumbber=JAVATools.getmainunnumber(requestid);
        Map<String,Object> map=getUnumber(tableunnumbber);
        if(map==null || map.size()<=0){
            return  JAVATools.Apiretrun("400","失败");
        }
        String ngjdbh=Util.null2String(map.get("ngjdbh"));
        String data=Util.null2String(map.get("sj"));
        if("".equals(ngjdbh) || "".equals(data)){
              return  JAVATools.Apiretrun("400","失败");
        }
        Map<String,String> datamap= getdatamap(data);
        if(datamap==null || datamap.size()<=0){
            return  JAVATools.Apiretrun("200","");
    }
        String ngjdnodeid=JAVATools.getnodeunnumbertoid(ngjdbh);


        String tablename = Util.null2String(JAVATools.getMaintable(requestid));//主表
        String sql="select a.nodeid lastnodeid,a.operator,a.isSubmitDirect lastIsSubmitDirect, a.logtype " +
                "  from workflow_requestlog a, workflow_nownode b " +
                " where a.requestid = b.requestid " +
                "   and a.destnodeid = b.nownodeid " +
                "   and b.requestid =? " +
                "   and a.destnodeid = ? " +
                " order by a.logid desc ";
        Map<String,Object> returnmap= DBTools.getsqltomap(sql,requestid,ngjdnodeid);

        if(returnmap==null || returnmap.size()<=0){

            return  JAVATools.Apiretrun("200","");
        }
        String lastnodeid= Util.null2String(returnmap.get("lastnodeid"));
        String isSubmitDirect= Util.null2String(returnmap.get("lastissubmitdirect"));
        String operator= Util.null2String(returnmap.get("operator"));
        String isnotkeyvalueStr= Util.null2String(datamap.get(lastnodeid));
        if("".equals(isnotkeyvalueStr) || "".equals(operator) || !"1".equals(isSubmitDirect)){
            return JAVATools.Apiretrun("200","");
        }
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(isnotkeyvalueStr.split("_")[1], operator);
            return  JAVATools.Apiretrun("200",jsonObject.toString());
        }catch (Exception exo){

            return  JAVATools.Apiretrun("400","失败");

        }

    }





    private Map<String,Object> getUnumber(String number){
        String sql="select ngjdbh,sj from uf_zdjd where 1=1";
        Map<String,String> paramswhere=new HashMap<>();
        paramswhere.put("zbbh",number);
        return JAVATools.GetTableToMap(sql,paramswhere);
    }

    private  Map<String,String> getdatamap(String data){
        Map<String,String> datamap=new HashMap<>();
        Arrays.stream(data.split(",")).forEach((model)->{
            if(model.split(":").length==2){

                String key=model.split(":")[0];
                String value=model.split(":")[1];
                if(!"".equals(key) && !"".equals(value)){
                    String keynodeid=JAVATools.getnodeunnumbertoid(key);
                    if(!"".equals(keynodeid)){
                        datamap.put(keynodeid,value);
                    }

                }

            }
        });
        return datamap;
    }

    private void updateoperate(Map<String,String> map){
        RecordSet rs=new RecordSet();
        String tablename= Util.null2String(map.get("tablename"));
        String requestid=Util.null2String(map.get("requestid"));
        String datavalue=Util.null2String(map.get("datavalue"));
        if("".equals(tablename) || "".equals(requestid)|| "".equals(datavalue)){
            return ;
        }
        String updateset="";
        if(datavalue.split("_").length==2){
            updateset=datavalue.split("_")[0]+"="+datavalue.split("_")[1]+","+datavalue.split("_")[1]+"=''";
        }else{
            updateset=datavalue+"=''";
        }
        String updatesql="update "+tablename+" set "+updateset+
                " where requestid=?";
        JAVATools.SetCmdwritelog("差旅类流程修改",updatesql+"requestid"+requestid);

        rs.executeUpdate(updatesql,requestid);
    }

    private void updateall(Map<String,String> map,
                           Map<String,String> datavalue){
        RecordSet rs=new RecordSet();
        String tablename=Util.null2String(map.get("tablename"));
        String requestid=Util.null2String(map.get("requestid"));

        if("".equals(tablename) || "".equals(requestid)|| null==datavalue ||datavalue.size()<=0){
            return ;
        }

        String updateset="";
        for (String key:datavalue.keySet()){
            String value=datavalue.get(key);
            if(!"".equals(value)){
//                updateset.append(value.split("_")[0]+"=''");
                updateset += updateset==""?value.split("_")[0]+"=''" : ","+value.split("_")[0]+"=''";
            }
        }

        String updatesql="update "+tablename+" set "+ updateset.toString()+
                " where requestid=?";
        JAVATools.SetCmdwritelog("差旅类流程修改",updatesql+"requestid"+requestid);
        rs.executeUpdate(updatesql,requestid);
    }

}
