let mysql = require('mysql');
let mysqlcn = require('../../terry/mysqlcn.js');
let query = mysqlcn.query;
let qs = require('querystring');
let encryption = require('../ordinary/encryption.js');
let http = require('http');

const DAY_SEC = 86400

let express = require('express');
let router = express.Router();


let get_recharge_info = function(req, res) {
	let real_ip = req.get("X-Real-IP") || req.get("X-Forwarded-For") || req.ip;
	console.log("post getorderid,host:%s, ip:%s,path:%s,time:%s",req.hostname,real_ip,req.path,new Date());
	let data = "";
	req.on("data",function(chunk){
	  data += chunk;
	});
	req.on("end",function(){
		try{
			recharge_info(data,res);
		} catch(e) {
			console.log(e)
			res.send(JSON.stringify( {'erron':1,'desc': "fail"}));
		}
	});
	req.on("error",function(err){
		res.send(JSON.stringify( {'erron':1,'desc': "fail"}));
	});
}

// 日充值总数
let get_day_recharge_all = function(serverid, time){
    let sql = "select ifnull(sum(money),0) total FROM payinfo where flag = 2 and serverid = ? and submittime between ? and ?"
    let values = [serverid, time, +time+DAY_SEC]
    sql = mysql.format(sql, values)
    query(sql, function(qerr,vals,fields_){
        if (qerr==null){
            console.log(vals[0].total);
            return vals[0].total
        }else{
            console.log("查询失败");
            try{
                res.send(JSON.stringify({ 'erron':'400','msg':'稍后请求'}));
            }
            catch(e){}
        }
    });
}

// 月充值总数
let get_month_recharge_all = function(serverid, time){
    let sql = "select ifnull(sum(money),0) total FROM payinfo where flag = 2 and serverid = ? and submittime between ? and ?"
    let values = [serverid, +time-29*DAY_SEC, +time+DAY_SEC]
    sql = mysql.format(sql, values)
    query(sql, function(qerr,vals,fields_){
        if (qerr==null){
            console.log(vals[0].total);
            return vals[0].total
        }else{
            console.log(qerr);
            console.log("查询失败");
            try{
                res.send(JSON.stringify({ 'erron':'400','msg':'稍后请求'}));
            }
            catch(e){}
        }
    });
}

// 日充值玩家数量
let get_day_recharge_player = function(serverid, time){
    let sql = "select ifnull(count(*),0) total FROM payinfo where flag = 2 and serverid = ? and submittime between ? and ?"
    let values = [serverid, time, +time+DAY_SEC]
    sql = mysql.format(sql, values)
    console.log("sql:%s",sql)
    query(sql, function(qerr,vals,fields_){
        if (qerr==null){
            console.log(vals[0].total);
            return vals[0].total
        }else{
            console.log(qerr);
            console.log("查询失败");
            try{
                res.send(JSON.stringify({ 'erron':'400','msg':'稍后请求'}));
            }
            catch(e){}
        }
    });
}

let recharge_info = function(args,res){
    let receipt = null
	try {
		receipt = qs.parse(args);//JSON.parse(args)
	} catch(e) {
			console.log("解析失败!");
			try {res.send("3");}
			catch(e){}
			return
	}
    console.log(receipt);
    const serverid = receipt.serverid
    const time = receipt.starttime
    let day_recharge = 0
    let month_recharge = 0
    let day_player = 0
    let sql = "select ifnull(sum(money),0) total FROM payinfo where flag = 2 and serverid = ? and submittime between ? and ?"
    let values = [serverid, time, +time+DAY_SEC]
    sql = mysql.format(sql, values)
    query(sql, function(qerr,vals,fields_){
        if (qerr==null){
            console.log(vals[0].total);
            day_recharge =  vals[0].total
            sql = "select ifnull(sum(money),0) total FROM payinfo where flag = 2 and serverid = ? and submittime between ? and ?"
            values = [serverid, +time-29*DAY_SEC, +time+DAY_SEC]
            sql = mysql.format(sql, values)
            query(sql, function(qerr,vals,fields_){
                if (qerr==null){
                    console.log(vals[0].total);
                    month_recharge = vals[0].total
                    sql = "select ifnull(count(*),0) total FROM payinfo where flag = 2 and serverid = ? and submittime between ? and ?"
                    values = [serverid, time, +time+DAY_SEC]
                    sql = mysql.format(sql, values)
                    query(sql, function(qerr,vals,fields_){
                        if (qerr==null){
                            console.log(vals[0].total);
                            day_player = vals[0].total
                            console.log("day_recharge:%d, month_recharge:%d, day_player:%d.", day_recharge, month_recharge, day_player)
                            var returndata = JSON.stringify({
                                'erron': 0,
                                'msg': "",
                                'data': {
                                    'day_recharge':day_recharge,
                                    'month_recharge': month_recharge,
                                    'day_player': day_player,
                                }
                            });
                            console.log("查询充值数据:%s",returndata);
                            res.send(returndata);
                        }else{
                            console.log(qerr);
                            console.log("查询失败");
                            try{
                                res.send(JSON.stringify({ 'erron':'400','msg':'稍后请求'}));
                            }
                            catch(e){}
                        }
                    });

                }else{
                    console.log(qerr);
                    console.log("查询失败");
                    try{
                        res.send(JSON.stringify({ 'erron':'400','msg':'稍后请求'}));
                    }
                    catch(e){}
                }
            });

        }else{
            console.log("查询失败");
            try{
                res.send(JSON.stringify({ 'erron':'400','msg':'稍后请求'}));
            }
            catch(e){}
        }
    });



    // let sql = "SELECT * FROM services";
    // query(sql,function(qerr,vals,fields_){
    //     if (qerr==null){
    //         len = vals.length
    //         let data = new Array()
    //         for (let i = 0;i < vals.length; ++i){
    //             console.log("查询服务器id:%s",vals[i].id);
    //             // get_player_uid(vals[i],channel,account,vals[i].id,add_server);
    //             data[i] = {
    //                 "server_id": vals[i].id,
    //                 "server_name": vals[i].name,
    //             }
	// 			console.log("写入查询服务器id:%s",vals[i].id);
    //         }
    //         let returndata = JSON.stringify({
    //             'code': 0,
    //             'msg': "",
    //             'data': data,
    //         });
    //         console.log("查找mysql玩家角色服务器列表:%s",returndata);
    //         // res.send(returndata);
    //     }else{
    //         console.log("打开mysql库中服务器列表失败");
    //         try{
    //             res.send(JSON.stringify({ 'erron':'400','msg':'稍后请求'}));
    //         }
    //         catch(e){}
    //     }
    // });
}

router.post('/recharge_info', get_recharge_info);


module.exports = router;