package com.engine.htsc.cooperation;

import com.alibaba.fastjson.JSON;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cooperation.task.TaskTools;
import weaver.soa.workflow.request.RequestInfo;

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.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ReloadXzUser {


    @POST
    @Path("/reloadzUser")
    @Produces({MediaType.TEXT_PLAIN})
    public String reloadzUser(@Context HttpServletRequest request, @Context HttpServletResponse response){
        PropBean pb = new PropBean();

        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            String xzbillid = Util.null2String(request.getParameter("billid"));
            if( xzbillid==""||Util.getIntValue(xzbillid)<0){
                apidatas.put("flag", "false");
                return  JSON.toJSONString(apidatas);
            }
            Map<String,Object> map=new HashMap<>();
            RequestInfo requestinfo=new RequestInfo();
            requestinfo.setRequestid(xzbillid);
            map.put("RequestInfo",requestinfo);
            RecordSet rs = new RecordSet();
            BaseBean beanBean = new BaseBean();
            beanBean.writeLog("开始做协作任务人员同步！！！！");
            String jbr="";
            String tzydr="";
            String distinctjbr="";
            String distincttzydr="";
            TaskTools tasktools=new TaskTools();
            String sqlvalue=" select wm_concat_old(jbr) as jbr,wm_concat_old(gzr) as gzr from uf_rw  where xzsy='"+xzbillid+"'";
            beanBean.writeLog("协作查询所有jbr和gzr的sql："+sqlvalue);
            rs.execute(sqlvalue);
            if(rs.next()){
                jbr=rs.getString("jbr").toString();
                tzydr=rs.getString("gzr").toString();
            }
            distinctjbr=disinctry(jbr.split(","));
            distincttzydr=disinctry(tzydr.split(","));
            //    " jbr,tzydr"
            String sqlupdatevalue="update uf_xzsy set jbr=?,tzydr=?   where id='"+xzbillid+"'";
            beanBean.writeLog("协作跟新语句："+sqlupdatevalue+" jbr:"+distinctjbr+"tzydr:"+distincttzydr);
            rs.executeUpdate(sqlupdatevalue, distinctjbr,distincttzydr);
            apidatas.put("flag", "true");


        }catch (Exception e) {
            apidatas.put("flag", "false");
            apidatas.put("error", e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }
    public String disinctry(String[] array){

        if(array==null||array.length<0){
            return "";
        }
        String ryvalue="";
        List<String> listarray= java.util.Arrays.asList(array);
        List list=(List) listarray.stream().distinct().collect(Collectors.toList());
        ryvalue=String.join(",", list);

        return ryvalue;
    }
}
