*/
require_once(APP_PATH."/protected/vendor/autoload.php");
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
class AsyncWorkCommand extends CConsoleCommand{
public function run($args) {
error_reporting(E_ALL);
ini_set('memory_limit', '512M');
ignore_user_abort(true);
set_time_limit(0);
$time_H = time();
while (true) {
sleep(3);
$time = time();
if($time - $time_H > 3600){
exit();
}
//用户注册后异步任务
$list = new ARedisList('after_user_reg');
$auth = new Auth(Yii::app()->params['qiniuConfig']['ak'],Yii::app()->params['qiniuConfig']['sk']);
$bmgr = new BucketManager($auth);
while ($list->getCount() > 0) {
$user_id = $list->pop();
try {
$userAr = RUser::get(new MongoId($user_id));
//用户注册后默认关注几个圈子
$z_group = new ZGroup();
$default_groups = $z_group->get_default_fllow_group();
$mongo = new MongoClient(DB_CONNETC);
$db = $mongo->wozhua_data;
$collection = $db->selectCollection('groups');
$conditions = array(
'_id' => array('$in' => $default_groups)
);
$collection->update(
$conditions,
array('$inc' => array("users_count" => 1)),
array("multiple" => true)
);
//注册后默认关注几个用户
$default_fllow_users = Yii::app()->params['default_fllow_users'];
$user_node = new UserNodeRecord($userAr->_id);
foreach ($default_fllow_users as $fuser_id) {
$user_node->follow($fuser_id);
}
//抓取头像到七牛
$url = $userAr->avatar;
if(strpos($url,'qiniu')===false){
$bucket = Yii::app()->params['qiniuConfig']['avatars'];
$key = dechex(time()).rand(10,99).'.jpg';
list($ret, $err) = $bmgr->fetch($url, $bucket, $key);
echo "=====> fetch $url to bucket: $bucket key: $key\n";
if ($err !== null) {
file_put_contents(APP_PATH.'/download/log/after_user_reg.log',var_export($err,true),FILE_APPEND);
} else {
if(YII_DEBUG == true){
$userAr->avatar = 'http://wozhua-test.qiniudn.com/'.$key;
}else{
$userAr->avatar = 'http://avatars.wozhua.mobi/'.$key;
}
}
}
$userAr->update(array('avatar'),true);
//发送私信推送
$model = new ZMessage();
$from_user = Yii::app()->params['sys_user'];
$message_data = array(
'from_user' => $from_user,
'to_user' => $user_id,
'content' => '感谢您注册握爪,这里是新人必看的握爪操作指南,同时一定要遵守握爪社区总规则哦~',
'pics' => array(),
'voice' => array(),
'video'=> array()
);
$result = $model->addMessage($message_data);
} catch (Exception $e) {
var_dump($e);
continue;
}
}
//七牛语音转码消息队列
$list = new ARedisList(Yii::app()->params['voice_conv_list']);
$qiniu_config = Yii::app()->params['qiniuConfig'];
$bucket = $qiniu_config['voice'];
while ($list->getCount() > 0) {
$key = $list->pop();
try {
$status = CommonFn::ConvVoice($bucket,$key);
} catch (Exception $e) {
continue;
}
}
//打标签异步任务
$list = new ARedisList('add_tag');
while ($list->getCount() > 0) {
$res = $list->pop();
if(ENVIRONMENT != 'product'){
continue;
}
$data = unserialize($res);
try {
$getui = Service::factory('PushService');
$cids = $getui->queryCID($data['user_id']);
if($cids){
$tags = [];
foreach ($cids as $cid){
$res = $getui->getCidTags($cid);
$new_tags = explode(' ',$res);
$new_tags[] = $data['tag'];
$res = $getui->setTag($cid,$new_tags);
var_dump($res);
}
}
} catch (Exception $e) {
continue;
}
}
//订单支付成功后异步任务
$list = new ARedisList('o2o_after_pay_success');
while ($list->getCount() > 0) {
$res = $list->pop();
$input_data = json_decode($res,true);
try {
$order = ROrder::get(new MongoId($input_data['order_no']));
$criteria = new EMongoCriteria();
$criteria->user('==',$order->user);
$criteria->status('notin',array(0,-1,-2));
$order_count = ROrder::model()->count($criteria);
if($order_count == 1){
$criteria = new EMongoCriteria();
$criteria->channel('==','20150814');
$criteria->user('==',$order->user);
$coupon_status = CouponCode::model()->count($criteria);
if($coupon_status){
$criteria = new EMongoCriteria();
$criteria->date('==',(int)date('Ymd'));
$offCountModel = OfflineOrderCount::model()->find($criteria);
if(!$offCountModel){
$offCountModel = new OfflineOrderCount();
$offCountModel->date = intval(date('Ymd'));
$offCountModel->count = 1;
$offCountModel->save();
}else{
$offCountModel->count += 1;
$offCountModel->update(array('count'),true);
}
}
}
$key = $input_data['id'].'_send_sms';
$cache = new ARedisCache();
$have_send = $cache->get($key);
if(!$have_send){
$order_info = $order->parseRow($order);
$month = date('m');
$day = date('d');
$address = $order_info['address']['poi']['name'].$order_info['address']['detail'];
if(date('w') == 0 || date('w') == 6){
$info = '我们将在工作时间为您安排';
}else{
$info = '我们正在为您安排';
}
$master = '保洁师';//训犬师
CommonSMS::send('order_pay_success',array('month'=>$month,'day'=>$day,'address'=>$address,'info'=>$info,'master'=>$master,'mobile'=>$order_info['address']['mobile']));
$cache->set($key,1,86400);
}
//计算订单完成时间
if($order->technician){
$cost_time = Service::factory('OrderTimeCalService')->evaluaTime($order->products);
$cost_hours = ceil($cost_time/60);
$time_stamp = $order->booking_time;
FreeTimeRecord::TechUnsetFreetime($order->technician,$time_stamp - 3600);
for ($i=0; $i < $cost_hours; $i++) {
$unset_time_stamp = $time_stamp + $i*3600;
FreeTimeRecord::TechUnsetFreetime($order->technician,$unset_time_stamp);
}
FreeTimeRecord::TechUnsetFreetime($order->technician,$unset_time_stamp + 3600);
}
// 通知被选择的技师
if ($order->technician) {
$tech = TechInfo::get(intval($order->technician));
if ($tech->weixin_userid) {
$wechat = O2oApp::getWechatActive();
$url_prefix = ENVIRONMENT == 'product' ? 'http://api.wozhua.mobi' : 'http://apitest.wozhua.mobi';
$wechat_data = [
'touser' => $tech->weixin_userid,
'msgtype' => 'news',
'agentid' => '24',
'news' => [
'articles' => [
[
'title' => '握爪提示-新订单',
'description' => $tech->name.'你好!刚刚有一个新的订单被分配给你,请点击查看。',
'url' => $url_prefix.'/index.php?r=o2o/myOrder/info&order='.(string)$order->_id.'&user='.$tech->_id,
],
],
],
];
$wechat->sendMessage($wechat_data);
}
}
foreach ($order->products as $product) {
$product_obj = Product::get($product['product']);
if($product_obj){
switch ($product_obj->type) {
case '1':
$start_time = time();
$end_time = strtotime(date('Y-m-d',$start_time+2592000));
$coupon_id = new MongoId('556671c20eb9fb2e488be078');
Service::factory('CouponService')->giveCoupon($order->user,$coupon_id,$start_time,$end_time);
break;
case '2':
$start_time = time();
$end_time = strtotime(date('Y-m-d',$start_time+2592000));
$coupon_id = new MongoId('55667f6c0eb9fb14518b6e0a');
Service::factory('CouponService')->giveCoupon($order->user,$coupon_id,$start_time,$end_time);
break;
case '3':
$start_time = time();
$end_time = strtotime(date('Y-m-d',$start_time+2592000));
$coupon_id = new MongoId('55667f4a0eb9fb39518b6c06');
Service::factory('CouponService')->giveCoupon($order->user,$coupon_id,$start_time,$end_time);
break;
}
}
}
} catch (Exception $e) {
continue;
}
}
//删帖后异步任务
$list = new ARedisList('topic_del_new_list');
$from_user = Yii::app()->params['sys_user'];
$z_message = new ZMessage();
$z_topic = new ZTopic();
while ($list->getCount() > 0) {
$res = $list->pop();
try {
$data = unserialize($res);
$topic = Topic::get(new MongoId($data['id']));
if($topic){
Service::factory('ScoreService')->cutDownScore((string)$topic->user,'score_deltopic',2);
$z_topic->syncData($topic->_id,'del');
if($data['reason']){
$content = '你的帖子 『'.mb_substr($topic->content,0,20,'utf-8').'』 被管理员删除了。'.$data['reason'];
$data = array(
'from_user' => $from_user,
'to_user' => (string)$topic->user,
'content' => $content,
'pics' => array(),
'voice' => array(),
'video'=> array()
);
$z_message->addMessage($data);
}
}
} catch (Exception $e) {
continue;
}
}
//点喜欢后异步任务
$list = new ARedisList('after_like_work');
while ($list->getCount() > 0) {
$key = $list->pop();
try {
$likeAr = Like::get(new MongoId($key));
if($likeAr && $likeAr->type == 'topic'){
// $parsed_like = $likeAr->parseRow($likeAr);
// $getui = Service::factory('PushService');
// $gt_content = [
// 'title' => $parsed_like['user']['user_name'],
// 'content' => '喜欢了你的帖子',
// 'type' => 'like',
// 'data' => $parsed_like
// ];
// $getui->pushMessageToSingle($getui->TransmissionTemplate(['transmissionType'=> 2,'alert' => $parsed_like['user']['user_name'].'喜欢了你的帖子','transmissionContent'=>$gt_content]),$parsed_like['like_obj']['user']['id']);
$like_obj = Topic::get($likeAr->like_obj);
$author = RUser::get($like_obj->user);
$z_like = new ZLike();
$z_like->addAction($likeAr);
//帖子的被赞数加一
$like_obj->like_count = $like_obj->like_count+1;
//帖子的作者的被赞数加一
$author->liked_count = $author->liked_count+1;
$like_obj->update(array('like_count'), true);
$author->update(array('liked_count'), true);
//用户行为记录
CommonFn::setInteraction($likeAr->user,$author->_id,'like');
}
} catch (Exception $e) {
continue;
}
}
//发送短信异步任务
$list = new ARedisList('send_sms_list');
$smsservice = Service::factory('SendSMSService');
while ($list->getCount() > 0) {
$key = $list->pop();
try {
$sms_data = json_decode($key,true);
$smsservice->send_sms($sms_data['tpl'],$sms_data['mobile']);
} catch (Exception $e) {
continue;
}
}
//帖子查看数异步处理
$list = new ARedisList('topic_viewcounter_list');
while ($list->getCount() > 0) {
$topic_id = $list->pop();
$topic = Topic::get(new MongoId($topic_id));
$topic->visit_count = $topic->visit_count+10;
$topic->update(array('visit_count'),true);
}
}
}
}