123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 16/6/26
- * Time: 下午9:30
- */
- require_once (BASE_ROOT_PATH . '/helper/account_helper.php');
- require_once (BASE_ROOT_PATH . '/helper/bonus_helper.php');
- require_once (BASE_ROOT_PATH . '/helper/push_helper.php');
- class notify_helper
- {
- private static function secs_days($secs)
- {
- $days = intval($secs / 86400) + ($secs % 86400 != 0) ? 1 : 0;
- return $days;
- }
- static public function onBonusExpire($warn_remain_days, $warn_interval_days)
- {
- $mod_bonus = Model("user_bonus");
- $items = $mod_bonus->getNeedWarn($warn_remain_days, $warn_interval_days);
- if($items == false) return;
- $member_amount = [];
- $member_day = [];
- $ids = [];
- foreach ($items as $key => $val)
- {
- $bonus = bonus\user_bonus::create_by_param($val);
- $leftsecs = $bonus->usable_time() - time();
- $remain_days = self::secs_days($leftsecs);
- $amount = $bonus->remain_amount();
- if(isset($member_amount[$bonus->user_id()]) == false) {
- $member_amount[$bonus->user_id()] = $amount;
- $member_day[$bonus->user_id()] = $remain_days;
- } else {
- $member_amount[$bonus->user_id()] += $amount;
- }
- $ids[] = $bonus->bonus_id();
- }
- foreach ($member_amount as $member_id => $amount) {
- $remain_days = $member_day[$member_id];
- push_helper::notice_expring($member_id,$amount,$remain_days);
- }
- if (!empty($ids)) {
- $mod_bonus->edit(array('bonus_id' => array('in', $ids)),array("notify_time" => time()));
- }
- }
- static public function bonus_expired($maxtm)
- {
- $mod_bonus = Model("user_bonus");
- $items = $mod_bonus->getExpired($maxtm);
- if(empty($items)) return false;
- $member_amount = [];
- foreach ($items as $key => $val)
- {
- $bonus = bonus\user_bonus::create_by_param($val);
- $user_id = $bonus->user_id();
- $amount = $bonus->remain_amount();
- $logger = new bonus\recorder($user_id);
- $logger->bonus_expire($val);
- $bonus_id = $bonus->bonus_id();
- if(isset($member_amount[$bonus->user_id()]) == false) {
- $member_amount[$bonus->user_id()] = $amount;
- } else {
- $member_amount[$bonus->user_id()] += $amount;
- }
- $mod_bonus->edit(['bonus_id' => $bonus_id],["expired" => 1]);
- }
- foreach ($member_amount as $member_id => $amount)
- {
- try
- {
- push_helper::notice_expired($member_id,$amount);
- }
- catch (Exception $ex) {
- Log::record("bonus_expired error: {$ex->getMessage()}",Log::ERR);
- }
- }
- return true;
- }
- static public function release_bonus()
- {
- $bonus_type = Model('bonus_type');
- $condition = ['remain_amount' => ['gt',0],'is_refund' => 0,'send_end_date' => ['lt',time() - 60 * 60]];
- $bonus_types = $bonus_type->getTypeList($condition,'','*','',false);
- if(empty($bonus_types) || count($bonus_types) <= 0) {
- return;
- }
- foreach($bonus_types as $item)
- {
- try
- {
- $trans = new trans_wapper(null,__METHOD__);
- $success = $bonus_type->edit(['type_id' => $item['type_id']],['is_refund' => 1,'refund_time' => time()]);
- if (!$success) {
- $sresult = implode(',',$item);
- Log::record("bonus refund 更新状态失败,result:{$sresult}.");
- }
- else {
- $type = bonus\type::create_by_paramer($item);
- QueueClient::push('onBonusChange', ['change_type' => 'bonus_refund', 'buyer_id' => $type->sender_id(), 'order_sn' => $type->getType_sn()]);
- $condition = ['type_id' =>$item['type_id'],'bonus_status' => ['in',[0,1]]];
- Model('user_bonus')->where($condition)->delete();
- }
- $trans->commit();
- }
- catch (Exception $e)
- {
- $trans->rollback();
- $sresult = implode(',',$item);
- Log::record('bonus refund : error:' . $e->getMessage() . " result:{$sresult}.");
- }
- }
- }
- static public function onFcodeWarning()
- {
- $pre_secs = 86400 * 5;
- $mod_member = Model('member');
- $i = 0;
- while (true)
- {
- $start = $i * 1000;
- $items = Model()->table('goods_fcode')
- ->field('fc_id,mobile,goods_commonid,usable_time')
- ->where(array('usable_time&usable_time' => ['_multi'=>true,['gt',time() - $pre_secs],['lt',time()]],'fc_state' => 0,'warning_time' => 0))
- ->order('fc_id desc')
- ->limit("{$start},1000")->select();
- if(empty($items)) {
- return;
- }
- $i++;
- foreach ($items as $item)
- {
- $fc_id = $item['fc_id'];
- $cid = $item['goods_commonid'];
- $usable_time = $item['usable_time'];
- $mobile = $item['mobile'];
- $mid = self::member_id($mod_member,$mobile);
- if($mid!= false && $mid > 0) {
- push_helper::fcode_warning($cid,$mid,$usable_time);
- }
- Model()->table('goods_fcode')->where(['fc_id' => $fc_id])->update(['warning_time' => time()]);
- }
- }
- }
- static private function member_id($mod_member,$mobile)
- {
- $items = $mod_member->where(['member_mobile' => $mobile])->select();
- if(!empty($items)) {
- return intval($items[0]['member_id']);
- }
- return false;
- }
- }
|