bargain.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2018/1/31
  6. * Time: 下午4:56
  7. */
  8. namespace async;
  9. use room;
  10. use trans_wapper;
  11. use Exception;
  12. use Log;
  13. use push_helper;
  14. use bonus;
  15. use bonus_helper;
  16. class bargain
  17. {
  18. private $mBargain;
  19. public function __construct($bargain_id)
  20. {
  21. $mod_bargain = Model('room_bargain');
  22. $info = $mod_bargain->getBargainById($bargain_id);
  23. $this->mBargain = new room\bargain($info);
  24. }
  25. public function onClose()
  26. {
  27. if($this->mBargain->closed()) {
  28. $this->reward();
  29. }
  30. else
  31. {
  32. if($this->mBargain->has_over()) {
  33. $this->close();
  34. $this->reward();
  35. }
  36. else
  37. {
  38. $discount_cent = intval($this->mBargain->discount() * 100 + 0.5);
  39. $dest = $this->mBargain->goods_price() - $this->mBargain->lowest_price();
  40. $dest_cent = intval($dest * 100 + 0.5);
  41. if($dest_cent <= $discount_cent) {
  42. $this->close();
  43. $this->reward();
  44. }
  45. else {
  46. return false;
  47. }
  48. }
  49. }
  50. return true;
  51. }
  52. private function close()
  53. {
  54. $mod_bargain = Model('room_bargain');
  55. $mod_bargain->editBargain($this->mBargain->bargain_id(),['closed' => 1]);
  56. }
  57. private function reward()
  58. {
  59. if($this->mBargain->rewarded()) {
  60. return false;
  61. }
  62. else {
  63. try
  64. {
  65. $mod_bargain = Model('room_bargain');
  66. $trans = new trans_wapper($mod_bargain);
  67. $mod_bargain->editBargain($this->mBargain->bargain_id(),['rewarded' => 1]);
  68. $trans->commit();
  69. $this->send_bonus($this->mBargain->discount(),$this->mBargain->creator());
  70. }
  71. catch (Exception $ex) {
  72. $trans->rollback();
  73. Log::record($ex->getTraceAsString(),Log::ERR);
  74. }
  75. }
  76. }
  77. private function send_bonus($discount,$userid)
  78. {
  79. $cent = intval($discount * 100 + 0.5);
  80. if($cent <= 0) return false;
  81. $params = bonus\parameters::bargain_fixed(100,$discount);
  82. foreach ($params as $param) {
  83. $type = bonus_helper::create_type_input($param);
  84. $money = $type->getTotal_amount();
  85. $ret = bonus_helper::make_bonus($param, $param['rate_money']);
  86. if ($ret != false) {
  87. $type_sn = $ret['type_sn'];
  88. bonus_helper::send($type_sn, [$userid]);
  89. push_helper::bargain_bonus($userid, $discount, $type_sn);
  90. } else {
  91. return false;
  92. }
  93. }
  94. }
  95. }