
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class AutoIncrMember {

    public static Map<String, Long> totalmap = new HashMap();
    public static void main(String[] args) throws Exception {




        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://bj-cdb-ihmswuq0.sql.tencentcdb.com:59832/qkyxzx?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false","qunke#u03","Qk@mYPw2o2O6+6");

        while(true){
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            String now = format.format(new Date());
            add(now,conn);

            Thread.sleep(30000);
        }
        //conn.close();


    }

    public void updateparent( Connection conn,String parentid) throws SQLException {
        String sql = "select sum(b.purchased_members) from qkj_live b where b.id in ("+parentid+") and live_status=1";
            Statement stmt = conn.createStatement();
            //6、执行sql
            ResultSet rs = stmt.executeQuery(sql);
            //7、处理结果
            if(rs.next()){
                int count =rs.getInt(1);
                sql = "update    qkj_live  set  purchased_members  =  "+count+" where id=199";
                System.out.println(sql);
                updatesql(sql,conn);
            }
            rs.close();
            //8、释放资源
            stmt.close();
    }


    public static void add(String now,Connection conn) throws Exception{

        String[] times = {"18:00-19:00,60","19:00-19:40,40"};
        String[] lives = {"201:8102"};//,"164:58000"
        realincr(now,conn,times,lives);
    }

    public static void realincr( String now,Connection conn,String[] times ,String[] lives ) throws Exception{
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(String live:lives){
            String liveid = live.split(":")[0];
            Integer count = Integer.parseInt(live.split(":")[1]);

            for(String time:times){
                String start = time.split(",")[0].split("-")[0];
                String end = time.split(",")[0].split("-")[1];
                if(now.compareTo(start)>=0 &&  now.compareTo(end)<0){

                }else{
                    System.out.println("no start now="+now+" start="+start+" end="+end);
                    continue;
                }
                long last = (format.parse("2022-07-16 "+end+":00").getTime()-format.parse("2022-07-16 "+start+":00").getTime())/1000;
                String percent = time.split(",")[1];
                int currenttotal = count*Integer.parseInt(percent)/100;
                long incr = currenttotal*30/last;
                long num = getnum(now,start,end,incr);
                System.out.println("liveid="+liveid+" num="+num+" incr="+incr+" currenttotal="+currenttotal+" count="+count);
                if(totalmap.get(liveid)==null){
                    totalmap.put(liveid,num);
                }else{
                    totalmap.put(liveid,totalmap.get(liveid)+num);
                }
                String sql = "update    qkj_live set  purchased_members  =  purchased_members+"+num+" where id = "+liveid+" and live_status=1";
                updatesql(sql,conn);
            }


        }
    }

    public static long getnum(String now,String start,String end,long incr){

        System.out.print(now+" ");
        if(now.compareTo(start)>=0 &&  now.compareTo(end)<0){

        }else{
            int random2 =    (int) ((Math.random() *10) ) ;
            incr = random2;
        }
        int random =    (int) ((Math.random() *10+1) +incr) ;
        if(random<0){
            random=0;
        }
        int MAX = 10000;
        if(random>MAX ){
            random = random%MAX;
        }
        return random;

    }


    public static void updatesql(String sql,Connection conn) throws SQLException {
        Statement s = conn.createStatement();
        s.execute(sql);
        s.close();
    }

}
