*/ class GiveCouponsCommand extends CConsoleCommand{ public function run($args) { error_reporting(E_ALL); ini_set('memory_limit', '256M'); set_time_limit(0); /* *给所有的上海市用户发放优惠券 */ $criteria = new EMongoCriteria(); $criteria->addCond('city_info.province','==','上海市'); // $criteria->addCond('user_name','==','逗你玩'); //$criteria->addCond('user_name','==','charlie'); var_dump(RUser::model()->count($criteria)); sleep(3); $cursor = RUser::model()->findAll($criteria); $start_time = time(); //发放优惠券可用开始时间 $end_time = $start_time + 86400*30; //发放优惠券过期时间 $coupon_ids = array(); //发放优惠券id数组,可发放多张 $coupon_ids[] = '56c58f37a84ea0554d8d3161'; $coupon_ids[] = '56c58f80a84ea0a6258c51af'; $coupon_ids[] = '56c58e2fa84ea0b14c8d448a'; $i = 1; $cache = new ARedisCache(); foreach ($cursor as $value) { $user_id = (string)$value->_id; $sendstat = $cache->get('sendcoupons_'.$user_id); echo $i++; if($sendstat){ continue; } $cache->set('sendcoupons_'.$user_id,1,86400); $smsservice = Service::factory('SendSMSService'); var_dump($this->SendCoupon($value->_id,$coupon_ids,$start_time,$end_time)); //TODO:发送短信 try { if($value->shop_address){ foreach ($value->shop_address as $address) { if(isset($address['mobile'])){ $status = $cache->get('sendsms_'.$address['mobile']); if(!$status){ $sms_data['tpl'] = '【壹管家】送你60元代金券,可用于全部上门服务,下单立减→http://t.cn/ 回T退订'; $sms_data['mobile'] = $address['mobile']; $list = new ARedisList('send_sms_list'); $list->push(json_encode($sms_data)); $cache->set('sendsms_'.$address['mobile'],1,86400); } } } }else{ $data = array(); $time_str = CommonFn::sgmdate("Y年n月d日", time(),1); $data['content'] = '送你60元代金券,可用于全部上门服务,下单立减→点击这里预约'; $data['title'] = '系统通知'; $data['custom'] = array( 'type'=>'user_status', 'data'=>array( 'status'=>1, 'user_id'=>$user_id, 'time'=>time(), 'time_str'=>$time_str ) ); $data['account'] = $user_id; $ios_data = array(); $ios_data['alert'] = '系统通知:' . $data['content']; $ios_data['account'] = $user_id; $ios_data['custom'] = array( 't'=>'a', 'v'=>1, ); $z_push = new ZPush(); $z_push->PushSingleAccount($data,$ios_data); } } catch (Exception $e) { continue; } } } private function SendCoupon($user_id,$coupons,$start_time,$end_time){ $cache = new ARedisCache(); $status = $cache->get('send_coupon_status_20160526'.(string)$user_id); if(!$status){ $status = true; foreach ($coupons as $coupon_id) { $coupon_id = new MongoId($coupon_id); $status = $status&&Service::factory('CouponService')->giveCoupon($user_id,$coupon_id,$start_time,$end_time); } $cache->set('send_coupon_status_'.(string)$user_id,1,86400); return $status; }else { return true; } } }