package weaver.interfaces.htsc.duban;

import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.collaboration.service.TaskLogService;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
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.comInfo.PropBean;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class TaskUserUpdateXzModeExpand extends AbstractModeExpandJavaCodeNew {
    /**
     * 执行模块扩展动作
     * @param param
     *  param包含(但不限于)以下数据
     *  user 当前用户
     *  importtype 导入方式(仅在批量导入的接口动作会传输) 1 追加，2覆盖,3更新，获取方式(int)param.get("importtype")
     *  导入链接中拼接的特殊参数(仅在批量导入的接口动作会传输)，比如a=1，可通过param.get("a")获取参数值
     *  页面链接拼接的参数，比如b=2,可以通过param.get("b")来获取参数
     * @return
     */
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        BaseBean beanBean = new BaseBean();

        result = reloadUser(param);

        if("true".equals(result.get("flag"))){
            result = ToSmsUser(param);
        }
        beanBean.writeLog("任务结束查看！！！！"+result.get("flag"));
        return result;
    }


    /***
     *
     * @param param
     * @return
     */
    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 distinctjbr ="";
        String distincttzydr ="";
        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());
                if(billid>0){

                    String mainid = "" ;
                    String rwlx = "" ;
                    String xzsy = "" ;
                    String dbsy = "" ;
                    String sql =" select xzsy,dbsy,rwlx,jbr,gzr,cjr from uf_rw where id="+billid;
                    beanBean.writeLog("协作根据任务查询主表id："+sql);
                    rs.execute(sql);
                    if(rs.next()){
                        rwlx = rs.getString("rwlx");
                        xzsy = rs.getString("xzsy");
                        dbsy = rs.getString("dbsy");
                    }
                    beanBean.writeLog("rwlx："+rwlx);
                    if("0".equals(rwlx))  //协助
                    {
                        if(!"".equals(xzsy)){
                            String rwids = "" ;
                            sql = " select * from uf_rw where xzsy="+xzsy;
                            rs.execute(sql);
                            while (rs.next()){
                                String rwid = Util.null2String(rs.getString("id"));
                                rwids += rwids==""? rwid : ","+rwid;
                            }
                            sql = " update uf_xzsy set rwdx='"+rwids+"' where id="+xzsy;
                            rs.execute(sql);
                        }
                    }else if("1".equals(rwlx)){  //督办

                        if(!"".equals(dbsy)){
                            String rwids = "" ;
                            sql = " select * from uf_rw where dbsy="+dbsy;
                            rs.execute(sql);
                            while (rs.next()){
                                String rwid = Util.null2String(rs.getString("id"));
                                rwids += rwids==""? rwid : ","+rwid;
                            }
                            sql = " update uf_xzsx set rwdx='"+rwids+"' where id="+dbsy;
                            rs.execute(sql);
                        }
                    }


                    try{
                        rwRefactor(billid,user);
                    }catch (Exception e){
                        beanBean.writeLog("e22:"+e);
                    }

//                    if("0".equals(rwlx))
//                    {
//                        mainid = xzsy ;
//                        String tzydr="";
//                        String jbr = "" ;
//                        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 = Util.null2String(rs.getString("jbr"));
//                            tzydr = Util.null2String(rs.getString("gzr"));
//                        }
//                        //获取所有经办人和通知阅读人的主次账号
//                        distinctjbr = disinctry(jbr.split(","));
//                        distincttzydr = disinctry(tzydr.split(","));
//                        String jbrzzhyc= JAVATools.GetZctogid(distinctjbr);
//                        String tzydzzhyc=JAVATools.GetZctogid(distincttzydr);
//
//                        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);
//
//                        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 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 context="";
        String sjlx="0";//1是评论，0是反馈
        String jllx="1";//1是任务，0是协作
        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());
                if(billid>0){

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

                    if("0".equals(rwlx)){
                        mainid = xzsy ;
                    }else if("1".equals(rwlx)){
                        mainid = dbsy ;
                    }
                    if(!"".equals(cjzt) && !"0".equals(cjzt) && !"-1".equals(cjzt)){
                        result.put("flag", "true");
                        return result;
                    }
                    context="任务标题：\""+rwbt+"\"";
                    TaskLogService taskLogService= new TaskLogService();
                    String title = "新建了任务";
                    taskLogService.createTaskLog(user,mainid,"",jllx,sjlx,context,title);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("errmsg","自定义出错信息"+e.getMessage());
            result.put("flag", "false");
        }
        return result;
    }


    public void rwRefactor(int billid,User user){
        RecordSet recordset = new RecordSet();
        BaseBean bb = new BaseBean();
        bb.writeLog("rwRefactor");
        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= JAVATools.GetZctogid(jbr);
        }
        if(!"".equals(gzr)){
            maingzr= JAVATools.GetZctogid(gzr);
        }

        if(!"".equals(cjr)){
            maincjr= JAVATools.GetZctogid(cjr);
        }

        String sql_update=" update uf_rw set jbrzzhyc=?,tzydzzhyc=?,cjrzczhyc=? where id="+billid;
        bb.writeLog("sql_update:"+sql_update);
        recordset.executeQuery(sql_update,mainjbr,maingzr,maincjr);
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        JSONObject jsonObject = propBean.getModeInfoByUUID(propBean.getPropName("UF_RW_CUBEUUID"));

        int modeid =  Integer.parseInt(jsonObject.get("modeid").toString());
        bb.writeLog("modeid:"+modeid);

        bb.writeLog("userid:"+user.getUID());
        bb.writeLog("modeid:"+modeid);
        bb.writeLog("billid:"+billid);
        modeRightInfo.editModeDataShare(user.getUID(),modeid,billid);
    }

    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);
        return ryvalue;
    }
}
