package weaver.interfaces.htsc.cooperation.task;

import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.collaboration.service.TaskLogService;
import com.sap.db.jdbc.Hash;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.GlobatTools.Smstools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.soa.workflow.request.RequestInfo;

import java.util.*;
import java.util.stream.Collectors;

public class TaskTools {


    public Map<String, String> reloadUser(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        RecordSet rs = new RecordSet();
        PropBean propBean=new PropBean();
        BaseBean beanBean = new BaseBean();
        beanBean.writeLog("开始做协作任务人员同步！！！！");
        String jbr="";
        String tzydr="";
        String distinctjbr="";
        String distincttzydr="";
        String mainid="";

        try {
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo)param.get("RequestInfo");
            User user = (User)param.get("user");
            if(requestInfo!=null){
                billid = Util.getIntValue(requestInfo.getRequestid());
                /*  modeid = Util.getIntValue(requestInfo.getWorkflowid());*/
                if(billid>0){
                    //第一步 查询主表的id
                    mainid= sqlmaintableid(billid);
                    //协作主账号明细表
                    rwRefactor(billid,user);
                    if(mainid==""){
                      //  result.put("errmsg","获取主表id为空");
                        result.put("flag", "true");
                        return result;
                    }
                    //将所有的协作任务里面的近半人主次账号放入

                    String sqlvalue=" select wm_concat_old(jbr) as jbr,wm_concat_old(gzr) as gzr from uf_rw  where xzsy='"+mainid+"'";
                    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(","));
                    String jbrzzhyc=JAVATools.GetZctogid(distinctjbr);
                    String tzydzzhyc=JAVATools.GetZctogid(distincttzydr);
                    //    " jbr,tzydr"GetZctogid GetZctogid
                    String sqlupdatevalue="update uf_xzsy set jbr=?,tzydr=?,jbrzzhyc=?,tzydzzhyc=?  where id='"+mainid+"'";
                    beanBean.writeLog("协作跟新语句："+sqlupdatevalue+" jbr:"+distinctjbr+" tzydr:"+distincttzydr+" jbrzzhyc:"+jbrzzhyc+" tzydzzhyc:"+tzydzzhyc);
                    rs.executeUpdate(sqlupdatevalue, distinctjbr,distincttzydr,jbrzzhyc,tzydzzhyc);

                 //   if(getufzxxzzt(mainid)){
                        ModeRightInfo modeRightInfo = new ModeRightInfo();
                        JSONObject jsonObject=propBean.getModeInfoByUUID(propBean.getPropName("UF_XZSY_CUBEUUIUD"));
                        int nodeid=  Integer.parseInt(jsonObject.get("modeid").toString());
                        modeRightInfo.editModeDataShare(user.getUID(),nodeid,Integer.parseInt(mainid));
                    //       }
                        result.put("flag", "true");



                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("errmsg","自定义出错信息"+e.getMessage());
            result.put("flag", "false");
        }
        return result;
    }


  /*  public void rwryzcloa (String mainid){
        RecordSet rs=new RecordSet();
        String sqlvalue=" select jbr as jbr,gzr as gzr from uf_rw  where xzsy='"+mainid+"'";
        rs.execute(sqlvalue) ;
        if(rs.getCounts()<=0){
            JAVATools.SetCmdwritelog("协作事宜主次账号问题",sqlvalue);
            return;
        }
        if(rs.next()){

        }

    }*/

    public void rwRefactor(int billid,User user){
        RecordSet recordset=new RecordSet();
        PropBean propBean=new PropBean();
        String jbr="";
        String gzr="";
        String cjr="";
        String mainjbr="";
        String maingzr="";
        String maincjr="";
        String sql=" select jbr,gzr,cjr from uf_rw where id="+billid;
        recordset.execute(sql);
        if (recordset.next()) {
            jbr=Util.null2String(recordset.getString("jbr"));
            gzr=Util.null2String(recordset.getString("gzr"));
            cjr=Util.null2String(recordset.getString("cjr"));
        }
        if(!"".equals(jbr)){
        //    mainjbr=getAllhrmmainid(jbr);
            mainjbr=JAVATools.GetZctogid(jbr);
        }
        if(!"".equals(gzr)){
           // maingzr=getAllhrmmainid(gzr);
            maingzr=JAVATools.GetZctogid(gzr);
        }

        if(!"".equals(cjr)){
            // maingzr=getAllhrmmainid(gzr);
            maincjr=JAVATools.GetZctogid(cjr);
        }
        String sql_update="update uf_rw set jbrzzhyc=?,tzydzzhyc=?,cjrzczhyc=? where id="+billid;
        recordset.executeQuery(sql_update,mainjbr,maingzr,maincjr);
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        JSONObject jsonObject=propBean.getModeInfoByUUID(propBean.getPropName("UF_RW_CUBEUUID"));
        int nodeid=  Integer.parseInt(jsonObject.get("modeid").toString());
        modeRightInfo.editModeDataShare(user.getUID(),nodeid,billid);
    }

    public String getAllhrmmainid(String value){
        String hrmmaindistid="";
        if("".equals(value)){
            return "";
        }
        for (String hrmidvalue:value.split(",")) {
            hrmmaindistid+=JAVATools.getmainhrmid(hrmidvalue)+",";
        }
        if(hrmmaindistid.length()>1){
            hrmmaindistid=hrmmaindistid.substring(0,hrmmaindistid.length()-1);
        }
        return  hrmmaindistid;
    }

    public boolean getufzxxzzt(String id){
        RecordSet rs=new RecordSet();
        boolean flag=false;
        try {
            String sql = "select xzzt from  uf_xzsy where id='" + id + "'";
            rs.execute(sql);
            if (rs.next()) {
                String xzzt = Util.null2String(rs.getString("xzzt"));
                if ("1".equals(xzzt)) {
                    flag = true;
                }
            }
        }catch (Exception exo){
            System.out.println("获取协作事宜报错" + exo);
        }

        return  flag;
    }


    public Map<String, String> ToSmsUser(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        RecordSet rs = new RecordSet();
        BaseBean beanBean = new BaseBean();
        beanBean.writeLog("开始做协作任务消息发送！！！！");
        Map<String, String> rwMap = new HashMap<String, String>();

        String jbr="";
        String gzr="";
        String rylist="";
        String rwbt="";
        String context="";
        String linkUrl="";
        String LinkModelUrl="";
        String xgwj="";
        String rwbh="";
        String sjlx="0";//1是评论，0是反馈
        String jllx="1";//1是任务，0是协作
        String mainid="";
        String rwzt="";
        String cjzt="";
        try {
            User user = (User)param.get("user");
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo)param.get("RequestInfo");


            if(requestInfo!=null){
                billid = Util.getIntValue(requestInfo.getRequestid());
                /*  modeid = Util.getIntValue(requestInfo.getWorkflowid());*/
                if(billid>0){
                    //第一步 查询主表的id
                    rwMap= getmaintableInfo(billid);
                    mainid=rwMap.get("mainid");
                    cjzt=rwMap.get("cjzt");
                    rwzt=rwMap.get("rwzt");
                    if(mainid==""){
                       // result.put("errmsg","获取主表id为空");
                        result.put("flag", "true");
                        return result;
                    }
                    if(!"".equals(cjzt)&&!"0".equals(cjzt)&&!"-1".equals(cjzt)){
                        //result.put("data", "任务状态不为0");
                        result.put("flag", "true");
                        return result;
                    }

                    String sqlvalue="select  * from uf_rw  where id="+billid;
                    rs.execute(sqlvalue);
                    if(rs.next()){
                        jbr= Util.null2String(rs.getString("jbr"));
                        gzr= Util.null2String(rs.getString("gzr"));
                        rwbt = Util.null2String(rs.getString("rwbt"));
                        xgwj = Util.null2String(rs.getString("xgwj"));
                        rwbh = Util.null2String(rs.getString("rwbh"));
                    }
                    rylist= getarray(jbr,gzr);
                    context="任务标题：\""+rwbt+"\"";
                    TaskLogService taskLogService=new TaskLogService();
                    mainid= sqlmaintableid(billid);
                    String title="新建了任务";
                    taskLogService.createTaskLog(user,mainid,"",jllx,sjlx,context,title);
                    Set<String> setuseridvalue=new HashSet<>();
                        if(rylist.length()>0) {
                            setuseridvalue = new HashSet(Arrays.asList(rylist.split(",")));
                        }
                    Smstools smstools=new Smstools();
                    PropBean propBean = new PropBean();
                    result.put("flag", "true");


                   /* int smstypeid=Integer.parseInt(Util.null2String(propBean.getPropName("uf_ry_sms")));
                    JSONObject jsonObject=propBean.getModeInfoByUUID(propBean.getPropName("UF_RW_CUBEUUID"));
                 if(jsonObject.size()<=0){
                     result.put("errmsg","消息失败UF_RW_CUBEUUID");
                     result.put("flag", "false");
                     return result;
                 }
                    String rwmodeid=jsonObject.get("modeid").toString();
                    String formid=jsonObject.get("formid").toString();
                    String link="";
                    if(setuseridvalue.size()>0){
                        boolean flag=smstools.sendMessage(smstypeid,setuseridvalue,title,context,link,link);
                        if(flag==false){
                            result.put("errmsg","消息发送失败");
                            result.put("flag", "false");
                        }
                    }*/

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("errmsg","自定义出错信息"+e.getMessage());
            result.put("flag", "false");
        }
        return result;
    }

    public String sqlmaintableid(int billid){
        RecordSet rs = new RecordSet();
        BaseBean beanBean = new BaseBean();
        String mainid="";

        String sql=" select  xzsy from uf_rw where id="+billid;
        beanBean.writeLog("协作根据任务查询主表id："+sql);
        rs.execute(sql);
        if(rs.next()){
            mainid=rs.getString("xzsy");
        }
        return mainid;
    }


    public Map<String,String> getmaintableInfo(int billid){
        RecordSet rs = new RecordSet();
        Map<String,String> maplist= new HashMap<>();
        BaseBean beanBean = new BaseBean();
        String mainid="";

        String sql=" select  xzsy,cjzt,rwzt from uf_rw where id="+billid;
        beanBean.writeLog("协作根据任务查询主表id："+sql);
        rs.execute(sql);
        if(rs.next()){
            maplist.put("mainid",Util.null2String(rs.getString("xzsy")));
            maplist.put("cjzt",Util.null2String(rs.getString("cjzt")));
            maplist.put("rwzt",Util.null2String(rs.getString("cjzt")));

        }
        return maplist;
    }

    public String disinctry(String[] array){

        if(array==null||array.length<0){
            return "";
        }
        String ryvalue="";
        List<String> listarray= Arrays.asList(array);
        List list=(List) listarray.stream().distinct().collect(Collectors.toList());
        ryvalue=String.join(",", list);
    /*    for (String value:array) {
            if(value!=""&&ryvalue.indexOf(value)<0){
                ryvalue+=value+",";
            }
        }
        if(ryvalue.length()>1){
            ryvalue=ryvalue.substring(0,ryvalue.length()-1);
        }*/
        return ryvalue;
    }


    public String getarray(String jbr,String gzr){
        String rylist="";
        if(jbr==""&&gzr==""){
            return rylist;
        }else if(jbr==""&&gzr!=""){
            rylist=gzr;
        }else if(jbr!=""&&gzr==""){
            rylist=jbr;
        }else{
            rylist=jbr+","+gzr;
        }
        return rylist;
    }
}
