// 获取最近的1天的订单数据,昵称,时间。
public function getNearestOrder()
{
// 获取真实订单
$service_order = M('service_order');
$service_order_list = $service_order->where(['first_pay_time' => ['gt', strtotime(date('Y-m-d'))]])->order('id desc')->select();
$user = M('user');
$true_tmp = [];
foreach ($service_order_list as $k => &$v) {
$tmp = [];
$user_info = $user->where(['id' => $v['uid']])->find();
$tmp['nickname'] = Func::encryptName2($user_info['nickname']);
$tmp['pay_time'] = DateHelper::timeTran(date('Y-m-d H:i', $v['first_pay_time']));
$tmp['time'] = $v['first_pay_time'];
$true_tmp[] = $tmp;
}
// 获取Redis中的数据
$redis = Red::create();
$order_redis_key = C('APP_ID') . ':nearest_order_data:' . date('Y-m-d');
$order_redis_data = $redis->get($order_redis_key);
if (!$order_redis_data) {
// TODO 造假
$user = M('user');
$out_tmp = [];
$user_nickname_arr = $user->order('rand()')->limit(100)->getField('nickname', true);
for ($i = 0; $i < 10; $i++) {
$nickname = $this->getNickName($user_nickname_arr);
$fake_order = $this->getFakeOrder($nickname);
$out_tmp[] = $fake_order;
}
// 合并
foreach ($true_tmp as $k => $v) {
array_unshift($out_tmp, $v);
}
// 去重
$out_tmp = Func::arrayUnique($out_tmp);
// 排序
$order_column = array_column($out_tmp, 'time');
array_multisort($order_column, SORT_DESC, SORT_NUMERIC, $out_tmp);
// 存储redis
$redis->set($order_redis_key, json_encode($out_tmp), 86400);
} else {
// 从redis抽取
$order_redis_data = json_decode($order_redis_data, true);
$out_tmp = [];
// 重置数据
foreach ($order_redis_data as $ok => $ov) {
$tmp = [];
$tmp['nickname'] = $ov['nickname'];
$tmp['pay_time'] = DateHelper::timeTran(date('Y-m-d H:i', $ov['time']));
$tmp['time'] = $ov['time'];
$out_tmp[] = $tmp;
}
// 合并真实订单
foreach ($true_tmp as $k => $v) {
array_unshift($out_tmp, $v);
}
// 去重
$out_tmp = Func::arrayUnique($out_tmp);
// 排序
$order_column = array_column($out_tmp, 'time');
array_multisort($order_column, SORT_DESC, SORT_NUMERIC, $out_tmp);
// 超过10分钟没有下单,造假一个订单
if (time() - $out_tmp[0]['time'] > 600 && date('H') < 19) { // 再造一个假数据
$user = M('user');
$user_nickname_arr = $user->order('rand()')->limit(100)->getField('nickname', true);
$nickname = $this->getNickName($user_nickname_arr);
$fake_order = $this->getFakeOrder($nickname,60);
array_unshift($out_tmp, $fake_order);
}
// 存储redis
$redis->set($order_redis_key, json_encode($out_tmp), 86400);
}
$this->json->S($out_tmp);
}
protected function getFakeOrder($nickname,$duration = 3600)
{
$time = time() - mt_rand(0, $duration);
$tmp['nickname'] = $nickname;
$tmp['pay_time'] = DateHelper::timeTran(date('Y-m-d H:i', $time));
$tmp['time'] = $time;
return $tmp;
}
protected function getNickName($user_nickname_arr)
{
$head_pos = mt_rand(0, count($user_nickname_arr) - 1);
$tail_pos = mt_rand(0, count($user_nickname_arr) - 1);
$nickname = $user_nickname_arr[$head_pos] . $user_nickname_arr[$tail_pos];
$nickname = Func::encryptName2($nickname);
return $nickname;
}
模拟昵称,随机时间,数组合并,数组去重,数组排序。