notify_helper.php 5.8 KB

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