mysql 同一秒入库问题_redis 数据统计(用自增id防止同一秒并发过大没统计成功)...

/**

* 检测 今日 coin 是否正常 统一查询

* 保存统计过的最大自增id*/

public functioncheckCoin() {$model = newUserStat();$connection = $model->getReadConnection();$redis = self::getRedis();$userStatListKey = UserStat::USER_STAT_LIST_KEY;$redisData = $redis->hGetAll($userStatListKey);if ($redisData) {$sql = <<

SELECT (SELECT concat(IFNULL(sum(coin),0),'_',ifnull(max(id),:max_recharge_id)) FROM wallet_recharge_log WHERE pay_status = 1 AND id > :max_recharge_id ) ASrecharge,(SELECT concat(IFNULL(sum(-send_coin),0),'_',ifnull(max(id),:max_send_gift_id)) AS send_total FROM gift_log WHERE id > :max_send_gift_id) ASsend_gift,(SELECT concat(IFNULL(sum(CASE type WHEN 1 THEN coin ELSE -coin END),0),'_',ifnull(max(id),:max_wallet_order_id)) FROM wallet_order_log WHERE id > :max_wallet_order_id ) ASwallet_order,(SELECT IFNULL(sum(-coin),0) AS has_total_coin FROM user_wallet) AShas_coin,(SELECT IFNULL(sum(-pay_coin),0) AS total FROM live_pay_log WHERE `status` =2) ASlive_pay,(SELECT concat(IFNULL(sum(-coin),0),'_',ifnull(max(id),:max_barrage_pay_id)) FROM user_barrage_log WHERE id > :max_barrage_pay_id) ASbarrage_pay,(SELECT concat(IFNULL(sum(-coin),0),'_',ifnull(max(id),:max_vip_pay_id)) FROM user_vip_log WHERE id > :max_vip_pay_id) ASvip_pay,(SELECT concat(IFNULL(sum(coin),0),'_',ifnull(max(id),:max_exchange_get_id)) FROM wallet_exchange_log WHERE id > :max_exchange_get_id) ASexchange_get;

SQL;$bind_arr =['max_recharge_id' => $redisData['max_recharge_id'],

'max_send_gift_id' => $redisData['max_send_gift_id'],

'max_wallet_order_id' => $redisData['max_wallet_order_id'],

'max_barrage_pay_id' => $redisData['max_barrage_pay_id'],

'max_vip_pay_id' => $redisData['max_vip_pay_id'],

'max_exchange_get_id' => $redisData['max_exchange_get_id'],];$result = $connection->query($sql, $bind_arr)->fetch();$change_coin = $redisData['has_coin'] - $result['has_coin'];$result['recharge'] = explode('_',$result['recharge']);$result['send_gift'] = explode('_',$result['send_gift']);$result['wallet_order'] = explode('_',$result['wallet_order']);$result['barrage_pay'] = explode('_',$result['barrage_pay']);$result['vip_pay'] = explode('_',$result['vip_pay']);$result['exchange_get'] = explode('_',$result['exchange_get']);$redis_arr =['has_coin' => $result['has_coin'],

'live_pay' => $result['live_pay'],

'recharge' => $redisData['recharge'] + $result['recharge'][0],

'send_gift' => $redisData['send_gift'] + $result['send_gift'][0],

'wallet_order' => $redisData['wallet_order'] + $result['wallet_order'][0],

'barrage_pay' => $redisData['barrage_pay'] + $result['barrage_pay'][0],

'vip_pay' => $redisData['vip_pay'] + $result['vip_pay'][0],

'exchange_get' => $redisData['exchange_get'] + $result['exchange_get'][0],];$max_id_arr =['max_recharge_id' => $result['recharge'][1],

'max_send_gift_id' => $result['send_gift'][1],

'max_wallet_order_id' => $result['wallet_order'][1],

'max_barrage_pay_id' => $result['barrage_pay'][1],

'max_vip_pay_id' => $result['vip_pay'][1],

'max_exchange_get_id' => $result['exchange_get'][1],];echo "send_gift : [{$result['send_gift'][0]}];change_coin [$change_coin]']\n\n";

}else{$sql = <<

SELECT (SELECT concat(IFNULL(sum(coin),0),'_',ifnull(max(id),0)) FROM wallet_recharge_log WHERE pay_status = 1 ) ASrecharge,(SELECT concat(IFNULL(sum(-send_coin),0),'_',ifnull(max(id),0)) AS send_total FROM gift_log) ASsend_gift,(SELECT concat(IFNULL(sum(CASE type WHEN 1 THEN coin ELSE -coin END),0),'_',ifnull(max(id),0)) FROM wallet_order_log ) ASwallet_order,(SELECT IFNULL(sum(-coin),0) AS has_total_coin FROM user_wallet) AShas_coin,(SELECT IFNULL(sum(-pay_coin),0) AS total FROM live_pay_log WHERE `status` =2) ASlive_pay,(SELECT concat(IFNULL(sum(-coin),0),'_',ifnull(max(id),0)) FROM user_barrage_log ) ASbarrage_pay,(SELECT concat(IFNULL(sum(-coin),0),'_',ifnull(max(id),0)) FROM user_vip_log) ASvip_pay,(SELECT concat(IFNULL(sum(coin),0),'_',ifnull(max(id),0)) FROM wallet_exchange_log) ASexchange_get;

SQL;$result = $connection->query($sql)->fetch();$result['recharge'] = explode('_',$result['recharge']);$result['send_gift'] = explode('_',$result['send_gift']);$result['wallet_order'] = explode('_',$result['wallet_order']);$result['barrage_pay'] = explode('_',$result['barrage_pay']);$result['vip_pay'] = explode('_',$result['vip_pay']);$result['exchange_get'] = explode('_',$result['exchange_get']);$redis_arr =['has_coin' => $result['has_coin'],

'live_pay' => $result['live_pay'],

'recharge' => $result['recharge'][0],

'send_gift' => $result['send_gift'][0],

'wallet_order' => $result['wallet_order'][0],

'barrage_pay' => $result['barrage_pay'][0],

'vip_pay' => $result['vip_pay'][0],

'exchange_get' => $result['exchange_get'][0],];$max_id_arr =['max_recharge_id' => $result['recharge'][1],

'max_send_gift_id' => $result['send_gift'][1],

'max_wallet_order_id' => $result['wallet_order'][1],

'max_barrage_pay_id' => $result['barrage_pay'][1],

'max_vip_pay_id' => $result['vip_pay'][1],

'max_exchange_get_id' => $result['exchange_get'][1],];

}$coin_diff_today = array_sum($redis_arr);$coin_limit = Kv::get('coin_limit', '0');//coin是否正常

if (abs($coin_diff_today) < $coin_limit) {//正常

$redis_arr['status'] = 1;

}else{//异常

$redis_arr['status'] = 2;

}$redis_arr = array_merge($redis_arr,$max_id_arr);$redis_arr['coin_diff_today'] = $coin_diff_today;$redis->hMset($userStatListKey,$redis_arr);if(!$redisData){$redis->expire($userStatListKey,3600 * 24);

}return $redis_arr;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值