notify_helper.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/6/26
  6. * Time: 下午9:30
  7. */
  8. require_once (BASE_ROOT_PATH . '/helper/account_helper.php');
  9. require_once (BASE_ROOT_PATH . '/helper/bonus_helper.php');
  10. require_once (BASE_ROOT_PATH . '/helper/push_helper.php');
  11. class notify_helper
  12. {
  13. private static function secs_days($secs)
  14. {
  15. $days = intval($secs / 86400) + ($secs % 86400 != 0) ? 1 : 0;
  16. return $days;
  17. }
  18. static public function onBonusExpire($warn_remain_days, $warn_interval_days)
  19. {
  20. $mod_bonus = Model("user_bonus");
  21. $items = $mod_bonus->getNeedWarn($warn_remain_days, $warn_interval_days);
  22. if($items == false) return;
  23. $member_amount = [];
  24. $member_day = [];
  25. $ids = [];
  26. foreach ($items as $key => $val)
  27. {
  28. $bonus = bonus\user_bonus::create_by_param($val);
  29. $leftsecs = $bonus->usable_time() - time();
  30. $remain_days = self::secs_days($leftsecs);
  31. $amount = $bonus->remain_amount();
  32. if(isset($member_amount[$bonus->user_id()]) == false) {
  33. $member_amount[$bonus->user_id()] = $amount;
  34. $member_day[$bonus->user_id()] = $remain_days;
  35. } else {
  36. $member_amount[$bonus->user_id()] += $amount;
  37. }
  38. $ids[] = $bonus->bonus_id();
  39. }
  40. foreach ($member_amount as $member_id => $amount) {
  41. $remain_days = $member_day[$member_id];
  42. push_helper::notice_expring($member_id,$amount,$remain_days);
  43. }
  44. if (!empty($ids)) {
  45. $mod_bonus->edit(array('bonus_id' => array('in', $ids)),array("notify_time" => time()));
  46. }
  47. }
  48. static public function bonus_expired($maxtm)
  49. {
  50. $mod_bonus = Model("user_bonus");
  51. $items = $mod_bonus->getExpired($maxtm);
  52. if(empty($items)) return false;
  53. $member_amount = [];
  54. foreach ($items as $key => $val)
  55. {
  56. $bonus = bonus\user_bonus::create_by_param($val);
  57. $user_id = $bonus->user_id();
  58. $amount = $bonus->remain_amount();
  59. $logger = new bonus\recorder($user_id);
  60. $logger->bonus_expire($val);
  61. $bonus_id = $bonus->bonus_id();
  62. if(isset($member_amount[$bonus->user_id()]) == false) {
  63. $member_amount[$bonus->user_id()] = $amount;
  64. } else {
  65. $member_amount[$bonus->user_id()] += $amount;
  66. }
  67. $mod_bonus->edit(['bonus_id' => $bonus_id],["expired" => 1]);
  68. }
  69. foreach ($member_amount as $member_id => $amount)
  70. {
  71. try
  72. {
  73. push_helper::notice_expired($member_id,$amount);
  74. }
  75. catch (Exception $ex) {
  76. Log::record("bonus_expired error: {$ex->getMessage()}",Log::ERR);
  77. }
  78. }
  79. return true;
  80. }
  81. static public function release_bonus()
  82. {
  83. $bonus_type = Model('bonus_type');
  84. $condition = ['remain_amount' => ['gt',0],'is_refund' => 0,'send_end_date' => ['lt',time() - 60 * 60]];
  85. $bonus_types = $bonus_type->getTypeList($condition,'','*','',false);
  86. if(empty($bonus_types) || count($bonus_types) <= 0) {
  87. return;
  88. }
  89. foreach($bonus_types as $item)
  90. {
  91. try
  92. {
  93. $trans = new trans_wapper(null,__METHOD__);
  94. $success = $bonus_type->edit(['type_id' => $item['type_id']],['is_refund' => 1,'refund_time' => time()]);
  95. if (!$success) {
  96. $sresult = implode(',',$item);
  97. Log::record("bonus refund 更新状态失败,result:{$sresult}.");
  98. }
  99. else {
  100. $type = bonus\type::create_by_paramer($item);
  101. QueueClient::push('onBonusChange', ['change_type' => 'bonus_refund', 'buyer_id' => $type->sender_id(), 'order_sn' => $type->getType_sn()]);
  102. $condition = ['type_id' =>$item['type_id'],'bonus_status' => ['in',[0,1]]];
  103. Model('user_bonus')->where($condition)->delete();
  104. }
  105. $trans->commit();
  106. }
  107. catch (Exception $e)
  108. {
  109. $trans->rollback();
  110. $sresult = implode(',',$item);
  111. Log::record('bonus refund : error:' . $e->getMessage() . " result:{$sresult}.");
  112. }
  113. }
  114. }
  115. static public function onFcodeWarning()
  116. {
  117. $pre_secs = 86400 * 5;
  118. $mod_member = Model('member');
  119. $i = 0;
  120. while (true)
  121. {
  122. $start = $i * 1000;
  123. $items = Model()->table('goods_fcode')
  124. ->field('fc_id,mobile,goods_commonid,usable_time')
  125. ->where(array('usable_time&usable_time' => ['_multi'=>true,['gt',time() - $pre_secs],['lt',time()]],'fc_state' => 0,'warning_time' => 0))
  126. ->order('fc_id desc')
  127. ->limit("{$start},1000")->select();
  128. if(empty($items)) {
  129. return;
  130. }
  131. $i++;
  132. foreach ($items as $item)
  133. {
  134. $fc_id = $item['fc_id'];
  135. $cid = $item['goods_commonid'];
  136. $usable_time = $item['usable_time'];
  137. $mobile = $item['mobile'];
  138. $mid = self::member_id($mod_member,$mobile);
  139. if($mid!= false && $mid > 0) {
  140. push_helper::fcode_warning($cid,$mid,$usable_time);
  141. }
  142. Model()->table('goods_fcode')->where(['fc_id' => $fc_id])->update(['warning_time' => time()]);
  143. }
  144. }
  145. }
  146. static private function member_id($mod_member,$mobile)
  147. {
  148. $items = $mod_member->where(['member_mobile' => $mobile])->select();
  149. if(!empty($items)) {
  150. return intval($items[0]['member_id']);
  151. }
  152. return false;
  153. }
  154. }