package weaver.cronjob.mingfeng;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;


public class SuijimaDingshiCronService extends BaseBean {
    private final Logger log = LoggerFactory.getLogger(SuijimaDingshiCronService.class);
    public synchronized void execute() {
        String suijimaid = getPropValue("mingfengtopersoncard", "mf.suijimaid");
        RecordSet recordSeta = new RecordSet();
        RecordSet recordSetb = new RecordSet();
        RecordSet recordSetc = new RecordSet();
        RecordSet recordSetd = new RecordSet();
        RecordSet recordSete = new RecordSet();
        String sql = "select id from HrmResource";
        recordSeta.executeQuery(sql);
        while (recordSeta.next()) {
            String userid = recordSeta.getString("id");
            String sql2 = "select count(id) as nums from cus_fielddata where scopeid = 1 and scope = 'HrmCustomFieldByInfoType' and id = "+userid;
            recordSetb.executeQuery(sql2);
            recordSetb.next();
            int nums = recordSetb.getInt("nums");
            if(nums == 0) {
                String suijima = getSuijishu(recordSetc,suijimaid);
                String sql5 = "insert into cus_fielddata("+suijimaid+",scopeid,scope,id) values (?,?,?,?)";
                recordSetd.executeUpdate(sql5,suijima,1,"HrmCustomFieldByInfoType",userid);
            }else {
                String sql6 = "select "+suijimaid+" from cus_fielddata where scopeid = 1 and scope = 'HrmCustomFieldByInfoType' and id = "+userid;
                recordSete.executeQuery(sql6);
                recordSete.next();
                String sjm = recordSete.getString(suijimaid);
                if(sjm.length() == 0) {
                    String suijima = getSuijishu(recordSetc,suijimaid);
                    String sql7 = "update cus_fielddata set "+suijimaid+" = '"+suijima+"' where scopeid = 1 and scope = 'HrmCustomFieldByInfoType' and id = "+userid;
                    recordSetd.executeUpdate(sql7);
                }
            }
        }
    }

    private synchronized String getSuijishu(RecordSet recordSeta,String suijimaid) {
        int flag = 10;
        String rid = "";
        while (flag == 10) {
            int randnum = (int)(Math.random() * 90000000) + 10000000;
            rid = String.valueOf(randnum);
            String sql = "select count(id) as nums from cus_fielddata where scopeid = 1 and scope = 'HrmCustomFieldByInfoType' and "+suijimaid+" = '"+rid+"'";
            recordSeta.executeQuery(sql);
            recordSeta.next();
            int nums = recordSeta.getInt("nums");
            if( nums == 0) {
                flag = 13;
            }
        }
        return rid;
    }
}

