notify_helper.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. 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. //todo 以后检查时间是否可能是负数
  21. $mod_bonus = Model("user_bonus");
  22. $items = $mod_bonus->getNeedWarn($warn_remain_days, $warn_interval_days);
  23. if($items == false) return;
  24. $ids = array();
  25. foreach ($items as $key => $val) {
  26. $bonus = bonus\user_bonus::create_by_param($val);
  27. $leftsecs = $bonus->usable_time() - time();
  28. $remain_days = self::secs_days($leftsecs);
  29. $amount = $bonus->remain_amount();
  30. $param = array();
  31. $param['member_id'] = $bonus->user_id();
  32. $param['text'] = "红包过期通知:您价值{$amount}元的红包将在{$remain_days}天内过期,请尽快分享给好友或者买买买~";
  33. $param['go_type'] = 'bonus';
  34. QueueClient::push('upushSendMsg', $param);
  35. array_push($ids,$bonus->bonus_id());
  36. }
  37. if (!empty($ids)) {
  38. $mod_bonus->edit(array('bonus_id' => array('in', $ids)),array("notify_time" => time()));
  39. }
  40. }
  41. static public function bonus_expired()
  42. {
  43. $mod_bonus = Model("user_bonus");
  44. $items = $mod_bonus->getExpired();
  45. $ids = array();
  46. foreach ($items as $key => $val) {
  47. $bonus = bonus\user_bonus::create_by_param($val);
  48. $user_id = $bonus->user_id();
  49. $amount = $bonus->remain_amount();
  50. $pred = new predeposit_helper($user_id);
  51. $pred->bonus_expire($val);
  52. $param = array();
  53. $param['member_id'] = $user_id;
  54. $param['text'] = "红包过期通知:很遗憾,您价值{$amount}元的红包已经过期了,您早点分享给朋友,也是个人情:(";
  55. $param['go_type'] = 'bonus';
  56. QueueClient::push('upushSendMsg', $param);
  57. array_push($ids,$bonus->bonus_id());
  58. }
  59. if (!empty($ids)) {
  60. $mod_bonus->edit(array('bonus_id' => array('in', $ids)),array("expired" => 1));
  61. }
  62. }
  63. static public function release_bonus()
  64. {
  65. $bonus_type = Model('bonus_type');
  66. $one_hour = 60 * 60;
  67. $condition = array('remain_amount' => array('gt',0),'is_refund' => 0,'send_end_date' =>array('lt',time() - $one_hour));
  68. $bonus_types = $bonus_type->getTypeList($condition,'','*','',false);
  69. if(empty($bonus_types) || count($bonus_types) <= 0) {
  70. return;
  71. }
  72. foreach($bonus_types as $type)
  73. {
  74. try
  75. {
  76. $pred = new predeposit_helper($type['sender_id']);
  77. Model::beginTransaction();
  78. $bonus_type->edit(array('type_id' =>$type['type_id']),array('is_refund' => 1,'refund_time' => time()));
  79. if ($bonus_type->affected_rows() <= 0) {
  80. Model::rollback();
  81. $sresult = implode(',',$type);
  82. Log::record("bonus refund 更新状态失败,result:{$sresult}.");
  83. } else {
  84. $pred->bonus_refund($type);
  85. $condition = array('type_id' =>$type['type_id'],'bonus_status' => array('in','0,1'));
  86. Model('user_bonus')->where($condition)->delete();
  87. Model::commit();
  88. }
  89. }
  90. catch (Exception $e)
  91. {
  92. Model::rollback();
  93. $sresult = implode(',',$type);
  94. Log::record('bonus refund : error:' . $e->getMessage() . " result:{$sresult}.");
  95. }
  96. }
  97. }
  98. }