package weaver.interfaces.htsc.workflowdoc;

import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.schedule.BaseCronJob;

import java.text.SimpleDateFormat;
import java.util.*;


public class HrmResource2ModeJob extends BaseCronJob {
    @Override
    public void execute() {
        hrmResource2Mode();
    }

    public void hrmResource2Mode()
    {
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        try{
            bb.writeLog("进入--HrmResource2ModeJob--定时器");

            String where = " and l.id in(select id from hrmresource where lastmoddate>=to_char(sysdate-3,'yyyy-mm-dd') \n" +
                           " and lastmoddate <= to_char(sysdate,'yyyy-mm-dd')) ";

            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 formmodeid = "";
            rs.executeQuery( "select m.id from modeinfo m left join WORKFLOW_BILL b on m.formid = b.id where b.tablename = 'uf_grouprelation'" );
            if(rs.next()){
                formmodeid = Util.null2String( rs.getString( "id" ) );
            }

            List<Map<String,String>> deptlist = new ArrayList<Map<String,String>>();
            Map<String,String> deptmap = null;

            String sql =" select l.id,l.workcode,l.departmentid,d.departmentcode\n" +
                        " from hrmresource l\n" +
                        " inner join hrmdepartment d on d.id = l.departmentid\n" +
                        " where l.status=1 "+where;
            rs.execute(sql);
            while (rs.next()){
                String id = Util.null2String(rs.getString("id"));
                String workcode = Util.null2String(rs.getString("workcode"));
                String departmentcode = Util.null2String(rs.getString("departmentcode"));

                deptmap = new HashMap<String,String>();
                deptmap.put("ryid",id);
                deptmap.put("workcode",workcode);
                deptmap.put("groupcode",departmentcode);
                deptmap.put("formmodeid",formmodeid+"");
                deptmap.put("modedatacreater",modedatacreater+"");
                deptmap.put("modedatacreatertype",modedatacreatertype+"");
                deptmap.put("modedatacreatedate",modedatacreatedate+"");
                deptmap.put("modedatacreatetime",modedatacreatetime+"");

                deptlist.add(deptmap);
            }

            bb.writeLog("hrmResource2Mode--开始部门新增");
            try {
                for(int p=0;p<deptlist.size();p++ )
                {
                    Map<String,String> doc_map = deptlist.get(p);
                    String dttablename = "" ;
                    String dttablenvalue = "" ;
                    for (Map.Entry<String, String> entry : doc_map.entrySet())
                    {
                        String fieldname = Util.null2String(entry.getKey());
                        String fieldvalue = Util.null2String(entry.getValue());
                        dttablename += dttablename==""? fieldname :","+fieldname ;
                        dttablenvalue += dttablenvalue=="" ? "'"+fieldvalue+"'" :",'"+fieldvalue+"'" ;
                    }
                    if(dttablename !="" && dttablenvalue !="")
                    {
                        sql = " insert into uf_grouprelation("+dttablename+") values ("+dttablenvalue+")";
                        rs.execute(sql);
                    }
                }
            }catch (Exception e){
                bb.writeLog(e);
            }

            bb.writeLog("hrmResource2Mode--结束部门新增");

            List<Map<String,String>> szjglist = new ArrayList<Map<String,String>>();
            Map<String,String> szjgmap = null;
            List<Map<String,String>> rolelist = new ArrayList<Map<String,String>>();
            Map<String,String> rolemap = null;

            List<Map<String,String>> grouplist = new ArrayList<Map<String,String>>();
            Map<String,String> groupmap = null;


            for(int i=0;i<deptlist.size();i++)
            {
                String szjg = "" ;
                Map<String,String> dept_map = deptlist.get(i);
                String departmentcode = Util.null2String(dept_map.get("groupcode"));
                String workcode = Util.null2String(dept_map.get("workcode"));
                String id = Util.null2String(dept_map.get("ryid"));
                sql = " select t.departmentcode from HrmDepartment t where t.supdepid is null or t.supdepid = 0 start with t.departmentcode='"+departmentcode+"' connect by prior t.supdepid=t.id ";
                rs.execute(sql);
                if(rs.next()){
                    szjg = Util.null2String(rs.getString("departmentcode"));
                }
                szjgmap = new HashMap<String,String>();
                szjgmap.put("ryid",id);
                szjgmap.put("workcode",workcode);
                szjgmap.put("groupcode",szjg);
                szjgmap.put("formmodeid",formmodeid+"");
                szjgmap.put("modedatacreater",modedatacreater+"");
                szjgmap.put("modedatacreatertype",modedatacreatertype+"");
                szjgmap.put("modedatacreatedate",modedatacreatedate+"");
                szjgmap.put("modedatacreatetime",modedatacreatetime+"");
                szjglist.add(szjgmap);

                sql =   " select 'Role_'||d.id as roleid\n" +
                        " from hrmrolemembers l\n" +
                        " inner join hrmroles d on d.id = l.roleid\n" +
                        " where resourceid= "+id;
                rs.execute(sql);
                while (rs.next()){
                    String roleid = Util.null2String(rs.getString("roleid"));
                    rolemap = new HashMap<String,String>();
                    rolemap.put("ryid",id);
                    rolemap.put("workcode",workcode);
                    rolemap.put("groupcode",roleid);
                    rolemap.put("formmodeid",formmodeid+"");
                    rolemap.put("modedatacreater",modedatacreater+"");
                    rolemap.put("modedatacreatertype",modedatacreatertype+"");
                    rolemap.put("modedatacreatedate",modedatacreatedate+"");
                    rolemap.put("modedatacreatetime",modedatacreatetime+"");
                    rolelist.add(rolemap);
                }

                sql =   " select 'Group_'||groupid as groupid \n" +
                        " from hrmgroupmembers \n" +
                        " where userid= "+id;
                rs.execute(sql);
                while (rs.next()){
                    String groupid = Util.null2String(rs.getString("groupid"));
                    groupmap = new HashMap<String,String>();
                    groupmap.put("ryid",id);
                    groupmap.put("workcode",workcode);
                    groupmap.put("groupcode",groupid);
                    groupmap.put("formmodeid",formmodeid+"");
                    groupmap.put("modedatacreater",modedatacreater+"");
                    groupmap.put("modedatacreatertype",modedatacreatertype+"");
                    groupmap.put("modedatacreatedate",modedatacreatedate+"");
                    groupmap.put("modedatacreatetime",modedatacreatetime+"");
                    grouplist.add(groupmap);
                }
            }

            bb.writeLog("hrmResource2Mode--开始机构新增");
            try {
                for(int p=0;p<szjglist.size();p++ )
                {
                    Map<String,String> doc_map = szjglist.get(p);
                    String dttablename = "" ;
                    String dttablenvalue = "" ;
                    for (Map.Entry<String, String> entry : doc_map.entrySet())
                    {
                        String fieldname = Util.null2String(entry.getKey());
                        String fieldvalue = Util.null2String(entry.getValue());
                        dttablename += dttablename==""? fieldname :","+fieldname ;
                        dttablenvalue += dttablenvalue=="" ? "'"+fieldvalue+"'" :",'"+fieldvalue+"'" ;
                    }
                    if(dttablename !="" && dttablenvalue !="")
                    {
                        sql = " insert into uf_grouprelation("+dttablename+") values ("+dttablenvalue+")";
                        rs.execute(sql);
                    }
                }
            }catch (Exception e){
                bb.writeLog(e);
            }
            bb.writeLog("hrmResource2Mode--结束机构新增");

            bb.writeLog("hrmResource2Mode--开始群组新增");
            try {
                for(int p=0;p<grouplist.size();p++ )
                {
                    Map<String,String> doc_map = grouplist.get(p);
                    String dttablename = "" ;
                    String dttablenvalue = "" ;
                    for (Map.Entry<String, String> entry : doc_map.entrySet())
                    {
                        String fieldname = Util.null2String(entry.getKey());
                        String fieldvalue = Util.null2String(entry.getValue());
                        dttablename += dttablename==""? fieldname :","+fieldname ;
                        dttablenvalue += dttablenvalue=="" ? "'"+fieldvalue+"'" :",'"+fieldvalue+"'" ;
                    }
                    if(dttablename !="" && dttablenvalue !="")
                    {
                        sql = " insert into uf_grouprelation("+dttablename+") values ("+dttablenvalue+")";
                        rs.execute(sql);
                    }
                }
            }catch (Exception e){
                bb.writeLog(e);
            }
            bb.writeLog("hrmResource2Mode--结束群组新增");

            bb.writeLog("hrmResource2Mode--开始角色新增");
            try {
                for(int p=0;p<rolelist.size();p++ )
                {
                    Map<String,String> doc_map = rolelist.get(p);
                    String dttablename = "" ;
                    String dttablenvalue = "" ;
                    for (Map.Entry<String, String> entry : doc_map.entrySet())
                    {
                        String fieldname = Util.null2String(entry.getKey());
                        String fieldvalue = Util.null2String(entry.getValue());
                        dttablename += dttablename==""? fieldname :","+fieldname ;
                        dttablenvalue += dttablenvalue=="" ? "'"+fieldvalue+"'" :",'"+fieldvalue+"'" ;
                    }
                    if(dttablename !="" && dttablenvalue !="")
                    {
                        sql = " insert into uf_grouprelation("+dttablename+") values ("+dttablenvalue+")";
                        rs.execute(sql);
                    }
                }
            }catch (Exception e){
                bb.writeLog(e);
            }
            bb.writeLog("hrmResource2Mode--结束角色新增");
        }catch (Exception e){
            bb.writeLog("e:"+e);
        }
    }


    /***
     *
     */
    public void hrmResource2ModeInit(){
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        try{
            bb.writeLog("进入--HrmResource2ModeJob--定时器");

            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 formmodeid = "";
            rs.executeQuery( "select m.id from modeinfo m left join WORKFLOW_BILL b on m.formid = b.id where b.tablename = 'uf_grouprelation'" );
            if(rs.next()){
                formmodeid = Util.null2String( rs.getString( "id" ) );
            }

            List<Map<String,String>> deptlist = new ArrayList<Map<String,String>>();
            Map<String,String> deptmap = null;

            String sql =" select l.id,l.workcode,l.departmentid,d.departmentcode\n" +
                    " from hrmresource l\n" +
                    " inner join hrmdepartment d on d.id = l.departmentid\n" +
                    " where l.status=1 ";
            rs.execute(sql);
            while (rs.next()){
                String id = Util.null2String(rs.getString("id"));
                String workcode = Util.null2String(rs.getString("workcode"));
                String departmentcode = Util.null2String(rs.getString("departmentcode"));

                deptmap = new HashMap<String,String>();
                deptmap.put("ryid",id);
                deptmap.put("workcode",workcode);
                deptmap.put("groupcode",departmentcode);
                deptmap.put("formmodeid",formmodeid+"");
                deptmap.put("modedatacreater",modedatacreater+"");
                deptmap.put("modedatacreatertype",modedatacreatertype+"");
                deptmap.put("modedatacreatedate",modedatacreatedate+"");
                deptmap.put("modedatacreatetime",modedatacreatetime+"");

                deptlist.add(deptmap);
            }

            bb.writeLog("hrmResource2Mode--开始部门新增");
            try {
                for(int p=0;p<deptlist.size();p++ )
                {
                    Map<String,String> doc_map = deptlist.get(p);
                    String dttablename = "" ;
                    String dttablenvalue = "" ;
                    for (Map.Entry<String, String> entry : doc_map.entrySet())
                    {
                        String fieldname = Util.null2String(entry.getKey());
                        String fieldvalue = Util.null2String(entry.getValue());
                        dttablename += dttablename==""? fieldname :","+fieldname ;
                        dttablenvalue += dttablenvalue=="" ? "'"+fieldvalue+"'" :",'"+fieldvalue+"'" ;
                    }
                    if(dttablename !="" && dttablenvalue !="")
                    {
                        sql = " insert into uf_grouprelation("+dttablename+") values ("+dttablenvalue+")";
                        rs.execute(sql);
                    }
                }
            }catch (Exception e){
                bb.writeLog(e);
            }

            bb.writeLog("hrmResource2Mode--结束部门新增");

            List<Map<String,String>> szjglist = new ArrayList<Map<String,String>>();
            Map<String,String> szjgmap = null;
            List<Map<String,String>> rolelist = new ArrayList<Map<String,String>>();
            Map<String,String> rolemap = null;

            List<Map<String,String>> grouplist = new ArrayList<Map<String,String>>();
            Map<String,String> groupmap = null;


            for(int i=0;i<deptlist.size();i++)
            {
                String szjg = "" ;
                Map<String,String> dept_map = deptlist.get(i);
                String departmentcode = Util.null2String(dept_map.get("groupcode"));
                String workcode = Util.null2String(dept_map.get("workcode"));
                String id = Util.null2String(dept_map.get("ryid"));
                sql = " select t.departmentcode from HrmDepartment t where t.supdepid is null or t.supdepid = 0 start with t.departmentcode='"+departmentcode+"' connect by prior t.supdepid=t.id ";
                rs.execute(sql);
                if(rs.next()){
                    szjg = Util.null2String(rs.getString("departmentcode"));
                }
                szjgmap = new HashMap<String,String>();
                szjgmap.put("ryid",id);
                szjgmap.put("workcode",workcode);
                szjgmap.put("groupcode",szjg);
                szjgmap.put("formmodeid",formmodeid+"");
                szjgmap.put("modedatacreater",modedatacreater+"");
                szjgmap.put("modedatacreatertype",modedatacreatertype+"");
                szjgmap.put("modedatacreatedate",modedatacreatedate+"");
                szjgmap.put("modedatacreatetime",modedatacreatetime+"");
                szjglist.add(szjgmap);

                sql =   " select 'Role_'||d.id as roleid\n" +
                        " from hrmrolemembers l\n" +
                        " inner join hrmroles d on d.id = l.roleid\n" +
                        " where resourceid= "+id;
                rs.execute(sql);
                while (rs.next()){
                    String roleid = Util.null2String(rs.getString("roleid"));
                    rolemap = new HashMap<String,String>();
                    rolemap.put("ryid",id);
                    rolemap.put("workcode",workcode);
                    rolemap.put("groupcode",roleid);
                    rolemap.put("formmodeid",formmodeid+"");
                    rolemap.put("modedatacreater",modedatacreater+"");
                    rolemap.put("modedatacreatertype",modedatacreatertype+"");
                    rolemap.put("modedatacreatedate",modedatacreatedate+"");
                    rolemap.put("modedatacreatetime",modedatacreatetime+"");
                    rolelist.add(rolemap);
                }

                sql =   " select 'Group_'||groupid as groupid \n" +
                        " from hrmgroupmembers \n" +
                        " where userid= "+id;
                rs.execute(sql);
                while (rs.next()){
                    String groupid = Util.null2String(rs.getString("groupid"));
                    groupmap = new HashMap<String,String>();
                    groupmap.put("ryid",id);
                    groupmap.put("workcode",workcode);
                    groupmap.put("groupcode",groupid);
                    groupmap.put("formmodeid",formmodeid+"");
                    groupmap.put("modedatacreater",modedatacreater+"");
                    groupmap.put("modedatacreatertype",modedatacreatertype+"");
                    groupmap.put("modedatacreatedate",modedatacreatedate+"");
                    groupmap.put("modedatacreatetime",modedatacreatetime+"");
                    grouplist.add(groupmap);
                }
            }

            bb.writeLog("hrmResource2Mode--开始机构新增");
            try {
                for(int p=0;p<szjglist.size();p++ )
                {
                    Map<String,String> doc_map = szjglist.get(p);
                    String dttablename = "" ;
                    String dttablenvalue = "" ;
                    for (Map.Entry<String, String> entry : doc_map.entrySet())
                    {
                        String fieldname = Util.null2String(entry.getKey());
                        String fieldvalue = Util.null2String(entry.getValue());
                        dttablename += dttablename==""? fieldname :","+fieldname ;
                        dttablenvalue += dttablenvalue=="" ? "'"+fieldvalue+"'" :",'"+fieldvalue+"'" ;
                    }
                    if(dttablename !="" && dttablenvalue !="")
                    {
                        sql = " insert into uf_grouprelation("+dttablename+") values ("+dttablenvalue+")";
                        rs.execute(sql);
                    }
                }
            }catch (Exception e){
                bb.writeLog(e);
            }
            bb.writeLog("hrmResource2Mode--结束机构新增");

            bb.writeLog("hrmResource2Mode--开始群组新增");
            try {
                for(int p=0;p<grouplist.size();p++ )
                {
                    Map<String,String> doc_map = grouplist.get(p);
                    String dttablename = "" ;
                    String dttablenvalue = "" ;
                    for (Map.Entry<String, String> entry : doc_map.entrySet())
                    {
                        String fieldname = Util.null2String(entry.getKey());
                        String fieldvalue = Util.null2String(entry.getValue());
                        dttablename += dttablename==""? fieldname :","+fieldname ;
                        dttablenvalue += dttablenvalue=="" ? "'"+fieldvalue+"'" :",'"+fieldvalue+"'" ;
                    }
                    if(dttablename !="" && dttablenvalue !="")
                    {
                        sql = " insert into uf_grouprelation("+dttablename+") values ("+dttablenvalue+")";
                        rs.execute(sql);
                    }
                }
            }catch (Exception e){
                bb.writeLog(e);
            }
            bb.writeLog("hrmResource2Mode--结束群组新增");

            bb.writeLog("hrmResource2Mode--开始角色新增");
            try {
                for(int p=0;p<rolelist.size();p++ )
                {
                    Map<String,String> doc_map = rolelist.get(p);
                    String dttablename = "" ;
                    String dttablenvalue = "" ;
                    for (Map.Entry<String, String> entry : doc_map.entrySet())
                    {
                        String fieldname = Util.null2String(entry.getKey());
                        String fieldvalue = Util.null2String(entry.getValue());
                        dttablename += dttablename==""? fieldname :","+fieldname ;
                        dttablenvalue += dttablenvalue=="" ? "'"+fieldvalue+"'" :",'"+fieldvalue+"'" ;
                    }
                    if(dttablename !="" && dttablenvalue !="")
                    {
                        sql = " insert into uf_grouprelation("+dttablename+") values ("+dttablenvalue+")";
                        rs.execute(sql);
                    }
                }
            }catch (Exception e){
                bb.writeLog(e);
            }
            bb.writeLog("hrmResource2Mode--结束角色新增");
        }catch (Exception e){
            bb.writeLog("e:"+e);
        }
    }

    public static void main(String[] args) {

    }

}