//
var mysql = require('mysql');
var mysqlcn = require('./mysqlcn.js');
var query = mysqlcn.query;
var checkorder = require('./checkorder.js');
var config = require('../pay/config.js');

var doRepairOrder = function(){
	try{
		//console.log("------------doRepairOrder");
		// flag=1 的是没有发货的， o
		// talkingdata=1的是未向talkingdata 报道的订单
		query("select * from payinfo where flag=1 or talkingdata=1 and istest=2",function(qerr,vals,fields){
			if (vals!=null){
				for (var i=0;i<vals.length;++i){
					if (vals[i].flag==1){//玩家已支付补发钻石
						console.log("开始用户 %s 的订单 %s 自动补单!", vals[i].uid, vals[i].orderid);
						var uid = vals[i].uid;
						var orderid = vals[i].orderid;
						checkorder.pushproduct(query,vals[i].serverid,vals[i].orderid,function(ok){
							if (ok) {
								console.log("用户 %s 的订单 %s，补单成功!", uid, orderid);
							}
							else{
								console.log("用户 %s 的订单 %s，补单失败!", uid, orderid);
							}
						})
					}
					if (vals[i].talkingdata==1){//向tokingdata补发报告
						var option = config.reverse(vals[i].platform, vals[i].channel, vals[i].cfid);
						var TDappid = config.getAppID(vals[i].channel)
						console.log("向tokingdata补发报告TDappid=%s!", TDappid);
						checkorder.pushtalkingdata(
							query,
							TDappid,
							vals[i].platform,
							vals[i].uid,
							vals[i].orderid,
							option.price,
							option.typename,
							option.diamond,
							vals[i].submittime ? vals[i].submittime : vals[i].overtime,
							vals[i].payment_type,
							vals[i].serverid)
					}
				}
			}
		})
	}catch(err){
		console.log(err);
	}
};

setInterval(doRepairOrder, 30000);
