<%@ page import="weaver.general.*" %>
<%@page import="weaver.conn.RecordSet"%>
<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.formmode.setup.ModeRightInfo" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.*" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>

<%
    BaseBean bb = new BaseBean();
    String uf_tablename = "uf_zdyszb";
    RecordSet rs = new RecordSet();
    String datas = Util.null2String(request.getParameter("datas"));
    String pxfs = "";
    bb.writeLog("datas:"+datas);
    String data = "";
    if(StringUtils.isNotEmpty(datas)){
        JSONObject jsonObject = JSONObject.parseObject(datas);
        pxfs = jsonObject.getString("pxfs");
        data = jsonObject.getString("data");
    }

    User user  = HrmUserVarify.getUser(request , response) ;
    int userid = user.getUID();
    String billid = "";
    String sql = " select * from "+uf_tablename+" where ry = ?" ;
    rs.executeQuery(sql,new Object[]{userid});
    if(rs.next()){
        billid = Util.null2String(rs.getString("id"));
    }
    bb.writeLog("billid:"+billid);
    bb.writeLog("pxfs:"+pxfs);
    boolean api_status = false;
    if(StringUtils.isNotEmpty(data))
    {
        if(StringUtils.isEmpty(billid)){
            api_status = insertCustomMenuInfo(data,pxfs,userid,uf_tablename);
        }else{
            api_status = updateCustomMenuInfo(billid,data,pxfs,uf_tablename);
        }
    }

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("api_status",api_status);
    jsonObject.put("userid",user.getUID());
    jsonObject.put("lastname",user.getLastname());
%>
<%=jsonObject.toString()  %>


<%!
    /***
     *
     * @param datas
     * @param pxfs
     * @return
     */
    public boolean insertCustomMenuInfo(String datas,String pxfs,int userid,String uf_tablename)
    {
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        String currentTime = TimeUtil.getCurrentTimeString();
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
        String modedatacreatedate = formatter1.format(new Date());
        String modedatacreatetime = formatter2.format(new Date());
        String modedatacreater = "1";
        String modedatacreatertype = "0";
        String modedatamodifier = "1";
        boolean istrue = false;
        try {

            bb.writeLog("start insertCustomMenuInfo");
            String formmodeid = "";
            //查询模块id
            String formModeSql =" select k.id from modeinfo k  \n" +
                                " inner join workflow_bill l on formid = l.id\n" +
                                " where l.tablename = ? ";
            bb.writeLog("formModeSql:"+formModeSql);
            rs.executeQuery(formModeSql,new Object[]{uf_tablename});
            if (rs.next()) {
                formmodeid = Util.null2String(rs.getString("id"));
            }
            bb.writeLog("formmodeid:"+formmodeid);
            int count = 0;
            String uuid = UUID.randomUUID().toString();
            String sql = " insert into "+uf_tablename+" (formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,modedatamodifier,modedatamodifydatetime,ry,pxfs,uuid) " +
                         " values (?,?,?,?,?,?,?,?,?,?)";
            bb.writeLog("sql:"+sql);
            boolean isTrue = rs.executeUpdate(sql, new Object[]{formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,modedatamodifier,currentTime,userid,pxfs,uuid});
            bb.writeLog("sql:"+sql);
            if(isTrue){
                count++;
            }
            if(count >0)
            {
                String data_id = "" ;
                sql = " select id from "+uf_tablename+" where uuid=? " ;
                bb.writeLog("sql:"+sql);
                rs.executeQuery(sql,new Object[]{uuid});
                bb.writeLog("sql:"+sql);
                if(rs.next()){
                    data_id = Util.null2String(rs.getString("id"));
                }
                if(StringUtils.isNotEmpty(data_id)){
                    modeRightInfo.editModeDataShare(Integer.valueOf(modedatacreater), Integer.parseInt(formmodeid), Integer.parseInt(data_id));
                    bb.writeLog("权限更新成功，:");
                    istrue = true;
                    if(istrue){
                        int errcount = 0;
                        if(StringUtils.isNotEmpty(datas))
                        {
                            JSONArray jsonArray = JSONArray.parseArray(datas);
                            for(int i=0;i<jsonArray.size();i++){
                                JSONObject jsonObject = jsonArray.getJSONObject(i);

                                String iconname = Util.null2String(jsonObject.getString("iconname"));
                                String iconuid = Util.null2String(jsonObject.getString("iconuid"));
                                String iconurl = Util.null2String(jsonObject.getString("iconurl"));
                                String linktype = Util.null2String(jsonObject.getString("linktype"));
                                String mtype = Util.null2String(jsonObject.getString("mType"));
                                String menuid = Util.null2String(jsonObject.getString("menuid"));
                                String menulink = Util.null2String(jsonObject.getString("menulink"));
                                String menuname = Util.null2String(jsonObject.getString("menuname"));
                                String menunamemultlang = Util.null2String(jsonObject.getString("menunamemultlang"));
                                String menutype = Util.null2String(jsonObject.getString("menutype"));
                                String parentid = Util.null2String(jsonObject.getString("parentid"));
                                String syspagemenuid = Util.null2String(jsonObject.getString("syspagemenuid"));
                                String uuid_dt1 = Util.null2String(jsonObject.getString("id"));
                                String sx = i+"";

                                sql = " insert into "+uf_tablename+"_dt1(mainid,iconname,iconuid,iconurl,linktype,mtype,menuid,menulink,menuname,menunamemultlang,menutype,parentid,syspagemenuid,sx,uuid) " +
                                      " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                                bb.writeLog("sql:"+sql);
                                boolean flag = rs.executeUpdate(sql,new Object[]{data_id,iconname,iconuid,iconurl,linktype,mtype,menuid,menulink,menuname,menunamemultlang,menutype,parentid,syspagemenuid,sx,uuid_dt1});
                                bb.writeLog("flag:"+flag);
                                if(!flag){
                                    errcount++;
                                }
                            }
                        }
                        if(errcount >0){
                            istrue = false;
                        }
                    }
                }else{
                    istrue = false;
                }
            }else{
                istrue = false;
            }
        }catch (Exception e){

        }
        return istrue;
    }


    /***
     *
     * @return
     */
    public boolean updateCustomMenuInfo(String billid,String datas,String pxfs,String uf_tablename)
    {
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        boolean istrue = false;
        try {
            int errcount = 0;

            String mainsql = " update "+uf_tablename+" set pxfs=? where id = ?" ;

            boolean flag2 = rs.executeUpdate(mainsql,new Object[]{pxfs,billid});
            bb.writeLog(mainsql);
            bb.writeLog(flag2);
            if(flag2){
                if(StringUtils.isNotEmpty(datas))
                {
                    if("[]".equals(datas)){

                        String sql = " delete from "+uf_tablename+"_dt1 where mainid = ? ";
                        bb.writeLog("sql:"+sql);
                        boolean flag = rs.executeUpdate(sql,new Object[]{billid});
                        bb.writeLog("flag:"+flag);

                    }else{
                        JSONArray jsonArray = JSONArray.parseArray(datas);
                        String dataids = "" ;
                        for(int i=0;i<jsonArray.size();i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            String dataid = jsonObject.getString("id");
                            if (StringUtils.isNotEmpty(dataid)) {
                                dataids += StringUtils.isEmpty(dataids) ? "'"+dataid+"'" :",'"+dataid+"'" ;
                            }
                        }
                        if(StringUtils.isNotEmpty(dataids)){
                            String sql = " delete from "+uf_tablename+"_dt1 where mainid = ? and uuid not in("+dataids+") ";
                            bb.writeLog("sql:"+sql);
                            boolean flag = rs.executeUpdate(sql,new Object[]{billid});
                            bb.writeLog("flag:"+flag);
                        }

                        for(int i=0;i<jsonArray.size();i++){
                            JSONObject jsonObject = jsonArray.getJSONObject(i);

                            String iconname = Util.null2String(jsonObject.getString("iconname"));
                            String iconuid = Util.null2String(jsonObject.getString("iconuid"));
                            String iconurl = Util.null2String(jsonObject.getString("iconurl"));
                            String linktype = Util.null2String(jsonObject.getString("linktype"));
                            String mtype = Util.null2String(jsonObject.getString("mType"));
                            String menuid = Util.null2String(jsonObject.getString("menuid"));
                            String menulink = Util.null2String(jsonObject.getString("menulink"));
                            String menuname = Util.null2String(jsonObject.getString("menuname"));
                            String menunamemultlang = Util.null2String(jsonObject.getString("menunamemultlang"));
                            String menutype = Util.null2String(jsonObject.getString("menutype"));
                            String parentid = Util.null2String(jsonObject.getString("parentid"));
                            String syspagemenuid = Util.null2String(jsonObject.getString("syspagemenuid"));
                            String uuid = Util.null2String(jsonObject.getString("id"));
                            String sx = i+"";
                            bb.writeLog("uuid:"+uuid);
                            bb.writeLog("sx:"+sx);
                            if(StringUtils.isNotEmpty(uuid))
                            {
                                int count = 0;
                                String sql = " select * from "+uf_tablename+"_dt1 where uuid = ?" ;
                                rs.executeQuery(sql,new Object[]{uuid});
                                if(rs.next()){
                                    count++;
                                }
                                bb.writeLog("count:"+count);

                                if(count > 0){
                                    sql = " update "+uf_tablename+"_dt1 set iconname=?,iconuid=?,iconurl=?,linktype=?,mtype=?,menuid=?,menulink=?,menuname=?,menunamemultlang=?,menutype=?,parentid=?,syspagemenuid=?,sx=? where uuid = ? ";
                                    boolean flag = rs.executeUpdate(sql,new Object[]{iconname,iconuid,iconurl,linktype,mtype,menuid,menulink,menuname,menunamemultlang,menutype,parentid,syspagemenuid,sx,uuid});
                                    bb.writeLog("flag:"+flag);
                                    if(!flag){
                                        errcount++;
                                    }
                                }else{
                                    sql = " insert into "+uf_tablename+"_dt1(mainid,iconname,iconuid,iconurl,linktype,mtype,menuid,menulink,menuname,menunamemultlang,menutype,parentid,syspagemenuid,sx,uuid) " +
                                            " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                                    bb.writeLog("sql:"+sql);
                                    boolean flag = rs.executeUpdate(sql,new Object[]{billid,iconname,iconuid,iconurl,linktype,mtype,menuid,menulink,menuname,menunamemultlang,menutype,parentid,syspagemenuid,sx,uuid});
                                    bb.writeLog("flag:"+flag);
                                    if(!flag){
                                        errcount++;
                                    }
                                }
                            }else{
                                String sql = " insert into "+uf_tablename+"_dt1(mainid,iconname,iconuid,iconurl,linktype,mtype,menuid,menulink,menuname,menunamemultlang,menutype,parentid,syspagemenuid,sx,uuid) " +
                                        " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                                bb.writeLog("sql:"+sql);
                                boolean flag = rs.executeUpdate(sql,new Object[]{billid,iconname,iconuid,iconurl,linktype,mtype,menuid,menulink,menuname,menunamemultlang,menutype,parentid,syspagemenuid,sx,uuid});
                                bb.writeLog("flag:"+flag);
                                if(!flag){
                                    errcount++;
                                }
                            }
                        }
                    }
                }
            }

            if(errcount >0){
                istrue = false;
            }else{
                istrue = true;
            }
        }catch (Exception e){

        }
        return istrue;
    }

%>



